1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import java.lang.reflect.*;
12 * Abstracts away the small irregularities in JVM implementations.
14 * The default Platform class supports a vanilla JDK 1.1
15 * JVM. Subclasses are provided for other VMs. Methods whose names
16 * start with an underscore are meant to be overridden by
17 * subclasses. If you create a subclass of Platform, you should put
18 * it in the org.xwt.plat package, and add code to this file's static
19 * block to detect the new platform.
21 public abstract class Platform {
23 public Platform() { platform = this; }
25 // Static Data /////////////////////////////////////////////////////////////////////////////////////
27 static boolean clipboardReadEnabled = false; ///< true iff inside a C-v/A-v/Press3 trap handler
28 static Platform platform = null; ///< The appropriate Platform object for this JVM
29 static boolean alreadyDetectedProxy = false; ///< true if proxy autodetection has already been run
30 static org.xwt.HTTP.Proxy cachedProxyInfo = null; ///< the result of proxy autodetection
31 public static String build = "unknown"; ///< the current build
33 // VM Detection Logic /////////////////////////////////////////////////////////////////////
35 // If you create a new subclass of Platform, you should add logic
36 // here to detect it. Do not reference your class directly -- use
39 public static void forceLoad() {
40 System.err.print("Detecting JVM...");
42 String vendor = System.getProperty("java.vendor", "");
43 String version = System.getProperty("java.version", "");
44 String os_name = System.getProperty("os.name", "");
45 String os_version = System.getProperty("os.version", "");
46 String platform_class = null;
48 if (vendor.startsWith("Free Software Foundation")) {
49 if (os_name.startsWith("Window")) platform_class = "Win32";
50 else if (os_name.startsWith("Linux")) platform_class = "Linux";
51 else if (os_name.startsWith("SunOS")) platform_class = "Solaris";
52 else if (os_name.startsWith("Solaris")) platform_class = "Solaris";
53 else platform_class = "X11";
55 else if (!version.startsWith("1.0") && !version.startsWith("1.1")) platform_class = "Java2";
57 if (platform_class == null) {
58 Log.error(Platform.class, "Unable to detect JVM");
59 criticalAbort("Unable to detect JVM");
62 System.err.println(" " + os_name + " ==> org.xwt.plat." + platform_class);
63 if (platform_class != null) Class.forName("org.xwt.plat." + platform_class).newInstance();
65 String term = Platform.getEnv("TERM");
66 Log.color = term != null && term.length() != 0 && !term.equals("cygwin");
69 build = (String)Class.forName("org.xwt.Build").getField("build").get(null);
70 Log.diag(Platform.class, "XWT build: " + build);
71 } catch (ClassNotFoundException cnfe) {
72 Log.warn(Platform.class, "XWT build: unknown");
73 } catch (Exception e) {
74 Log.info(Platform.class, "exception while detecting build:");
75 Log.info(Platform.class, e);
78 Log.diag(Platform.class, "XWT VM detection: vendor = " + vendor);
79 Log.diag(Platform.class, " version = " + version);
80 Log.diag(Platform.class, " os = " + os_name + " [version " + os_version + "]");
81 Log.diag(Platform.class, " platform = " + platform.getDescriptiveName());
82 Log.diag(Platform.class, " class = " + platform.getClass().getName());
85 } catch (Exception e) {
86 Log.error(Platform.class, "Exception while trying to detect JVM");
87 Log.error(Platform.class, e);
88 criticalAbort("Unable to detect JVM");
94 // Methods to be Overridden ///////////////////////////////////////////////////////////////////
96 protected Surface _createSurface(Box b, boolean framed) { return null; }
97 protected Picture _createPicture(JS r) { return null; }
98 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return null; }
99 protected Font.Glyph _createGlyph(org.xwt.Font f, char c) { return new DefaultGlyph(f, c); }
101 public static PixelBuffer createPixelBuffer(int w, int h, Surface s) { return platform._createPixelBuffer(w, h, s); }
102 public static Picture createPicture(JS r) { return platform._createPicture(r); }
103 public static Font.Glyph createGlyph(org.xwt.Font f, char c) { return platform._createGlyph(f, c); }
104 public static Surface createSurface(Box b, boolean framed, boolean refreshable) {
105 Surface ret = platform._createSurface(b, framed);
106 ret.setInvisible(false);
108 Surface.allSurfaces.addElement(ret);
109 ret.dirty(0, 0, b.width, b.height);
113 if (b.get("titlebar") != null) ret.setTitleBarText((String)b.get("titlebar"));
115 Log.warn(Platform.class, e);
120 /** a string describing the VM */
121 protected String getDescriptiveName() { return "Generic Java 1.1 VM"; }
123 /** invoked after initialization messages have been printed; useful for additional platform detection log messages */
124 protected void postInit() { }
126 /** the human-readable name of the key mapped to XWT's 'alt' key */
127 public static String altKeyName() { return platform._altKeyName(); }
128 protected String _altKeyName() { return "alt"; }
130 /** returns the contents of the clipboard */
131 public static Object getClipBoard() { return clipboardReadEnabled ? platform._getClipBoard() : null; }
132 protected String _getClipBoard() { return null; }
134 /** sets the contents of the clipboard */
135 public static void setClipBoard(String s) { platform._setClipBoard(s); }
136 protected void _setClipBoard(String s) { }
138 /** returns the width of the screen, in pixels */
139 public static int getScreenWidth() { return platform._getScreenWidth(); }
140 protected int _getScreenWidth() { return 640; }
142 /** returns the height of the screen, in pixels */
143 public static int getScreenHeight() { return platform._getScreenHeight(); }
144 protected int _getScreenHeight() { return 480; }
146 /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
147 protected void _criticalAbort(String message) { System.exit(-1); }
148 public static void criticalAbort(String message) {
149 Log.info(Platform.class, "Critical Abort:");
150 Log.info(Platform.class, message);
151 platform._criticalAbort(message);
154 /** if true, org.xwt.Surface will generate a Click automatically after a press and a release */
155 public static boolean needsAutoClick() { return platform._needsAutoClick(); }
156 protected boolean _needsAutoClick() { return false; }
158 /** if true, org.xwt.Surface will generate a DoubleClick automatically after recieving two clicks in a short period of time */
159 public static boolean needsAutoDoubleClick() { return platform._needsAutoDoubleClick(); }
160 protected boolean _needsAutoDoubleClick() { return false; }
162 /** returns true iff the platform has a case-sensitive filesystem */
163 public static boolean isCaseSensitive() { return platform._isCaseSensitive(); }
164 protected boolean _isCaseSensitive() { return true; }
166 /** returns an InputStream to the builtin xwar */
167 public static InputStream getBuiltinInputStream() { return platform._getBuiltinInputStream(); }
168 protected InputStream _getBuiltinInputStream() {return getClass().getClassLoader().getResourceAsStream("org/xwt/builtin.jar");}
170 /** returns the value of the environment variable key, or null if no such key exists */
171 public static String getEnv(String key) { return platform._getEnv(key); }
172 protected String _getEnv(String key) {
174 String os = System.getProperty("os.name").toLowerCase();
176 if (os.indexOf("windows 9") != -1 || os.indexOf("windows me") != -1) {
177 // hack -- jdk1.2/1.3 on Win32 pop open an ugly DOS box; 1.4 does not
178 if (platform.getClass().getName().endsWith("Java12")) return null;
179 p = Runtime.getRuntime().exec("command.com /c set");
180 } else if (os.indexOf("windows") > -1) {
181 // hack -- jdk1.2/1.3 on Win32 pop open an ugly DOS box; 1.4 does not
182 if (platform.getClass().getName().endsWith("Java12")) return null;
183 p = Runtime.getRuntime().exec("cmd.exe /c set");
185 p = Runtime.getRuntime().exec("env");
187 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
189 while ((s = br.readLine()) != null)
190 if (s.startsWith(key + "="))
191 return s.substring(key.length() + 1);
192 } catch (Exception e) {
193 Log.info(this, "Exception while reading from environment:");
199 /** convert a JPEG into an Image */
200 public static synchronized void decodeJPEG(InputStream is, Picture p) { platform._decodeJPEG(is, p); }
201 protected abstract void _decodeJPEG(InputStream is, Picture p);
203 /** displays a platform-specific "open file" dialog and returns the chosen filename, or null if the user hit cancel */
204 protected String _fileDialog(String suggestedFileName, boolean write) { return null; }
205 public static String fileDialog(String suggestedFileName, boolean write) throws org.xwt.js.JSExn {
206 return platform._fileDialog(suggestedFileName, write);
209 /** default implementation is Eric Albert's BrowserLauncher.java */
210 protected void _newBrowserWindow(String url) {
212 Class c = Class.forName("edu.stanford.ejalbert.BrowserLauncher");
213 Method m = c.getMethod("openURL", new Class[] { String.class });
214 m.invoke(null, new String[] { url });
215 } catch (Exception e) {
216 Log.warn(this, "exception trying to open a browser window");
221 /** opens a new browser window */
222 public static void newBrowserWindow(String url) {
223 if (!(url.startsWith("https://") || url.startsWith("http://") || url.startsWith("ftp://") || url.startsWith("mailto:"))) {
224 Log.info(Platform.class, "xwt.newBrowserWindow() only supports http and https urls");
227 // check the URL for well-formedness, as a defense against buffer overflow attacks
230 if (u.startsWith("https")) u = "http" + u.substring(5);
232 } catch (MalformedURLException e) {
233 Log.info(Platform.class, "URL " + url + " is not well-formed");
234 Log.info(Platform.class, e);
236 Log.info(Platform.class, "newBrowserWindow, url = " + url);
237 platform._newBrowserWindow(url);
240 /** detects proxy settings */
241 protected synchronized org.xwt.HTTP.Proxy _detectProxy() { return null; }
242 public static synchronized org.xwt.HTTP.Proxy detectProxy() {
244 if (cachedProxyInfo != null) return cachedProxyInfo;
245 if (alreadyDetectedProxy) return null;
246 alreadyDetectedProxy = true;
248 Log.info(Platform.class, "attempting environment-variable DNS proxy detection");
249 cachedProxyInfo = org.xwt.HTTP.Proxy.detectProxyViaManual();
250 if (cachedProxyInfo != null) return cachedProxyInfo;
252 Log.info(Platform.class, "attempting " + platform.getClass().getName() + " proxy detection");
253 cachedProxyInfo = platform._detectProxy();
254 if (cachedProxyInfo != null) return cachedProxyInfo;
256 return cachedProxyInfo;
259 /** returns a Scheduler instance; used to implement platform-specific schedulers */
260 protected Scheduler _getScheduler() { return new Scheduler(); }
261 public static Scheduler getScheduler() { return platform._getScheduler(); }
263 // FEATURE: be more efficient for many of the subclasses
264 public static class DefaultGlyph extends Font.Glyph {
265 private Picture p = null;
266 public DefaultGlyph(org.xwt.Font f, char c) { super(f, c); }
267 public Picture getPicture() {
268 if (p == null && isLoaded) {
269 Picture p = createPicture(null);
270 p.data = new int[data.length];
271 for(int i=0; i<data.length; i++) p.data[i] = (data[i] & 0xff) << 24;
273 p.width = this.width;
274 p.height = this.height;