1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 loco.c for the Openbox window manager
4 Copyright (c) 2008 Derek Foreman
5 Copyright (c) 2008 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
21 #include "obt/mainloop.h"
22 #include "obt/display.h"
23 #include "obt/mainloop.h"
28 #include <GL/glxext.h>
29 #include <GL/glxtokens.h>
31 <dana> you want CreateNotify, DestroyNotify, MapNotify, UnmapNotify, and
33 <dana> and then xdamage or whatever
39 /*const GLchar *err_file = strrchr(err_path, '/');*/ \
40 GLenum gl_error = glGetError(); \
44 for (; (gl_error); gl_error = glGetError()) \
45 fprintf(stderr, "%s: %d caught at line %u\n", \
46 __FUNCTION__, gl_error, __LINE__); \
47 /*(const GLchar*)gluErrorString(gl_error)*/ \
52 #define REFRESH_RATE (G_USEC_PER_SEC/60)
60 gint type; /* XXX make this an enum */
67 typedef struct _LocoList {
68 struct _LocoList *next;
69 struct _LocoList *prev;
73 static Window loco_root;
74 static Window loco_overlay;
75 /* Maps X Window ID -> LocoWindow* */
76 static GHashTable *window_map;
77 /* Maps X Window ID -> LocoList* which is in the stacking_top/bottom list */
78 static GHashTable *stacking_map;
79 /* From top to bottom */
80 static LocoList *stacking_top;
81 static LocoList *stacking_bottom;
82 static VisualID visualIDs[MAX_DEPTH + 1];
83 static XVisualInfo *glxPixmapVisuals[MAX_DEPTH + 1];
84 static int loco_screen_num;
85 static gboolean full_redraw_required;
86 static gboolean redraw_required;
88 typedef void (*BindEXTFunc)(Display *, GLXDrawable, int, const int *);
89 typedef void (*ReleaseEXTFunc)(Display *, GLXDrawable, int);
91 BindEXTFunc BindTexImageEXT;
92 ReleaseEXTFunc ReleaseTexImageEXT;
94 static void show_window(LocoWindow *lw);
95 static void remove_window(LocoWindow *lw);
98 static void print_stacking()
102 for (it = stacking_top; it; it = it->next) {
103 printf("0x%lx\n", it->window->id);
108 static glong timecompare(GTimeVal *a, GTimeVal *b)
111 if ((r = a->tv_sec - b->tv_sec)) return r;
112 return a->tv_usec - b->tv_usec;
115 static void timeadd(GTimeVal *t, glong microseconds)
117 t->tv_usec += microseconds;
118 while (t->tv_usec >= G_USEC_PER_SEC) {
119 t->tv_usec -= G_USEC_PER_SEC;
122 while (t->tv_usec < 0) {
123 t->tv_usec += G_USEC_PER_SEC;
128 int create_glxpixmap(LocoWindow *lw)
130 XVisualInfo *visinfo;
132 static const int attrs[] =
134 GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
138 static const int drawable_tfp_attrs[] =
140 GLX_CONFIG_CAVEAT, GLX_NONE,
141 GLX_DOUBLEBUFFER, False,
147 GLX_RENDER_TYPE, GLX_RGBA_BIT,
148 GLX_BIND_TO_TEXTURE_RGBA_EXT, True, // additional for tfp
153 pixmap = XCompositeNameWindowPixmap(obt_display, lw->id);
154 visinfo = glxPixmapVisuals[lw->depth];
156 printf("no visinfo for depth %d\n", lw->depth);
161 GLXFBConfig *fbconfigs = glXChooseFBConfig(obt_display,
163 drawable_tfp_attrs, &count);
167 for(i = 0; i < count; ++i ) {
169 glXGetFBConfigAttrib(obt_display,
170 fbconfigs[i], GLX_VISUAL_ID, &value);
171 if(value == (int)visinfo->visualid)
179 printf("fbconfigless\n");
183 lw->glpixmap = glXCreatePixmap(obt_display, fbc, pixmap, attrs);
184 return !!lw->glpixmap;
187 int bindPixmapToTexture(LocoWindow *lw)
189 if (lw->glpixmap != None)
190 return 1; /* already bound */
192 if (!create_glxpixmap(lw))
193 return 0; /* horrible failure */
196 if (screen->queryDrawable (screen->display->display,
198 GLX_TEXTURE_TARGET_EXT,
201 fprintf (stderr, "%s: glXQueryDrawable failed\n", programName);
203 glXDestroyGLXPixmap (screen->display->display, texture->pixmap);
204 texture->pixmap = None;
210 glBindTexture(GL_TEXTURE_2D, lw->texname);
212 BindTexImageEXT(obt_display, lw->glpixmap, GLX_FRONT_LEFT_EXT, NULL);
214 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
215 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
218 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
219 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
223 void releasePixmapFromTexture(LocoWindow *lw)
226 glBindTexture(GL_TEXTURE_2D, lw->texname);
228 ReleaseTexImageEXT(obt_display, lw->glpixmap, GLX_FRONT_LEFT_EXT);
230 glBindTexture(GL_TEXTURE_2D, 0);
234 void destroy_glxpixmap(LocoWindow *lw)
237 releasePixmapFromTexture(lw);
239 glXDestroyGLXPixmap(obt_display, lw->glpixmap);
244 static void full_composite(void)
249 /* XXX if (full_redraw_required) */
250 glClear(GL_COLOR_BUFFER_BIT);
252 for (it = stacking_bottom; it != stacking_top; it = it->prev) {
253 if (it->window->input_only) continue;
254 if (!it->window->visible) continue;
255 /* XXX if (!full_redraw_required && !it->window->damaged) continue; */
257 if (!full_redraw_required) {
258 /* XXX if the window is transparent, then clear the background
262 glBindTexture(GL_TEXTURE_2D, it->window->texname);
263 ret = bindPixmapToTexture(it->window);
265 glColor3f(1.0, 1.0, 1.0);
266 glVertex2i(it->window->x, it->window->y);
268 glVertex2i(it->window->x + it->window->w, it->window->y);
270 glVertex2i(it->window->x + it->window->w,
271 it->window->y + it->window->h);
273 glVertex2i(it->window->x, it->window->y + it->window->h);
277 it->window->damaged = FALSE;
279 glXSwapBuffers(obt_display, loco_overlay);
281 full_redraw_required = redraw_required = FALSE;
284 static LocoList* loco_list_prepend(LocoList **top, LocoList **bottom,
287 LocoList *n = g_new(LocoList, 1);
292 if (n->next) n->next->prev = n;
295 if (!*bottom) *bottom = n;
299 static void loco_list_delete_link(LocoList **top, LocoList **bottom,
302 LocoList *prev = pos->prev;
303 LocoList *next = pos->next;
317 static void loco_list_move_before(LocoList **top, LocoList **bottom,
318 LocoList *move, LocoList *before)
320 LocoList *prev, *next;
322 /* these won't move it anywhere */
323 if (move == before || move->next == before) return;
328 /* remove it from the list */
329 if (next) next->prev = prev;
331 if (prev) prev->next = next;
337 move->prev = before->prev;
338 move->next->prev = move;
339 if (move->prev) move->prev->next = move;
342 /* after the bottom */
343 move->prev = *bottom;
345 if (move->prev) move->prev->next = move;
349 if (!move->prev) *top = move;
352 /* Returns a LocoWindow structure */
353 static LocoWindow* find_window(Window window)
355 return g_hash_table_lookup(window_map, &window);
358 /* Returns a node from the stacking_top/bottom list */
359 static LocoList* find_stacking(Window window)
361 return g_hash_table_lookup(stacking_map, &window);
364 static void add_window(Window window)
368 XWindowAttributes attrib;
370 printf("add window 0x%lx\n", window);
372 if (!XGetWindowAttributes(obt_display, window, &attrib))
375 lw = g_new0(LocoWindow, 1);
377 lw->input_only = attrib.class == InputOnly;
380 lw->w = attrib.width;
381 lw->h = attrib.height;
382 lw->depth = attrib.depth;
384 g_hash_table_insert(window_map, &lw->id, lw);
385 /* new windows are at the top */
386 it = loco_list_prepend(&stacking_top, &stacking_bottom, lw);
387 g_hash_table_insert(stacking_map, &lw->id, it);
389 if (!lw->input_only) {
390 glGenTextures(1, &lw->texname);
391 // glTexImage2D(TARGET, 0, GL_RGB, lw->w, lw->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
392 lw->damage = XDamageCreate(obt_display, window, XDamageReportNonEmpty);
397 if (attrib.map_state != IsUnmapped)
401 static void remove_window(LocoWindow *lw)
403 printf("remove window 0x%lx\n", lw->id);
405 LocoList *pos = find_stacking(lw->id);
408 if (!lw->input_only) {
409 glDeleteTextures(1, &lw->texname);
410 obt_display_ignore_errors(TRUE);
411 XDamageDestroy(obt_display, lw->damage);
412 obt_display_ignore_errors(FALSE);
415 loco_list_delete_link(&stacking_top, &stacking_bottom, pos);
416 g_hash_table_remove(stacking_map, &lw->id);
417 g_hash_table_remove(window_map, &lw->id);
424 static void show_window(LocoWindow *lw)
431 /* get the window's semantic type (for different effects!) */
432 lw->type = 0; /* XXX set this to the default type */
433 if (OBT_PROP_GETA32(lw->id, NET_WM_WINDOW_TYPE, ATOM, &type, &ntype)) {
434 /* use the first value that we know about in the array */
435 for (i = 0; i < ntype; ++i) {
436 /* XXX SET THESE TO AN ENUM'S VALUES */
437 if (type[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP))
439 if (type[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU))
441 /* XXX there are more TYPES that need to be added to prop.h */
446 full_redraw_required = redraw_required = TRUE;
449 static void hide_window(LocoWindow *lw, gboolean destroyed)
451 /* if destroyed, then the window is no longer available */
453 destroy_glxpixmap(lw);
455 full_redraw_required = redraw_required = TRUE;
458 static void configure_window(LocoWindow *lw, const XConfigureEvent *e)
460 LocoList *above, *pos;
462 pos = find_stacking(e->window);
463 above = find_stacking(e->above);
465 g_assert(pos != NULL && pos->window != NULL);
466 if (e->above && !above)
467 printf("missing windows from the stacking list!!\n");
469 if ((lw->x != e->x) || (lw->y != e->y)) {
473 full_redraw_required = redraw_required = TRUE;
476 if ((lw->w != e->width) || (lw->h != e->height)) {
480 destroy_glxpixmap(lw);
482 full_redraw_required = redraw_required = TRUE;
485 if (pos->next != above) {
486 printf("Window 0x%lx above 0x%lx\n", pos->window->id,
487 above ? above->window->id : 0);
488 loco_list_move_before(&stacking_top, &stacking_bottom, pos, above);
490 full_redraw_required = redraw_required = TRUE;
494 static void damage_window(LocoWindow *lw)
498 pos = find_stacking(lw->id);
500 /* XXX if it is transparent, then damage any windows below it that
503 /* damage any windows above it that intersect */
504 for (it = pos; it; it = it->prev) {
505 const LocoWindow *lwa = it->window;
506 if (lwa->x < lw->x + lw->w && lwa->x + lwa->w > lw->x &&
507 lwa->y < lw->y + lw->h && lwa->y + lwa->h > lw->y)
509 it->window->damaged = TRUE;
514 redraw_required = TRUE;
517 void COMPOSTER_RAWR(const XEvent *e, gpointer data)
519 //g_print("COMPOSTER_RAWR() %d\n", e->type);
521 if (e->type == ConfigureNotify) {
523 printf("Window 0x%lx moved or something\n", e->xconfigure.window);
525 lw = find_window(e->xconfigure.window);
527 configure_window(lw, &e->xconfigure);
530 else if (e->type == CreateNotify) {
531 add_window(e->xmap.window);
533 else if (e->type == DestroyNotify) {
534 LocoWindow *lw = find_window(e->xdestroywindow.window);
536 hide_window(lw, TRUE);
540 printf("destroy notify for unknown window 0x%lx\n",
541 e->xdestroywindow.window);
543 else if (e->type == ReparentNotify) {
544 if (e->xreparent.parent == loco_root)
545 add_window(e->xreparent.window);
547 LocoWindow *lw = find_window(e->xreparent.window);
549 printf("window 0x%lx reparented from root\n", lw->id);
550 hide_window(lw, FALSE);
554 printf("reparent notify away from root for unknown window "
555 "0x%lx\n", e->xreparent.window);
559 else if (e->type == MapNotify) {
560 LocoWindow *lw = find_window(e->xmap.window);
564 printf("map notify for unknown window 0x%lx\n",
567 else if (e->type == UnmapNotify) {
568 LocoWindow *lw = find_window(e->xunmap.window);
570 hide_window(lw, FALSE);
572 printf("unmap notify for unknown window 0x%lx\n",
575 else if (e->type == obt_display_extension_damage_basep + XDamageNotify) {
576 const XDamageNotifyEvent *de;
579 de = (const XDamageNotifyEvent*)e;
580 lw = find_window(de->drawable);
583 /* mark the damage as fixed - we know about it now */
584 XDamageSubtract(obt_display, lw->damage, None, None);
586 else if (de->drawable == loco_root) {
587 XDamageSubtract(obt_display, de->damage, None, None);
588 full_redraw_required = redraw_required = TRUE;
593 static void find_all_windows(gint screen)
598 if (!XQueryTree(obt_display, loco_root, &w, &w, &children, &nchild))
601 for (i = 0; i < nchild; ++i)
602 if (children[i] != None) add_window(children[i]);
604 if (children) XFree(children);
607 static gboolean compositor_timeout(gpointer data)
611 return TRUE; /* repeat */
614 static guint window_hash(Window *w) { return *w; }
615 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
617 void loco_set_mainloop(gint screen_num, ObtMainLoop *loop)
619 int db, stencil, depth;
620 int i, j, value, count;
622 XVisualInfo *vi, tvis, *visinfo;
625 { GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, GLX_RGBA, None };
627 loco_screen_num = screen_num;
628 loco_root = obt_root(screen_num);
629 loco_overlay = XCompositeGetOverlayWindow(obt_display, loco_root);
630 XserverRegion region = XFixesCreateRegion(obt_display, NULL, 0);
632 XFixesSetWindowShapeRegion (obt_display,
637 XFixesSetWindowShapeRegion (obt_display,
643 XFixesDestroyRegion (obt_display, region);
645 vi = glXChooseVisual(obt_display, screen_num, config);
646 cont = glXCreateContext(obt_display, vi, NULL, GL_TRUE);
648 printf("context creation failed\n");
649 glXMakeCurrent(obt_display, loco_overlay, cont);
651 BindTexImageEXT = (BindEXTFunc)
652 glXGetProcAddress((const guchar*)"glXBindTexImageEXT");
653 ReleaseTexImageEXT = (ReleaseEXTFunc)
654 glXGetProcAddress((const guchar*)"glXReleaseTexImageEXT");
656 w = WidthOfScreen(ScreenOfDisplay(obt_display, screen_num));
657 h = HeightOfScreen(ScreenOfDisplay(obt_display, screen_num));
658 glViewport(0, 0, w, h);
659 glMatrixMode(GL_PROJECTION);
661 printf("Setting up an orthographic projection of %dx%d\n", w, h);
662 glOrtho(0, w, h, 0.0, -1.0, 100.0);
663 glMatrixMode(GL_MODELVIEW);
665 glClear(GL_COLOR_BUFFER_BIT);
666 glEnable(GL_TEXTURE_2D);
668 glXSwapBuffers(obt_display, loco_overlay);
669 glClearColor(1.0, 0.0, 0.0, 1.0);
670 // glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
671 // glEnable(GL_BLEND);
675 for (i = 0; i <= MAX_DEPTH; i++) {
678 visinfo = XGetVisualInfo(obt_display, VisualDepthMask, &tvis, &count);
679 for (j = 0; j < count; j++) {
680 glXGetConfig(obt_display, &visinfo[j], GLX_USE_GL, &value);
684 glXGetConfig(obt_display, &visinfo[j], GLX_DOUBLEBUFFER, &value);
689 glXGetConfig(obt_display, &visinfo[j], GLX_STENCIL_SIZE, &value);
694 glXGetConfig(obt_display, &visinfo[j], GLX_DEPTH_SIZE, &value);
699 visualIDs[i] = visinfo[j].visualid;
703 for (i = 0 ;i <= MAX_DEPTH; i++) {
704 tvis.visualid = visualIDs[i];
705 glxPixmapVisuals[i] = XGetVisualInfo(obt_display, VisualIDMask, &tvis, &count);
706 if (glxPixmapVisuals[i])
707 printf("supporting depth %d\n", i);
709 obt_main_loop_x_add(loop, COMPOSTER_RAWR, NULL, NULL);
710 window_map = g_hash_table_new((GHashFunc)window_hash,
711 (GEqualFunc)window_comp);
712 stacking_map = g_hash_table_new((GHashFunc)window_hash,
713 (GEqualFunc)window_comp);
714 stacking_top = stacking_bottom = NULL;
716 XGrabServer(obt_display);
718 XCompositeRedirectSubwindows(obt_display, loco_root,
719 CompositeRedirectManual);
720 find_all_windows(screen_num);
722 XUngrabServer(obt_display);
724 full_redraw_required = redraw_required = TRUE;
726 obt_main_loop_timeout_add(loop, REFRESH_RATE,
727 (GSourceFunc)compositor_timeout,
731 void loco_shutdown(void)