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