2003/06/10 21:45:35
[org.ibex.core.git] / src / org / xwt / Main.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.lang.reflect.*;
5 import java.applet.*;
6 import java.net.*;
7 import java.util.*;
8 import java.io.*;
9 import java.awt.*;
10 import org.bouncycastle.util.encoders.Base64;
11 import org.xwt.js.*;
12 import org.xwt.util.*;
13
14 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
15 public class Main {
16
17     /**
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
22      *  IP).
23      */
24     public static java.net.InetAddress originAddr = null;
25
26     /** the URL where the initial xwar came from. */
27     public static String origin = null;
28
29     /** true iff the user asked for rendered regions to be shown with a red rectangle */
30     public static boolean showRenders = false;
31
32     /** don't check if all surfaces are gone (and quit) until this is true */
33     public static boolean doneInitializing = false;
34
35     /** common entry point */
36     public static void main(String[] args) {
37         try {
38             int startargs = 0;
39             while (true) {
40                 if (startargs > args.length - 1) {
41                     System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
42                     System.err.println("");
43                     System.err.println("Options:");
44                     System.err.println("    -s show rendering activity with red rectangles");
45                     System.err.println("    -v verbose logging");
46                     System.err.println("    -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
47                     System.err.println("");
48                     System.err.println("Source-location is one of the following:");
49                     System.err.println("    - the path to an xwar file");
50                     System.err.println("    - the path to a directory to be used as an xwar");
51                     System.err.println("    - the http url of an xwar");
52                     System.err.println("");
53                     System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
54                     Runtime.getRuntime().exit(-1);
55                 }
56                 else if (args[startargs].equals("-s")) showRenders = true;
57                 else if (args[startargs].equals("-v")) Log.verbose = true;
58                 else if (args[startargs].equals("-l")) startargs++;
59                 else break;
60                 startargs++;
61             }
62             final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
63
64             Platform.forceLoad();
65             if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
66
67             InputStream is = Platform.getBuiltinInputStream();
68             if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
69             Resources.loadArchive(is);
70            
71             if (Log.on) Log.log(Main.class, "loading scar image");
72             PNG png = PNG.decode(new ByteArrayInputStream(Resources.getResource("org.xwt.builtin.scar.png")), "bundled scar image");
73             Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight());
74
75             String initialTemplate = "main";
76
77             if (args.length > startargs) {
78                 if (args[startargs].startsWith("http://")) {
79                     if (Log.on) Log.log(Main.class, "downloading xwar");
80                     origin = args[startargs];
81                     initialTemplate = "org.xwt.builtin.splash";
82
83                 } else {
84                     if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
85
86                     // HACK because MSIE turns \'s into /'s in URLs... argh!!
87                     if (Platform.platform.getClass().getName().endsWith("Win32"))
88                         args[startargs] = args[startargs].replace('/', '\\');
89
90                     File f = new File(args[startargs]);
91                     origin = "file:" + f.getAbsolutePath();
92                     if (f.isDirectory()) {
93                         Resources.loadDirectory(f);
94                         Surface.scarAllSurfacesFromNowOn = true;
95                     } else {
96                         initialTemplate = "org.xwt.builtin.splash";
97                     }
98
99                     if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
100                 }
101             }
102             
103             if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
104             final String initialTemplate_f = initialTemplate;
105             ThreadMessage.newthread(new JS.Function(-1, "java", null, null) {
106                     public Object _call(JS.Array args) throws JS.Exn {
107                         new Box(initialTemplate_f, null);
108                         doneInitializing = true;
109                         if (Surface.allSurfaces.size() == 0) {
110                             Log.log(this, "exiting because all surfaces are gone");
111                             Platform.exit();
112                         }
113                         return null;
114                     }
115                 });
116            
117             // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
118             //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
119             //new org.xwt.util.Semaphore().block();
120             Platform.running();
121
122         } catch (Throwable e) {
123             e.printStackTrace();
124             Platform.criticalAbort("exception in Main.main(): " + e);
125         }
126     }
127
128 }