sgl

annotate src/wsys_cocoa.m @ 14:5b8fb89fe63f

cocoa...
author John Tsiombikas <nuclear@siggraph.org>
date Mon, 16 May 2011 23:09:53 +0300
parents e989ab58ec5b
children a16b34ac3f2a
rev   line source
nuclear@13 1 /* mac-framework: -framework Cocoa */
nuclear@13 2
nuclear@13 3 #include "config.h"
nuclear@13 4
nuclear@13 5 #ifdef USE_WSYS_MODULE_COCOA
nuclear@13 6
nuclear@13 7 #import <Cocoa/Cocoa.h>
nuclear@13 8 #include "sgl.h"
nuclear@13 9
nuclear@13 10 @interface OpenGLView : NSOpenGLView
nuclear@13 11 {
nuclear@13 12 }
nuclear@13 13
nuclear@13 14 //-(id) initWithFrame: (NSRect) frameRect;
nuclear@13 15
nuclear@13 16 -(void) drawRect: (NSRect) rect;
nuclear@13 17 -(void) reshape;
nuclear@13 18 /*-(void) keyDown: (NSEvent*) ev;
nuclear@13 19 -(void) keyUp: (NSEvent*) ev;
nuclear@13 20 -(void) mouseDown: (NSEvent*) ev;
nuclear@13 21 -(void) mouseUp: (NSEvent*) ev;
nuclear@13 22 -(void) rightMouseDown: (NSEvent*) ev;
nuclear@13 23 -(void) rightMouseUp: (NSEvent*) ev;
nuclear@13 24 -(void) otherMouseDown: (NSEvent*) ev;
nuclear@13 25 -(void) otherMouseUp: (NSEvent*) ev;
nuclear@13 26 -(void) mouseDragged: (NSEvent*) ev;
nuclear@13 27 -(void) rightMouseDragged: (NSEvent*) ev;
nuclear@13 28 -(void) otherMouseDragged: (NSEvent*) ev;*/
nuclear@13 29
nuclear@13 30 -(BOOL) acceptsFirstResponder;
nuclear@13 31 @end
nuclear@13 32
nuclear@13 33 struct window {
nuclear@13 34 int wid;
nuclear@13 35 NSWindow *win;
nuclear@13 36 OpenGLView *view;
nuclear@13 37 struct window *next;
nuclear@13 38 };
nuclear@13 39
nuclear@13 40
nuclear@13 41 static int init(void);
nuclear@13 42 static void shutdown(void);
nuclear@13 43
nuclear@13 44 /* video mode switching */
nuclear@13 45 static int set_vidmode(int xsz, int ysz);
nuclear@13 46 static int get_vidmode(int *xsz, int *ysz);
nuclear@13 47
nuclear@13 48 /* create/destroy windows */
nuclear@13 49 static int create_window(int xsz, int ysz, unsigned int flags);
nuclear@13 50 static void close_window(int wid);
nuclear@13 51
nuclear@13 52 /* window management */
nuclear@13 53 static int set_active(int wid);
nuclear@13 54 static struct window *find_window(int wid);
nuclear@13 55 static int activate_window(struct window *win);
nuclear@13 56 static int set_title(const char *str);
nuclear@13 57 static void redisplay(void);
nuclear@13 58 static void swap_buffers(void);
nuclear@13 59
nuclear@13 60 static int get_modifiers(void);
nuclear@13 61
nuclear@13 62 /* event handling and friends */
nuclear@13 63 static void set_event(int idx, int enable);
nuclear@13 64 static int process_events(void);
nuclear@13 65 static int handle_event(NSEvent *ev);
nuclear@13 66
nuclear@13 67
nuclear@13 68 static struct wsys_module ws = {
nuclear@13 69 "cocoa", 0,
nuclear@13 70 init,
nuclear@13 71 shutdown,
nuclear@13 72 set_vidmode,
nuclear@13 73 get_vidmode,
nuclear@13 74 create_window,
nuclear@13 75 close_window,
nuclear@13 76 set_active,
nuclear@13 77 set_title,
nuclear@13 78 redisplay,
nuclear@13 79 swap_buffers,
nuclear@13 80 get_modifiers,
nuclear@13 81 set_event,
nuclear@13 82 process_events,
nuclear@13 83 0
nuclear@13 84 };
nuclear@13 85
nuclear@13 86 static NSApplication *app;
nuclear@13 87 static struct window *winlist, *active_win;
nuclear@13 88
nuclear@13 89
nuclear@13 90 void sgl_register_cocoa(void)
nuclear@13 91 {
nuclear@13 92 sgl_register_cocoa(&ws);
nuclear@13 93 }
nuclear@13 94
nuclear@13 95
nuclear@13 96 @implementation OpenGLView
nuclear@13 97
nuclear@13 98 -(void) drawRect: (NSRect) rect
nuclear@13 99 {
nuclear@13 100 sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY);
nuclear@13 101 if(func) {
nuclear@13 102 func();
nuclear@13 103 }
nuclear@13 104 }
nuclear@13 105
nuclear@13 106 -(void) reshape
nuclear@13 107 {
nuclear@13 108 sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE);
nuclear@13 109 if(func) {
nuclear@13 110 func(x, y);
nuclear@13 111 }
nuclear@13 112 }
nuclear@13 113
nuclear@13 114 -(BOOL) acceptsFirstResponder
nuclear@13 115 {
nuclear@13 116 return YES;
nuclear@13 117 }
nuclear@13 118 @end
nuclear@13 119
nuclear@13 120 static int init(void)
nuclear@13 121 {
nuclear@13 122 app = [NSApplication sharedApplication];
nuclear@13 123 }
nuclear@13 124
nuclear@13 125 static void shutdown(void)
nuclear@13 126 {
nuclear@13 127 while(winlist) {
nuclear@13 128 struct window *win = winlist;
nuclear@13 129 winlist = winlist->next;
nuclear@13 130
nuclear@13 131 /* TODO destroy window */
nuclear@13 132 free(win);
nuclear@13 133 }
nuclear@13 134 }
nuclear@13 135
nuclear@13 136
nuclear@13 137 /* video mode switching */
nuclear@13 138 static int set_vidmode(int xsz, int ysz)
nuclear@13 139 {
nuclear@13 140 return 0; /* TODO */
nuclear@13 141 }
nuclear@13 142
nuclear@13 143 static int get_vidmode(int *xsz, int *ysz)
nuclear@13 144 {
nuclear@13 145 return 0; /* TODO */
nuclear@13 146 }
nuclear@13 147
nuclear@13 148
nuclear@13 149 /* create/destroy windows */
nuclear@13 150 static int create_window(int xsz, int ysz, unsigned int flags)
nuclear@13 151 {
nuclear@13 152 NSWindow *nswin;
nuclear@13 153 NSRect rect;
nuclear@13 154 NSView *view;
nuclear@13 155 unsigned int style;
nuclear@13 156 struct window *win;
nuclear@13 157 static int next_id = 1;
nuclear@13 158
nuclear@13 159 if(!(win = malloc(sizeof *win))) {
nuclear@13 160 return -1;
nuclear@13 161 }
nuclear@13 162
nuclear@13 163 /* create the window and attach the OpenGL view */
nuclear@13 164 rect.origin.x = rect.origin.y = 0;
nuclear@13 165 rect.size.width = xsz;
nuclear@13 166 rect.size.height = ysz;
nuclear@13 167
nuclear@13 168 style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask |
nuclear@13 169 NSResizableWindowMask;
nuclear@13 170
nuclear@13 171 nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style
nuclear@13 172 backing: NSBackingStoreBuffered defer: YES];
nuclear@13 173 view = [[OpenGLView alloc] initWithFrame: rect];
nuclear@13 174 [nswin setContentView: view];
nuclear@13 175 [view release];
nuclear@13 176
nuclear@13 177 win->win = nswin;
nuclear@13 178 win->view = view;
nuclear@13 179 win->wid = next_id++;
nuclear@13 180 win->next = winlist;
nuclear@13 181 winlist = win;
nuclear@13 182
nuclear@13 183 return win->wid;
nuclear@13 184 }
nuclear@13 185
nuclear@13 186 static void close_window(int wid)
nuclear@13 187 {
nuclear@13 188 struct window *win, *prev, dummy;
nuclear@13 189 sgl_close_callback_t close_func;
nuclear@13 190
nuclear@13 191 dummy.next = win = winlist;
nuclear@13 192 prev = &dummy;
nuclear@13 193
nuclear@13 194 while(win) {
nuclear@13 195 if(win->wid == wid) {
nuclear@13 196 if(!(close_func = sgl_get_callback(SGL_CLOSE))) {
nuclear@13 197 close_func(wid);
nuclear@13 198 }
nuclear@13 199 [win->win close];
nuclear@13 200
nuclear@13 201 if(active_win == win) {
nuclear@13 202 activate_window(winlist);
nuclear@13 203 }
nuclear@13 204
nuclear@13 205 prev->next = win->next;
nuclear@13 206 free(win);
nuclear@13 207 return;
nuclear@13 208 }
nuclear@13 209 prev = win;
nuclear@13 210 win = win->next;
nuclear@13 211 }
nuclear@13 212 }
nuclear@13 213
nuclear@13 214
nuclear@13 215 /* window management */
nuclear@13 216 static int set_active(int wid)
nuclear@13 217 {
nuclear@13 218 }
nuclear@13 219
nuclear@13 220 static struct window *find_window(int wid)
nuclear@13 221 {
nuclear@13 222 struct window *win = winlist;
nuclear@13 223
nuclear@13 224 while(win) {
nuclear@13 225 if(win->win == id) {
nuclear@13 226 return win;
nuclear@13 227 }
nuclear@13 228 win = win->next;
nuclear@13 229 }
nuclear@13 230 return 0;
nuclear@13 231 }
nuclear@13 232
nuclear@13 233 static int activate_window(struct window *win)
nuclear@13 234 {
nuclear@13 235 if(!win) {
nuclear@13 236 return -1;
nuclear@13 237 }
nuclear@13 238 [win->ctx makeCurrentContext];
nuclear@13 239 active_win = win;
nuclear@13 240 return 0;
nuclear@13 241 }
nuclear@13 242
nuclear@13 243 static int set_title(const char *str)
nuclear@13 244 {
nuclear@13 245 NSString *nsstr;
nuclear@13 246
nuclear@13 247 nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding];
nuclear@13 248 [active_win->win setTitle: nsstr];
nuclear@13 249 [nsstr release];
nuclear@13 250 }
nuclear@13 251
nuclear@13 252 static void redisplay(void)
nuclear@13 253 {
nuclear@13 254 [active_win->view setNeedsRedisplay: YES];
nuclear@13 255 }
nuclear@13 256
nuclear@13 257 static void swap_buffers(void)
nuclear@13 258 {
nuclear@13 259 [active_win->flushBuffer];
nuclear@13 260 }
nuclear@13 261
nuclear@13 262
nuclear@13 263 static int get_modifiers(void)
nuclear@13 264 {
nuclear@13 265 return 0; /* TODO */
nuclear@13 266 }
nuclear@13 267
nuclear@13 268
nuclear@13 269 /* event handling and friends */
nuclear@13 270 static void set_event(int idx, int enable)
nuclear@13 271 {
nuclear@13 272 }
nuclear@13 273
nuclear@13 274 static int process_events(void)
nuclear@13 275 {
nuclear@13 276 NSAutoreleasePool *pool;
nuclear@13 277 int state;
nuclear@14 278 sgl_idle_callback_t idle;
nuclear@13 279
nuclear@13 280 pool = [[NSAutoreleasePool alloc] init];
nuclear@13 281
nuclear@13 282 for(;;) {
nuclear@13 283 NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask
nuclear@13 284 untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES];
nuclear@13 285 if(ev == nil) {
nuclear@13 286 break;
nuclear@13 287 }
nuclear@13 288
nuclear@13 289 handle_event(ev);
nuclear@13 290 }
nuclear@13 291 }
nuclear@13 292
nuclear@13 293 static int handle_event(NSEvent *ev)
nuclear@13 294 {
nuclear@14 295 switch([ev type]) {
nuclear@14 296 case NSKeyDown:
nuclear@14 297 case NSKeyUp:
nuclear@14 298
nuclear@13 299 }
nuclear@13 300
nuclear@13 301 #endif /* USE_WSYS_MODULE_COCOA */