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