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