sgl
diff src/wsys_cocoa.m @ 13:e989ab58ec5b
trying to figure out how cocoa works
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Mon, 16 May 2011 23:05:57 +0300 |
parents | |
children | 5b8fb89fe63f |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/wsys_cocoa.m Mon May 16 23:05:57 2011 +0300 1.3 @@ -0,0 +1,297 @@ 1.4 +/* mac-framework: -framework Cocoa */ 1.5 + 1.6 +#include "config.h" 1.7 + 1.8 +#ifdef USE_WSYS_MODULE_COCOA 1.9 + 1.10 +#import <Cocoa/Cocoa.h> 1.11 +#include "sgl.h" 1.12 + 1.13 +@interface OpenGLView : NSOpenGLView 1.14 +{ 1.15 +} 1.16 + 1.17 +//-(id) initWithFrame: (NSRect) frameRect; 1.18 + 1.19 +-(void) drawRect: (NSRect) rect; 1.20 +-(void) reshape; 1.21 +/*-(void) keyDown: (NSEvent*) ev; 1.22 +-(void) keyUp: (NSEvent*) ev; 1.23 +-(void) mouseDown: (NSEvent*) ev; 1.24 +-(void) mouseUp: (NSEvent*) ev; 1.25 +-(void) rightMouseDown: (NSEvent*) ev; 1.26 +-(void) rightMouseUp: (NSEvent*) ev; 1.27 +-(void) otherMouseDown: (NSEvent*) ev; 1.28 +-(void) otherMouseUp: (NSEvent*) ev; 1.29 +-(void) mouseDragged: (NSEvent*) ev; 1.30 +-(void) rightMouseDragged: (NSEvent*) ev; 1.31 +-(void) otherMouseDragged: (NSEvent*) ev;*/ 1.32 + 1.33 +-(BOOL) acceptsFirstResponder; 1.34 +@end 1.35 + 1.36 +struct window { 1.37 + int wid; 1.38 + NSWindow *win; 1.39 + OpenGLView *view; 1.40 + struct window *next; 1.41 +}; 1.42 + 1.43 + 1.44 +static int init(void); 1.45 +static void shutdown(void); 1.46 + 1.47 +/* video mode switching */ 1.48 +static int set_vidmode(int xsz, int ysz); 1.49 +static int get_vidmode(int *xsz, int *ysz); 1.50 + 1.51 +/* create/destroy windows */ 1.52 +static int create_window(int xsz, int ysz, unsigned int flags); 1.53 +static void close_window(int wid); 1.54 + 1.55 +/* window management */ 1.56 +static int set_active(int wid); 1.57 +static struct window *find_window(int wid); 1.58 +static int activate_window(struct window *win); 1.59 +static int set_title(const char *str); 1.60 +static void redisplay(void); 1.61 +static void swap_buffers(void); 1.62 + 1.63 +static int get_modifiers(void); 1.64 + 1.65 +/* event handling and friends */ 1.66 +static void set_event(int idx, int enable); 1.67 +static int process_events(void); 1.68 +static int handle_event(NSEvent *ev); 1.69 + 1.70 + 1.71 +static struct wsys_module ws = { 1.72 + "cocoa", 0, 1.73 + init, 1.74 + shutdown, 1.75 + set_vidmode, 1.76 + get_vidmode, 1.77 + create_window, 1.78 + close_window, 1.79 + set_active, 1.80 + set_title, 1.81 + redisplay, 1.82 + swap_buffers, 1.83 + get_modifiers, 1.84 + set_event, 1.85 + process_events, 1.86 + 0 1.87 +}; 1.88 + 1.89 +static NSApplication *app; 1.90 +static struct window *winlist, *active_win; 1.91 + 1.92 + 1.93 +void sgl_register_cocoa(void) 1.94 +{ 1.95 + sgl_register_cocoa(&ws); 1.96 +} 1.97 + 1.98 + 1.99 +@implementation OpenGLView 1.100 + 1.101 +-(void) drawRect: (NSRect) rect 1.102 +{ 1.103 + sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY); 1.104 + if(func) { 1.105 + func(); 1.106 + } 1.107 +} 1.108 + 1.109 +-(void) reshape 1.110 +{ 1.111 + sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE); 1.112 + if(func) { 1.113 + func(x, y); 1.114 + } 1.115 +} 1.116 + 1.117 +-(BOOL) acceptsFirstResponder 1.118 +{ 1.119 + return YES; 1.120 +} 1.121 +@end 1.122 + 1.123 +static int init(void) 1.124 +{ 1.125 + app = [NSApplication sharedApplication]; 1.126 +} 1.127 + 1.128 +static void shutdown(void) 1.129 +{ 1.130 + while(winlist) { 1.131 + struct window *win = winlist; 1.132 + winlist = winlist->next; 1.133 + 1.134 + /* TODO destroy window */ 1.135 + free(win); 1.136 + } 1.137 +} 1.138 + 1.139 + 1.140 +/* video mode switching */ 1.141 +static int set_vidmode(int xsz, int ysz) 1.142 +{ 1.143 + return 0; /* TODO */ 1.144 +} 1.145 + 1.146 +static int get_vidmode(int *xsz, int *ysz) 1.147 +{ 1.148 + return 0; /* TODO */ 1.149 +} 1.150 + 1.151 + 1.152 +/* create/destroy windows */ 1.153 +static int create_window(int xsz, int ysz, unsigned int flags) 1.154 +{ 1.155 + NSWindow *nswin; 1.156 + NSRect rect; 1.157 + NSView *view; 1.158 + unsigned int style; 1.159 + struct window *win; 1.160 + static int next_id = 1; 1.161 + 1.162 + if(!(win = malloc(sizeof *win))) { 1.163 + return -1; 1.164 + } 1.165 + 1.166 + /* create the window and attach the OpenGL view */ 1.167 + rect.origin.x = rect.origin.y = 0; 1.168 + rect.size.width = xsz; 1.169 + rect.size.height = ysz; 1.170 + 1.171 + style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | 1.172 + NSResizableWindowMask; 1.173 + 1.174 + nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style 1.175 + backing: NSBackingStoreBuffered defer: YES]; 1.176 + view = [[OpenGLView alloc] initWithFrame: rect]; 1.177 + [nswin setContentView: view]; 1.178 + [view release]; 1.179 + 1.180 + win->win = nswin; 1.181 + win->view = view; 1.182 + win->wid = next_id++; 1.183 + win->next = winlist; 1.184 + winlist = win; 1.185 + 1.186 + return win->wid; 1.187 +} 1.188 + 1.189 +static void close_window(int wid) 1.190 +{ 1.191 + struct window *win, *prev, dummy; 1.192 + sgl_close_callback_t close_func; 1.193 + 1.194 + dummy.next = win = winlist; 1.195 + prev = &dummy; 1.196 + 1.197 + while(win) { 1.198 + if(win->wid == wid) { 1.199 + if(!(close_func = sgl_get_callback(SGL_CLOSE))) { 1.200 + close_func(wid); 1.201 + } 1.202 + [win->win close]; 1.203 + 1.204 + if(active_win == win) { 1.205 + activate_window(winlist); 1.206 + } 1.207 + 1.208 + prev->next = win->next; 1.209 + free(win); 1.210 + return; 1.211 + } 1.212 + prev = win; 1.213 + win = win->next; 1.214 + } 1.215 +} 1.216 + 1.217 + 1.218 +/* window management */ 1.219 +static int set_active(int wid) 1.220 +{ 1.221 +} 1.222 + 1.223 +static struct window *find_window(int wid) 1.224 +{ 1.225 + struct window *win = winlist; 1.226 + 1.227 + while(win) { 1.228 + if(win->win == id) { 1.229 + return win; 1.230 + } 1.231 + win = win->next; 1.232 + } 1.233 + return 0; 1.234 +} 1.235 + 1.236 +static int activate_window(struct window *win) 1.237 +{ 1.238 + if(!win) { 1.239 + return -1; 1.240 + } 1.241 + [win->ctx makeCurrentContext]; 1.242 + active_win = win; 1.243 + return 0; 1.244 +} 1.245 + 1.246 +static int set_title(const char *str) 1.247 +{ 1.248 + NSString *nsstr; 1.249 + 1.250 + nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding]; 1.251 + [active_win->win setTitle: nsstr]; 1.252 + [nsstr release]; 1.253 +} 1.254 + 1.255 +static void redisplay(void) 1.256 +{ 1.257 + [active_win->view setNeedsRedisplay: YES]; 1.258 +} 1.259 + 1.260 +static void swap_buffers(void) 1.261 +{ 1.262 + [active_win->flushBuffer]; 1.263 +} 1.264 + 1.265 + 1.266 +static int get_modifiers(void) 1.267 +{ 1.268 + return 0; /* TODO */ 1.269 +} 1.270 + 1.271 + 1.272 +/* event handling and friends */ 1.273 +static void set_event(int idx, int enable) 1.274 +{ 1.275 +} 1.276 + 1.277 +static int process_events(void) 1.278 +{ 1.279 + NSAutoreleasePool *pool; 1.280 + int state; 1.281 + 1.282 + pool = [[NSAutoreleasePool alloc] init]; 1.283 + 1.284 + for(;;) { 1.285 + NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask 1.286 + untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES]; 1.287 + if(ev == nil) { 1.288 + break; 1.289 + } 1.290 + 1.291 + handle_event(ev); 1.292 + } 1.293 +} 1.294 + 1.295 +static int handle_event(NSEvent *ev) 1.296 +{ 1.297 + /* TODO */ 1.298 +} 1.299 + 1.300 +#endif /* USE_WSYS_MODULE_COCOA */