2003/09/20 05:03:47
[org.ibex.core.git] / src / org / xwt / plat / Darwin.cc
index 914ba77..068b766 100644 (file)
@@ -7,33 +7,40 @@
 #define APPLE_GCC
 #endif
 
+#define __private_extern__
+#include <mach-o/dyld.h>
+
 #include "POSIX.cc"
 #include "OpenGL.cc"
 
 #include <java/lang/Object.h>
 #include <java/lang/Error.h>
 
-#include <org/xwt/plat/Carbon.h>
-#include <org/xwt/plat/Carbon$CarbonSurface.h>
-#include <org/xwt/plat/Carbon$GLCarbonSurface.h>
-#include <org/xwt/plat/Carbon$GLCarbonDoubleBuffer.h>
-#include <org/xwt/plat/Carbon$CarbonMessage.h>
-#include <org/xwt/plat/Carbon$CarbonOpenGL.h>
-#include <org/xwt/plat/Carbon$FileDialogHelper.h>
+#include <org/xwt/plat/Darwin.h>
+#include <org/xwt/plat/Darwin$CarbonSurface.h>
+#include <org/xwt/plat/Darwin$GLCarbonSurface.h>
+#include <org/xwt/plat/Darwin$GLCarbonPixelBuffer.h>
+#include <org/xwt/plat/Darwin$CarbonMessage.h>
+#include <org/xwt/plat/Darwin$CarbonOpenGL.h>
+#include <org/xwt/plat/Darwin$FileDialogHelper.h>
 #include <org/xwt/plat/GCJ$Retainer.h>
-#include <org/xwt/Proxy.h>
+#include <org/xwt/HTTP$Proxy.h>
 #include <org/xwt/util/Semaphore.h>
 
-#include <Carbon/Carbon.h>
-#include <CoreFoundation/CoreFoundation.h>
-// For proxy stuff
-#include <SystemConfiguration/SystemConfiguration.h>
-// For LSOpenURLRef
-#include <ApplicationServices/ApplicationServices.h>
+#include <stdlib.h>
+#include <objc/objc.h>
 
-#include <AGL/agl.h>
+#include "DarwinCarbonHeaders.h"
 
-#include <stdlib.h>
+#define XWT_CARBON_NO_BUNDLE_HACK
+#ifdef XWT_CARBON_NO_BUNDLE_HACK
+extern "C" {
+    OSErr CPSEnableForegroundOperation(ProcessSerialNumber *psn);
+    OSErr CPSSetFrontProcess(ProcessSerialNumber *psn);
+}
+#endif
+
+#include "DarwinWeakSymbols.h"
 
 using namespace org::xwt::plat;
 using gnu::gcj::RawData;
