initial checkin
[org.ibex.nanogoat.git] / src / org / ibex / plat / Darwin.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
2 // Authors: Brian Alliet and Evan Jones
3
4 package org.ibex.plat;
5
6 import gnu.gcj.RawData;
7 import org.ibex.util.*;
8 import org.ibex.js.*;
9 import org.ibex.*;
10
11 public class Darwin extends POSIX {
12     private static final Class openGLClass = OpenGL.class;
13     static Darwin singleton;
14     private CarbonOpenGL openGL;
15     boolean jaguar; // true if we are on OS X >= 10.2
16     
17     // General Methods
18     protected String _getAltKeyName() { return "Option"; }
19     protected boolean _needsAutoClick() { return false; }
20     protected boolean _needsAutoDoubleClick() { return false; }
21     protected String getDescriptiveName() { return "GCJ Darwin Binary"; }
22     protected boolean _isCaseSensitive() { return false; /* Well, not always, could be UFS */ }
23     
24     
25     // Native Methods
26     protected int    _getScreenWidth() { return cgScreenWidth(); }
27     protected int    _getScreenHeight() { return cgScreenHeight(); }
28     private native static int cgScreenWidth();
29     private native static int cgScreenHeight();
30     protected native void   _newBrowserWindow(String url);
31     protected native HTTP.Proxy   natDetectProxy();
32     private   native void    natInit();
33
34     private native String natGetClipBoard();
35     private native void natSetClipBoard(String text);
36     protected void _setClipBoard(final String text) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetClipBoard(text); } }); }
37     protected String _getClipBoard() {
38         final Semaphore sem = new Semaphore();
39         final String[] result = new String[1]; // Kind of like a pointer
40         CarbonMessage.add(new CarbonMessage() { public void perform() { result[0] = natGetClipBoard(); sem.release(); } });
41         sem.block();
42         return result[0];
43     }
44     
45     private static class FileDialogHelper {
46         public FileDialogHelper(boolean save) { this.save = save; }
47         public boolean save;
48         public Semaphore sem = new Semaphore();
49         public String fileName;
50         public String saveName;
51     }
52     private native void natFileDialog(FileDialogHelper helper, String suggestedFileName, boolean write);
53     protected String _fileDialog(final String fn, final boolean w) {
54         final FileDialogHelper helper = new FileDialogHelper(w);
55         CarbonMessage.add(new CarbonMessage() { public void perform() { natFileDialog(helper,fn,w); } });
56         helper.sem.block();
57         if(w)
58             return helper.fileName + "/" + helper.saveName;
59         else
60             return helper.fileName;
61     }
62
63     
64     static void abort(String err) {
65         throw new Error(err);
66     }
67     
68     public Darwin() {
69         synchronized(Darwin.class) {
70             if(singleton != null) abort("Tried to instansiate Darwin more than once");
71             singleton = this;
72         }
73     }
74     
75     protected synchronized HTTP.Proxy _detectProxy() {
76         return natDetectProxy();
77     }
78     
79     private static native final boolean isJaguar();
80     
81     public void postInit() {
82         jaguar = isJaguar();
83         try {
84             openGL = new CarbonOpenGL();
85             openGL.init();
86         } catch(OpenGL.NotSupportedException e) {
87             Log.info(this,"WARNING: OpenGL support not available: " + e);
88             // FEATURE: fall back to quartz 2d
89             throw new Error("No OpenGL support");
90         }
91         natInit();
92     }
93     
94     protected Scheduler _getScheduler() { return new DarwinScheduler(); }
95     protected native void runApplicationEventLoop();
96     private class DarwinScheduler extends org.ibex.Scheduler {
97         public void run() {
98             new Thread() { public void run() { defaultRun(); } }.start();
99             runApplicationEventLoop();
100         }
101     }
102     
103     private final class CarbonOpenGL extends OpenGL {
104         public RawData rawPixelFormat;
105         public RawData rawSharedContext;
106         public int maxAglSurfaceTexSize;
107         public int maxSurfaceWidth;
108         public int maxSurfaceHeight;
109         
110         private native boolean initPixelFormat();
111         private native void initSharedContext();
112         
113         public CarbonOpenGL() throws NotSupportedException {
114             if(!jaguar)
115                 throw new NotSupportedException("OpenGL requires Mac OS X 10.2 or greater");
116             if(!initPixelFormat())
117                 throw new NotSupportedException("Couldn't get an acceptable pixel format");
118             initSharedContext();
119         }
120         
121         public void init() throws NotSupportedException {
122             super.init();
123             maxAglSurfaceTexSize = rectangularTextures ? maxRectTexSize : maxTexSize;
124             if(renderer.startsWith("ATI Radeon 7500")) {
125                 maxAglSurfaceTexSize = Math.min(rectangularTextures ? 1600 : 1024,maxAglSurfaceTexSize);
126                 Log.info(this,"Working around Radeon 7500 bug: maxAglSurfaceTexSize: " + maxAglSurfaceTexSize);
127             }
128             maxSurfaceWidth = maxSurfaceHeight = maxAglSurfaceTexSize;
129         }
130         protected native void activateSharedContext();
131     }
132     
133     static abstract class CarbonSurface extends Surface.DoubleBufferedSurface {  
134         RawData rawWindowRef;
135         int modifiers;
136         int winWidth;
137         int winHeight;
138         
139         boolean pendingResize;
140          
141         private native void natSetInvisible(boolean i);
142         public void setInvisible(final boolean i) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetInvisible(i); } }); }
143         private native void nat_setMaximized(boolean b);
144         public void _setMaximized(final boolean b) { CarbonMessage.add(new CarbonMessage() { public void perform() { nat_setMaximized(b); } }); }
145         private native void nat_setMinimized(boolean b);
146         public void _setMinimized(final boolean b) { CarbonMessage.add(new CarbonMessage() { public void perform() { nat_setMinimized(b); } }); }
147         private native void natSetIcon(Picture p);
148         public void setIcon(final Picture p) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetIcon(p); } }); }
149         private native void natSetTitleBarText(String s);
150         public void setTitleBarText(final String s) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetTitleBarText(s); } }); }
151         private native void natSetSize(int w, int h);
152         public void _setSize(final int w, final int h) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetSize(w,h); } }); }
153         private native void natSetLocation();
154         public void setLocation() { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetLocation(); } }); }
155         private native void natToFront();
156         public void toFront() { CarbonMessage.add(new CarbonMessage() { public void perform() { natToFront(); } }); }
157         private native void natToBack();
158         public void toBack() { CarbonMessage.add(new CarbonMessage() { public void perform() { natToBack(); } }); }
159         private native void natSetLimits(int minWidth, int minHeight, int maxWidth, int maxHeight);
160         public void setLimits(final int mnw, final int mnh, final int mxw, final int mxh) {
161             if(Darwin.singleton.jaguar)
162                 CarbonMessage.add(new CarbonMessage() { public void perform() { natSetLimits(mnw,mnh,mxw,mxh); } });
163         }
164         private native void natSyncCursor(int n);
165         public void syncCursor() {
166             int n;
167             if(cursor.equals("default")) n = 0;
168             else if(cursor.equals("wait")) n = 1;
169             else if(cursor.equals("crosshair")) n = 2;
170             else if(cursor.equals("text")) n = 3;
171             else if(cursor.equals("hand")) n = 4;
172             else if(cursor.equals("move")) n = 5;
173             else if(cursor.equals("east") || cursor.equals("west")) n = 6;
174             else n = 0; 
175             final int n_ = n;
176             CarbonMessage.add(new CarbonMessage() { public void perform() { natSyncCursor(n_); } });
177         }
178        
179         /* Drawing stuff */
180         public abstract void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
181
182         public final void _dispose() { CarbonMessage.add(new CarbonMessage() { public void perform() { natDispose(); } }); }
183         public native void natDispose();
184         
185         public final native void natInit(boolean framed);
186         
187         public CarbonSurface(Box root, final boolean framed) {
188             super(root);
189             final Semaphore sem = new Semaphore();
190             CarbonMessage.add(new CarbonMessage() { public void perform() { CarbonSurface.this.natInit(framed); sem.release(); } });
191             sem.block();
192         }
193         
194         public void needsReshape() { }
195         protected static native void blitLock();
196         protected static native void blitUnlock();
197         protected static native void blitWait();
198     }
199     
200     static class GLCarbonPixelBuffer extends OpenGL.GLPixelBuffer {
201         RawData rawCTX;
202         RawData rawWindowRef;
203         int textureName;
204         boolean rectTexture;
205         CarbonOpenGL gl;
206         
207         private native void natInit();
208         private static native void natCleanup(RawData rawWindowRef, RawData rawCTX);
209         
210         
211         private static final int fixupDimension(CarbonOpenGL gl, int n) {
212             if(!gl.rectangularTextures) n = OpenGL.roundToPowerOf2(n);
213             return Math.min(n,gl.maxAglSurfaceTexSize);
214         }
215         public GLCarbonPixelBuffer(int w, int h, final CarbonOpenGL gl) {
216             super(fixupDimension(gl,w),fixupDimension(gl,h));
217             this.gl = gl;
218             rectTexture = gl.hasRectangularTextures();
219             final Semaphore sem = new Semaphore();
220             CarbonMessage.add(new CarbonMessage() { public void perform() { GLCarbonPixelBuffer.this.natInit(); sem.release(); } });
221             sem.block();
222         }
223         public native void activateContext();
224         protected void finalize() {
225             CarbonMessage.add(new CarbonMessage() { public void perform() { natCleanup(rawWindowRef,rawCTX); } });
226             gl.deleteTexture(textureName);
227         }
228     }
229     
230     static class GLCarbonSurface extends CarbonSurface {
231         RawData rawCTX;
232         CarbonOpenGL gl;
233         boolean needsReshape;
234                 
235         private final native void natInit();
236         private final native void flush();
237         private final native void clear();
238         
239         public GLCarbonSurface(Box root, boolean framed, CarbonOpenGL gl) {
240             super(root,framed);
241             this.gl = gl;
242             natInit();
243         }
244         
245         public void setLimits(int mnw,int mnh, int mxw, int mxh) {
246             mxw = Math.min(mxw,gl.maxSurfaceWidth);
247             mxh = Math.min(mxh,gl.maxSurfaceHeight);
248             super.setLimits(mnw,mnh,mxw,mxh);
249         }
250         
251         public void _setSize(int w, int h) {
252             w = Math.min(w,gl.maxSurfaceWidth);
253             h = Math.min(h,gl.maxSurfaceWidth);
254             super._setSize(w,h);
255         }
256         
257         private native void natBlit(GLCarbonPixelBuffer db, int sx, int sy, int dx, int dy, int dx2, int dy2);
258         public void blit(PixelBuffer db, int sx, int sy, int dx, int dy, int dx2, int dy2) {
259             natBlit((GLCarbonPixelBuffer)db,sx,sy,dx,dy,dx2,dy2);
260         }
261         
262         
263         // The blit_lock ensures the window size does not change through the entire blit operation.
264         public void render() {
265             /*
266             blitLock();
267             while(pendingResize) blitWait();
268             */
269             if(needsReshape) {
270                 needsReshape = false;
271                 
272                 reshape(winWidth,winHeight);
273                 clear();
274                 Dirty(0,0,winWidth,winHeight);
275             }
276             super.render();
277             flush();
278             /*
279             blitUnlock();
280             */
281         }
282         
283         private native void reshape(int w, int h);
284         // blit_lock is assumed to be held
285         public void needsReshape() { needsReshape = true; }
286
287         public native void natDispose();
288     }
289
290     /*private class QZCarbonPixelBuffer extends PixelBuffer {
291         
292         public QZCarbonPixelBuffer(int width, int height) {
293         }
294     }
295        private class QZCarbonSurface extends CarbonSurface {
296         public QZCarbonSurface(Box root, boolean framed) {
297             super(b,root);
298         }
299         public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
300     }
301     
302     private class QZCarbonPicture extends Picture {
303         int width;
304         int height;
305         
306         public final int getWidth() { return width; }
307         public final int getHeight() { return height; }
308                 
309         public QZCarbonPicture(int w, int h) {
310             this.width = w;
311             this.height = h;
312         }
313     }*/
314     
315     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) {
316         if(openGL != null)
317             return new GLCarbonPixelBuffer(w,h,openGL);
318         else
319             return /*new QZCarbonPixelBuffer(w,h)*/ null;
320     }
321     protected Surface _createSurface(Box b, boolean framed) {
322         if(openGL != null)
323             return new GLCarbonSurface(b,framed, openGL);
324         else
325             return /*new QZCarbonSufrace(b,framed)*/ null;
326     }
327     protected Picture _createPicture(JS r) {
328         if(openGL != null)
329             return openGL._createPicture(r, true);
330         else
331             return /*new QZCarbonPicture(data,w,h);*/ null;
332     }
333     protected org.ibex.Font.Glyph  _createGlyph(org.ibex.Font f, char c) {
334         if(openGL != null)
335             return openGL._createGlyph(f, c);
336         else
337             return super.createGlyph(f, c);
338     }
339     
340     /* A message that is sent through the carbon event queue */
341     private static abstract class CarbonMessage {
342         public abstract void perform();
343         
344         static { natInit(); }
345         public static native void natInit();
346         public static native void add(CarbonMessage m);
347     }
348 }