1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
2 // Authors: Brian Alliet and Evan Jones
6 import gnu.gcj.RawData;
12 public class Darwin extends POSIX {
13 static Darwin singleton;
14 private CarbonOpenGL openGL;
15 boolean jaguar; // true if we are on OS X >= 10.2
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 */ }
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();
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(); } });
45 private static class FileDialogHelper {
46 public FileDialogHelper(boolean save) { this.save = save; }
48 public Semaphore sem = new Semaphore();
49 public String fileName;
50 public String saveName;
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); } });
58 return helper.fileName + "/" + helper.saveName;
60 return helper.fileName;
64 static void abort(String err) {
69 synchronized(Darwin.class) {
70 if(singleton != null) abort("Tried to instansiate Darwin more than once");
75 protected synchronized HTTP.Proxy _detectProxy() {
76 return natDetectProxy();
79 private static native final boolean isJaguar();
81 public void postInit() {
84 openGL = new CarbonOpenGL();
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");
94 protected native void runApplicationEventLoop();
95 private class DarwinScheduler extends org.xwt.Scheduler {
97 new Thread() { public void run() { defaultRun(); } }.start();
98 runApplicationEventLoop();
102 private final class CarbonOpenGL extends OpenGL {
103 public RawData rawPixelFormat;
104 public RawData rawSharedContext;
105 public int maxAglSurfaceTexSize;
106 public int maxSurfaceWidth;
107 public int maxSurfaceHeight;
109 private native boolean initPixelFormat();
110 private native void initSharedContext();
112 public CarbonOpenGL() throws NotSupportedException {
114 throw new NotSupportedException("OpenGL requires Mac OS X 10.2 or greater");
115 if(!initPixelFormat())
116 throw new NotSupportedException("Couldn't get an acceptable pixel format");
120 public void init() throws NotSupportedException {
122 maxAglSurfaceTexSize = rectangularTextures ? maxRectTexSize : maxTexSize;
123 if(renderer.startsWith("ATI Radeon 7500")) {
124 maxAglSurfaceTexSize = Math.min(rectangularTextures ? 1600 : 1024,maxAglSurfaceTexSize);
125 Log.info(this,"Working around Radeon 7500 bug: maxAglSurfaceTexSize: " + maxAglSurfaceTexSize);
127 maxSurfaceWidth = maxSurfaceHeight = maxAglSurfaceTexSize;
129 protected native void activateSharedContext();
132 static abstract class CarbonSurface extends Surface.DoubleBufferedSurface {
133 RawData rawWindowRef;
138 boolean pendingResize;
140 private native void natSetInvisible(boolean i);
141 public void setInvisible(final boolean i) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetInvisible(i); } }); }
142 private native void nat_setMaximized(boolean b);
143 public void _setMaximized(final boolean b) { CarbonMessage.add(new CarbonMessage() { public void perform() { nat_setMaximized(b); } }); }
144 private native void nat_setMinimized(boolean b);
145 public void _setMinimized(final boolean b) { CarbonMessage.add(new CarbonMessage() { public void perform() { nat_setMinimized(b); } }); }
146 private native void natSetIcon(Picture p);
147 public void setIcon(final Picture p) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetIcon(p); } }); }
148 private native void natSetTitleBarText(String s);
149 public void setTitleBarText(final String s) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetTitleBarText(s); } }); }
150 private native void natSetSize(int w, int h);
151 public void _setSize(final int w, final int h) { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetSize(w,h); } }); }
152 private native void natSetLocation();
153 public void setLocation() { CarbonMessage.add(new CarbonMessage() { public void perform() { natSetLocation(); } }); }
154 private native void natToFront();
155 public void toFront() { CarbonMessage.add(new CarbonMessage() { public void perform() { natToFront(); } }); }
156 private native void natToBack();
157 public void toBack() { CarbonMessage.add(new CarbonMessage() { public void perform() { natToBack(); } }); }
158 private native void natSetLimits(int minWidth, int minHeight, int maxWidth, int maxHeight);
159 public void setLimits(final int mnw, final int mnh, final int mxw, final int mxh) {
160 if(Darwin.singleton.jaguar)
161 CarbonMessage.add(new CarbonMessage() { public void perform() { natSetLimits(mnw,mnh,mxw,mxh); } });
163 private native void natSyncCursor(int n);
164 public void syncCursor() {
166 if(cursor.equals("default")) n = 0;
167 else if(cursor.equals("wait")) n = 1;
168 else if(cursor.equals("crosshair")) n = 2;
169 else if(cursor.equals("text")) n = 3;
170 else if(cursor.equals("hand")) n = 4;
171 else if(cursor.equals("move")) n = 5;
172 else if(cursor.equals("east") || cursor.equals("west")) n = 6;
175 CarbonMessage.add(new CarbonMessage() { public void perform() { natSyncCursor(n_); } });
179 public abstract void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
181 public final void _dispose() { CarbonMessage.add(new CarbonMessage() { public void perform() { natDispose(); } }); }
182 public native void natDispose();
184 public final native void natInit(boolean framed);
186 public CarbonSurface(Box root, final boolean framed) {
188 final Semaphore sem = new Semaphore();
189 CarbonMessage.add(new CarbonMessage() { public void perform() { CarbonSurface.this.natInit(framed); sem.release(); } });
193 public void needsReshape() { }
194 protected static native void blitLock();
195 protected static native void blitUnlock();
196 protected static native void blitWait();
199 static class GLCarbonPixelBuffer extends OpenGL.GLPixelBuffer {
201 RawData rawWindowRef;
206 private native void natInit();
207 private static native void natCleanup(RawData rawWindowRef, RawData rawCTX);
210 private static final int fixupDimension(CarbonOpenGL gl, int n) {
211 if(!gl.rectangularTextures) n = OpenGL.roundToPowerOf2(n);
212 return Math.min(n,gl.maxAglSurfaceTexSize);
214 public GLCarbonPixelBuffer(int w, int h, final CarbonOpenGL gl) {
215 super(fixupDimension(gl,w),fixupDimension(gl,h));
217 rectTexture = gl.hasRectangularTextures();
218 final Semaphore sem = new Semaphore();
219 CarbonMessage.add(new CarbonMessage() { public void perform() { GLCarbonPixelBuffer.this.natInit(); sem.release(); } });
222 public native void activateContext();
223 protected void finalize() {
224 CarbonMessage.add(new CarbonMessage() { public void perform() { natCleanup(rawWindowRef,rawCTX); } });
225 gl.deleteTexture(textureName);
229 static class GLCarbonSurface extends CarbonSurface {
232 boolean needsReshape;
234 private final native void natInit();
235 private final native void flush();
236 private final native void clear();
238 public GLCarbonSurface(Box root, boolean framed, CarbonOpenGL gl) {
244 public void setLimits(int mnw,int mnh, int mxw, int mxh) {
245 mxw = Math.min(mxw,gl.maxSurfaceWidth);
246 mxh = Math.min(mxh,gl.maxSurfaceHeight);
247 super.setLimits(mnw,mnh,mxw,mxh);
250 public void _setSize(int w, int h) {
251 w = Math.min(w,gl.maxSurfaceWidth);
252 h = Math.min(h,gl.maxSurfaceWidth);
256 private native void natBlit(GLCarbonPixelBuffer db, int sx, int sy, int dx, int dy, int dx2, int dy2);
257 public void blit(PixelBuffer db, int sx, int sy, int dx, int dy, int dx2, int dy2) {
258 natBlit((GLCarbonPixelBuffer)db,sx,sy,dx,dy,dx2,dy2);
262 // The blit_lock ensures the window size does not change through the entire blit operation.
263 public void render() {
265 while(pendingResize) blitWait();
267 needsReshape = false;
269 reshape(winWidth,winHeight);
271 Dirty(0,0,winWidth,winHeight);
272 //Dirty(0,0,getWidth(),getHeight());
279 private native void reshape(int w, int h);
280 // blit_lock is assumed to be held
281 public void needsReshape() { needsReshape = true; }
283 public native void natDispose();
286 /*private class QZCarbonPixelBuffer extends PixelBuffer {
288 public QZCarbonPixelBuffer(int width, int height) {
291 private class QZCarbonSurface extends CarbonSurface {
292 public QZCarbonSurface(Box root, boolean framed) {
295 public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
298 private class QZCarbonPicture extends Picture {
302 public final int getWidth() { return width; }
303 public final int getHeight() { return height; }
305 public QZCarbonPicture(int w, int h) {
311 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) {
313 return new GLCarbonPixelBuffer(w,h,openGL);
315 return /*new QZCarbonPixelBuffer(w,h)*/ null;
317 protected Surface _createSurface(Box b, boolean framed) {
319 return new GLCarbonSurface(b,framed, openGL);
321 return /*new QZCarbonSufrace(b,framed)*/ null;
323 protected Picture _createPicture(JS r) {
325 return openGL._createPicture(r, true);
327 return /*new QZCarbonPicture(data,w,h);*/ null;
329 protected org.xwt.Font.Glyph _createGlyph(org.xwt.Font f, char c) {
331 return openGL._createGlyph(f, c);
333 return super.createGlyph(f, c);
336 /* A message that is sent through the carbon event queue */
337 private static abstract class CarbonMessage {
338 public abstract void perform();
340 static { natInit(); }
341 public static native void natInit();
342 public static native void add(CarbonMessage m);