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