// Copyright 2000-2005 the Contributors, as shown in the revision logs. // Licensed under the GNU General Public License version 2 ("the License"). // You may not use this file except in compliance with the License. package org.ibex.plat; import java.lang.reflect.*; import java.net.*; import java.io.*; import org.ibex.js.*; import org.ibex.util.*; import org.ibex.graphics.*; import org.ibex.core.*; import org.ibex.crypto.*; import org.ibex.core.*; import org.ibex.net.*; /** * Abstracts away the small irregularities in JVM implementations. * * The default Platform class supports a vanilla JDK 1.1 * JVM. Subclasses are provided for other VMs. Methods whose names * start with an underscore are meant to be overridden by * subclasses. If you create a subclass of Platform, you should put * it in the org.ibex.plat package, and add code to this file's static * block to detect the new platform. */ public abstract class Platform { public Platform() { platform = this; } // Static Data ///////////////////////////////////////////////////////////////////////////////////// public static boolean clipboardReadEnabled = false; ///< true iff inside a C-v/A-v/Press3 trap handler public static Platform platform = null; ///< The appropriate Platform object for this JVM public static boolean alreadyDetectedProxy = false; ///< true if proxy autodetection has already been run public static org.ibex.net.HTTP.Proxy cachedProxyInfo = null; ///< the result of proxy autodetection public static String build = "unknown"; ///< the current build // VM Detection Logic ///////////////////////////////////////////////////////////////////// // If you create a new subclass of Platform, you should add logic // here to detect it. Do not reference your class directly -- use // reflection. public static void forceLoad() { System.err.print("Detecting JVM..."); try { String vendor = System.getProperty("java.vendor", ""); String version = System.getProperty("java.version", ""); String os_name = System.getProperty("os.name", ""); String os_version = System.getProperty("os.version", ""); String platform_class = null; if (vendor.startsWith("Free Software Foundation")) { if (os_name.startsWith("Window")) platform_class = "Win32"; else if (os_name.startsWith("Linux")) platform_class = "Linux"; else if (os_name.startsWith("SunOS")) platform_class = "Solaris"; else if (os_name.startsWith("Solaris")) platform_class = "Solaris"; else if (os_name.startsWith("Darwin")) platform_class = "Darwin"; else platform_class = "X11"; } else if (version.startsWith("1.4")) platform_class = "Java4"; else if (!version.startsWith("1.0") && !version.startsWith("1.1")) platform_class = "Java2"; if (platform_class == null) { Log.error(Platform.class, "Unable to detect JVM"); criticalAbort("Unable to detect JVM"); } System.err.println(" " + os_name + " ==> org.ibex.plat." + platform_class); try { if (platform_class != null) Class.forName("org.ibex.plat." + platform_class).newInstance(); } catch (InstantiationException e) { throw e.getCause(); } String term = Platform.getEnv("TERM"); Log.color = term != null && term.length() != 0 && !term.equals("cygwin"); try { build = (String)Class.forName("org.ibex.Build").getField("build").get(null); Log.diag(Platform.class, "Ibex build: " + build); } catch (ClassNotFoundException cnfe) { Log.warn(Platform.class, "Ibex build: unknown"); } catch (Exception e) { Log.info(Platform.class, "exception while detecting build:"); Log.info(Platform.class, e); } Log.diag(Platform.class, "Ibex VM detection: vendor = " + vendor); Log.diag(Platform.class, " version = " + version); Log.diag(Platform.class, " os = " + os_name + " [version " + os_version + "]"); Log.diag(Platform.class, " platform = " + platform.getDescriptiveName()); Log.diag(Platform.class, " class = " + platform.getClass().getName()); platform.postInit(); } catch (Throwable e) { Log.error(Platform.class, "Exception while trying to detect JVM"); Log.error(Platform.class, e); criticalAbort("Unable to detect JVM"); } } // Methods to be Overridden /////////////////////////////////////////////////////////////////// protected Surface _createSurface(Box b, boolean framed) { return null; } protected Picture _createPicture(JS r) { return null; } protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return null; } protected Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new DefaultGlyph(f, c); } public static PixelBuffer createPixelBuffer(int w, int h, Surface s) { return platform._createPixelBuffer(w, h, s); } public static Picture createPicture(JS r) { return platform._createPicture(r); } public static Font.Glyph createGlyph(org.ibex.graphics.Font f, char c) { return platform._createGlyph(f, c); } public static Surface createSurface(Box b, boolean framed, boolean refreshable) { Surface ret = platform._createSurface(b, framed); ret.setInvisible(false); if (refreshable) { Surface.allSurfaces.addElement(ret); b.dirty(); ret.Refresh(); } try { if (b.get(JSU.S("titlebar")) != null) ret.setTitleBarText(JSU.toString(b.get(JSU.S("titlebar")))); } catch (JSExn e) { Log.warn(Platform.class, e); } return ret; } /** a string describing the VM */ protected String getDescriptiveName() { return "Generic Java 1.1 VM"; } /** invoked after initialization messages have been printed; useful for additional platform detection log messages */ protected void postInit() { } /** the human-readable name of the key mapped to Ibex's 'alt' key */ public static String altKeyName() { return platform._altKeyName(); } protected String _altKeyName() { return "alt"; } /** returns the contents of the clipboard */ public static Object getClipBoard() { return clipboardReadEnabled ? platform._getClipBoard() : null; } protected String _getClipBoard() { return null; } /** sets the contents of the clipboard */ public static void setClipBoard(String s) { platform._setClipBoard(s); } protected void _setClipBoard(String s) { } /** returns the width of the screen, in pixels */ public static int getScreenWidth() { return platform._getScreenWidth(); } protected int _getScreenWidth() { return 640; } /** returns the height of the screen, in pixels */ public static int getScreenHeight() { return platform._getScreenHeight(); } protected int _getScreenHeight() { return 480; } /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */ protected void _criticalAbort(String message) { System.exit(-1); } public static void criticalAbort(String message) { Log.info(Platform.class, "Critical Abort:"); Log.info(Platform.class, message); platform._criticalAbort(message); } /** if true, org.ibex.Surface will generate a Click automatically after a press and a release */ public static boolean needsAutoClick() { return platform._needsAutoClick(); } protected boolean _needsAutoClick() { return false; } /** if true, org.ibex.Surface will generate a DoubleClick automatically after recieving two clicks in a short period of time */ public static boolean needsAutoDoubleClick() { return platform._needsAutoDoubleClick(); } protected boolean _needsAutoDoubleClick() { return false; } /** returns true iff the platform has a case-sensitive filesystem */ public static boolean isCaseSensitive() { return platform._isCaseSensitive(); } protected boolean _isCaseSensitive() { return true; } /** returns an InputStream to the builtin xwar */ public static InputStream getBuiltinInputStream() { return platform._getBuiltinInputStream(); } protected InputStream _getBuiltinInputStream() {return getClass().getClassLoader().getResourceAsStream("org/ibex/builtin.jar");} /** returns the value of the environment variable key, or null if no such key exists */ public static String getEnv(String key) { return platform._getEnv(key); } protected String _getEnv(String key) { try { String os = System.getProperty("os.name").toLowerCase(); Process p; if (os.indexOf("windows 9") != -1 || os.indexOf("windows me") != -1) { // hack -- jdk1.2/1.3 on Win32 pop open an ugly DOS box; 1.4 does not if (platform.getClass().getName().endsWith("Java12")) return null; p = Runtime.getRuntime().exec("command.com /c set"); } else if (os.indexOf("windows") > -1) { // hack -- jdk1.2/1.3 on Win32 pop open an ugly DOS box; 1.4 does not if (platform.getClass().getName().endsWith("Java12")) return null; p = Runtime.getRuntime().exec("cmd.exe /c set"); } else { p = Runtime.getRuntime().exec("env"); } BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String s; while ((s = br.readLine()) != null) if (s.startsWith(key + "=")) return s.substring(key.length() + 1); } catch (Exception e) { Log.info(this, "Exception while reading from environment:"); Log.info(this, e); } return null; } /** convert a JPEG into an Image */ public static synchronized void decodeJPEG(InputStream is, Picture p) { platform._decodeJPEG(is, p); } protected abstract void _decodeJPEG(InputStream is, Picture p); /** displays a platform-specific "open file" dialog and returns the chosen filename, or null if the user hit cancel */ protected String _fileDialog(String suggestedFileName, boolean write) { return null; } public static String fileDialog(String suggestedFileName, boolean write) throws org.ibex.js.JSExn { return platform._fileDialog(suggestedFileName, write); } /** default implementation is Eric Albert's BrowserLauncher.java */ protected void _newBrowserWindow(String url) { try { Class c = Class.forName("edu.stanford.ejalbert.BrowserLauncher"); Method m = c.getMethod("openURL", new Class[] { String.class }); m.invoke(null, new String[] { url }); } catch (Exception e) { Log.warn(this, "exception trying to open a browser window"); Log.warn(this, e); } } /** opens a new browser window */ public static void newBrowserWindow(String url) { if (!(url.startsWith("https://") || url.startsWith("http://") || url.startsWith("ftp://") || url.startsWith("mailto:"))) { Log.info(Platform.class, "ibex.newBrowserWindow() only supports http and https urls"); return; } // check the URL for well-formedness, as a defense against buffer overflow attacks // FIXME check URL without using URL class /* try { String u = url; if (u.startsWith("https")) u = "http" + u.substring(5); new URL(u); } catch (MalformedURLException e) { Log.info(Platform.class, "URL " + url + " is not well-formed"); Log.info(Platform.class, e); } */ Log.info(Platform.class, "newBrowserWindow, url = " + url); platform._newBrowserWindow(url); } /** detects proxy settings */ protected synchronized org.ibex.net.HTTP.Proxy _detectProxy() { return null; } public static synchronized org.ibex.net.HTTP.Proxy detectProxy() { if (cachedProxyInfo != null) return cachedProxyInfo; if (alreadyDetectedProxy) return null; alreadyDetectedProxy = true; // FIXME /* Log.info(Platform.class, "attempting environment-variable DNS proxy detection"); cachedProxyInfo = org.ibex.net.HTTP.Proxy.detectProxyViaManual(); if (cachedProxyInfo != null) return cachedProxyInfo; Log.info(Platform.class, "attempting " + platform.getClass().getName() + " proxy detection"); cachedProxyInfo = platform._detectProxy(); if (cachedProxyInfo != null) return cachedProxyInfo; */ return cachedProxyInfo; } /** returns a Scheduler instance; used to implement platform-specific schedulers */ protected Scheduler _getScheduler() { return new Scheduler(); } public static Scheduler getScheduler() { return platform._getScheduler(); } // FEATURE: be more efficient for many of the subclasses public static class DefaultGlyph extends Font.Glyph { private Picture p = null; public DefaultGlyph(org.ibex.graphics.Font f, char c) { super(f, c); } public Picture getPicture() { if (p == null && isLoaded) { Picture p = createPicture(null); p.data = new int[data.length]; for(int i=0; i