sgl

view src/wsys_cocoa.m @ 29:171aa71b97e7

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Jun 2011 13:50:41 +0300
parents 0697fbd075b6
children 124195562f7e
line source
1 /* SimplyGL window system module for Cocoa */
2 /* mac-framework: -framework Cocoa */
4 #include "config.h"
6 #ifdef USE_WSYS_MODULE_COCOA
8 #import <Cocoa/Cocoa.h>
9 #include "sgl.h"
10 #include "wsys.h"
12 @interface OpenGLView : NSOpenGLView
13 {
14 int foo;
15 }
17 -(id) initWithFrame: (NSRect) frame pixelFormat: (NSOpenGLPixelFormat*) pf;
19 -(void) drawRect: (NSRect) rect;
20 -(void) reshape;
21 /*-(void) keyDown: (NSEvent*) ev;
22 -(void) keyUp: (NSEvent*) ev;
23 -(void) mouseDown: (NSEvent*) ev;
24 -(void) mouseUp: (NSEvent*) ev;
25 -(void) rightMouseDown: (NSEvent*) ev;
26 -(void) rightMouseUp: (NSEvent*) ev;
27 -(void) otherMouseDown: (NSEvent*) ev;
28 -(void) otherMouseUp: (NSEvent*) ev;
29 -(void) mouseDragged: (NSEvent*) ev;
30 -(void) rightMouseDragged: (NSEvent*) ev;
31 -(void) otherMouseDragged: (NSEvent*) ev;*/
33 -(BOOL) acceptsFirstResponder;
34 @end
37 @interface AppDelegate : NSObject
38 {
39 int foo;
40 }
41 -(BOOL) applicationShouldTerminate: (NSApplication*) app;
42 -(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*)app;
43 @end
46 struct window {
47 int wid;
48 int width, height;
49 NSWindow *win;
50 OpenGLView *view;
51 NSOpenGLContext *ctx;
52 int needs_redisplay;
53 struct window *next;
54 };
57 static int init(void);
58 static void shutdown(void);
60 /* video mode switching */
61 static int set_vidmode(int xsz, int ysz);
62 static int get_vidmode(int *xsz, int *ysz);
64 /* create/destroy windows */
65 static int create_window(int xsz, int ysz, unsigned int flags);
66 static void close_window(int wid);
68 /* window management */
69 static int set_active(int wid);
70 static struct window *find_window(int wid);
71 static int activate_window(struct window *win);
72 static int set_title(const char *str);
73 static void redisplay(void);
74 static void swap_buffers(void);
76 static int get_modifiers(void);
78 /* event handling and friends */
79 static void set_event(int idx, int enable);
80 static int process_events(void);
82 static void fill_attr(NSOpenGLPixelFormatAttribute *attr, unsigned int flags);
85 static struct wsys_module ws = {
86 "cocoa", 0,
87 init,
88 shutdown,
89 set_vidmode,
90 get_vidmode,
91 create_window,
92 close_window,
93 set_active,
94 set_title,
95 redisplay,
96 swap_buffers,
97 get_modifiers,
98 set_event,
99 process_events,
100 0
101 };
103 static struct window *winlist, *active_win;
104 static int quit_main_loop;
107 void sgl_register_cocoa(void)
108 {
109 sgl_register_module(&ws);
110 }
113 @implementation OpenGLView
115 -(id) initWithFrame: (NSRect) frame pixelFormat: (NSOpenGLPixelFormat*) pf
116 {
117 self = [super initWithFrame: frame pixelFormat: pf];
118 return self;
119 }
121 -(void) drawRect: (NSRect) rect
122 {
123 sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY);
124 if(func) {
125 func();
126 }
127 }
129 -(void) reshape
130 {
131 NSSize sz;
132 sgl_reshape_callback_t func;
134 sz = [self bounds].size;
136 if((func = sgl_get_callback(SGL_RESHAPE)) && (sz.width != active_win->width ||
137 sz.height != active_win->height)) {
138 active_win->width = sz.width;
139 active_win->height = sz.height;
140 func(sz.width, sz.height);
141 }
142 }
144 -(BOOL) acceptsFirstResponder
145 {
146 return YES;
147 }
148 @end
150 @implementation AppDelegate
151 -(BOOL) applicationShouldTerminate: (NSApplication*) app
152 {
153 return NSTerminateNow;
154 }
156 -(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app
157 {
158 return YES;
159 }
161 -(void) applicationWillTerminate: (NSNotification*) notification
162 {
163 printf("applicationWillTerminate\n");
164 }
166 -(BOOL) windowShouldClose: (id) win
167 {
168 return YES;
169 }
171 -(void) windowWillClose: (NSNotification*) notification
172 {
173 [NSApp terminate: nil];
174 }
175 @end
177 static int init(void)
178 {
179 AppDelegate *delegate;
181 [NSApplication sharedApplication];
183 delegate = [[AppDelegate alloc] init];
184 [NSApp setDelegate: delegate];
185 return 0;
186 }
188 static void shutdown(void)
189 {
190 while(winlist) {
191 struct window *win = winlist;
192 winlist = winlist->next;
194 /* TODO destroy window */
195 free(win);
196 }
198 quit_main_loop = 1;
199 }
202 /* video mode switching */
203 static int set_vidmode(int xsz, int ysz)
204 {
205 return 0; /* TODO */
206 }
208 static int get_vidmode(int *xsz, int *ysz)
209 {
210 return 0; /* TODO */
211 }
214 /* create/destroy windows */
215 static int create_window(int xsz, int ysz, unsigned int flags)
216 {
217 NSAutoreleasePool *pool;
218 NSWindow *nswin;
219 NSRect rect;
220 OpenGLView *view;
221 NSOpenGLPixelFormat *pf;
222 NSOpenGLPixelFormatAttribute attr[32];
223 unsigned int style;
224 struct window *win;
225 static int next_id = 1;
227 if(!(win = malloc(sizeof *win))) {
228 return -1;
229 }
231 pool = [[NSAutoreleasePool alloc] init];
233 /* create the view */
234 fill_attr(attr, flags);
235 pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes: attr] autorelease];
236 view = [[OpenGLView alloc] initWithFrame: rect pixelFormat: pf];
238 /* create the window and attach the OpenGL view */
239 rect.origin.x = rect.origin.y = 0;
240 rect.size.width = xsz;
241 rect.size.height = ysz;
243 style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask |
244 NSResizableWindowMask;
246 nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style
247 backing: NSBackingStoreBuffered defer: YES];
249 [nswin setTitle: @"OpenGL/Cocoa"];
250 [nswin setReleasedWhenClosed: YES];
251 [nswin setContentView: view];
252 [nswin makeFirstResponder: view];
253 [nswin makeKeyAndOrderFront: nil];
254 [view release];
256 win->win = nswin;
257 win->view = view;
258 win->ctx = [view openGLContext];
259 win->wid = next_id++;
260 win->needs_redisplay = 1;
261 win->next = winlist;
262 winlist = win;
264 if(!active_win) {
265 activate_window(win);
266 }
268 [pool drain];
269 return win->wid;
270 }
272 static void close_window(int wid)
273 {
274 struct window *win, *prev, dummy;
275 sgl_close_callback_t close_func;
277 dummy.next = win = winlist;
278 prev = &dummy;
280 while(win) {
281 if(win->wid == wid) {
282 if(!(close_func = sgl_get_callback(SGL_CLOSE))) {
283 close_func(wid);
284 }
285 [win->win close];
287 if(active_win == win) {
288 activate_window(winlist);
289 }
291 prev->next = win->next;
292 free(win);
293 return;
294 }
295 prev = win;
296 win = win->next;
297 }
298 }
301 /* window management */
302 static int set_active(int wid)
303 {
304 struct window *win = find_window(wid);
305 return activate_window(win);
306 }
308 static struct window *find_window(int wid)
309 {
310 struct window *win = winlist;
312 while(win) {
313 if(win->wid == wid) {
314 return win;
315 }
316 win = win->next;
317 }
318 return 0;
319 }
321 static int activate_window(struct window *win)
322 {
323 if(!win) {
324 return -1;
325 }
326 [win->ctx makeCurrentContext];
327 active_win = win;
328 return 0;
329 }
331 static int set_title(const char *str)
332 {
333 NSString *nsstr;
335 nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding];
336 [active_win->win setTitle: nsstr];
337 [nsstr release];
338 return 0;
339 }
341 static void redisplay(void)
342 {
343 active_win->needs_redisplay = 1;
344 }
346 static void swap_buffers(void)
347 {
348 [active_win->ctx flushBuffer];
349 }
352 static int get_modifiers(void)
353 {
354 return 0; /* TODO */
355 }
358 /* event handling and friends */
359 static void set_event(int idx, int enable)
360 {
361 }
363 static int process_events(void)
364 {
365 NSAutoreleasePool *pool;
366 NSRunLoop *runloop;
367 NSDate *block, *nonblock, *limdate;
369 sgl_idle_callback_t idle;
370 sgl_display_callback_t disp;
371 struct window *win;
373 pool = [[NSAutoreleasePool alloc] init];
375 idle = sgl_get_callback(SGL_IDLE);
376 disp = sgl_get_callback(SGL_DISPLAY);
378 win = winlist;
379 while(win) {
380 if(win->needs_redisplay && disp) {
381 activate_window(win);
382 disp();
383 win->needs_redisplay = 0;
384 }
385 win = win->next;
386 }
388 runloop = [[NSRunLoop currentRunLoop] retain];
389 block = [runloop limitDateForMode: NSDefaultRunLoopMode];
390 nonblock = [[NSDate distantPast] retain];
391 limdate = idle ? nonblock : block;
393 while(!quit_main_loop) {
394 NSEvent *ev = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: limdate
395 inMode: NSDefaultRunLoopMode dequeue: YES];
396 if(!ev) break;
398 [NSApp sendEvent: ev];
399 if(limdate == block) {
400 limdate = nonblock;
401 }
402 }
404 if(idle) {
405 idle();
406 }
408 [runloop release];
409 [pool drain];
410 return quit_main_loop ? -1 : 0;
411 }
413 static void fill_attr(NSOpenGLPixelFormatAttribute *attr, unsigned int flags)
414 {
415 int i = 0;
417 /* this is very important. makes pixelformat selection behave like GLX
418 * where any non-zero value will denote "choose highest possible". This
419 * is pretty much what we intend, as the user doesn't actually pass any
420 * of these numbers.
421 */
422 attr[i++] = NSOpenGLPFAMaximumPolicy;
424 attr[i++] = NSOpenGLPFAColorSize;
425 attr[i++] = 1;
427 if(flags & SGL_DOUBLE) {
428 attr[i++] = NSOpenGLPFADoubleBuffer;
429 }
430 if(flags & SGL_DEPTH) {
431 attr[i++] = NSOpenGLPFADepthSize;
432 attr[i++] = 1;
433 }
434 if(flags & SGL_STENCIL) {
435 attr[i++] = NSOpenGLPFAStencilSize;
436 attr[i++] = 8; /* max-policy has no effect on stencil selection */
437 }
438 if(flags & SGL_STEREO) {
439 attr[i++] = NSOpenGLPFAStereo;
440 }
441 if(flags & SGL_MULTISAMPLE) {
442 attr[i++] = NSOpenGLPFASampleBuffers;
443 attr[i++] = 1;
444 attr[i++] = NSOpenGLPFASamples;
445 attr[i++] = 4; /* TODO don't hardcode, query */
446 }
447 attr[i++] = 0;
448 }
450 #else
451 int sgl_wsys_cocoa_silence_the_fucking_empty_file_warnings;
452 #endif /* USE_WSYS_MODULE_COCOA */