1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 import org.bouncycastle.util.encoders.Base64;
10 import org.xwt.util.*;
11 import org.xwt.translators.*;
13 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
17 * Used for security checks. If this is null, it means that only
18 * scripts originating from the local filesystem are loaded in
19 * the engine (maximum permissions). If scripts have been loaded
20 * from multiple locations, this will be 0.0.0.0 (the invalid
23 public static java.net.InetAddress originAddr = null;
24 public static String originHost = 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;
32 /** don't check if all surfaces are gone (and quit) until this is true */
33 public static boolean doneInitializing = false;
35 /** common entry point */
36 public static void main(String[] args) {
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);
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++;
62 final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
65 if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
67 InputStream is = Platform.getBuiltinInputStream();
68 if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
69 Resources.loadArchive(is);
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());
75 String initialTemplate = "main";
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";
84 if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
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('/', '\\');
90 File f = new File(args[startargs]);
91 origin = "file:" + f.getAbsolutePath();
92 if (f.isDirectory()) {
93 Resources.loadDirectory(f);
94 Surface.scarAllSurfacesFromNowOn = true;
96 initialTemplate = "org.xwt.builtin.splash";
99 if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
103 if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
104 final String initialTemplate_f = initialTemplate;
105 ThreadMessage.newthread(new JS.Callable() {
106 public Object call(JS.Array args) throws JS.Exn {
108 Template.getTemplate(initialTemplate_f, null).apply(b, null, null, null, 0, 0);
109 doneInitializing = true;
110 if (Surface.allSurfaces.size() == 0) {
111 Log.log(this, "exiting because all surfaces are gone");
118 // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
119 //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
120 //new org.xwt.util.Semaphore().block();
123 } catch (Throwable e) {
125 Platform.criticalAbort("exception in Main.main(): " + e);