questionable patch: merge of a lot of stuff from the svg branch
[org.ibex.core.git] / src / org / ibex / plat / Platform.java
index 4b110a6..e3e68e6 100644 (file)
@@ -1,11 +1,19 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex;
+// 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.
@@ -23,10 +31,10 @@ public abstract class Platform {
 
     // Static Data /////////////////////////////////////////////////////////////////////////////////////
 
-    static boolean clipboardReadEnabled = false;       ///< true iff inside a C-v/A-v/Press3 trap handler
-    static Platform platform = null;                   ///< The appropriate Platform object for this JVM
-    static boolean alreadyDetectedProxy = false;       ///< true if proxy autodetection has already been run
-    static org.ibex.HTTP.Proxy cachedProxyInfo = null;  ///< the result of proxy autodetection
+    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 /////////////////////////////////////////////////////////////////////
@@ -43,7 +51,7 @@ public abstract class Platform {
             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";
@@ -52,8 +60,8 @@ public abstract class Platform {
                 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");
@@ -100,21 +108,21 @@ public abstract class Platform {
     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.Font f, char c) { return new DefaultGlyph(f, c); }
+    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.Font f, char c) { return platform._createGlyph(f, c); }
+    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);
-            ret.dirty(0, 0, b.width, b.height);
+            b.dirty();
             ret.Refresh();
         }
         try {
-            if (b.get("titlebar") != null) ret.setTitleBarText((String)b.get("titlebar"));
+            if (b.get(JSU.S("titlebar")) != null) ret.setTitleBarText(JSU.toString(b.get(JSU.S("titlebar"))));
         } catch (JSExn e) {
             Log.warn(Platform.class, e);
         }
@@ -245,21 +253,23 @@ public abstract class Platform {
     }
 
     /** detects proxy settings */
-    protected synchronized org.ibex.HTTP.Proxy _detectProxy() { return null; }
-    public static synchronized org.ibex.HTTP.Proxy detectProxy() {
+    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.HTTP.Proxy.detectProxyViaManual();
+        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;
    } 
 
@@ -270,7 +280,7 @@ public abstract class Platform {
     // FEATURE: be more efficient for many of the subclasses
     public static class DefaultGlyph extends Font.Glyph {
         private Picture p = null;
-        public DefaultGlyph(org.ibex.Font f, char c) { super(f, c); }
+        public DefaultGlyph(org.ibex.graphics.Font f, char c) { super(f, c); }
         public Picture getPicture() {
             if (p == null && isLoaded) {
                 Picture p = createPicture(null);
@@ -285,6 +295,141 @@ public abstract class Platform {
             return p;
         }
     }
+
+    /** Implements cooperative multitasking */
+    public static class Scheduler {
+
+    // Public API Exposed to org.ibex /////////////////////////////////////////////////
+
+    private static Scheduler singleton;
+    public static void add(Callable t) { Log.debug(Scheduler.class, "scheduling " + t); Scheduler.runnable.append(t); }
+    public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
+
+    private static Callable current = null;
+
+    private static volatile boolean rendering = false;
+    private static volatile boolean again = false;
+
+    /** synchronizd so that we can safely call it from an event-delivery thread, in-context */
+    public static void renderAll() {
+        if (rendering) { again = true; return; }
+        synchronized(Scheduler.class) {
+            try {
+                rendering = true;
+                do {
+                    // FEATURE: this could be cleaner
+                    again = false;
+                    for(int i=0; i<Surface.allSurfaces.size(); i++) {
+                        Surface s = ((Surface)Surface.allSurfaces.elementAt(i));
+                        do { s.render(); } while(s.abort);
+                    }
+                } while(again);
+            } finally {
+                rendering = false;
+            }
+        }
+    }
+
+    
+
+        // API which must be supported by subclasses /////////////////////////////////////
+
+        /**
+         *  SCHEDULER INVARIANT: all scheduler implementations MUST invoke
+         *  Surface.renderAll() after performing a Callable if no tasks remain
+         *  in the queue.  A scheduler may choose to invoke
+         *  Surface.renderAll() more often than that if it so chooses.
+         */
+        public void run() { defaultRun(); }
+        public Scheduler() { }
+
+
+        // Default Implementation //////////////////////////////////////////////////////
+
+        protected static Queue runnable = new Queue(50);
+        public void defaultRun() {
+            while(true) {
+                current = (Callable)runnable.remove(true);
+                try {
+                    // FIXME hideous
+                    synchronized(this) {
+                        for(int i=0; i<Surface.allSurfaces.size(); i++) {
+                            Surface s = (Surface)Surface.allSurfaces.elementAt(i);
+                            if (current instanceof JS) {
+                                s._mousex = Integer.MAX_VALUE;
+                                s._mousey = Integer.MAX_VALUE;
+                            } else {
+                                s._mousex = s.mousex;
+                                s._mousey = s.mousey;
+                            }
+                        }
+                        Log.debug(Scheduler.class, "performing " + current);
+                        current.run(null);
+                    }
+                    renderAll();
+                } catch (JSExn e) {
+                    Log.info(Scheduler.class, "a JavaScript thread spawned with ibex.thread() threw an exception:");
+                    Log.info(Scheduler.class,e);
+                } catch (Exception e) {
+                    Log.info(Scheduler.class, "a Callable threw an exception which was caught by the scheduler:");
+                    Log.info(Scheduler.class, e);
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+                // if an Error is thrown it will cause the engine to quit
+            }
+        }
+    }
+
+    /** Manages access to ~/.ibex */
+    public static class LocalStorage {
+
+        static String ibexDirName = System.getProperty("user.home") + java.io.File.separatorChar + ".ibex";
+
+        static java.io.File ibexDir = null;
+        static java.io.File cacheDir = null;
+
+        static {
+            try {
+                ibexDir = new java.io.File(ibexDirName);
+                if (!ibexDir.mkdirs()) ibexDir = null;
+                try {
+                    cacheDir = new java.io.File(ibexDirName + java.io.File.separatorChar + "cache");
+                    if (!cacheDir.mkdirs()) cacheDir = null;
+                } catch (Exception e) {
+                    Log.warn(LocalStorage.class, "unable to create cache directory " +
+                             ibexDirName + java.io.File.separatorChar + "cache");
+                }
+            } catch (Exception e) {
+                Log.warn(LocalStorage.class, "unable to create ibex directory " + ibexDirName);
+            }
+        }
+
+        // FEATURE: we ought to be able to do stuff like sha1-checking and date checking on cached resources    
+        public static class Cache {
+
+            private static void delTree(java.io.File f) throws IOException {
+                if (f.isDirectory()) {
+                    String[] s = f.list();
+                    for(int i=0; i<s.length; i++)
+                        delTree(new java.io.File(f.getPath() + java.io.File.separatorChar + s[i]));
+                }
+                f.delete();
+            }
+
+            public static void flush() throws IOException {
+                delTree(cacheDir);
+                cacheDir.mkdirs();
+            }
+
+            public static java.io.File getCacheFileForKey(String key) {
+                // FEATURE: be smarter here
+                return new java.io.File(cacheDir.getPath() + File.separatorChar + new String(Encode.toBase64(key.getBytes())));
+            }
+
+        }
+    }
+
 }