sgl

changeset 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 bf34fa677960
children 5b8fb89fe63f
files Makefile.in configure src/wsys_cocoa.m src/wsys_glut.c src/wsys_x11.c
diffstat 5 files changed, 310 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/Makefile.in	Sat May 14 12:02:22 2011 +0300
     1.2 +++ b/Makefile.in	Mon May 16 23:05:57 2011 +0300
     1.3 @@ -1,4 +1,5 @@
     1.4  src = $(wildcard src/*.c)
     1.5 +msrc = $(wildcard src/*.m)
     1.6  obj = $(src:.c=.o)
     1.7  dep = $(src:.c=.d)
     1.8  lib_a = libsgl.a
     1.9 @@ -16,6 +17,7 @@
    1.10  
    1.11  ifeq ($(shell uname -s), Darwin)
    1.12  	sys = mac
    1.13 +	obj += $(msrc:.m=.o)
    1.14  else
    1.15  	sys = unix
    1.16  endif
     2.1 --- a/configure	Sat May 14 12:02:22 2011 +0300
     2.2 +++ b/configure	Mon May 16 23:05:57 2011 +0300
     2.3 @@ -30,6 +30,10 @@
     2.4  
     2.5  	echo 'int main(void) { return 0; }' >$srcfile
     2.6  	cc -o $aout $srcfile $1 >/dev/null 2>/dev/null
     2.7 +	res=$?
     2.8 +
     2.9 +	rm -f $srcfile $aout
    2.10 +	return $res
    2.11  }
    2.12  
    2.13  # write beginning of config.h
    2.14 @@ -51,7 +55,7 @@
    2.15  echo 'Looking for usable window system modules ...'
    2.16  
    2.17  # collect all src/wsys_whatever.c files
    2.18 -all_files=`ls src/wsys_*.c 2>/dev/null`
    2.19 +all_files=`ls src/wsys_*.c src/wsys_*.m 2>/dev/null`
    2.20  
    2.21  for m in $all_files; do
    2.22  	# extract USE_WSYS_MODULE_* define
    2.23 @@ -65,9 +69,12 @@
    2.24  		fi
    2.25  	else
    2.26  		dep=`get_depline $m`
    2.27 +		if [ -z "$dep" ]; then
    2.28 +			dep=`get_framework $m`
    2.29 +		fi
    2.30  	fi
    2.31  
    2.32 -	name=`echo $m | sort | sed 's/src\/wsys_//' | sed 's/\.c//'`
    2.33 +	name=`echo $m | sort | sed 's/src\/wsys_//' | sed 's/\.c\|\.m//'`
    2.34  	`which echo` -n "-> trying module $name (needs: $dep) ... "
    2.35  
    2.36  	if try_link "$dep"; then
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/wsys_cocoa.m	Mon May 16 23:05:57 2011 +0300
     3.3 @@ -0,0 +1,297 @@
     3.4 +/* mac-framework: -framework Cocoa */
     3.5 +
     3.6 +#include "config.h"
     3.7 +
     3.8 +#ifdef USE_WSYS_MODULE_COCOA
     3.9 +
    3.10 +#import <Cocoa/Cocoa.h>
    3.11 +#include "sgl.h"
    3.12 +
    3.13 +@interface OpenGLView : NSOpenGLView
    3.14 +{
    3.15 +}
    3.16 +
    3.17 +//-(id) initWithFrame: (NSRect) frameRect;
    3.18 +
    3.19 +-(void) drawRect: (NSRect) rect;
    3.20 +-(void) reshape;
    3.21 +/*-(void) keyDown: (NSEvent*) ev;
    3.22 +-(void) keyUp: (NSEvent*) ev;
    3.23 +-(void) mouseDown: (NSEvent*) ev;
    3.24 +-(void) mouseUp: (NSEvent*) ev;
    3.25 +-(void) rightMouseDown: (NSEvent*) ev;
    3.26 +-(void) rightMouseUp: (NSEvent*) ev;
    3.27 +-(void) otherMouseDown: (NSEvent*) ev;
    3.28 +-(void) otherMouseUp: (NSEvent*) ev;
    3.29 +-(void) mouseDragged: (NSEvent*) ev;
    3.30 +-(void) rightMouseDragged: (NSEvent*) ev;
    3.31 +-(void) otherMouseDragged: (NSEvent*) ev;*/
    3.32 +
    3.33 +-(BOOL) acceptsFirstResponder;
    3.34 +@end
    3.35 +
    3.36 +struct window {
    3.37 +	int wid;
    3.38 +	NSWindow *win;
    3.39 +	OpenGLView *view;
    3.40 +	struct window *next;
    3.41 +};
    3.42 +
    3.43 +
    3.44 +static int init(void);
    3.45 +static void shutdown(void);
    3.46 +
    3.47 +/* video mode switching */
    3.48 +static int set_vidmode(int xsz, int ysz);
    3.49 +static int get_vidmode(int *xsz, int *ysz);
    3.50 +
    3.51 +/* create/destroy windows */
    3.52 +static int create_window(int xsz, int ysz, unsigned int flags);
    3.53 +static void close_window(int wid);
    3.54 +
    3.55 +/* window management */
    3.56 +static int set_active(int wid);
    3.57 +static struct window *find_window(int wid);
    3.58 +static int activate_window(struct window *win);
    3.59 +static int set_title(const char *str);
    3.60 +static void redisplay(void);
    3.61 +static void swap_buffers(void);
    3.62 +
    3.63 +static int get_modifiers(void);
    3.64 +
    3.65 +/* event handling and friends */
    3.66 +static void set_event(int idx, int enable);
    3.67 +static int process_events(void);
    3.68 +static int handle_event(NSEvent *ev);
    3.69 +
    3.70 +
    3.71 +static struct wsys_module ws = {
    3.72 +	"cocoa", 0,
    3.73 +	init,
    3.74 +	shutdown,
    3.75 +	set_vidmode,
    3.76 +	get_vidmode,
    3.77 +	create_window,
    3.78 +	close_window,
    3.79 +	set_active,
    3.80 +	set_title,
    3.81 +	redisplay,
    3.82 +	swap_buffers,
    3.83 +	get_modifiers,
    3.84 +	set_event,
    3.85 +	process_events,
    3.86 +	0
    3.87 +};
    3.88 +
    3.89 +static NSApplication *app;
    3.90 +static struct window *winlist, *active_win;
    3.91 +
    3.92 +
    3.93 +void sgl_register_cocoa(void)
    3.94 +{
    3.95 +	sgl_register_cocoa(&ws);
    3.96 +}
    3.97 +
    3.98 +
    3.99 +@implementation OpenGLView
   3.100 +
   3.101 +-(void) drawRect: (NSRect) rect
   3.102 +{
   3.103 +	sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY);
   3.104 +	if(func) {
   3.105 +		func();
   3.106 +	}
   3.107 +}
   3.108 +
   3.109 +-(void) reshape
   3.110 +{
   3.111 +	sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE);
   3.112 +	if(func) {
   3.113 +		func(x, y);
   3.114 +	}
   3.115 +}
   3.116 +
   3.117 +-(BOOL) acceptsFirstResponder
   3.118 +{
   3.119 +	return YES;
   3.120 +}
   3.121 +@end
   3.122 +
   3.123 +static int init(void)
   3.124 +{
   3.125 +	app = [NSApplication sharedApplication];
   3.126 +}
   3.127 +
   3.128 +static void shutdown(void)
   3.129 +{
   3.130 +	while(winlist) {
   3.131 +		struct window *win = winlist;
   3.132 +		winlist = winlist->next;
   3.133 +
   3.134 +		/* TODO destroy window */
   3.135 +		free(win);
   3.136 +	}
   3.137 +}
   3.138 +
   3.139 +
   3.140 +/* video mode switching */
   3.141 +static int set_vidmode(int xsz, int ysz)
   3.142 +{
   3.143 +	return 0;	/* TODO */
   3.144 +}
   3.145 +
   3.146 +static int get_vidmode(int *xsz, int *ysz)
   3.147 +{
   3.148 +	return 0;	/* TODO */
   3.149 +}
   3.150 +
   3.151 +
   3.152 +/* create/destroy windows */
   3.153 +static int create_window(int xsz, int ysz, unsigned int flags)
   3.154 +{
   3.155 +	NSWindow *nswin;
   3.156 +	NSRect rect;
   3.157 +	NSView *view;
   3.158 +	unsigned int style;
   3.159 +	struct window *win;
   3.160 +	static int next_id = 1;
   3.161 +
   3.162 +	if(!(win = malloc(sizeof *win))) {
   3.163 +		return -1;
   3.164 +	}
   3.165 +
   3.166 +	/* create the window and attach the OpenGL view */
   3.167 +	rect.origin.x = rect.origin.y = 0;
   3.168 +	rect.size.width = xsz;
   3.169 +	rect.size.height = ysz;
   3.170 +
   3.171 +	style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask |
   3.172 +		NSResizableWindowMask;
   3.173 +
   3.174 +	nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style
   3.175 +		backing: NSBackingStoreBuffered defer: YES];
   3.176 +	view = [[OpenGLView alloc] initWithFrame: rect];
   3.177 +	[nswin setContentView: view];
   3.178 +	[view release];
   3.179 +
   3.180 +	win->win = nswin;
   3.181 +	win->view = view;
   3.182 +	win->wid = next_id++;
   3.183 +	win->next = winlist;
   3.184 +	winlist = win;
   3.185 +
   3.186 +	return win->wid;
   3.187 +}
   3.188 +
   3.189 +static void close_window(int wid)
   3.190 +{
   3.191 +	struct window *win, *prev, dummy;
   3.192 +	sgl_close_callback_t close_func;
   3.193 +
   3.194 +	dummy.next = win = winlist;
   3.195 +	prev = &dummy;
   3.196 +
   3.197 +	while(win) {
   3.198 +		if(win->wid == wid) {
   3.199 +			if(!(close_func = sgl_get_callback(SGL_CLOSE))) {
   3.200 +				close_func(wid);
   3.201 +			}
   3.202 +			[win->win close];
   3.203 +
   3.204 +			if(active_win == win) {
   3.205 +				activate_window(winlist);
   3.206 +			}
   3.207 +
   3.208 +			prev->next = win->next;
   3.209 +			free(win);
   3.210 +			return;
   3.211 +		}
   3.212 +		prev = win;
   3.213 +		win = win->next;
   3.214 +	}
   3.215 +}
   3.216 +
   3.217 +
   3.218 +/* window management */
   3.219 +static int set_active(int wid)
   3.220 +{
   3.221 +}
   3.222 +
   3.223 +static struct window *find_window(int wid)
   3.224 +{
   3.225 +	struct window *win = winlist;
   3.226 +
   3.227 +	while(win) {
   3.228 +		if(win->win == id) {
   3.229 +			return win;
   3.230 +		}
   3.231 +		win = win->next;
   3.232 +	}
   3.233 +	return 0;
   3.234 +}
   3.235 +
   3.236 +static int activate_window(struct window *win)
   3.237 +{
   3.238 +	if(!win) {
   3.239 +		return -1;
   3.240 +	}
   3.241 +	[win->ctx makeCurrentContext];
   3.242 +	active_win = win;
   3.243 +	return 0;
   3.244 +}
   3.245 +
   3.246 +static int set_title(const char *str)
   3.247 +{
   3.248 +	NSString *nsstr;
   3.249 +
   3.250 +	nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding];
   3.251 +	[active_win->win setTitle: nsstr];
   3.252 +	[nsstr release];
   3.253 +}
   3.254 +
   3.255 +static void redisplay(void)
   3.256 +{
   3.257 +	[active_win->view setNeedsRedisplay: YES];
   3.258 +}
   3.259 +
   3.260 +static void swap_buffers(void)
   3.261 +{
   3.262 +	[active_win->flushBuffer];
   3.263 +}
   3.264 +
   3.265 +
   3.266 +static int get_modifiers(void)
   3.267 +{
   3.268 +	return 0;	/* TODO */
   3.269 +}
   3.270 +
   3.271 +
   3.272 +/* event handling and friends */
   3.273 +static void set_event(int idx, int enable)
   3.274 +{
   3.275 +}
   3.276 +
   3.277 +static int process_events(void)
   3.278 +{
   3.279 +	NSAutoreleasePool *pool;
   3.280 +	int state;
   3.281 +
   3.282 +	pool = [[NSAutoreleasePool alloc] init];
   3.283 +
   3.284 +	for(;;) {
   3.285 +		NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask
   3.286 +			untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES];
   3.287 +		if(ev == nil) {
   3.288 +			break;
   3.289 +		}
   3.290 +
   3.291 +		handle_event(ev);
   3.292 +	}
   3.293 +}
   3.294 +
   3.295 +static int handle_event(NSEvent *ev)
   3.296 +{
   3.297 +	/* TODO */
   3.298 +}
   3.299 +
   3.300 +#endif	/* USE_WSYS_MODULE_COCOA */
     4.1 --- a/src/wsys_glut.c	Sat May 14 12:02:22 2011 +0300
     4.2 +++ b/src/wsys_glut.c	Mon May 16 23:05:57 2011 +0300
     4.3 @@ -177,6 +177,7 @@
     4.4  			free(win);
     4.5  			break;
     4.6  		}
     4.7 +		prev = win;
     4.8  		win = win->next;
     4.9  	}
    4.10  
     5.1 --- a/src/wsys_x11.c	Sat May 14 12:02:22 2011 +0300
     5.2 +++ b/src/wsys_x11.c	Mon May 16 23:05:57 2011 +0300
     5.3 @@ -305,6 +305,7 @@
     5.4  			free(win);
     5.5  			return;
     5.6  		}
     5.7 +		prev = win;
     5.8  		win = win->next;
     5.9  	}
    5.10  }