sgl

changeset 6:0cb438c86b98

X11 sounds about ready
author John Tsiombikas <nuclear@siggraph.org>
date Fri, 13 May 2011 09:44:21 +0300
parents 0570e27e5ebc
children edbfc96fe80d
files include/sgl.h src/cb.c src/wsys_x11.c tests/simple/simple.c
diffstat 4 files changed, 94 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/include/sgl.h	Fri May 13 07:49:47 2011 +0300
     1.2 +++ b/include/sgl.h	Fri May 13 09:44:21 2011 +0300
     1.3 @@ -7,6 +7,16 @@
     1.4  #define SGL_STEREO		8
     1.5  #define SGL_MULTISAMPLE	16
     1.6  
     1.7 +typedef void (*sgl_create_callback_t)(int);
     1.8 +typedef void (*sgl_close_callback_t)(int);
     1.9 +typedef void (*sgl_display_callback_t)(void);
    1.10 +typedef void (*sgl_reshape_callback_t)(int, int);
    1.11 +typedef void (*sgl_keyboard_callback_t)(int, int);
    1.12 +typedef void (*sgl_mouse_callback_t)(int, int, int, int, int);
    1.13 +typedef void (*sgl_motion_callback_t)(int, int, int);
    1.14 +typedef void (*sgl_passive_callback_t)(int, int, int);
    1.15 +typedef void (*sgl_idle_callback_t)(void);
    1.16 +
    1.17  enum {
    1.18  	SGL_CREATE,
    1.19  	SGL_CLOSE,
    1.20 @@ -51,4 +61,14 @@
    1.21  void sgl_set_callback(int idx, void (*func)());
    1.22  void (*sgl_get_callback(int idx))();
    1.23  
    1.24 +void sgl_create_callback(sgl_create_callback_t func);
    1.25 +void sgl_close_callback(sgl_close_callback_t func);
    1.26 +void sgl_display_callback(sgl_display_callback_t func);
    1.27 +void sgl_reshape_callback(sgl_reshape_callback_t func);
    1.28 +void sgl_keyboard_callback(sgl_keyboard_callback_t func);
    1.29 +void sgl_mouse_callback(sgl_mouse_callback_t func);
    1.30 +void sgl_motion_callback(sgl_motion_callback_t func);
    1.31 +void sgl_passive_callback(sgl_passive_callback_t func);
    1.32 +void sgl_idle_callback(sgl_idle_callback_t func);
    1.33 +
    1.34  #endif	/* SGL_H_ */
     2.1 --- a/src/cb.c	Fri May 13 07:49:47 2011 +0300
     2.2 +++ b/src/cb.c	Fri May 13 09:44:21 2011 +0300
     2.3 @@ -64,6 +64,53 @@
     2.4  	return cb->func[idx];
     2.5  }
     2.6  
     2.7 +
     2.8 +void sgl_create_callback(sgl_create_callback_t func)
     2.9 +{
    2.10 +	sgl_set_callback(SGL_CREATE, func);
    2.11 +}
    2.12 +
    2.13 +void sgl_close_callback(sgl_close_callback_t func)
    2.14 +{
    2.15 +	sgl_set_callback(SGL_CLOSE, func);
    2.16 +}
    2.17 +
    2.18 +void sgl_display_callback(sgl_display_callback_t func)
    2.19 +{
    2.20 +	sgl_set_callback(SGL_DISPLAY, func);
    2.21 +}
    2.22 +
    2.23 +void sgl_reshape_callback(sgl_reshape_callback_t func)
    2.24 +{
    2.25 +	sgl_set_callback(SGL_RESHAPE, func);
    2.26 +}
    2.27 +
    2.28 +void sgl_keyboard_callback(sgl_keyboard_callback_t func)
    2.29 +{
    2.30 +	sgl_set_callback(SGL_KEYBOARD, func);
    2.31 +}
    2.32 +
    2.33 +void sgl_mouse_callback(sgl_mouse_callback_t func)
    2.34 +{
    2.35 +	sgl_set_callback(SGL_MOUSE, func);
    2.36 +}
    2.37 +
    2.38 +void sgl_motion_callback(sgl_motion_callback_t func)
    2.39 +{
    2.40 +	sgl_set_callback(SGL_MOTION, func);
    2.41 +}
    2.42 +
    2.43 +void sgl_passive_callback(sgl_passive_callback_t func)
    2.44 +{
    2.45 +	sgl_set_callback(SGL_PASSIVE, func);
    2.46 +}
    2.47 +
    2.48 +void sgl_idle_callback(sgl_idle_callback_t func)
    2.49 +{
    2.50 +	sgl_set_callback(SGL_IDLE, func);
    2.51 +}
    2.52 +
    2.53 +
    2.54  /* notify the window system module as to which events are active */
    2.55  static void notify_wsys(void)
    2.56  {
     3.1 --- a/src/wsys_x11.c	Fri May 13 07:49:47 2011 +0300
     3.2 +++ b/src/wsys_x11.c	Fri May 13 09:44:21 2011 +0300
     3.3 @@ -139,6 +139,7 @@
     3.4  	unsigned int attr_valid;
     3.5  	long evmask;
     3.6  	struct window *wnode;
     3.7 +	void (*func)();
     3.8  
     3.9  	if(!(wnode = malloc(sizeof *wnode))) {
    3.10  		return -1;
    3.11 @@ -198,7 +199,17 @@
    3.12  
    3.13  	if(!active_win) {
    3.14  		set_active(win);
    3.15 +	} else {
    3.16 +		activate_window(wnode);
    3.17  	}
    3.18 +
    3.19 +	if((func = sgl_get_callback(SGL_CREATE))) {
    3.20 +		func(win);
    3.21 +	}
    3.22 +	if((func = sgl_get_callback(SGL_RESHAPE))) {
    3.23 +		func(xsz, ysz);
    3.24 +	}
    3.25 +	activate_window(prev_active);
    3.26  	return win;
    3.27  }
    3.28  
    3.29 @@ -263,6 +274,8 @@
    3.30  static void close_window(int id)
    3.31  {
    3.32  	struct window dummy, *win, *prev;
    3.33 +	sgl_close_callback_t close_func;
    3.34 +
    3.35  	dummy.next = winlist;
    3.36  
    3.37  	prev = &dummy;
    3.38 @@ -270,6 +283,9 @@
    3.39  
    3.40  	while(win) {
    3.41  		if(win->win == id) {
    3.42 +			if(!(close_func = sgl_get_callback(SGL_CLOSE))) {
    3.43 +				close_func(id);
    3.44 +			}
    3.45  			glXDestroyContext(dpy, win->ctx);
    3.46  			XDestroyWindow(dpy, win->win);
    3.47  			prev->next = win->next;
    3.48 @@ -465,7 +481,7 @@
    3.49  			func = sgl_get_callback(SGL_PASSIVE);
    3.50  		}
    3.51  		if(func) {
    3.52 -			func(xev->xmotion.x, xev->xmotion.y);
    3.53 +			func(0, xev->xmotion.x, xev->xmotion.y);
    3.54  		}
    3.55  		break;
    3.56  
    3.57 @@ -478,7 +494,7 @@
    3.58  		}
    3.59  		if((func = sgl_get_callback(SGL_MOUSE))) {
    3.60  			int bn = xev->xbutton.button - 1;
    3.61 -			func(bn, state, xev->xbutton.x, xev->xbutton.y);
    3.62 +			func(0, bn, state, xev->xbutton.x, xev->xbutton.y);
    3.63  		}
    3.64  		break;
    3.65  
     4.1 --- a/tests/simple/simple.c	Fri May 13 07:49:47 2011 +0300
     4.2 +++ b/tests/simple/simple.c	Fri May 13 09:44:21 2011 +0300
     4.3 @@ -6,8 +6,8 @@
     4.4  void disp(void);
     4.5  void reshape(int x, int y);
     4.6  void keyb(int key, int state);
     4.7 -void mouse(int bn, int state, int x, int y);
     4.8 -void motion(int x, int y);
     4.9 +void mouse(int pidx, int bn, int state, int x, int y);
    4.10 +void motion(int pidx, int x, int y);
    4.11  
    4.12  
    4.13  int main(void)
    4.14 @@ -18,11 +18,11 @@
    4.15  
    4.16  	sgl_create_window(640, 480, SGL_DOUBLE | SGL_DEPTH);
    4.17  
    4.18 -	sgl_set_callback(SGL_DISPLAY, disp);
    4.19 -	sgl_set_callback(SGL_RESHAPE, reshape);
    4.20 -	sgl_set_callback(SGL_KEYBOARD, keyb);
    4.21 -	sgl_set_callback(SGL_MOUSE, mouse);
    4.22 -	sgl_set_callback(SGL_MOTION, motion);
    4.23 +	sgl_display_callback(disp);
    4.24 +	sgl_reshape_callback(reshape);
    4.25 +	sgl_keyboard_callback(keyb);
    4.26 +	sgl_mouse_callback(mouse);
    4.27 +	sgl_motion_callback(motion);
    4.28  
    4.29  	sgl_event_loop();
    4.30  	sgl_quit();
    4.31 @@ -72,12 +72,12 @@
    4.32  	}
    4.33  }
    4.34  
    4.35 -void mouse(int bn, int state, int x, int y)
    4.36 +void mouse(int pidx, int bn, int state, int x, int y)
    4.37  {
    4.38  	printf("mouse: button%d %s (ptr: %d %d)\n", bn, state ? "pressed" : "released", x, y);
    4.39  }
    4.40  
    4.41 -void motion(int x, int y)
    4.42 +void motion(int pidx, int x, int y)
    4.43  {
    4.44  	printf("mouse dragged to: (%d %d)\n", x, y);
    4.45  }