2003/09/23 08:24:59
[org.ibex.core.git] / src / org / xwt / Main.java
1 // Copyright 2003 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 import org.xwt.translators.*;
12
13 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
14 public class Main {
15
16     /**
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
21      *  IP).
22      */
23     public static java.net.InetAddress originAddr = null;
24     public static String originHost = 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             /* FIXME
63             final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
64
65             Platform.forceLoad();
66             if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
67
68             InputStream is = Platform.getBuiltinInputStream();
69             if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
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             Res initialRR = null;
77
78             // FIXME: after applying initial template, check numsurfaces; if zero, exit
79             if (args.length > startargs) {
80                 if (args[startargs].startsWith("http://")) {
81                     if (Log.on) Log.log(Main.class, "downloading xwar");
82                     initialRR = Res.stringToRes("file:" + args[startargs] + "!");
83                     origin = args[startargs];
84                     initialTemplate = "org.xwt.builtin.splash";
85
86                 } else {
87                     if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
88
89                     // HACK because MSIE turns \'s into /'s in URLs... argh!!
90                     if (Platform.platform.getClass().getName().endsWith("Win32"))
91                         args[startargs] = args[startargs].replace('/', '\\');
92
93                     File f = new File(args[startargs]);
94                     origin = "file:" + f.getAbsolutePath();
95                     if (f.isDirectory()) {
96                         initialRR = Res.stringToRes("file:" + args[startargs]);
97                         Surface.scarAllSurfacesFromNowOn = true;
98                     } else {
99                         initialRR = Res.stringToRes("file:" + args[startargs] + "!");
100                         initialTemplate = "org.xwt.builtin.splash";
101                     }
102
103                     if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
104                 }
105             }
106             
107             if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
108             final String initialTemplate_f = initialTemplate;
109             final Res initialRR_f = initialRR;
110             ThreadMessage.newthread(new JS.Callable() {
111                     public Object call(JS.Array args) throws JS.Exn {
112                         Box b = new Box();
113                         try {
114                             Template t = Template.buildTemplate(Res.stringToRes("file:.").getInputStream(), "RESOURCE");
115                             t.apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f);
116                             //FIXME
117                             //Template.getTemplate(initialTemplate_f, null).apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f);
118                             doneInitializing = true;
119                             if (Surface.allSurfaces.size() == 0) {
120                                 Log.log(this, "exiting because all surfaces are gone");
121                                 Platform.exit();
122                             }
123                         } catch (Exception e) {
124                             Log.log(Main.class, e);
125                         }
126                         return null;
127                     }
128                 });
129            
130             // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
131             //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
132             //new org.xwt.util.Semaphore().block();
133             Platform.running();
134             */
135         } catch (Throwable e) {
136             e.printStackTrace();
137             Platform.criticalAbort("exception in Main.main(): " + e);
138         }
139     }
140
141 }