sgl

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