@@ -42,13 +49,13 @@ using java::lang::Object;
 
 namespace org { namespace xwt { namespace plat {
 
-namespace carbon { }
-using namespace carbon;
+namespace darwin { }
+using namespace darwin;
+
+#pragma mark ------ Darwin Namespace ------
 
-#pragma mark ------ Carbon Namespace ------
-namespace carbon {
-    // We put everything that isn't in org.xwt.plat.Carbon in 
-    // org.xwt.plat.carbon to prevent namespace conflicts
+    // We put everything that isn't in org.xwt.plat.Darwin in 
+    // org.xwt.plat.darwin to prevent namespace conflicts
     
     template <bool CHECK> static inline int CompileTimeCheck() { const int something_is_wrong=1; something_is_wrong++; return 0; }
     template <> static inline int CompileTimeCheck<true>() { return 0; }
@@ -125,28 +132,32 @@ namespace carbon {
     const static UInt32 kEventCarbonMessage = 'xwtb';
     const static UInt32 kEventParamCarbonMessage = 'xwtc';
     
-    pascal OSStatus carbon::carbonMessageEventHandler(EventHandlerCallRef handler, EventRef e, void *userData);
+    OSStatus our_carbonMessageEventHandler(EventHandlerCallRef handler, EventRef e, void *userData);
     void fileDialogEventHandler(NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, void *userData);
 
-} // end namespace
 
-jboolean Carbon::isJaguar() {
+
+jboolean Darwin::isJaguar() {
     SInt32 version;
     OSStatus r = Gestalt(gestaltSystemVersion, &version);
     checkStatus(r,"Gestalt");
     return version >= 0x1020;
 }
 
-void Carbon$CarbonMessage::natInit() {
+void Darwin::linkMe() {
+    load_weak_symbols();
+}
+
+void Darwin$CarbonMessage::natInit() {
     OSStatus r;
     
-    EventHandlerUPP upp = NewEventHandlerUPP(carbonMessageEventHandler);
+    EventHandlerUPP upp = NewEventHandlerUPP(our_carbonMessageEventHandler);
     EventTypeSpec eventTypes = { kEventClassCarbonMessage, kEventCarbonMessage };
     r = InstallEventHandler(GetApplicationEventTarget(),upp,1,&eventTypes,NULL,NULL);
     checkStatus(r,"InstallEventHandler");
 }
 
-void Carbon$CarbonMessage::add(Carbon$CarbonMessage *msg) {
+void Darwin$CarbonMessage::add(Darwin$CarbonMessage *msg) {
     EventRef event;
     OSStatus r;
     
@@ -162,11 +173,11 @@ void Carbon$CarbonMessage::add(Carbon$CarbonMessage *msg) {
 }
 
 
-pascal OSStatus carbon::carbonMessageEventHandler(EventHandlerCallRef handler, EventRef e, void *userData) {
+OSStatus our_carbonMessageEventHandler(EventHandlerCallRef handler, EventRef e, void *userData) {
     UInt32 eKind = GetEventKind(e);
     UInt32 eClass = GetEventClass(e);
     OSStatus r;
-    Carbon$CarbonMessage *msg;
+    Darwin$CarbonMessage *msg;
     if(eClass != kEventClassCarbonMessage || eKind != kEventCarbonMessage)
         return eventNotHandledErr;
     r = GetEventParameter(e,kEventParamCarbonMessage,typeVoidPtr,NULL,sizeof(msg),NULL,&msg);
@@ -179,7 +190,7 @@ pascal OSStatus carbon::carbonMessageEventHandler(EventHandlerCallRef handler, E
 
 #pragma mark ------ Utility Functions ------
 
-void carbon::funcFailed(char *func,int r){
+void funcFailed(char *func,int r){
     fprintf(stderr,"%s() failed (%d)\n",func,r);
     exit(EXIT_FAILURE);
 } 
@@ -189,7 +200,7 @@ void carbon::funcFailed(char *func,int r){
 #pragma mark ----- Carbon Surface Methods ----
 
 
-void Carbon$CarbonSurface::natSyncCursor(jint n) {
+void Darwin$CarbonSurface::natSyncCursor(jint n) {
     ThemeCursor c;
     // see Carbon.java for what these numbers mean
     switch(n) {
@@ -204,46 +215,46 @@ void Carbon$CarbonSurface::natSyncCursor(jint n) {
     SetThemeCursor(c);
 }
    
-void Carbon$CarbonSurface::natSetInvisible(jboolean b) {
+void Darwin$CarbonSurface::natSetInvisible(jboolean b) {
     WindowRef window = (WindowRef) rawWindowRef;
     fprintf(stderr,"Making window %s\n",b?"invisible":"visible");
     if(b) HideWindow(window);
     else ShowWindow(window);
 }
 
-void Carbon$CarbonSurface::nat_setMaximized(jboolean b) {
+void Darwin$CarbonSurface::nat_setMaximized(jboolean b) {
     WindowRef window = (WindowRef) rawWindowRef;
     Point ideal = { 10000, 10000 };
     OSStatus r = ZoomWindowIdeal(window,(b?inZoomOut:inZoomIn),&ideal);
     checkStatus(r,"ZoomWindowIdeal");
 }
 
-void Carbon$CarbonSurface::nat_setMinimized(jboolean b) {
+void Darwin$CarbonSurface::nat_setMinimized(jboolean b) {
     WindowRef window = (WindowRef) rawWindowRef;
     if((IsWindowCollapsed(window) ? 1 : 0) == (b ? 1 : 0)) return;
     OSStatus r = CollapseWindow(window,b);
     checkStatus(r,"CollapseWindow");
 }
 
-void Carbon$CarbonSurface::natSetTitleBarText(jstring js) {
+void Darwin$CarbonSurface::natSetTitleBarText(jstring js) {
     SmartCFString s = js;
     WindowRef window = (WindowRef) rawWindowRef;
     SetWindowTitleWithCFString(window,s);
 }
 
-void Carbon$CarbonSurface::natToBack() {
+void Darwin$CarbonSurface::natToBack() {
     WindowRef window = (WindowRef) rawWindowRef;
     SendBehind(window,NULL);
 }
 
-void Carbon$CarbonSurface::natToFront() {
+void Darwin$CarbonSurface::natToFront() {
     WindowRef window = (WindowRef) rawWindowRef;
     fprintf(stderr,"SelectWindow()\n");
     SelectWindow(window);
 }
 
 #pragma mark ---- Window Event Handler ----
-namespace carbon {
+namespace darwin {
 static const EventTypeSpec eventTypeSpecs[] = {
     // kEventClassCommand
     // { kEventClassCommand, ??? },
@@ -273,8 +284,8 @@ static const EventTypeSpec eventTypeSpecs[] = {
     { kEventClassMouse, kEventMouseWheelMoved },
 };
     
-pascal OSStatus windowEventHandler(EventHandlerCallRef handler, EventRef e, void *userData) {
-    Carbon$CarbonSurface *surface = (Carbon$CarbonSurface*) userData;
+OSStatus our_windowEventHandler(EventHandlerCallRef handler, EventRef e, void *userData) {
+    Darwin$CarbonSurface *surface = (Darwin$CarbonSurface*) userData;
     UInt32 eKind = GetEventKind(e);
     UInt32 eClass = GetEventClass(e);
     OSStatus r;
@@ -453,11 +464,11 @@ pascal OSStatus windowEventHandler(EventHandlerCallRef handler, EventRef e, void
             r = GetEventParameter(e,kEventParamDirectObject,typeWindowRef,NULL,sizeof(window),NULL,&window);
             checkStatus(r,"kEventClassWindow/GetEventParameter");
             
-            if((RawData*)window != surface->rawWindowRef) Carbon::abort(JvNewStringLatin1("window != surface->window"));
+            if((RawData*)window != surface->rawWindowRef) Darwin::abort(JvNewStringLatin1("window != surface->window"));
             
             switch(eKind) {
                 case kEventWindowUpdate: {
-                    surface->Dirty(0,0,surface->width,surface->height);
+                    surface->Dirty(0,0,surface->root->width,surface->root->height);
                     return noErr;
                 }
                 case kEventWindowBoundsChanged: {
@@ -517,7 +528,7 @@ pascal OSStatus windowEventHandler(EventHandlerCallRef handler, EventRef e, void
 }
 } // end namespace
 
-void Carbon$CarbonSurface::natInit(jboolean framed) {
+void Darwin$CarbonSurface::natInit(jboolean framed) {
     WindowRef window;
     Rect rect;
     WindowClass wc = framed ? kDocumentWindowClass : kPlainWindowClass;
@@ -530,9 +541,18 @@ void Carbon$CarbonSurface::natInit(jboolean framed) {
     rect.top = 0; rect.left = 0; rect.bottom = 10; rect.right=10;
     r = CreateNewWindow(wc,attr,&rect,&window);
     checkStatus(r,"CreateNewWindow");
+
+    id NSWindowClass = objc_getClass("NSWindow");
+    printf("nswindowclass is %x\n", NSWindowClass);
+    SEL selector = sel_registerName("initWithWindowRef");
+    printf("selector is %s\n", selector);
+    objc_method method = class_getClassMethod(NSWindowClass, selector);
+    printf("method is %x\n", method);
+    id windowInstance = objc_msgSend(NSWindowClass, selector, window);
+    printf("instance is %x\n", windowInstance);
     
     GCJ$Retainer::retain(this); // Need to account for the EventHandlers pointer to us
-    EventHandlerUPP upp = NewEventHandlerUPP(windowEventHandler);
+    EventHandlerUPP upp = NewEventHandlerUPP(our_windowEventHandler);
     
     r = InstallWindowEventHandler(window,upp,sizeof(eventTypeSpecs)/sizeof(EventTypeSpec),eventTypeSpecs,this,NULL);
     checkStatus(r,"InstallWindowEventHandler");
@@ -541,15 +561,15 @@ void Carbon$CarbonSurface::natInit(jboolean framed) {
     rawEventHandlerUPP = (RawData*) upp;  
 }
 
-void Carbon$CarbonSurface::natDispose() {
+void Darwin$CarbonSurface::natDispose() {
     WindowRef window = (WindowRef) rawWindowRef;
     DisposeWindow(window);
 }
 
-void Carbon$CarbonSurface::natSetIcon(org::xwt::Picture *_p) {
+void Darwin$CarbonSurface::natSetIcon(org::xwt::Picture *_p) {
 }
 
-void Carbon$CarbonSurface::natSetLocation(jint x, jint y) {
+void Darwin$CarbonSurface::natSetLocation(jint x, jint y) {
     WindowRef window = (WindowRef) rawWindowRef;
     Rect rect;
     OSStatus r = GetWindowBounds(window,kWindowStructureRgn,&rect);
@@ -564,7 +584,7 @@ void Carbon$CarbonSurface::natSetLocation(jint x, jint y) {
     checkStatus(r,"ConstrainWindowToScreen");
 }
 
-void Carbon$CarbonSurface::natSetSize(jint w, jint h) {
+void Darwin$CarbonSurface::natSetSize(jint w, jint h) {
     WindowRef window = (WindowRef) rawWindowRef;
     Rect rect;
     OSStatus r = GetWindowBounds(window,kWindowStructureRgn,&rect);
@@ -577,7 +597,7 @@ void Carbon$CarbonSurface::natSetSize(jint w, jint h) {
     checkStatus(r,"ConstrainWindowToScreen");
 }
 
-void Carbon$CarbonSurface::natSetLimits(jint minw, jint minh, jint maxw, jint maxh) {
+void Darwin$CarbonSurface::natSetLimits(jint minw, jint minh, jint maxw, jint maxh) {
     WindowRef window = (WindowRef) rawWindowRef;
     const int maxMax = 32767;
     const int minMinW = 80;
@@ -593,8 +613,8 @@ void Carbon$CarbonSurface::natSetLimits(jint minw, jint minh, jint maxw, jint ma
 
 
 #pragma mark ------ Carbon Methods ------
-void carbon::fileDialogEventHandler(NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, void *userData) {
-    Carbon$FileDialogHelper *helper = (Carbon$FileDialogHelper*) userData;
+void fileDialogEventHandler(NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, void *userData) {
+    Darwin$FileDialogHelper *helper = (Darwin$FileDialogHelper*) userData;
     NavDialogRef dialog = callBackParms->context;
     OSStatus r;
     switch(callBackSelector) {
@@ -629,7 +649,7 @@ void carbon::fileDialogEventHandler(NavEventCallbackMessage callBackSelector, Na
     }
 }
 
-void Carbon::natFileDialog(Carbon$FileDialogHelper *helper,jstring suggestion_, jboolean write) {
+void Darwin::natFileDialog(Darwin$FileDialogHelper *helper,jstring suggestion_, jboolean write) {
     NavDialogRef dlg;
     SmartCFString suggestion = suggestion_;
     CFStringRef message = CFSTR("By selecting a file in this dialog you are giving this XWT application permission to access that file.");
@@ -637,7 +657,7 @@ void Carbon::natFileDialog(Carbon$FileDialogHelper *helper,jstring suggestion_,
     WindowRef window = FrontWindow();
     NavDialogCreationOptions options;
         
-    NavEventUPP handler = NewNavEventUPP(carbon::fileDialogEventHandler);
+    NavEventUPP handler = NewNavEventUPP(fileDialogEventHandler);
     
     NavGetDefaultDialogCreationOptions(&options);
     options.optionFlags =
@@ -660,7 +680,7 @@ void Carbon::natFileDialog(Carbon$FileDialogHelper *helper,jstring suggestion_,
     NavDialogRun(dlg);
 }
 
-jstring Carbon::natGetClipBoard() {
+jstring Darwin::natGetClipBoard() {
     ScrapRef scrap;
     OSStatus r;
     Size size,size2;
@@ -685,7 +705,7 @@ jstring Carbon::natGetClipBoard() {
     return js;
 }
 
-void Carbon::natSetClipBoard(jstring js) {
+void Darwin::natSetClipBoard(jstring js) {
     unsigned int length = js->length();
     ScrapRef scrap;
     OSStatus r;
@@ -697,9 +717,9 @@ void Carbon::natSetClipBoard(jstring js) {
     checkStatus(r,"PutScrapFlavor");
 }
 
-Proxy *Carbon::natDetectProxy() {
-    using org::xwt::Proxy;
-    Proxy *p=0;
+HTTP$Proxy *Darwin::natDetectProxy() {
+    using org::xwt::HTTP$Proxy;
+    HTTP$Proxy *p=0;
     CFStringRef string;
     CFNumberRef number;
     SmartCFString smartString;
@@ -718,7 +738,7 @@ Proxy *Carbon::natDetectProxy() {
         number = (CFNumberRef) CFDictionaryGetValue(proxyInfo, kSCPropNetProxies ## proto ## Port); \
         if(number != NULL && CFGetTypeID(number) != CFNumberGetTypeID()) number = NULL;    \
         if(string && number && CFNumberGetValue(number,kCFNumberIntType,&i) && i) {        \
-            if(!p) p = new Proxy();                                                        \
+            if(!p) p = new HTTP$Proxy();                                                        \
             p->var ## ProxyHost = cfStringToJString(string);                           \
             p->var ## ProxyPort = i;                                                   \
         }                                                                                  \
@@ -806,15 +826,15 @@ doproto(SOCKS,socks)
 */
 }
 
-jint Carbon::cgScreenWidth() {
-    return CGDisplayPixelsWide(kCGDirectMainDisplay);
+jint Darwin::cgScreenWidth() {
+    return CGDisplayPixelsWide(((CGDirectDisplayID)0));
 }
 
-jint Carbon::cgScreenHeight() {
-    return CGDisplayPixelsHigh(kCGDirectMainDisplay);
+jint Darwin::cgScreenHeight() {
+    return CGDisplayPixelsHigh(((CGDirectDisplayID)0));
 }
 
-void Carbon::_newBrowserWindow(jstring js) {
+void Darwin::_newBrowserWindow(jstring js) {
     SmartCFString cfs = js;
     CFURLRef url = CFURLCreateWithString(kCFAllocatorDefault,cfs,NULL);
     SmartCFString scheme = CFURLCopyScheme(url);
@@ -823,18 +843,11 @@ void Carbon::_newBrowserWindow(jstring js) {
     CFRelease(url);
 }
 
-void Carbon::_exit() {
+void Darwin::_exit() {
     QuitApplicationEventLoop();
 }
 
-#define XWT_CARBON_NO_BUNDLE_HACK
-#ifdef XWT_CARBON_NO_BUNDLE_HACK
-extern "C" {
-    OSErr CPSEnableForegroundOperation(ProcessSerialNumber *psn);
-    OSErr CPSSetFrontProcess(ProcessSerialNumber *psn);
-}
-#endif
-void Carbon::natInit() {     
+void Darwin::natInit() {     
     OSStatus r;
     #ifdef XWT_CARBON_NO_BUNDLE_HACK
     {        
@@ -862,19 +875,19 @@ void Carbon::natInit() {
     #endif
 }
 
-void Carbon::_running() {
+void Darwin::_running() {
     RunApplicationEventLoop();
     ExitToShell();
 }
 
 #pragma mark ------ OpenGL Functions -----
 
-void Carbon$CarbonOpenGL::activateSharedContext() {
+void Darwin$CarbonOpenGL::activateSharedContext() {
     AGLContext ctx = (AGLContext) rawSharedContext;
     aglSetCurrentContext(ctx);
 }
 
-jboolean Carbon$CarbonOpenGL::initPixelFormat() {
+jboolean Darwin$CarbonOpenGL::initPixelFormat() {
     GLint attr[] = {
         AGL_NO_RECOVERY,
         AGL_RGBA,
@@ -890,13 +903,13 @@ jboolean Carbon$CarbonOpenGL::initPixelFormat() {
     return rawPixelFormat != 0;
 }
 
-void Carbon$CarbonOpenGL::initSharedContext() {
+void Darwin$CarbonOpenGL::initSharedContext() {
     AGLPixelFormat fmt =  (AGLPixelFormat) rawPixelFormat;
     rawSharedContext = (RawData*) aglCreateContext(fmt,NULL);
     checkStatus(rawSharedContext,"aglCreateContext");
 }
 
-void Carbon$GLCarbonDoubleBuffer::natInit() {
+void Darwin$GLCarbonPixelBuffer::natInit() {
     WindowClass wc = kPlainWindowClass;
     WindowAttributes attr = 0;
     WindowRef window;
@@ -945,19 +958,19 @@ void Carbon$GLCarbonDoubleBuffer::natInit() {
     rawCTX = (RawData*) ctx;
 }
 
-void Carbon$GLCarbonDoubleBuffer::activateContext() {
+void Darwin$GLCarbonPixelBuffer::activateContext() {
     AGLContext ctx = (AGLContext) rawCTX;
     aglSetCurrentContext(ctx);
 }
 
-void Carbon$GLCarbonDoubleBuffer::natCleanup(RawData* rawWindowRef, RawData* rawCTX) {
+void Darwin$GLCarbonPixelBuffer::natCleanup(RawData* rawWindowRef, RawData* rawCTX) {
     WindowRef window = (WindowRef) rawWindowRef;
     AGLContext ctx = (AGLContext) rawCTX;
     aglDestroyContext(ctx);
     DisposeWindow(window);
 }
 
-void Carbon$GLCarbonSurface::natBlit(Carbon$GLCarbonDoubleBuffer *db, jint sx1, jint sy1, jint dx1, jint dy1, jint dx2, jint dy2) {
+void Darwin$GLCarbonSurface::natBlit(Darwin$GLCarbonPixelBuffer *db, jint sx1, jint sy1, jint dx1, jint dy1, jint dx2, jint dy2) {
     AGLContext ctx = (AGLContext) rawCTX;
     int sx2 = sx1 + (dx2-dx1);
     int sy2 = sy1 + (dy2-dy1);
@@ -1011,7 +1024,7 @@ void Carbon$GLCarbonSurface::natBlit(Carbon$GLCarbonDoubleBuffer *db, jint sx1,
     glFlush();
 }
 
-void Carbon$GLCarbonSurface::natReshape(jint w, jint h) {
+void Darwin$GLCarbonSurface::natReshape(jint w, jint h) {
     AGLContext ctx = (AGLContext) rawCTX;
     
     aglSetCurrentContext (ctx);
@@ -1027,7 +1040,7 @@ void Carbon$GLCarbonSurface::natReshape(jint w, jint h) {
     checkGLError();
 }
 
-void Carbon$GLCarbonSurface::natInit() {
+void Darwin$GLCarbonSurface::natInit() {
     WindowRef window = (WindowRef) rawWindowRef;
     AGLContext ctx,shared;
     AGLPixelFormat fmt;
@@ -1052,10 +1065,13 @@ void Carbon$GLCarbonSurface::natInit() {
     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 }
 
-void Carbon$GLCarbonSurface::natDispose() {
+void Darwin$GLCarbonSurface::natDispose() {
     AGLContext ctx = (AGLContext) rawCTX;
     aglDestroyContext(ctx);
-    Carbon$CarbonSurface::natDispose();
+    Darwin$CarbonSurface::natDispose();
 }
 
 } } } // end namepsace org::xwt::plat
+
+
+