# HG changeset patch # User John Tsiombikas # Date 1305877374 -10800 # Node ID 01a576351090845ed80e9afd9633ce146efec1b3 # Parent a16b34ac3f2ab91828f3d93225205d4f6c4159df trying to fix the cocoa module diff -r a16b34ac3f2a -r 01a576351090 src/wsys_cocoa.m --- a/src/wsys_cocoa.m Tue May 17 11:21:09 2011 +0300 +++ b/src/wsys_cocoa.m Fri May 20 10:42:54 2011 +0300 @@ -4,20 +4,22 @@ #ifdef USE_WSYS_MODULE_COCOA -#import +#import +#import + #include "sgl.h" #include "wsys.h" -@interface OpenGLView : NSOpenGLView +@interface GLView : NSOpenGLView { - int foo; + struct window *win; } -/*-(id) initWithFrame: (NSRect) frameRect;*/ +-(id) initWithFrame: (NSRect) frameRect; -(void) drawRect: (NSRect) rect; -(void) reshape; -/*-(void) keyDown: (NSEvent*) ev; +-(void) keyDown: (NSEvent*) ev; -(void) keyUp: (NSEvent*) ev; -(void) mouseDown: (NSEvent*) ev; -(void) mouseUp: (NSEvent*) ev; @@ -27,18 +29,23 @@ -(void) otherMouseUp: (NSEvent*) ev; -(void) mouseDragged: (NSEvent*) ev; -(void) rightMouseDragged: (NSEvent*) ev; --(void) otherMouseDragged: (NSEvent*) ev;*/ +-(void) otherMouseDragged: (NSEvent*) ev; -(BOOL) acceptsFirstResponder; @end +@interface AppDelegate +{ +} +-(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*)app; +@end + struct window { int wid; int width, height; NSWindow *win; - OpenGLView *view; + GLView *view; NSOpenGLContext *ctx; - int needs_redisplay; struct window *next; }; @@ -88,8 +95,9 @@ 0 }; -static NSApplication *app; static struct window *winlist, *active_win; +static NSDate *block, *nonblock; +static NSRunLoop *runloop; void sgl_register_cocoa(void) @@ -98,7 +106,11 @@ } -@implementation OpenGLView +@implementation GLView + +-(id) initWithFrame: (NSRect) frameRect +{ +} -(void) drawRect: (NSRect) rect { @@ -129,9 +141,30 @@ } @end +@implementation AppDelegate : NSObject +-(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*)app +{ + return YES; +} +@end + static int init(void) { - app = [NSApplication sharedApplication]; + if(NSApp) { + sgl_log("wsys_cocoa: multiple calls to init\n"); + return 0; + } + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + [NSApplication sharedApplication]; + [NSApp setDelegate: delegate]; + + runloop = [[NSRunLoop currentRunLoop] retain]; + block = [runloop limitDateForMode: NSDefaultRunLoopMode]; + nonblock = [[NSDate distantPast] retain]; + + [pool drain]; return 0; } @@ -141,9 +174,13 @@ struct window *win = winlist; winlist = winlist->next; - /* TODO destroy window */ + [win->win close]; free(win); } + + [NSApp stop: nil]; + [NSApp terminate: nil]; + NSApp = 0; } @@ -164,9 +201,8 @@ { NSWindow *nswin; NSRect rect; - OpenGLView *view; + GLView *view; unsigned int style; - struct window *win; static int next_id = 1; if(!(win = malloc(sizeof *win))) { @@ -174,24 +210,25 @@ } /* create the window and attach the OpenGL view */ - rect.origin.x = rect.origin.y = 0; - rect.size.width = xsz; - rect.size.height = ysz; + rect = NSMakeRect(0, 0, xsz, ysz); + view = [[GLView alloc] initWithFrame: rect]; - style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | - NSResizableWindowMask; + style = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | + NSMiniaturizableWindowMask; nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: YES]; - view = [[OpenGLView alloc] initWithFrame: rect]; + [nswin setTitle: @"OpenGL/Cocoa"]; + [nswin setReleaseWhenClosed: YES]; [nswin setContentView: view]; - [view release]; + [nswin makeFirstResponder: view]; + [nswin makeKeyAndOrderFront: nil]; + + [pool drain]; win->win = nswin; win->view = view; - win->ctx = [view openGLContext]; win->wid = next_id++; - win->needs_redisplay = 1; win->next = winlist; winlist = win; @@ -295,38 +332,23 @@ static int process_events(void) { NSAutoreleasePool *pool; + NSDate *limdate; sgl_idle_callback_t idle; - sgl_display_callback_t disp; - struct window *win; - int res = 0; + + idle = sgl_get_callback(SGL_IDLE); pool = [[NSAutoreleasePool alloc] init]; - - idle = sgl_get_callback(SGL_IDLE); - disp = sgl_get_callback(SGL_DISPLAY); - - win = winlist; - while(win) { - if(win->needs_redisplay && disp) { - activate_window(win); - disp(); - win->needs_redisplay = 0; - } - win = win->next; - } + limdate = idle ? nonblock : block; for(;;) { - NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask - untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES]; - if(ev == nil) { - break; + NSEvent *ev = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: limdate + inMode: NSDefaultRunLoopMode dequeue: YES]; + if(!ev) break; + + [NSApp sendEvent: ev]; + if(limdate == block) { + limdate = nonblock; } - - res = handle_event(ev); - } - - if(idle) { - idle(); } [pool drain];