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";
78 if (args.length > startargs) {
79 if (args[startargs].startsWith("http://")) {
80 if (Log.on) Log.log(Main.class, "downloading xwar");
81 initialRR = Res.stringToRes("file:" + args[startargs] + "!");
82 origin = args[startargs];
83 initialTemplate = "org.xwt.builtin.splash";
86 if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
88 // HACK because MSIE turns \'s into /'s in URLs... argh!!
89 if (Platform.platform.getClass().getName().endsWith("Win32"))
90 args[startargs] = args[startargs].replace('/', '\\');
92 File f = new File(args[startargs]);
93 origin = "file:" + f.getAbsolutePath();
94 if (f.isDirectory()) {
95 initialRR = Res.stringToRes("file:" + args[startargs]);
96 Surface.scarAllSurfacesFromNowOn = true;
98 initialRR = Res.stringToRes("file:" + args[startargs] + "!");
99 initialTemplate = "org.xwt.builtin.splash";
102 if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
106 if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
107 final String initialTemplate_f = initialTemplate;
108 final Res initialRR_f = initialRR;
109 ThreadMessage.newthread(new JS.Callable() {
110 public Object call(JS.Array args) throws JS.Exn {
113 Template t = Template.buildTemplate(Res.stringToRes("file:.").getInputStream(), "RESOURCE");
114 t.apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f);
116 //Template.getTemplate(initialTemplate_f, null).apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f);
117 doneInitializing = true;
118 if (Surface.allSurfaces.size() == 0) {
119 Log.log(this, "exiting because all surfaces are gone");
122 } catch (Exception e) {
123 Log.log(Main.class, e);
129 // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
130 //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
131 //new org.xwt.util.Semaphore().block();
134 } catch (Throwable e) {
136 Platform.criticalAbort("exception in Main.main(): " + e);