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