9700d1e45494b019ae93997b2ea90f351530db11
[org.ibex.core.git] / src / org / ibex / plat / Win32.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
2 package org.ibex.plat;
3
4 import org.ibex.util.*;
5 import java.util.*;
6 import org.ibex.js.*;
7 import org.ibex.graphics.*;
8 import org.ibex.core.*;
9 import org.ibex.net.*;
10
11 /** Platform specific code for GCJ-compiled Win32 binaries */
12 public class Win32 extends GCJ {
13     // Initialization ////////////////////////////////////////////////////////////////////////////
14
15     // Win32 often asks for a DC/Handle when it doesn't really need one
16     static int desktop_handle = 0;
17     static int desktop_dc = 0;
18
19     // Cursors
20     static int wait_cursor = 0;
21     static int default_cursor = 0;
22     static int crosshair_cursor = 0;
23     static int text_cursor = 0;
24     static int move_cursor = 0;
25     static int sizenesw_cursor = 0;
26     static int sizens_cursor = 0;
27     static int sizenwse_cursor = 0;
28     static int sizewe_cursor = 0;
29     static int hand_cursor = 0;
30     
31     /** reverse lookup from hwnd to Win32Surface */
32     public static Hashtable hwndToWin32SurfaceMap = new Hashtable();
33
34     /** lets us know that natInit() is finished */
35     public static Semaphore messagePumpStarted = new Semaphore();
36
37     /** ThreadId of the message pump thread, used to send it messages */
38     public static int messagePumpThread = 0;
39
40     public static native String getTempPath();
41     public static native void natInit();
42     public static native void natPreInit();
43
44     protected native String _fileDialog(String suggestedFileName, boolean write);
45
46     public Win32() { natPreInit(); }
47
48     public void postInit() {
49         new Thread() { public void run() { natInit(); } }.start();
50         messagePumpStarted.block();
51     }
52
53
54     // Implementation of Platform methods /////////////////////////////////////////////////////////
55
56     protected native String _getEnv(String key);
57     protected boolean _needsAutoClick() { return true; }
58     protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
59     protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
60     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Win32PixelBuffer(w, h, (Win32Surface)owner); }
61     protected Picture _createPicture(JS r) { return new Win32Picture(r); }
62     protected native int _getScreenWidth();
63     protected native int _getScreenHeight();
64     protected boolean _supressDirtyOnResize() { return false; }
65     protected native void _criticalAbort(String message);
66     protected native String _getClipBoard();
67     protected native void _setClipBoard(String s);
68     protected boolean _isCaseSensitive() { return false; }
69
70     private native void __detectProxy(String[] container);
71
72     protected synchronized HTTP.Proxy _detectProxy() {
73
74         String[] container = new String[] { null, null, null };
75         if (Log.on) Log.info(this, "accessing Win32 registry");
76         __detectProxy(container);
77         if (container[2] == null && container[0] == null) {
78             if (Log.on) Log.info(this, "no proxy settings in the Win32 registry");
79             return null;
80         }
81         
82         if (Log.on) Log.info(this, "PAC Script URL: " + container[2]);
83         if (Log.on) Log.info(this, "Proxy Server String: " + container[0]);
84         if (Log.on) Log.info(this, "Proxy Override String: " + container[1]);
85
86         HTTP.Proxy ret = new HTTP.Proxy();
87         if (container[2] != null) {
88             // FIXME
89             //ret.proxyAutoConfigFunction = HTTP.Proxy.getProxyAutoConfigFunction(container[2]);
90             if (ret.proxyAutoConfigFunction != null) return ret;
91         }
92
93         if (container[0] == null) return null;
94         StringTokenizer st = new StringTokenizer(container[0], ";", false);
95         while(st.hasMoreTokens()) try {
96             String s = st.nextToken().trim();
97             String protocol, host;
98             if (s.indexOf(':') == -1) {
99                 continue;
100             } else if (s.indexOf("://") != -1) {
101                 protocol = s.substring(0, s.indexOf("://"));
102                 s = s.substring(s.indexOf("://") + 3);
103                 host = s.substring(0, s.indexOf(':'));
104             } else if (s.indexOf('=') == -1) {
105                 protocol = "http";
106                 host = s.substring(0, s.indexOf(':'));
107             } else {
108                 protocol = s.substring(0, s.indexOf('='));
109                 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
110             }
111             int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
112             if (protocol.equals("http")) {
113                 ret.httpProxyHost = host;
114                 ret.httpProxyPort = port;
115             } else if (protocol.equals("https")) {
116                 ret.httpsProxyHost = host;
117                 ret.httpsProxyPort = port;
118             } else if (protocol.equals("socks")) {
119                 ret.socksProxyHost = host;
120                 ret.socksProxyPort = port;
121             }
122         } catch (NumberFormatException nfe) { }
123
124         if (container[1] != null) {
125             st = new StringTokenizer(container[1], ";", false);
126             ret.excluded = new String[st.countTokens()];
127             for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
128         }
129         return ret;
130     }
131
132     protected native boolean _newBrowserWindow_(String url);
133     protected void _newBrowserWindow(String url) {
134         if (!_newBrowserWindow_(url))
135             if (Log.on) Log.info(this, "ShellExecuteEx() failed trying to open url " + url);
136     }
137
138     // Win32Surface ////////////////////////////////////////////////////////////////////////////
139
140     public static class Win32Surface extends Surface {
141
142     public PixelBuffer getPixelBuffer() { return null; }  // FIXME
143
144         // hack
145         String cursor;
146         
147         /** used to block while waiting for the message pump thread to create a hwnd for us */
148         public Semaphore hwndCreated = new Semaphore();
149
150         /** nothing more than a method version of WndProc, so we can access instance members/methods */
151         public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
152
153         /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
154         boolean inside = false;
155
156         /** true iff we have 'captured' the mouse with SetCapture() */
157         boolean captured = false;
158
159         public int hwnd = -1;
160         public int hdc = 0;
161
162         public int current_cursor = default_cursor;
163
164         /** used to restore the cursor immediately before ReleaseCapture() */
165         public int previous_cursor = 0;
166
167         public native void natInit(boolean framed);
168         public Win32Surface(Box b, final boolean framed) {
169             super(b);
170             natInit(framed);
171             hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
172         }
173
174         public void syncCursor() {
175             if (cursor.equals("default")) current_cursor = default_cursor;
176             else if (cursor.equals("wait")) current_cursor = wait_cursor;
177             else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
178             else if (cursor.equals("text")) current_cursor = text_cursor;
179             else if (cursor.equals("move")) current_cursor = move_cursor;
180             else if (cursor.equals("hand")) current_cursor = hand_cursor;
181             else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
182             else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
183             else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
184             else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
185             postCursorChange();
186         }
187
188         public native void finalize();
189         public native void postCursorChange();
190         public native void toBack();
191         public native void toFront();
192         public native void _setMinimized(boolean m);
193         public native void setInvisible(boolean i);
194         public native void _setMaximized(boolean m);
195         public native void _setSize(int w, int h);
196         public native void setLocation();
197         public native void setTitleBarText(String s);
198         public native void setIcon(Picture p);
199         public native void _dispose();
200         public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
201     }
202
203
204     // Win32Picture ////////////////////////////////////////////////////////////////////////////
205
206     public static class Win32Picture extends Picture {
207
208         /** the Win32 bitmap version of this Picture */
209         int hbitmap = -1;
210
211         /** dc of the bitmap */
212         int hdc = -1;
213
214         /** true iff this Picture has translucent regions */
215         boolean hasalpha = false;
216
217         /** true iff this Picture has transparent regions but no translucent regions */
218         boolean hasmask = true;
219
220         /** if hasmask, this mask indicates which regions are transparent */
221         int hmask = -1;
222
223         /** dc of the mask */
224         int maskdc = -1;
225
226         public Win32Picture(JS r) { super(r); }
227         public int getWidth() { return width; };
228         public int getHeight() { return height; };
229         public int[] getData() { return data; }
230         boolean initialized = false;
231         public void init() { if (!initialized && isLoaded) natInit(); initialized = true; }
232         public native void natInit();
233     }
234     
235
236     // Win32PixelBuffer //////////////////////////////////////////////////////////////////////////
237
238     public static class Win32PixelBuffer implements PixelBuffer {
239         public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argb) { throw new Error("not implemented"); }
240
241         public void drawPicture(Picture p, Affine a, Mesh h) { throw new Error("drawPicture() not implemented"); }
242         public void drawGlyph(Font.Glyph source,Affine a,Mesh h,int rgb,int bg) { throw new Error("drawGlyph() not implemented"); }
243
244     public void drawLine(int x1, int y1, int x2, int y2, int color) { }
245     public void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb, int pc){}
246     public void stroke(Polygon p, int color){}
247     public void fill(Polygon p, Paint paint){}
248         int w = 0;
249         int h = 0;
250
251         int clipx1 = 0;
252         int clipy1 = 0;
253         int clipx2 = 0;
254         int clipy2 = 0;
255
256         int hdc = -1;
257         int hbitmap = -1;
258
259         public int getHeight() { return h; }
260         public int getWidth() { return w; }
261
262         public native void natInit();
263         public Win32PixelBuffer(int w, int h, Win32Surface owner) {
264             this.w = w;
265             this.h = h;
266             clipx2 = w;
267             clipy2 = h;
268             natInit();
269         }
270
271         public native void fillRect(int x, int y, int x2, int y2, int color);
272         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
273             ((Win32Picture)source).init();
274             drawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
275         }
276         public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
277             Win32Picture p = ((Win32Picture)((Platform.DefaultGlyph)source).getPicture());
278             p.init();
279             drawPicture(p, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
280         }
281         public native void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
282                                        int rgb, boolean alphaOnly);
283
284         public native void finalize();
285
286         // FIXME: try to use os acceleration
287         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
288             if (x1 == x3 && x2 == x4) {
289                 fillRect(x1, y1, x4, y2, argb);
290             } else for(int y=y1; y<y2; y++) {
291                 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
292                 int _y1 = (int)Math.floor(y);
293                 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
294                 int _y2 = (int)Math.floor(y) + 1;
295                 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
296                 fillRect(_x1, _y1, _x2, _y2, argb);
297             }
298         }
299     }
300
301 }
302