# HG changeset patch # User John Tsiombikas # Date 1305576357 -10800 # Node ID e989ab58ec5b20849d2af1194e68cee201ca3a26 # Parent bf34fa6779608fec94359617bcb83c06cb6fcbed trying to figure out how cocoa works diff -r bf34fa677960 -r e989ab58ec5b Makefile.in --- a/Makefile.in Sat May 14 12:02:22 2011 +0300 +++ b/Makefile.in Mon May 16 23:05:57 2011 +0300 @@ -1,4 +1,5 @@ src = $(wildcard src/*.c) +msrc = $(wildcard src/*.m) obj = $(src:.c=.o) dep = $(src:.c=.d) lib_a = libsgl.a @@ -16,6 +17,7 @@ ifeq ($(shell uname -s), Darwin) sys = mac + obj += $(msrc:.m=.o) else sys = unix endif diff -r bf34fa677960 -r e989ab58ec5b configure --- a/configure Sat May 14 12:02:22 2011 +0300 +++ b/configure Mon May 16 23:05:57 2011 +0300 @@ -30,6 +30,10 @@ echo 'int main(void) { return 0; }' >$srcfile cc -o $aout $srcfile $1 >/dev/null 2>/dev/null + res=$? + + rm -f $srcfile $aout + return $res } # write beginning of config.h @@ -51,7 +55,7 @@ echo 'Looking for usable window system modules ...' # collect all src/wsys_whatever.c files -all_files=`ls src/wsys_*.c 2>/dev/null` +all_files=`ls src/wsys_*.c src/wsys_*.m 2>/dev/null` for m in $all_files; do # extract USE_WSYS_MODULE_* define @@ -65,9 +69,12 @@ fi else dep=`get_depline $m` + if [ -z "$dep" ]; then + dep=`get_framework $m` + fi fi - name=`echo $m | sort | sed 's/src\/wsys_//' | sed 's/\.c//'` + name=`echo $m | sort | sed 's/src\/wsys_//' | sed 's/\.c\|\.m//'` `which echo` -n "-> trying module $name (needs: $dep) ... " if try_link "$dep"; then diff -r bf34fa677960 -r e989ab58ec5b src/wsys_cocoa.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wsys_cocoa.m Mon May 16 23:05:57 2011 +0300 @@ -0,0 +1,297 @@ +/* mac-framework: -framework Cocoa */ + +#include "config.h" + +#ifdef USE_WSYS_MODULE_COCOA + +#import +#include "sgl.h" + +@interface OpenGLView : NSOpenGLView +{ +} + +//-(id) initWithFrame: (NSRect) frameRect; + +-(void) drawRect: (NSRect) rect; +-(void) reshape; +/*-(void) keyDown: (NSEvent*) ev; +-(void) keyUp: (NSEvent*) ev; +-(void) mouseDown: (NSEvent*) ev; +-(void) mouseUp: (NSEvent*) ev; +-(void) rightMouseDown: (NSEvent*) ev; +-(void) rightMouseUp: (NSEvent*) ev; +-(void) otherMouseDown: (NSEvent*) ev; +-(void) otherMouseUp: (NSEvent*) ev; +-(void) mouseDragged: (NSEvent*) ev; +-(void) rightMouseDragged: (NSEvent*) ev; +-(void) otherMouseDragged: (NSEvent*) ev;*/ + +-(BOOL) acceptsFirstResponder; +@end + +struct window { + int wid; + NSWindow *win; + OpenGLView *view; + struct window *next; +}; + + +static int init(void); +static void shutdown(void); + +/* video mode switching */ +static int set_vidmode(int xsz, int ysz); +static int get_vidmode(int *xsz, int *ysz); + +/* create/destroy windows */ +static int create_window(int xsz, int ysz, unsigned int flags); +static void close_window(int wid); + +/* window management */ +static int set_active(int wid); +static struct window *find_window(int wid); +static int activate_window(struct window *win); +static int set_title(const char *str); +static void redisplay(void); +static void swap_buffers(void); + +static int get_modifiers(void); + +/* event handling and friends */ +static void set_event(int idx, int enable); +static int process_events(void); +static int handle_event(NSEvent *ev); + + +static struct wsys_module ws = { + "cocoa", 0, + init, + shutdown, + set_vidmode, + get_vidmode, + create_window, + close_window, + set_active, + set_title, + redisplay, + swap_buffers, + get_modifiers, + set_event, + process_events, + 0 +}; + +static NSApplication *app; +static struct window *winlist, *active_win; + + +void sgl_register_cocoa(void) +{ + sgl_register_cocoa(&ws); +} + + +@implementation OpenGLView + +-(void) drawRect: (NSRect) rect +{ + sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY); + if(func) { + func(); + } +} + +-(void) reshape +{ + sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE); + if(func) { + func(x, y); + } +} + +-(BOOL) acceptsFirstResponder +{ + return YES; +} +@end + +static int init(void) +{ + app = [NSApplication sharedApplication]; +} + +static void shutdown(void) +{ + while(winlist) { + struct window *win = winlist; + winlist = winlist->next; + + /* TODO destroy window */ + free(win); + } +} + + +/* video mode switching */ +static int set_vidmode(int xsz, int ysz) +{ + return 0; /* TODO */ +} + +static int get_vidmode(int *xsz, int *ysz) +{ + return 0; /* TODO */ +} + + +/* create/destroy windows */ +static int create_window(int xsz, int ysz, unsigned int flags) +{ + NSWindow *nswin; + NSRect rect; + NSView *view; + unsigned int style; + struct window *win; + static int next_id = 1; + + if(!(win = malloc(sizeof *win))) { + return -1; + } + + /* create the window and attach the OpenGL view */ + rect.origin.x = rect.origin.y = 0; + rect.size.width = xsz; + rect.size.height = ysz; + + style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | + NSResizableWindowMask; + + nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style + backing: NSBackingStoreBuffered defer: YES]; + view = [[OpenGLView alloc] initWithFrame: rect]; + [nswin setContentView: view]; + [view release]; + + win->win = nswin; + win->view = view; + win->wid = next_id++; + win->next = winlist; + winlist = win; + + return win->wid; +} + +static void close_window(int wid) +{ + struct window *win, *prev, dummy; + sgl_close_callback_t close_func; + + dummy.next = win = winlist; + prev = &dummy; + + while(win) { + if(win->wid == wid) { + if(!(close_func = sgl_get_callback(SGL_CLOSE))) { + close_func(wid); + } + [win->win close]; + + if(active_win == win) { + activate_window(winlist); + } + + prev->next = win->next; + free(win); + return; + } + prev = win; + win = win->next; + } +} + + +/* window management */ +static int set_active(int wid) +{ +} + +static struct window *find_window(int wid) +{ + struct window *win = winlist; + + while(win) { + if(win->win == id) { + return win; + } + win = win->next; + } + return 0; +} + +static int activate_window(struct window *win) +{ + if(!win) { + return -1; + } + [win->ctx makeCurrentContext]; + active_win = win; + return 0; +} + +static int set_title(const char *str) +{ + NSString *nsstr; + + nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding]; + [active_win->win setTitle: nsstr]; + [nsstr release]; +} + +static void redisplay(void) +{ + [active_win->view setNeedsRedisplay: YES]; +} + +static void swap_buffers(void) +{ + [active_win->flushBuffer]; +} + + +static int get_modifiers(void) +{ + return 0; /* TODO */ +} + + +/* event handling and friends */ +static void set_event(int idx, int enable) +{ +} + +static int process_events(void) +{ + NSAutoreleasePool *pool; + int state; + + pool = [[NSAutoreleasePool alloc] init]; + + for(;;) { + NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask + untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES]; + if(ev == nil) { + break; + } + + handle_event(ev); + } +} + +static int handle_event(NSEvent *ev) +{ + /* TODO */ +} + +#endif /* USE_WSYS_MODULE_COCOA */ diff -r bf34fa677960 -r e989ab58ec5b src/wsys_glut.c --- a/src/wsys_glut.c Sat May 14 12:02:22 2011 +0300 +++ b/src/wsys_glut.c Mon May 16 23:05:57 2011 +0300 @@ -177,6 +177,7 @@ free(win); break; } + prev = win; win = win->next; } diff -r bf34fa677960 -r e989ab58ec5b src/wsys_x11.c --- a/src/wsys_x11.c Sat May 14 12:02:22 2011 +0300 +++ b/src/wsys_x11.c Mon May 16 23:05:57 2011 +0300 @@ -305,6 +305,7 @@ free(win); return; } + prev = win; win = win->next; } }