sgl

view src/wsys_x11.c @ 22:b5c852d7661e

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 26 Jun 2011 23:57:53 +0300
parents e989ab58ec5b
children
line source
1 /* SimplyGL window system module for X11/GLX */
2 /* link-with: -lX11 */
4 #include "config.h"
6 #ifdef USE_WSYS_MODULE_X11
8 #include <stdlib.h>
9 #include <ctype.h>
10 #include <X11/Xlib.h>
11 #include <GL/glx.h>
12 #include "sgl.h"
13 #include "wsys.h"
14 #include "log.h"
17 struct window {
18 Window win;
19 GLXContext ctx;
20 int width, height;
21 int mapped;
22 long evmask;
23 int redisp_pending;
24 struct window *next;
25 };
27 static int init(void);
28 static void shutdown(void);
30 /* video mode switching */
31 static int set_vidmode(int xsz, int ysz);
32 static int get_vidmode(int *xsz, int *ysz);
34 /* create/destroy windows */
35 static int create_window(int xsz, int ysz, unsigned int flags);
36 static void fill_attr(int *attr, unsigned int flags);
37 static void print_visual_info(XVisualInfo *vis);
38 static void close_window(int id);
40 /* window management */
41 static int set_active(int id);
42 static struct window *find_window(int id);
43 static int activate_window(struct window *win);
44 static int set_title(const char *str);
45 static void redisplay(void);
46 static void swap_buffers(void);
48 static int get_modifiers(void);
50 /* event handling and friends */
51 static void set_bits(long *mask, long bits);
52 static void clear_bits(long *mask, long bits);
53 static void set_event(int idx, int enable);
54 static int process_events(void);
55 static int handle_event(XEvent *xev);
56 static void process_key(KeySym sym, int state);
57 static int translate_keysym(KeySym sym);
59 static struct wsys_module ws = {
60 "x11-glx", 0,
61 init,
62 shutdown,
63 set_vidmode,
64 get_vidmode,
65 create_window,
66 close_window,
67 set_active,
68 set_title,
69 redisplay,
70 swap_buffers,
71 get_modifiers,
72 set_event,
73 process_events,
74 0
75 };
77 static Display *dpy;
78 static Window root;
79 static int scr;
80 static Atom xa_wm_prot, xa_wm_del_win;
81 static struct window *winlist;
82 static struct window *active_win, *prev_active;
83 static int modkeys;
85 /* this is the only exported function, everything else should be static */
86 void sgl_register_x11(void)
87 {
88 sgl_register_module(&ws);
89 }
91 static int init(void)
92 {
93 if(dpy) {
94 sgl_log("warning: double init\n");
95 return 0;
96 }
98 winlist = 0;
99 active_win = prev_active = 0;
101 if(!(dpy = XOpenDisplay(0))) {
102 sgl_log("failed to open X display: %s\n", XDisplayName(0));
103 return -1;
104 }
105 scr = DefaultScreen(dpy);
106 root = RootWindow(dpy, scr);
108 xa_wm_prot = XInternAtom(dpy, "WM_PROTOCOLS", False);
109 xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
111 return 0;
112 }
114 static void shutdown(void)
115 {
116 if(!dpy) {
117 return;
118 }
120 while(winlist) {
121 struct window *win = winlist;
122 winlist = winlist->next;
124 glXDestroyContext(dpy, win->ctx);
125 XDestroyWindow(dpy, win->win);
126 free(win);
127 }
128 XCloseDisplay(dpy);
129 dpy = 0;
130 }
132 static int set_vidmode(int xsz, int ysz)
133 {
134 /* TODO */
135 return 0;
136 }
138 static int get_vidmode(int *xsz, int *ysz)
139 {
140 /* TODO */
141 return 0;
142 }
144 static int create_window(int xsz, int ysz, unsigned int flags)
145 {
146 int attr[32];
147 Window win;
148 GLXContext ctx;
149 XVisualInfo *vis;
150 XClassHint chint;
151 XSetWindowAttributes xattr;
152 unsigned int attr_valid;
153 long evmask;
154 struct window *wnode;
155 void (*func)();
157 if(!(wnode = malloc(sizeof *wnode))) {
158 return -1;
159 }
161 fill_attr(attr, flags);
163 if(!(vis = glXChooseVisual(dpy, scr, attr))) {
164 sgl_log("failed to find a suitable visual\n");
165 free(wnode);
166 return -1;
167 }
168 print_visual_info(vis);
170 if(!(ctx = glXCreateContext(dpy, vis, 0, True))) {
171 sgl_log("failed to create OpenGL context\n");
172 XFree(vis);
173 free(wnode);
174 return -1;
175 }
177 xattr.background_pixel = xattr.border_pixel = BlackPixel(dpy, scr);
178 xattr.colormap = XCreateColormap(dpy, root, vis->visual, AllocNone);
179 attr_valid = CWColormap | CWBackPixel | CWBorderPixel;
181 if(!(win = XCreateWindow(dpy, root, 0, 0, xsz, ysz, 0, vis->depth, InputOutput,
182 vis->visual, attr_valid, &xattr))) {
183 sgl_log("failed to create window\n");
184 glXDestroyContext(dpy, ctx);
185 XFree(vis);
186 free(wnode);
187 return -1;
188 }
189 XFree(vis);
191 evmask = StructureNotifyMask | VisibilityChangeMask;
192 XSelectInput(dpy, win, evmask);
194 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
196 chint.res_name = chint.res_class = "simplygl";
197 XSetClassHint(dpy, win, &chint);
199 XMapWindow(dpy, win);
201 wnode->win = win;
202 wnode->ctx = ctx;
203 wnode->width = xsz;
204 wnode->height = ysz;
205 wnode->mapped = 0;
206 wnode->evmask = evmask;
207 wnode->redisp_pending = 1;
208 wnode->next = winlist;
209 winlist = wnode;
211 if(!active_win) {
212 set_active(win);
213 } else {
214 activate_window(wnode);
215 }
216 set_title("OpenGL/X11");
218 if((func = sgl_get_callback(SGL_CREATE))) {
219 func(win);
220 }
221 if((func = sgl_get_callback(SGL_RESHAPE))) {
222 func(xsz, ysz);
223 }
224 activate_window(prev_active);
225 return win;
226 }
228 static void fill_attr(int *attr, unsigned int flags)
229 {
230 int i = 0;
232 attr[i++] = GLX_RGBA;
233 attr[i++] = GLX_RED_SIZE; attr[i++] = 1;
234 attr[i++] = GLX_GREEN_SIZE; attr[i++] = 1;
235 attr[i++] = GLX_BLUE_SIZE; attr[i++] = 1;
237 if(flags & SGL_DOUBLE) {
238 attr[i++] = GLX_DOUBLEBUFFER;
239 }
240 if(flags & SGL_DEPTH) {
241 attr[i++] = GLX_DEPTH_SIZE;
242 attr[i++] = 1;
243 }
244 if(flags & SGL_STENCIL) {
245 attr[i++] = GLX_STENCIL_SIZE;
246 attr[i++] = 1;
247 }
248 if(flags & SGL_STEREO) {
249 attr[i++] = GLX_STEREO;
250 }
251 #if defined(GLX_VERSION_1_4) || defined(GLX_ARB_multisample)
252 if(flags & SGL_MULTISAMPLE) {
253 attr[i++] = GLX_SAMPLE_BUFFERS_ARB;
254 attr[i++] = 1;
255 attr[i++] = GLX_SAMPLES_ARB;
256 attr[i++] = 1;
257 }
258 #endif /* defined GLX_VERSION_1_4 || GLX_ARB_multisample */
259 attr[i] = None;
260 }
262 static void print_visual_info(XVisualInfo *vis)
263 {
264 int rbits, gbits, bbits, zbits, sbits, stereo, aa, samples;
266 glXGetConfig(dpy, vis, GLX_RED_SIZE, &rbits);
267 glXGetConfig(dpy, vis, GLX_GREEN_SIZE, &gbits);
268 glXGetConfig(dpy, vis, GLX_BLUE_SIZE, &bbits);
269 glXGetConfig(dpy, vis, GLX_DEPTH_SIZE, &zbits);
270 glXGetConfig(dpy, vis, GLX_STENCIL_SIZE, &sbits);
271 glXGetConfig(dpy, vis, GLX_STEREO, &stereo);
272 #if defined(GLX_VERSION_1_4) || defined(GLX_ARB_multisample)
273 glXGetConfig(dpy, vis, GLX_SAMPLE_BUFFERS_ARB, &aa);
274 if(aa) {
275 glXGetConfig(dpy, vis, GLX_SAMPLES_ARB, &samples);
276 } else {
277 samples = 1;
278 }
279 #endif /* defined GLX_VERSION_1_4 || GLX_ARB_multisample */
281 sgl_log("got visual: %d%d%d d:%d s:%d", rbits, gbits, bbits, zbits, sbits);
282 sgl_log(" %s", stereo ? "stereo" : "mono");
283 sgl_log(" samples/pixel: %d\n", samples);
284 }
286 static void close_window(int id)
287 {
288 struct window dummy, *win, *prev;
289 sgl_close_callback_t close_func;
291 dummy.next = winlist;
293 prev = &dummy;
294 win = prev->next;
296 while(win) {
297 if(win->win == id) {
298 if(!(close_func = sgl_get_callback(SGL_CLOSE))) {
299 close_func(id);
300 }
301 glXDestroyContext(dpy, win->ctx);
302 XDestroyWindow(dpy, win->win);
303 prev->next = win->next;
304 free(win);
305 return;
306 }
307 prev = win;
308 win = win->next;
309 }
310 }
312 static int set_active(int id)
313 {
314 struct window *win = find_window(id);
315 if(!win) {
316 sgl_log("no such window: %d\n", id);
317 return -1;
318 }
319 /* only the user calls this, so don't revert this selection */
320 prev_active = win;
321 return activate_window(win);
322 }
324 static struct window *find_window(int id)
325 {
326 struct window *win = winlist;
328 while(win) {
329 if(win->win == id) {
330 return win;
331 }
332 win = win->next;
333 }
334 return 0;
335 }
337 static int activate_window(struct window *win)
338 {
339 if(glXMakeCurrent(dpy, win->win, win->ctx) == False) {
340 sgl_log("failed to activate window %d\n", (int)win->win);
341 return -1;
342 }
343 active_win = win;
344 return 0;
345 }
347 static int set_title(const char *str)
348 {
349 XTextProperty wm_name;
351 if(!str || !active_win) {
352 return -1;
353 }
354 XStringListToTextProperty((char**)&str, 1, &wm_name);
355 XSetWMName(dpy, active_win->win, &wm_name);
356 XSetWMIconName(dpy, active_win->win, &wm_name);
357 XFree(wm_name.value);
358 return 0;
359 }
361 static void redisplay(void)
362 {
363 active_win->redisp_pending = 1;
364 }
366 static void swap_buffers(void)
367 {
368 glXSwapBuffers(dpy, active_win->win);
369 }
371 static int get_modifiers(void)
372 {
373 return modkeys;
374 }
376 static void set_bits(long *mask, long bits)
377 {
378 *mask |= bits;
379 }
381 static void clear_bits(long *mask, long bits)
382 {
383 *mask &= ~bits;
384 }
386 static void set_event(int idx, int enable)
387 {
388 void (*op)(long*, long);
389 op = enable ? set_bits : clear_bits;
391 switch(idx) {
392 case SGL_DISPLAY:
393 op(&active_win->evmask, ExposureMask);
394 break;
396 case SGL_KEYBOARD:
397 op(&active_win->evmask, KeyPressMask | KeyReleaseMask);
398 break;
400 case SGL_MOUSE:
401 op(&active_win->evmask, ButtonPressMask | ButtonReleaseMask);
402 break;
404 case SGL_MOTION:
405 op(&active_win->evmask, ButtonMotionMask);
406 break;
408 case SGL_PASSIVE:
409 op(&active_win->evmask, PointerMotionMask);
410 break;
412 default:
413 return;
414 }
416 XSelectInput(dpy, active_win->win, active_win->evmask);
417 }
419 static int process_events(void)
420 {
421 XEvent xev;
422 void (*func)();
423 struct window *win;
425 prev_active = active_win;
427 win = winlist;
428 while(win) {
429 if(win->redisp_pending && (func = sgl_get_callback(SGL_DISPLAY))) {
430 activate_window(win);
431 func();
432 win->redisp_pending = 0;
433 }
434 win = win->next;
435 }
437 func = sgl_get_callback(SGL_IDLE);
438 if(!func) {
439 XNextEvent(dpy, &xev);
440 XPutBackEvent(dpy, &xev);
441 }
443 /* process all pending events... */
444 while(XPending(dpy)) {
445 XNextEvent(dpy, &xev);
446 if(handle_event(&xev) == -1) {
447 return -1;
448 }
450 if(!dpy) {
451 return -1;
452 }
453 }
455 if(func) {
456 /* ... and then call the idle function */
457 func();
458 }
460 activate_window(prev_active);
461 return 0;
462 }
464 /* returns 0, or -1 when the last window gets closed */
465 static int handle_event(XEvent *xev)
466 {
467 int state;
468 struct window *win;
469 void (*func)();
470 KeySym sym;
472 if((win = find_window(xev->xany.window))) {
473 activate_window(win);
474 } else {
475 return 0;
476 }
478 switch(xev->type) {
479 case MapNotify:
480 active_win->mapped = 1;
481 break;
483 case UnmapNotify:
484 active_win->mapped = 0;
485 break;
487 case Expose:
488 if(active_win->mapped && xev->xexpose.count == 0) {
489 if((func = sgl_get_callback(SGL_DISPLAY))) {
490 func();
491 active_win->redisp_pending = 0;
492 }
493 }
494 break;
496 case MotionNotify:
497 if(xev->xmotion.state) {
498 func = sgl_get_callback(SGL_MOTION);
499 } else {
500 func = sgl_get_callback(SGL_PASSIVE);
501 }
502 if(func) {
503 func(0, xev->xmotion.x, xev->xmotion.y);
504 }
505 break;
507 case ButtonPress:
508 if(1) {
509 state = 1;
510 } else {
511 case ButtonRelease:
512 state = 0;
513 }
514 if((func = sgl_get_callback(SGL_MOUSE))) {
515 int bn = xev->xbutton.button - 1;
516 func(0, bn, state, xev->xbutton.x, xev->xbutton.y);
517 }
518 break;
520 case KeyPress:
521 if(1) {
522 state = 1;
523 } else {
524 case KeyRelease:
525 state = 0;
526 }
527 sym = XLookupKeysym(&xev->xkey, 0);
528 process_key(sym, state);
530 if((func = sgl_get_callback(SGL_KEYBOARD))) {
531 func(translate_keysym(sym), state);
532 }
533 break;
535 case ConfigureNotify:
536 if((func = sgl_get_callback(SGL_RESHAPE))) {
537 if(xev->xconfigure.width != active_win->width || xev->xconfigure.height != active_win->height) {
538 active_win->width = xev->xconfigure.width;
539 active_win->height = xev->xconfigure.height;
541 func(xev->xconfigure.width, xev->xconfigure.height);
542 }
543 }
544 break;
546 case ClientMessage:
547 if(xev->xclient.message_type == xa_wm_prot) {
548 if(xev->xclient.data.l[0] == xa_wm_del_win) {
549 close_window(active_win->win);
550 if(!active_win) {
551 return -1;
552 }
553 }
554 }
555 break;
557 default:
558 break;
559 }
561 return 0;
562 }
564 static void process_key(KeySym sym, int state)
565 {
566 switch(sym) {
567 case XK_Shift_L:
568 case XK_Shift_R:
569 modkeys = state ? (modkeys | SGL_MOD_SHIFT) : (modkeys & ~SGL_MOD_SHIFT);
570 break;
572 case XK_Control_L:
573 case XK_Control_R:
574 modkeys = state ? (modkeys | SGL_MOD_CONTROL) : (modkeys & ~SGL_MOD_CONTROL);
575 break;
577 case XK_Alt_L:
578 case XK_Alt_R:
579 modkeys = state ? (modkeys | SGL_MOD_ALT) : (modkeys & ~SGL_MOD_ALT);
580 break;
582 default:
583 break;
584 }
585 }
587 static int translate_keysym(KeySym sym)
588 {
589 if(sym < 256) {
590 if(isalpha(sym) && (modkeys & SGL_MOD_SHIFT)) {
591 sym = toupper(sym);
592 }
593 return sym;
594 }
596 switch(sym) {
597 case XK_BackSpace:
598 return '\b';
599 case XK_Tab:
600 return '\t';
601 case XK_Linefeed:
602 return '\r';
603 case XK_Return:
604 return '\n';
605 case XK_Escape:
606 return 27;
607 default:
608 break;
609 }
610 return (int)sym;
611 }
613 #else
614 int sgl_wsys_x11_silence_the_fucking_empty_file_warnings;
615 #endif /* USE_WSYS_MODULE_X11 */