2003/02/25 22:09:41
[org.ibex.core.git] / src / org / xwt / Platform.java
index 6c97cae..1b6056a 100644 (file)
@@ -21,14 +21,23 @@ public class Platform {
 
     // Static Data /////////////////////////////////////////////////////////////////////////////////////
 
-    /** set to true during the delivery of a KeyPressed:C-v/A-v or Press3; it is safe to use a 'global' here,
-     *  since message delivery is single-threaded and non-preemptable
+    /**
+     *  set to true during the delivery of a KeyPressed:C-v/A-v or Press3; it is safe to use a
+     *  'global' here, since message delivery is single-threaded and non-preemptable
      */
     static boolean clipboardReadEnabled = false;
 
     /** The appropriate Platform object for this JVM */
     static Platform platform = null;
 
+    /** true if proxy autodetection has already been run */
+    static boolean alreadyDetectedProxy = false;
+
+    /** the result of proxy autodetection */
+    static Proxy cachedProxyInfo = null;
+
+    /** the current build */
+    public static String build = "unknown";
 
     // VM Detection Logic /////////////////////////////////////////////////////////////////////
 
@@ -40,39 +49,58 @@ public class Platform {
     // reflection.
 
     static {
-        System.out.println("Detecting JVM...");
+        System.err.println("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 (os_name.startsWith("Mac OS X")) platform_class = "MacOSX";
-            else if (vendor.startsWith("Free Software Foundation")) platform_class = "Win32";
-            else if (version.startsWith("1.1") && vendor.startsWith("Netscape")) platform_class = "Netscape";
+            //if (os_name.startsWith("Mac OS X")) platform_class = "MacOSX";
+            if (vendor.startsWith("Free Software Foundation")) {
+                if (os_name.startsWith("Window")) platform_class = "Win32";
+                else platform_class = "X11";
+            } else if (version.startsWith("1.1") && vendor.startsWith("Netscape")) platform_class = "Netscape";
             else if (version.startsWith("1.1") && vendor.startsWith("Microsoft")) platform_class = "Microsoft";
             else if (!version.startsWith("1.0") && !version.startsWith("1.1")) platform_class = "Java2";
 
+        // Disable 2d hardware acceleration on Jaguar
+        if (os_name.equals("Mac OS X") && os_version.equals("10.2"))
+        System.setProperty("com.apple.hwaccel", "false");
+
             if (platform_class != null) {
                 platform = (Platform)Class.forName("org.xwt.plat." + platform_class).newInstance();
                 platform.init();
             }
+
+            try {
+                build = (String)Class.forName("org.xwt.Build").getField("build").get(null);
+            } catch (ClassNotFoundException cnfe) {
+            } catch (Exception e) {
+                if (Log.on) Log.log(Platform.class, "exception while detecting build:");
+                if (Log.on) Log.log(Platform.class, e);
+            }
+            if (Log.on) Log.log(Platform.class, "XWT build: " + build);
+
             if (Log.on) Log.log(Platform.class, "XWT VM detection:   vendor = " + vendor);
             if (Log.on) Log.log(Platform.class, "                   version = " + version);
-            if (Log.on) Log.log(Platform.class, "                        os = " + os_name);
+            if (Log.on) Log.log(Platform.class, "                        os = " + os_name + " [version " + os_version + "]");
+            if (Log.on && Main.applet != null) Log.log(Platform.class, "                   browser = " + Main.applet.getParameter("browser"));
 
             if (platform_class == null) {
                 if (Log.on) Log.log(Platform.class, "Unable to detect JVM");
-                System.exit(-1);
+                new Platform().criticalAbort("Unable to detect JVM");
             }
 
             if (Log.on) Log.log(Platform.class, "                  platform = " + platform.getDescriptiveName());
             if (Log.on) Log.log(Platform.class, "                     class = " + platform.getClass().getName());
+            platform.postInit();
 
         } catch (Exception e) {
             if (Log.on) Log.log(Platform.class, "Exception while trying to detect JVM");
             if (Log.on) Log.log(Platform.class, e);
-            System.exit(-1);
+            new Platform().criticalAbort("Unable to detect JVM");
         }
 
     }
@@ -85,6 +113,7 @@ public class Platform {
 
     /** this initializes the platform; code in here can invoke methods on Platform since Platform.platform has already been set */
     protected void init() { }
+    protected void postInit() { }
 
     /** creates and returns a doublebuffer 'belonging' to <tt>owner</tt>; we need to associate DoubleBuffers to surfaces
      *  due to AWT 1.1 requirements (definately for Navigator, possibly also for MSJVM).
@@ -95,15 +124,17 @@ public class Platform {
     protected Surface _createSurface(Box b, boolean framed) { return null; }
 
     /** creates a socket object */
-    protected Socket _getSocket(String host, int port, boolean ssl) throws IOException {
-        return ssl ? new TinySSL(host, port) : new Socket(java.net.InetAddress.getByName(host), port);
+    protected Socket _getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
+        Socket ret = ssl ? new TinySSL(host, port, negotiate) : new Socket(java.net.InetAddress.getByName(host), port);
+        ret.setTcpNoDelay(true);
+        return ret;
     }
 
     /** creates and returns a picture */
     protected Picture _createPicture(int[] b, int w, int h) { return null; }
     
     /** should return true if it is safe to supress full-surface dirties immediately after a window resize */
-    protected boolean _supressDirtyOnResize() { return true; }
+    protected boolean _supressDirtyOnResize() { return false; }
 
     /** the human-readable name of the key mapped to XWT's 'alt' key */
     protected String _altKeyName() { return "alt"; }
@@ -146,26 +177,78 @@ public class Platform {
     }
     
     /** quits XWT */
-    protected void _exit() { System.exit(0); }
+    protected void _exit() {
+        if (Main.applet == null) {
+            System.exit(0);
+        } else {
+            // just block ourselves forever
+            // FIXME
+            new Semaphore().block();
+        }
+    }
 
     /** 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); }
+    protected void _criticalAbort(String message) { _exit(); }
 
     /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
     protected String _getDefaultFont() { return "sansserif10"; }
 
+    /** if true, org.xwt.Surface will generate a Click automatically after a press and a release */
     protected boolean _needsAutoClick() { return false; }
 
+    /** if true, org.xwt.Surface will generate a DoubleClick automatically after recieving two clicks in a short period of time */
+    protected boolean _needsAutoDoubleClick() { return false; }
+
     protected void _newBrowserWindow(String url) {
         if (Log.on) Log.log(this, "Platform " + platform.getClass().getName() + " cannot open browser windows");
         return;
     }
 
+    /** Returns null if XWT should always use direct connection; otherwise returns a ProxyInfo object with proxy settings */
+    protected synchronized Proxy _detectProxy() { return null; }
+
+    /** 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; }
+
+    /** returns true iff the platform has a case-sensitive filesystem */
+    protected boolean _isCaseSensitive() { return true; }
+
+    /** returns the value of the environment variable key, or null if no such key exists */
+    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) {
+            if (Log.on) Log.log(this, "Exception while reading from environment:");
+            if (Log.on) Log.log(this, e);
+        }
+        return null;
+    }
+
     // Static methods -- thunk to the instance /////////////////////////////////////////////////////////////////////////
 
     /** if true, org.xwt.Surface should generate Click messages automatically when a Release happens after a Press and the mouse has not moved much */
     public static boolean needsAutoClick() { return platform._needsAutoClick(); }
 
+    /** if true, org.xwt.Surface should generate DoubleClick messages automatically when needed */
+    public static boolean needsAutoDoubleClick() { return platform._needsAutoDoubleClick(); }
+
     /** should return true if it is safe to supress full-surface dirties immediately after a window resize */
     public static String getDefaultFont() { return platform._getDefaultFont(); }
 
@@ -178,8 +261,8 @@ public class Platform {
     /** returns the maximum ascent of all glyphs in a given platform-specific font */
     public static int getMaxAscent(String font) { return platform._getMaxAscent(font); }
 
-    /** returns the maximum descent of all glyphs in a given platform-specific font */
-    public static int getMaxDescent(String font) { return platform._getMaxDescent(font); }
+    /** returns the maximum descent of all glyphs in a given platform-specific font. Three pixel minimum ensures space for underline. */
+    public static int getMaxDescent(String font) { return Math.max(3, platform._getMaxDescent(font)); }
 
     /** returns the maximum number of threads that the XWT engine can create without adversely affecting the host OS */
     public static int maxThreads() { return platform._maxThreads(); }
@@ -200,7 +283,9 @@ public class Platform {
     public static void setClipBoard(String s) { platform._setClipBoard(s); }
 
     /** creates a socket object, with or without ssl encryption */
-    public static Socket getSocket(String host, int port, boolean ssl) throws IOException { return platform._getSocket(host, port, ssl); }
+    public static Socket getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
+        return platform._getSocket(host, port, ssl, negotiate);
+    }
 
     /** returns the width of the screen, in pixels */
     public static int getScreenWidth() { return platform._getScreenWidth(); }
@@ -217,12 +302,39 @@ public class Platform {
     /** creates and returns a picture */
     public static Picture createPicture(ImageDecoder i) { return platform._createPicture(i.getData(), i.getWidth(), i.getHeight()); }
 
-    /** creates and returns a picture */
+    /** returns true iff the platform has a case-sensitive filesystem */
+    public static boolean isCaseSensitive() { return platform._isCaseSensitive(); }
+
+    /** 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); }
+
+    /** displays a platform-specific "open file" dialog and returns the chosen filename, or null if the user hit cancel */
+    public static String fileDialog(String suggestedFileName, boolean write) {
+        if (!ThreadMessage.suspendThread()) return null;
+        try {
+            return platform._fileDialog(suggestedFileName, write);
+        } finally {
+            ThreadMessage.resumeThread();
+        }
+    }
+
+    /** opens a new browser window */
     public static void newBrowserWindow(String url) {
         if (!(url.startsWith("https://") || url.startsWith("http://") || url.startsWith("ftp://") || url.startsWith("mailto:"))) {
             if (Log.on) Log.log(Platform.class, "xwt.newBrowserWindow() only supports http and https urls");
             return;
         }
+
+        // check the URL for well-formedness, as a defense against buffer overflow attacks
+        try {
+            String u = url;
+            if (u.startsWith("https")) u = "http" + u.substring(5);
+            new URL(u);
+        } catch (MalformedURLException e) {
+            if (Log.on) Log.log(Platform.class, "URL " + url + " is not well-formed");
+            if (Log.on) Log.log(Platform.class, e);
+        }
+
         if (Log.on) Log.log(Platform.class, "newBrowserWindow, url = " + url);
         platform._newBrowserWindow(url);
     }
@@ -247,8 +359,8 @@ public class Platform {
     public static Surface createSurface(Box b, boolean framed, boolean refreshable) {
         Surface ret = platform._createSurface(b, framed);
         ret.setInvisible(b.invisible);
-        b.set(Box.size, 0, ret.width);
-        b.set(Box.size, 1, ret.height);
+        b.set(Box.size, 0, b.size(0) < Surface.scarPicture.getWidth() ? Surface.scarPicture.getWidth() : b.size(0));
+        b.set(Box.size, 1, b.size(1) < Surface.scarPicture.getHeight() ? Surface.scarPicture.getHeight() : b.size(1));
 
         Object titlebar = b.get("titlebar", null, true);
         if (titlebar != null) ret.setTitleBarText(titlebar.toString());
@@ -260,6 +372,8 @@ public class Platform {
             else if (Log.on) Log.log(Platform.class, "unable to load icon " + icon);
         }
 
+        ret.setLimits(b.dmin(0), b.dmin(1), b.dmax(0), b.dmax(1));
+
         if (refreshable) {
             Surface.refreshableSurfaceWasCreated = true;
             Surface.allSurfaces.addElement(ret);
@@ -269,6 +383,24 @@ public class Platform {
         return ret;
     }
 
+    /** detects proxy settings */
+    public static synchronized Proxy detectProxy() {
+
+        if (cachedProxyInfo != null) return cachedProxyInfo;
+        if (alreadyDetectedProxy) return null;
+        alreadyDetectedProxy = true;
+
+        if (Log.on) Log.log(Platform.class, "attempting environment-variable DNS proxy detection");
+        cachedProxyInfo = Proxy.detectProxyViaManual();
+        if (cachedProxyInfo != null) return cachedProxyInfo;
+
+        if (Log.on) Log.log(Platform.class, "attempting " + platform.getClass().getName() + " proxy detection");
+        cachedProxyInfo = platform._detectProxy();
+        if (cachedProxyInfo != null) return cachedProxyInfo;
+
+        return cachedProxyInfo;
+    }
+
     // Helpful font parsing stuff //////////////////////////////////////////////////////
 
     public static class ParsedFont {
@@ -293,7 +425,7 @@ public class Platform {
 
         public void parse(String font) {
             int i = 0;
-            while(i < font.length() && !Character.isDigit(font.charAt(i))) i++;
+            while(i < font.length() && !Character.isDigit(font.charAt(i)) && font.charAt(i) != '*') i++;
             name = font.substring(0, i).toLowerCase().replace('_', ' ');
             size = 10;
             italic = false;
@@ -301,16 +433,21 @@ public class Platform {
             underline = false;
             dotted_underline = false;
             if (i != font.length()) {
-                int j = i;
-                while (j < font.length() && Character.isDigit(font.charAt(j))) j++;
-                if (i != j) size = stoi(font.substring(i, j));
-                i = j;
+                if (font.charAt(i) == '*') {
+                    size = 0;
+                    i++;
+                } else {
+                    int j = i;
+                    while (j < font.length() && Character.isDigit(font.charAt(j))) j++;
+                    if (i != j) size = stoi(font.substring(i, j));
+                    i = j;
+                }
                 while(i < font.length()) {
                     switch (font.charAt(i)) {
-                    case 'b': bold = true;
-                    case 'i': italic = true;
-                    case 'd': dotted_underline = true;
-                    case 'u': underline = true;
+                    case 'b': bold = true; break;
+                    case 'i': italic = true; break;
+                    case 'd': dotted_underline = true; break;
+                    case 'u': underline = true; break;
                     }
                     i++;
                 }
@@ -321,3 +458,4 @@ public class Platform {
 
 }
 
+