25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.Library;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 // do it here, so it can be evaluated at compile time
|
|
18 // this saves a static ctor.
|
|
19 private int buildSWT_VERSION (int major, int minor) {
|
|
20 return major * 1000 + minor;
|
|
21 }
|
|
22
|
|
23
|
|
24 public class Library {
|
|
25
|
|
26 /* SWT Version - Mmmm (M=major, mmm=minor) */
|
|
27
|
|
28 /**
|
|
29 * SWT Major version number (must be >= 0)
|
|
30 */
|
|
31 static const int MAJOR_VERSION = 3;
|
|
32
|
|
33 /**
|
|
34 * SWT Minor version number (must be in the range 0..999)
|
|
35 */
|
|
36 static const int MINOR_VERSION = 449;
|
|
37
|
|
38 /**
|
|
39 * SWT revision number (must be >= 0)
|
|
40 */
|
|
41 static const int REVISION = 0;
|
|
42
|
|
43 /**
|
|
44 * The JAVA and SWT versions
|
|
45 */
|
|
46 //public static const int JAVA_VERSION;
|
|
47 public static const int SWT_VERSION = .buildSWT_VERSION(MAJOR_VERSION, MINOR_VERSION);
|
|
48
|
|
49 version( linux ){
|
|
50 static const String SEPARATOR = "\n";
|
|
51 }
|
|
52 else {
|
|
53 static assert( false, "only linux supported for this port" );
|
|
54 }
|
|
55
|
|
56
|
|
57 static int parseVersion(String aVersion) {
|
|
58 if (aVersion == null) return 0;
|
|
59 int major = 0, minor = 0, micro = 0;
|
|
60 int length = aVersion.length, index = 0, start = 0;
|
|
61 bool isDigit( char c ){
|
|
62 return c >= '0' && c <= '9';
|
|
63 }
|
|
64 while (index < length && isDigit(aVersion[index])) index++;
|
|
65 try {
|
51
|
66 if (start < length) major = Integer.parseInt( aVersion[start .. index] );
|
|
67 } catch (NumberFormatException e) {}
|
25
|
68 start = ++index;
|
|
69 while (index < length && isDigit(aVersion[index])) index++;
|
|
70 try {
|
51
|
71 if (start < length) minor = Integer.parseInt(aVersion[start .. index]);
|
|
72 } catch (NumberFormatException e) {}
|
25
|
73 start = ++index;
|
|
74 while (index < length && isDigit(aVersion[index])) index++;
|
|
75 try {
|
51
|
76 if (start < length) micro = Integer.parseInt(aVersion[start .. index]);
|
|
77 } catch (NumberFormatException e) {}
|
25
|
78 return buildJAVA_VERSION(major, minor, micro);
|
|
79 }
|
|
80
|
|
81 /**
|
|
82 * Returns the Java version number as an integer.
|
|
83 *
|
|
84 * @param major
|
|
85 * @param minor
|
|
86 * @param micro
|
|
87 * @return the version
|
|
88 */
|
|
89 public static int buildJAVA_VERSION (int major, int minor, int micro) {
|
|
90 return (major << 16) + (minor << 8) + micro;
|
|
91 }
|
|
92
|
|
93 /**
|
|
94 * Returns the SWT version number as an integer.
|
|
95 *
|
|
96 * @param major
|
|
97 * @param minor
|
|
98 * @return the version
|
|
99 */
|
|
100 public static int buildSWT_VERSION (int major, int minor) {
|
|
101 return .buildSWT_VERSION(major, minor);
|
|
102 }
|
|
103 /+ PORTING_LEFT
|
|
104 static bool extract (String fileName, String mappedName) {
|
|
105 FileOutputStream os = null;
|
|
106 InputStream is = null;
|
|
107 File file = new File(fileName);
|
|
108 try {
|
|
109 if (!file.exists ()) {
|
|
110 is = Library.class.getResourceAsStream ("/" + mappedName); //$NON-NLS-1$
|
|
111 if (is != null) {
|
|
112 int read;
|
|
113 byte [] buffer = new byte [4096];
|
|
114 os = new FileOutputStream (fileName);
|
|
115 while ((read = is.read (buffer)) != -1) {
|
|
116 os.write(buffer, 0, read);
|
|
117 }
|
|
118 os.close ();
|
|
119 is.close ();
|
|
120 if (!Platform.PLATFORM.equals ("win32")) { //$NON-NLS-1$
|
|
121 try {
|
|
122 Runtime.getRuntime ().exec (new String []{"chmod", "755", fileName}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$
|
|
123 } catch (Throwable e) {}
|
|
124 }
|
|
125 if (load (fileName)) return true;
|
|
126 }
|
|
127 }
|
|
128 } catch (Throwable e) {
|
|
129 try {
|
|
130 if (os != null) os.close ();
|
|
131 } catch (IOException e1) {}
|
|
132 try {
|
|
133 if (is != null) is.close ();
|
|
134 } catch (IOException e1) {}
|
|
135 }
|
|
136 if (file.exists ()) file.delete ();
|
|
137 return false;
|
|
138 }
|
|
139
|
|
140 static bool load (String libName) {
|
|
141 try {
|
|
142 if (libName.indexOf (SEPARATOR) != -1) {
|
|
143 System.load (libName);
|
|
144 } else {
|
|
145 System.loadLibrary (libName);
|
|
146 }
|
|
147 return true;
|
|
148 } catch (UnsatisfiedLinkError e) {}
|
|
149 return false;
|
|
150 }
|
|
151
|
|
152 /**
|
|
153 * Loads the shared library that matches the version of the
|
|
154 * Java code which is currently running. SWT shared libraries
|
|
155 * follow an encoding scheme where the major, minor and revision
|
|
156 * numbers are embedded in the library name and this along with
|
|
157 * <code>name</code> is used to load the library. If this fails,
|
|
158 * <code>name</code> is used in another attempt to load the library,
|
|
159 * this time ignoring the SWT version encoding scheme.
|
|
160 *
|
|
161 * @param name the name of the library to load
|
|
162 */
|
|
163 public static void loadLibrary (String name) {
|
|
164 loadLibrary (name, true);
|
|
165 }
|
|
166
|
|
167 /**
|
|
168 * Loads the shared library that matches the version of the
|
|
169 * Java code which is currently running. SWT shared libraries
|
|
170 * follow an encoding scheme where the major, minor and revision
|
|
171 * numbers are embedded in the library name and this along with
|
|
172 * <code>name</code> is used to load the library. If this fails,
|
|
173 * <code>name</code> is used in another attempt to load the library,
|
|
174 * this time ignoring the SWT version encoding scheme.
|
|
175 *
|
|
176 * @param name the name of the library to load
|
|
177 * @param mapName true if the name should be mapped, false otherwise
|
|
178 */
|
|
179 public static void loadLibrary (String name, boolean mapName) {
|
|
180 String prop = System.getProperty ("sun.arch.data.model"); //$NON-NLS-1$
|
|
181 if (prop is null) prop = System.getProperty ("com.ibm.vm.bitmode"); //$NON-NLS-1$
|
|
182 if (prop !is null) {
|
|
183 if ("32".equals (prop)) { //$NON-NLS-1$
|
|
184 if (0x1FFFFFFFFL is (int /*long*/)0x1FFFFFFFFL) {
|
|
185 throw new UnsatisfiedLinkError ("Cannot load 64-bit SWT libraries on 32-bit JVM"); //$NON-NLS-1$
|
|
186 }
|
|
187 }
|
|
188 if ("64".equals (prop)) { //$NON-NLS-1$
|
|
189 if (0x1FFFFFFFFL !is (int /*long*/)0x1FFFFFFFFL) {
|
|
190 throw new UnsatisfiedLinkError ("Cannot load 32-bit SWT libraries on 64-bit JVM"); //$NON-NLS-1$
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194
|
|
195 /* Compute the library name and mapped name */
|
|
196 String libName1, libName2, mappedName1, mappedName2;
|
|
197 if (mapName) {
|
|
198 String version = System.getProperty ("swt.version"); //$NON-NLS-1$
|
|
199 if (version == null) {
|
|
200 version = "" + MAJOR_VERSION; //$NON-NLS-1$
|
|
201 /* Force 3 digits in minor version number */
|
|
202 if (MINOR_VERSION < 10) {
|
|
203 version += "00"; //$NON-NLS-1$
|
|
204 } else {
|
|
205 if (MINOR_VERSION < 100) version += "0"; //$NON-NLS-1$
|
|
206 }
|
|
207 version += MINOR_VERSION;
|
|
208 /* No "r" until first revision */
|
|
209 if (REVISION > 0) version += "r" + REVISION; //$NON-NLS-1$
|
|
210 }
|
|
211 libName1 = name + "-" + Platform.PLATFORM + "-" + version; //$NON-NLS-1$ //$NON-NLS-2$
|
|
212 libName2 = name + "-" + Platform.PLATFORM; //$NON-NLS-1$
|
|
213 mappedName1 = System.mapLibraryName (libName1);
|
|
214 mappedName2 = System.mapLibraryName (libName2);
|
|
215 } else {
|
|
216 libName1 = libName2 = mappedName1 = mappedName2 = name;
|
|
217 }
|
|
218
|
|
219 /* Try loading library from swt library path */
|
|
220 String path = System.getProperty ("swt.library.path"); //$NON-NLS-1$
|
|
221 if (path != null) {
|
|
222 path = new File (path).getAbsolutePath ();
|
|
223 if (load (path + SEPARATOR + mappedName1)) return;
|
|
224 if (mapName && load (path + SEPARATOR + mappedName2)) return;
|
|
225 }
|
|
226
|
|
227 /* Try loading library from java library path */
|
|
228 if (load (libName1)) return;
|
|
229 if (mapName && load (libName2)) return;
|
|
230
|
|
231 /* Try loading library from the tmp directory if swt library path is not specified */
|
|
232 if (path == null) {
|
|
233 path = System.getProperty ("java.io.tmpdir"); //$NON-NLS-1$
|
|
234 path = new File (path).getAbsolutePath ();
|
|
235 if (load (path + SEPARATOR + mappedName1)) return;
|
|
236 if (mapName && load (path + SEPARATOR + mappedName2)) return;
|
|
237 }
|
|
238
|
|
239 /* Try extracting and loading library from jar */
|
|
240 if (path != null) {
|
|
241 if (extract (path + SEPARATOR + mappedName1, mappedName1)) return;
|
|
242 if (mapName && extract (path + SEPARATOR + mappedName2, mappedName2)) return;
|
|
243 }
|
|
244
|
|
245 /* Failed to find the library */
|
|
246 throw new UnsatisfiedLinkError ("no " + libName1 + " or " + libName2 + " in swt.library.path, java.library.path or the jar file"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
247 }
|
|
248 +/
|
|
249 }
|