1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
5 import java.lang.reflect.*;
11 import org.mozilla.javascript.*;
12 import org.bouncycastle.util.encoders.Base64;
14 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
15 public class Main extends Applet {
18 * Used for security checks. If this is null, it means that only
19 * scripts originating from the local filesystem are loaded in
20 * the engine (maximum permissions). If scripts have been loaded
21 * from multiple locations, this will be 0.0.0.0 (the invalid
24 public static java.net.InetAddress originAddr = null;
26 /** the URL where the initial xwar came from. */
27 public static String origin = null;
29 /** true iff the user asked for rendered regions to be shown with a red rectangle */
30 public static boolean showRenders = false;
33 public void paint(Graphics g) { } // do-nothing method to avoid repaints
34 public void update(Graphics g) { } // do-nothing method, to avoid repaints
36 public static Applet applet = null;
38 /** don't check if all surfaces are gone (and quit) until this is true */
39 public static boolean doneInitializing = false;
41 /** applet entry point */
43 if ("true".equals(getParameter("showRenders"))) showRenders = true;
44 if ("true".equals(getParameter("verbose"))) Log.verbose = true;
46 main(new String[] { getParameter("initial-xwar-url"), "main" });
49 /** common entry point */
50 public static void main(String[] args) {
54 if (startargs > args.length - 1) {
55 System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
56 System.err.println("");
57 System.err.println("Options:");
58 System.err.println(" -s show rendering activity with red rectangles");
59 System.err.println(" -v verbose logging");
60 System.err.println(" -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
61 System.err.println("");
62 System.err.println("Source-location is one of the following:");
63 System.err.println(" - the path to an xwar file");
64 System.err.println(" - the path to a directory to be used as an xwar");
65 System.err.println(" - the http url of an xwar");
66 System.err.println("");
67 System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
68 Runtime.getRuntime().exit(-1);
70 else if (args[startargs].equals("-s")) showRenders = true;
71 else if (args[startargs].equals("-v")) Log.verbose = true;
72 else if (args[startargs].equals("-l")) startargs++; // FIXME
76 final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
79 if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
81 // we do this here instead of in a static initializer to avoid threading hazards
82 if (Log.on) Log.log(Main.class, "loading scar image");
83 PNG png = PNG.decode(new ByteArrayInputStream(Base64.decode(scarPicture_png_base64)), "bundled scar image");
84 Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight());
86 InputStream is = Platform.getBuiltinInputStream();
87 if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
88 Resources.loadArchive(is);
90 String initialTemplate = "main";
92 if (args.length > startargs) {
93 if (args[startargs].startsWith("http://")) {
94 if (Log.on) Log.log(Main.class, "downloading xwar");
95 origin = args[startargs];
96 initialTemplate = "org.xwt.builtin.splash";
99 if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
101 // HACK because MSIE turns \'s into /'s in URLs... argh!!
102 if (Platform.platform.getClass().getName().endsWith("Win32"))
103 args[startargs] = args[startargs].replace('/', '\\');
105 File f = new File(args[startargs]);
106 origin = "file:" + f.getAbsolutePath();
107 if (f.isDirectory()) {
108 Resources.loadDirectory(f);
109 Surface.scarAllSurfacesFromNowOn = true;
111 initialTemplate = "org.xwt.builtin.splash";
114 if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
118 if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
119 final String initialTemplate_f = initialTemplate;
120 ThreadMessage.newthread(new JSObject.JSFunction() {
121 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] arg) throws JavaScriptException {
122 new Box(initialTemplate_f, null);
123 doneInitializing = true;
124 if (Surface.allSurfaces.size() == 0) {
125 Log.log(this, "exiting because all surfaces are gone");
132 // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
133 if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
134 new org.xwt.util.Semaphore().block();
136 } catch (Throwable e) {
138 Platform.criticalAbort("exception in Main.main(): " + e);
142 /** a base64-encoded png image of the scar */
143 public static final String scarPicture_png_base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