sgl
changeset 16:01a576351090
trying to fix the cocoa module
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Fri, 20 May 2011 10:42:54 +0300 |
parents | a16b34ac3f2a |
children | 33acb6b2d7a4 |
files | src/wsys_cocoa.m |
diffstat | 1 files changed, 71 insertions(+), 49 deletions(-) [+] |
line diff
1.1 --- a/src/wsys_cocoa.m Tue May 17 11:21:09 2011 +0300 1.2 +++ b/src/wsys_cocoa.m Fri May 20 10:42:54 2011 +0300 1.3 @@ -4,20 +4,22 @@ 1.4 1.5 #ifdef USE_WSYS_MODULE_COCOA 1.6 1.7 -#import <Cocoa/Cocoa.h> 1.8 +#import <Foundation/Foundation.h> 1.9 +#import <AppKit/AppKit.h> 1.10 + 1.11 #include "sgl.h" 1.12 #include "wsys.h" 1.13 1.14 -@interface OpenGLView : NSOpenGLView 1.15 +@interface GLView : NSOpenGLView 1.16 { 1.17 - int foo; 1.18 + struct window *win; 1.19 } 1.20 1.21 -/*-(id) initWithFrame: (NSRect) frameRect;*/ 1.22 +-(id) initWithFrame: (NSRect) frameRect; 1.23 1.24 -(void) drawRect: (NSRect) rect; 1.25 -(void) reshape; 1.26 -/*-(void) keyDown: (NSEvent*) ev; 1.27 +-(void) keyDown: (NSEvent*) ev; 1.28 -(void) keyUp: (NSEvent*) ev; 1.29 -(void) mouseDown: (NSEvent*) ev; 1.30 -(void) mouseUp: (NSEvent*) ev; 1.31 @@ -27,18 +29,23 @@ 1.32 -(void) otherMouseUp: (NSEvent*) ev; 1.33 -(void) mouseDragged: (NSEvent*) ev; 1.34 -(void) rightMouseDragged: (NSEvent*) ev; 1.35 --(void) otherMouseDragged: (NSEvent*) ev;*/ 1.36 +-(void) otherMouseDragged: (NSEvent*) ev; 1.37 1.38 -(BOOL) acceptsFirstResponder; 1.39 @end 1.40 1.41 +@interface AppDelegate 1.42 +{ 1.43 +} 1.44 +-(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*)app; 1.45 +@end 1.46 + 1.47 struct window { 1.48 int wid; 1.49 int width, height; 1.50 NSWindow *win; 1.51 - OpenGLView *view; 1.52 + GLView *view; 1.53 NSOpenGLContext *ctx; 1.54 - int needs_redisplay; 1.55 struct window *next; 1.56 }; 1.57 1.58 @@ -88,8 +95,9 @@ 1.59 0 1.60 }; 1.61 1.62 -static NSApplication *app; 1.63 static struct window *winlist, *active_win; 1.64 +static NSDate *block, *nonblock; 1.65 +static NSRunLoop *runloop; 1.66 1.67 1.68 void sgl_register_cocoa(void) 1.69 @@ -98,7 +106,11 @@ 1.70 } 1.71 1.72 1.73 -@implementation OpenGLView 1.74 +@implementation GLView 1.75 + 1.76 +-(id) initWithFrame: (NSRect) frameRect 1.77 +{ 1.78 +} 1.79 1.80 -(void) drawRect: (NSRect) rect 1.81 { 1.82 @@ -129,9 +141,30 @@ 1.83 } 1.84 @end 1.85 1.86 +@implementation AppDelegate : NSObject 1.87 +-(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*)app 1.88 +{ 1.89 + return YES; 1.90 +} 1.91 +@end 1.92 + 1.93 static int init(void) 1.94 { 1.95 - app = [NSApplication sharedApplication]; 1.96 + if(NSApp) { 1.97 + sgl_log("wsys_cocoa: multiple calls to init\n"); 1.98 + return 0; 1.99 + } 1.100 + 1.101 + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 1.102 + 1.103 + [NSApplication sharedApplication]; 1.104 + [NSApp setDelegate: delegate]; 1.105 + 1.106 + runloop = [[NSRunLoop currentRunLoop] retain]; 1.107 + block = [runloop limitDateForMode: NSDefaultRunLoopMode]; 1.108 + nonblock = [[NSDate distantPast] retain]; 1.109 + 1.110 + [pool drain]; 1.111 return 0; 1.112 } 1.113 1.114 @@ -141,9 +174,13 @@ 1.115 struct window *win = winlist; 1.116 winlist = winlist->next; 1.117 1.118 - /* TODO destroy window */ 1.119 + [win->win close]; 1.120 free(win); 1.121 } 1.122 + 1.123 + [NSApp stop: nil]; 1.124 + [NSApp terminate: nil]; 1.125 + NSApp = 0; 1.126 } 1.127 1.128 1.129 @@ -164,9 +201,8 @@ 1.130 { 1.131 NSWindow *nswin; 1.132 NSRect rect; 1.133 - OpenGLView *view; 1.134 + GLView *view; 1.135 unsigned int style; 1.136 - struct window *win; 1.137 static int next_id = 1; 1.138 1.139 if(!(win = malloc(sizeof *win))) { 1.140 @@ -174,24 +210,25 @@ 1.141 } 1.142 1.143 /* create the window and attach the OpenGL view */ 1.144 - rect.origin.x = rect.origin.y = 0; 1.145 - rect.size.width = xsz; 1.146 - rect.size.height = ysz; 1.147 + rect = NSMakeRect(0, 0, xsz, ysz); 1.148 + view = [[GLView alloc] initWithFrame: rect]; 1.149 1.150 - style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | 1.151 - NSResizableWindowMask; 1.152 + style = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | 1.153 + NSMiniaturizableWindowMask; 1.154 1.155 nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style 1.156 backing: NSBackingStoreBuffered defer: YES]; 1.157 - view = [[OpenGLView alloc] initWithFrame: rect]; 1.158 + [nswin setTitle: @"OpenGL/Cocoa"]; 1.159 + [nswin setReleaseWhenClosed: YES]; 1.160 [nswin setContentView: view]; 1.161 - [view release]; 1.162 + [nswin makeFirstResponder: view]; 1.163 + [nswin makeKeyAndOrderFront: nil]; 1.164 + 1.165 + [pool drain]; 1.166 1.167 win->win = nswin; 1.168 win->view = view; 1.169 - win->ctx = [view openGLContext]; 1.170 win->wid = next_id++; 1.171 - win->needs_redisplay = 1; 1.172 win->next = winlist; 1.173 winlist = win; 1.174 1.175 @@ -295,38 +332,23 @@ 1.176 static int process_events(void) 1.177 { 1.178 NSAutoreleasePool *pool; 1.179 + NSDate *limdate; 1.180 sgl_idle_callback_t idle; 1.181 - sgl_display_callback_t disp; 1.182 - struct window *win; 1.183 - int res = 0; 1.184 + 1.185 + idle = sgl_get_callback(SGL_IDLE); 1.186 1.187 pool = [[NSAutoreleasePool alloc] init]; 1.188 - 1.189 - idle = sgl_get_callback(SGL_IDLE); 1.190 - disp = sgl_get_callback(SGL_DISPLAY); 1.191 - 1.192 - win = winlist; 1.193 - while(win) { 1.194 - if(win->needs_redisplay && disp) { 1.195 - activate_window(win); 1.196 - disp(); 1.197 - win->needs_redisplay = 0; 1.198 - } 1.199 - win = win->next; 1.200 - } 1.201 + limdate = idle ? nonblock : block; 1.202 1.203 for(;;) { 1.204 - NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask 1.205 - untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES]; 1.206 - if(ev == nil) { 1.207 - break; 1.208 + NSEvent *ev = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: limdate 1.209 + inMode: NSDefaultRunLoopMode dequeue: YES]; 1.210 + if(!ev) break; 1.211 + 1.212 + [NSApp sendEvent: ev]; 1.213 + if(limdate == block) { 1.214 + limdate = nonblock; 1.215 } 1.216 - 1.217 - res = handle_event(ev); 1.218 - } 1.219 - 1.220 - if(idle) { 1.221 - idle(); 1.222 } 1.223 1.224 [pool drain];