2002/07/19 04:42:24
[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 org.xwt.util.*;
5 import java.lang.reflect.*;
6 import java.applet.*;
7 import java.net.*;
8 import java.util.*;
9 import java.io.*;
10 import java.awt.*;
11 import org.bouncycastle.util.encoders.Base64;
12
13 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
14 public class Main extends Applet {
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     public Main() { }
32     public void paint(Graphics g) { }    // do-nothing method to avoid repaints
33     public void update(Graphics g) { }   // do-nothing method, to avoid repaints
34
35     public static Applet applet = null;
36
37     /** applet entry point */
38     public void init() {
39         if ("true".equals(getParameter("showRenders"))) showRenders = true;
40         if ("true".equals(getParameter("verbose"))) Log.verbose = true;
41         applet = this;
42         main(new String[] { getParameter("initial-xwar-url"), "main" });
43     }
44
45     /** common entry point */
46     public static void main(String[] args) {
47         try {
48             int startargs = 0;
49             while (true) {
50                 if (startargs > args.length - 1) {
51                     System.out.println("Usage: xwt [-s] [-v] source-location [initial-template]");
52                     System.out.println("");
53                     System.out.println("Options:");
54                     System.out.println("    -s show rendering activity with red rectangles");
55                     System.out.println("    -v verbose logging");
56                     System.out.println("");
57                     System.out.println("Source-location is one of the following:");
58                     System.out.println("    - the path to an xwar file");
59                     System.out.println("    - the path to a directory to be used as an xwar");
60                     System.out.println("    - the http url of an xwar");
61                     System.out.println("");
62                     System.out.println("Initial-template is the resource name of the template to load; defaults to 'main'");
63                     Runtime.getRuntime().exit(-1);
64                 }
65                 else if (args[startargs].equals("-s")) showRenders = true;
66                 else if (args[startargs].equals("-v")) Log.verbose = true;
67                 else break;
68                 startargs++;
69             }
70             final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
71
72             Platform.forceLoad();
73             if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
74
75             // we do this here instead of in a static initializer to avoid threading hazards
76             if (Log.on) Log.log(Main.class, "loading scar image");
77             PNG png = PNG.decode(new ByteArrayInputStream(Base64.decode(scarPicture_png_base64)), "bundled scar image");
78             Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight());
79
80             InputStream is = Main.class.getClassLoader().getResourceAsStream("org/xwt/builtin.xwar");
81             if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
82             Resources.loadArchive(is);
83            
84             String initialTemplate = "main";
85
86             if (args.length > startargs) {
87                 if (args[startargs].startsWith("http://")) {
88                     if (Log.on) Log.log(Main.class, "downloading xwar");
89                     origin = args[startargs];
90                     initialTemplate = "org.xwt.builtin.splash";
91
92                 } else {
93                     if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
94
95                     // HACK because MSIE turns \'s into /'s in URLs... argh!!
96                     if (Platform.platform.getClass().getName().endsWith("Win32"))
97                         args[startargs] = args[startargs].replace('/', '\\');
98
99                     File f = new File(args[startargs]);
100                     origin = "file:" + f.getAbsolutePath();
101                     if (f.isDirectory()) {
102                         Resources.loadDirectory(f);
103                     } else {
104                         initialTemplate = "org.xwt.builtin.splash";
105                     }
106
107                     if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
108                 }
109             }
110             
111             if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
112             final String initialTemplate_f = initialTemplate;
113             MessageQueue.add(new Message() {
114                     public void perform() {
115                         new Box(initialTemplate_f, null);
116                     }
117                 });
118            
119             // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
120             if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
121             new org.xwt.util.Semaphore().block();
122
123         } catch (Throwable e) {
124             e.printStackTrace();
125             Platform.criticalAbort("exception in Main.main(): " + e);
126         }
127     }
128
129     /** a base64-encoded png image of the scar */
130     public static final String scarPicture_png_base64 = 
131         "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAA" +
132         "CXBIWXMAAAsRAAALEQF/ZF+RAAAAB3RJTUUH0gIYAgsIkuS+AwAAA7lJREFUeJzF1l9MW1Uc" +
133         "wPHvvb3lz/oHOssd0hXo+NNONow102xDSXzoJGiyGBIyDNMlJvjn0USNL74ZY3wnxOgeWIxo" +
134         "9uAmZBrMiNkYYw5xRGBFQdiuzgtl/Gmht7S9PsBM162lGwN+yU3uPTf3nM895/c7OXwOfSKI" +
135         "23Z1yDJt0Ms2hQhwARa3CyHeudkuhJj4sB0IMblhqxH3ALYacV/AViJSArYKkRawFYh1AZuN" +
136         "yAiwmQgpzTsBMMzVv14V00PWQGDhJpd/UNugdwBebIOFRwFINwOGwJHXPH/d9BdYrbXZpaV1" +
137         "ZUPeZ8RuiHnhXAvYNxMghEEMaXO79rjqyLeV6c5iD01Nrz4bevnYwYFVxJkWsG4WQM+B+I6e" +
138         "7/q0SH98srXuYufpk2NGo8Tx5jcF6/unDwyA5oVzmwUAiBlhReg622eG6L7xDuWbjp+iJSVO" +
139         "Kisfl2Z8p8QLENxoYqatgjzQAeFy4QvOs0ZX5bGmeunvWxK//jaNQQpLsPHqSJkDgKT6GrxD" +
140         "xZ6DkimrzFffUFS020dX50B8efaPyeyuN5ZZBW4IkQogqb6Gp0b9IxZJcmKzubCYvfT0KAjx" +
141         "ES3r1LtTQDzxg4dFiA5V/UCRZUtio+p7xTtyfdgqiEXIBW5cew6xv/p5ap8zBVbi8ez5hg9d" +
142         "O8DA6kxtCCG2w8/JiPngrMUgObE/VoHDeYAn9taytBxDRw+/VH9YlHJFR7Dxo4o1xF3xoAiD" +
143         "AMI1mKwJhcYUWa63hkKRyXx7saZliU9WH8XjqSUciWHLs3HrX6x2e9a0212yqEXthWGPNzdy" +
144         "9cdZkpZjCiLvwNvfwxcZAQASEe6JMWXGUeCM6TaKiqoxm8xMB26zM9+OOq2b9lYVWsrLyoWJ" +
145         "G1FzvKo8L3r1/D/JHWeKuCsJE5ZDfPr3/l6BYX14tJvgUhBbfh7TgQDuCkvYarKg3BCIRu1E" +
146         "V3bFUnWeyXLcUwVriE8UWTYUn//2UjTi1/3+iwSDi+zft5NSV27O6EiUwaE/CS8NaMaTLaPp" +
147         "BlgPcd8ybIczDlX9VJFlg73zs0va8qA+PtGHxWTGfz3O4NA483O/aHrriStLEEkHWA/xfw4k" +
148         "xzUYrQmF5hVZPlw42D216CrdPXfbKMzMRliYv6LReqJ/CVZISsBUkSonBHGd7bgZjiqy/F6F" +
149         "qsZCjR87c0w2DF++Nbn25xkNnhg1YGmBQxkD1hDFiix/7VDVxQcdcD1ERkeydphyqOrx5B3z" +
150         "YSMxJzKagTvRDKWKLH/VqKqPwgHAf3HDlnr6flmoAAAAAElFTkSuQmCC";
151 }