1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
4 import java.lang.reflect.*;
11 * Abstracts away the small irregularities in JVM implementations.
13 * The default Platform class supports a vanilla JDK 1.1
14 * JVM. Subclasses are provided for other VMs. Methods whose names
15 * start with an underscore are meant to be overridden by
16 * subclasses. If you create a subclass of Platform, you should put
17 * it in the org.xwt.plat package, and add code to this file's static
18 * block to detect the new platform.
20 public abstract class Platform {
22 public Platform() { platform = this; }
24 // Static Data /////////////////////////////////////////////////////////////////////////////////////
26 static boolean clipboardReadEnabled = false; ///< true iff inside a C-v/A-v/Press3 trap handler
27 static Platform platform = null; ///< The appropriate Platform object for this JVM
28 static boolean alreadyDetectedProxy = false; ///< true if proxy autodetection has already been run
29 static org.xwt.HTTP.Proxy cachedProxyInfo = null; ///< the result of proxy autodetection
30 public static String build = "unknown"; ///< the current build
32 // VM Detection Logic /////////////////////////////////////////////////////////////////////
34 public static void forceLoad() { } ///< do-nothing method that forces <clinit> to run
36 // If you create a new subclass of Platform, you should add logic
37 // here to detect it. Do not reference your class directly -- use
41 System.err.println("Detecting JVM...");
43 String vendor = System.getProperty("java.vendor", "");
44 String version = System.getProperty("java.version", "");
45 String os_name = System.getProperty("os.name", "");
46 String os_version = System.getProperty("os.version", "");
47 String platform_class = null;
49 if (os_name.startsWith("Darwin")) platform_class = "Darwin";
50 else if (vendor.startsWith("Free Software Foundation")) {
51 if (os_name.startsWith("Window")) platform_class = "Win32";
52 else platform_class = "X11";
53 } else if (version.startsWith("1.1") && vendor.startsWith("Netscape")) platform_class = "Netscape";
54 else if (version.startsWith("1.1") && vendor.startsWith("Microsoft")) platform_class = "Microsoft";
55 else if (!version.startsWith("1.0") && !version.startsWith("1.1")) platform_class = "Java2";
57 // Disable 2d hardware acceleration on Jaguar (do we need this?)
58 // if (os_name.equals("Mac OS X") && os_version.startsWith("10.2")) System.setProperty("com.apple.hwaccel", "false");
59 //System.setProperty("com.apple.hwaccel", "false");
61 if (platform_class != null)
62 Class.forName("org.xwt.plat." + platform_class).newInstance();
65 build = (String)Class.forName("org.xwt.Build").getField("build").get(null);
66 } catch (ClassNotFoundException cnfe) {
67 } catch (Exception e) {
68 if (Log.on) Log.info(Platform.class, "exception while detecting build:");
69 if (Log.on) Log.info(Platform.class, e);
71 if (Log.on) Log.diag(Platform.class, "XWT build: " + build);
73 if (Log.on) Log.diag(Platform.class, "XWT VM detection: vendor = " + vendor);
74 if (Log.on) Log.diag(Platform.class, " version = " + version);
75 if (Log.on) Log.diag(Platform.class, " os = " + os_name + " [version " + os_version + "]");
77 if (platform_class == null) {
78 if (Log.on) Log.info(Platform.class, "Unable to detect JVM");
79 criticalAbort("Unable to detect JVM");
82 if (Log.on) Log.diag(Platform.class, " platform = " + platform.getDescriptiveName());
83 if (Log.on) Log.diag(Platform.class, " class = " + platform.getClass().getName());
86 } catch (Exception e) {
87 if (Log.on) Log.info(Platform.class, "Exception while trying to detect JVM");
88 if (Log.on) Log.info(Platform.class, e);
89 criticalAbort("Unable to detect JVM");
95 // Methods to be Overridden ///////////////////////////////////////////////////////////////////
97 /** a string describing the VM */
98 protected String getDescriptiveName() { return "Generic Java 1.1 VM"; }
100 /** invoked after initialization messages have been printed; useful for additional platform detection log messages */
101 protected void postInit() { }
103 protected Surface _createSurface(Box b, boolean framed) { return null; }
104 protected Picture _createPicture(Stream r) { return null; }
105 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return null; }
106 protected Font.Glyph _createGlyph(org.xwt.Font f, char c) { return new DefaultGlyph(f, c); }
108 public static PixelBuffer createPixelBuffer(int w, int h, Surface s) { return platform._createPixelBuffer(w, h, s); }
109 public static Picture createPicture(Stream r) { return platform._createPicture(r); }
110 public static Font.Glyph createGlyph(org.xwt.Font f, char c) { return platform._createGlyph(f, c); }
111 public static Surface createSurface(Box b, boolean framed, boolean refreshable) {
112 Surface ret = platform._createSurface(b, framed);
113 ret.setInvisible(false);
114 ret.setLimits(b.minwidth, b.minheight, b.maxwidth, b.maxheight);
116 Surface.allSurfaces.addElement(ret);
117 ret.dirty(0, 0, b.width, b.height);
123 /** the human-readable name of the key mapped to XWT's 'alt' key */
124 public static String altKeyName() { return platform._altKeyName(); }
125 protected String _altKeyName() { return "alt"; }
127 /** returns the contents of the clipboard */
128 public static Object getClipBoard() { return clipboardReadEnabled ? platform._getClipBoard() : null; }
129 protected String _getClipBoard() { return null; }
131 /** sets the contents of the clipboard */
132 public static void setClipBoard(String s) { platform._setClipBoard(s); }
133 protected void _setClipBoard(String s) { }
135 /** returns the width of the screen, in pixels */
136 public static int getScreenWidth() { return platform._getScreenWidth(); }
137 protected int _getScreenWidth() { return 640; }
139 /** returns the height of the screen, in pixels */
140 public static int getScreenHeight() { return platform._getScreenHeight(); }
141 protected int _getScreenHeight() { return 480; }
143 /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
144 protected void _criticalAbort(String message) { System.exit(-1); }
146 /** if true, org.xwt.Surface will generate a Click automatically after a press and a release */
147 public static boolean needsAutoClick() { return platform._needsAutoClick(); }
148 protected boolean _needsAutoClick() { return false; }
150 /** if true, org.xwt.Surface will generate a DoubleClick automatically after recieving two clicks in a short period of time */
151 public static boolean needsAutoDoubleClick() { return platform._needsAutoDoubleClick(); }
152 protected boolean _needsAutoDoubleClick() { return false; }
154 /** returns true iff the platform has a case-sensitive filesystem */
155 public static boolean isCaseSensitive() { return platform._isCaseSensitive(); }
156 protected boolean _isCaseSensitive() { return true; }
158 /** returns an InputStream to the builtin xwar */
159 public static InputStream getBuiltinInputStream() { return platform._getBuiltinInputStream(); }
160 protected InputStream _getBuiltinInputStream() {
161 return this.getClass().getClassLoader().getResourceAsStream("org/xwt/builtin.jar");
164 /** returns the value of the environment variable key, or null if no such key exists */
165 public static String getEnv(String key) { return platform._getEnv(key); }
166 protected String _getEnv(String key) {
168 String os = System.getProperty("os.name").toLowerCase();
170 if (os.indexOf("windows 9") != -1 || os.indexOf("windows me") != -1) {
171 // hack -- jdk1.2/1.3 on Win32 pop open an ugly DOS box; 1.4 does not
172 if (platform.getClass().getName().endsWith("Java12")) return null;
173 p = Runtime.getRuntime().exec("command.com /c set");
174 } else if (os.indexOf("windows") > -1) {
175 // hack -- jdk1.2/1.3 on Win32 pop open an ugly DOS box; 1.4 does not
176 if (platform.getClass().getName().endsWith("Java12")) return null;
177 p = Runtime.getRuntime().exec("cmd.exe /c set");
179 p = Runtime.getRuntime().exec("env");
181 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
183 while ((s = br.readLine()) != null)
184 if (s.startsWith(key + "="))
185 return s.substring(key.length() + 1);
186 } catch (Exception e) {
187 if (Log.on) Log.info(this, "Exception while reading from environment:");
188 if (Log.on) Log.info(this, e);
193 /** convert a JPEG into an Image */
194 public static synchronized void decodeJPEG(InputStream is, Picture p) { platform._decodeJPEG(is, p); }
195 protected abstract void _decodeJPEG(InputStream is, Picture p);
197 /** displays a platform-specific "open file" dialog and returns the chosen filename, or null if the user hit cancel */
198 protected String _fileDialog(String suggestedFileName, boolean write) { return null; }
199 public static String fileDialog(String suggestedFileName, boolean write) throws org.xwt.js.JSExn {
200 return platform._fileDialog(suggestedFileName, write);
203 /** default implementation is Eric Albert's BrowserLauncher.java */
204 protected void _newBrowserWindow(String url) {
206 Class c = Class.forName("edu.stanford.ejalbert.BrowserLauncher");
207 Method m = c.getMethod("openURL", new Class[] { String.class });
208 m.invoke(null, new String[] { url });
209 } catch (Exception e) {
214 /** opens a new browser window */
215 public static void newBrowserWindow(String url) {
216 if (!(url.startsWith("https://") || url.startsWith("http://") || url.startsWith("ftp://") || url.startsWith("mailto:"))) {
217 if (Log.on) Log.info(Platform.class, "xwt.newBrowserWindow() only supports http and https urls");
221 // check the URL for well-formedness, as a defense against buffer overflow attacks
224 if (u.startsWith("https")) u = "http" + u.substring(5);
226 } catch (MalformedURLException e) {
227 if (Log.on) Log.info(Platform.class, "URL " + url + " is not well-formed");
228 if (Log.on) Log.info(Platform.class, e);
231 if (Log.on) Log.info(Platform.class, "newBrowserWindow, url = " + url);
232 platform._newBrowserWindow(url);
235 /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
236 public static void criticalAbort(String message) {
237 if (Log.on) Log.info(Platform.class, "Critical Abort:");
238 if (Log.on) Log.info(Platform.class, message);
239 platform._criticalAbort(message);
242 /** detects proxy settings */
243 protected synchronized org.xwt.HTTP.Proxy _detectProxy() { return null; }
244 public static synchronized org.xwt.HTTP.Proxy detectProxy() {
246 if (cachedProxyInfo != null) return cachedProxyInfo;
247 if (alreadyDetectedProxy) return null;
248 alreadyDetectedProxy = true;
250 if (Log.on) Log.info(Platform.class, "attempting environment-variable DNS proxy detection");
251 cachedProxyInfo = org.xwt.HTTP.Proxy.detectProxyViaManual();
252 if (cachedProxyInfo != null) return cachedProxyInfo;
254 if (Log.on) Log.info(Platform.class, "attempting " + platform.getClass().getName() + " proxy detection");
255 cachedProxyInfo = platform._detectProxy();
256 if (cachedProxyInfo != null) return cachedProxyInfo;
258 return cachedProxyInfo;
261 /** returns a Scheduler instance; used to implement platform-specific schedulers */
262 protected Scheduler _getScheduler() { return new Scheduler(); }
263 public static Scheduler getScheduler() { return platform._getScheduler(); }
265 // FEATURE: be more efficient for many of the subclasses
266 public static class DefaultGlyph extends Font.Glyph {
267 private Picture p = null;
268 public DefaultGlyph(org.xwt.Font f, char c) { super(f, c); }
269 public Picture getPicture() {
270 if (p == null && isLoaded) {
271 p = createPicture(null);
272 p.data = new int[data.length];
273 for(int i=0; i<data.length; i++) p.data[i] = (data[i] & 0xff) << 24;
275 p.width = this.width;
276 p.height = this.height;