]> icculus.org git repositories - dana/openbox.git/blob - loco/loco.c
destroy the glxpixmap when it is gone
[dana/openbox.git] / loco / loco.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    loco.c for the Openbox window manager
4    Copyright (c) 2008        Derek Foreman
5    Copyright (c) 2008        Dana Jansens
6
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.
11
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.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include <stdio.h>
21 #include "obt/mainloop.h"
22 #include "obt/display.h"
23 #include "obt/mainloop.h"
24 #include "obt/prop.h"
25 #include <glib.h>
26 #include <GL/glx.h>
27 #include <GL/glext.h>
28 #include <GL/glxext.h>
29 #include <GL/glxtokens.h>
30 /*
31 <dana> you want CreateNotify, DestroyNotify, MapNotify, UnmapNotify, and
32           ConfigureNotify
33           <dana> and then xdamage or whatever
34
35 */
36
37 #define glError()                                      \
38 {                                                      \
39     /*const GLchar *err_file = strrchr(err_path, '/');*/ \
40     GLenum        gl_error = glGetError();             \
41                                                        \
42     /*++err_file;*/                                        \
43                                                        \
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)*/ \
48 }
49
50
51 #define MAX_DEPTH 32
52
53 typedef struct {
54     Window id;
55     gint x, y, w, h;
56     gint depth;
57     gboolean input_only;
58     gboolean visible;
59     gint type; /* XXX make this an enum */
60     GLuint texname;
61     GLXPixmap glpixmap;
62 } LocoWindow;
63
64 typedef struct _LocoList {
65     struct _LocoList *next;
66     struct _LocoList *prev;
67     LocoWindow *window;
68 } LocoList;
69
70 static Window      loco_root;
71 static Window      loco_overlay;
72 /* Maps X Window ID -> LocoWindow* */
73 static GHashTable *window_map;
74 /* Maps X Window ID -> LocoList* which is in the stacking_top/bottom list */
75 static GHashTable *stacking_map;
76 /* From top to bottom */
77 static LocoList   *stacking_top;
78 static LocoList   *stacking_bottom;
79 static VisualID visualIDs[MAX_DEPTH + 1];
80 static XVisualInfo *glxPixmapVisuals[MAX_DEPTH + 1];
81 static int loco_screen_num;
82
83 void (*BindTexImageEXT)(Display *, GLXDrawable, int, const int *);
84 void (*ReleaseTexImageEXT)(Display *, GLXDrawable, int);
85
86 /*
87 static void print_stacking()
88 {
89     LocoList *it;
90
91     for (it = stacking_top; it; it = it->next) {
92         printf("0x%lx\n", it->window->id);
93     }
94 }
95 */
96
97 int create_glxpixmap(LocoWindow *lw)
98 {
99     XVisualInfo  *visinfo;
100     Pixmap pixmap;
101     static const int attrs[] =
102         {
103             GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
104             None
105         };
106
107     static const int drawable_tfp_attrs[] =
108         {
109             GLX_CONFIG_CAVEAT, GLX_NONE,
110             GLX_DOUBLEBUFFER, False,
111             GLX_DEPTH_SIZE, 0,
112             GLX_RED_SIZE, 1,
113             GLX_GREEN_SIZE, 1,
114             GLX_BLUE_SIZE, 1,
115             GLX_ALPHA_SIZE, 1,
116             GLX_RENDER_TYPE, GLX_RGBA_BIT,
117             GLX_BIND_TO_TEXTURE_RGBA_EXT, True, // additional for tfp
118             None
119         };
120     int count;
121
122     pixmap = XCompositeNameWindowPixmap(obt_display, lw->id);
123     visinfo = glxPixmapVisuals[lw->depth];
124     if (!visinfo) {
125         printf("no visinfo for depth %d\n", lw->depth);
126         return 0;
127     }
128
129     GLXFBConfig fbc;
130     GLXFBConfig *fbconfigs = glXChooseFBConfig(obt_display,
131                                                loco_screen_num,
132                                                drawable_tfp_attrs, &count);
133
134     int i;
135     fbc = 0;
136     for(i = 0; i < count; ++i ) {
137         int value;
138         glXGetFBConfigAttrib(obt_display,
139                              fbconfigs[i], GLX_VISUAL_ID, &value);
140         if(value == (int)visinfo->visualid)
141         {
142             fbc = fbconfigs[i];
143             XFree(fbconfigs);
144         }
145     }
146
147     if (!fbc) {
148         printf("fbconfigless\n");
149         return 0;
150     }
151
152     lw->glpixmap = glXCreatePixmap(obt_display, fbc, pixmap, attrs);
153 }
154
155 int bindPixmapToTexture(LocoWindow *lw)
156 {
157     unsigned int target;
158     int success = 0;
159
160     if (lw->glpixmap == None) {
161         create_glxpixmap(lw);
162     }
163     if (lw->glpixmap == None)
164         return 0;
165 /*
166     if (screen->queryDrawable (screen->display->display,
167                                texture->pixmap,
168                                GLX_TEXTURE_TARGET_EXT,
169                                &target))
170     {
171         fprintf (stderr, "%s: glXQueryDrawable failed\n", programName);
172
173         glXDestroyGLXPixmap (screen->display->display, texture->pixmap);
174         texture->pixmap = None;
175
176         return FALSE;
177     }
178 */
179
180     glBindTexture(GL_TEXTURE_2D, lw->texname);
181 glError();
182     BindTexImageEXT(obt_display, lw->glpixmap, GLX_FRONT_LEFT_EXT, NULL);
183
184     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
185     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
186
187     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
188     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
189     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
190     return 1;
191 }
192
193 void destroy_glxpixmap(LocoWindow *lw)
194 {
195     glXDestroyGLXPixmap(obt_display, lw->glpixmap);
196     lw->glpixmap = None;
197 }
198
199
200 void releasePixmapFromTexture (LocoWindow *lw)
201 {
202     if (lw->glpixmap) {
203         glEnable(GL_TEXTURE_2D);
204         glBindTexture(GL_TEXTURE_2D, lw->texname);
205
206         ReleaseTexImageEXT(obt_display, lw->glpixmap, GLX_FRONT_LEFT_EXT);
207
208         glBindTexture(GL_TEXTURE_2D, 0);
209         glDisable(GL_TEXTURE_2D);
210     }
211 }
212
213 static void full_composite(void)
214 {
215     int ret;
216     LocoList *it;
217     glClear(GL_COLOR_BUFFER_BIT);
218     for (it = stacking_bottom; it != stacking_top; it = it->prev) {
219         if (!it->window->visible)
220             continue;
221         ret = bindPixmapToTexture(it->window);
222         glBegin(GL_QUADS);
223         glColor3f(1.0, 1.0, 1.0);
224         glVertex2i(it->window->x, it->window->y);
225         glTexCoord2f(1, 0);
226         glVertex2i(it->window->x + it->window->w, it->window->y);
227         glTexCoord2f(1, 1);
228         glVertex2i(it->window->x + it->window->w, it->window->y + it->window->h);
229         glTexCoord2f(0, 1);
230         glVertex2i(it->window->x, it->window->y + it->window->h);
231         glTexCoord2f(0, 0);
232         glEnd();
233     }
234     glXSwapBuffers(obt_display, loco_overlay);
235 }
236
237 static LocoList* loco_list_prepend(LocoList **top, LocoList **bottom,
238                                    LocoWindow *window)
239 {
240     LocoList *n = g_new(LocoList, 1);
241     n->window = window;
242
243     n->prev = NULL;
244     n->next = *top;
245     if (n->next) n->next->prev = n;
246
247     *top = n;
248     if (!*bottom) *bottom = n;
249     return n;
250 }
251
252 static void loco_list_delete_link(LocoList **top, LocoList **bottom,
253                                   LocoList *pos)
254 {
255     LocoList *prev = pos->prev;
256     LocoList *next = pos->next;
257
258     if (next)
259         next->prev = prev;
260     if (prev)
261         prev->next = next;
262     if (!next)
263         *bottom = prev;
264     if (!prev)
265         *top = next;
266
267     g_free(pos);
268 }
269
270 static void loco_list_move_before(LocoList **top, LocoList **bottom,
271                                   LocoList *move, LocoList *before)
272 {
273     LocoList *prev, *next;
274
275     /* these won't move it anywhere */
276     if (move == before || move->next == before) return;
277
278     prev = move->prev;
279     next = move->next;
280
281     /* remove it from the list */
282     if (next) next->prev = prev;
283     else      *bottom = prev;
284     if (prev) prev->next = next;
285     else      *top = next;
286
287     /* reinsert it */
288     if (before) {
289         move->next = before;
290         move->prev = before->prev;
291         move->next->prev = move;
292         if (move->prev) move->prev->next = move;
293     }
294     else {
295         /* after the bottom */
296         move->prev = *bottom;
297         move->next = NULL;
298         if (move->prev) move->prev->next = move;
299         *bottom = move;
300     }
301
302     if (!move->prev) *top = move;
303 }
304
305 /* Returns a LocoWindow structure */
306 static LocoWindow* find_window(Window window)
307 {
308     return g_hash_table_lookup(window_map, &window);
309 }
310
311 /* Returns a node from the stacking_top/bottom list */
312 static LocoList* find_stacking(Window window)
313 {
314     return g_hash_table_lookup(stacking_map, &window);
315 }
316
317 void composite_setup_window(LocoWindow *win)
318 {
319     if (win->input_only) return;
320
321     XCompositeRedirectWindow(obt_display, win->id, CompositeRedirectAutomatic);
322     /*something useful = */XDamageCreate(obt_display, win->id, XDamageReportRawRectangles);
323 }
324
325 static void add_window(Window window)
326 {
327     LocoWindow *lw;
328     LocoList *it;
329     XWindowAttributes attrib;
330
331     printf("add window 0x%lx\n", window);
332
333     if (!XGetWindowAttributes(obt_display, window, &attrib))
334         return;
335
336     lw = g_new0(LocoWindow, 1);
337     lw->id = window;
338     lw->input_only = attrib.class == InputOnly;
339     lw->x = attrib.x;
340     lw->y = attrib.y;
341     lw->w = attrib.width;
342     lw->h = attrib.height;
343     lw->depth = attrib.depth;
344     lw->glpixmap = None;
345     glGenTextures(1, &lw->texname);
346   //  glTexImage2D(TARGET, 0, GL_RGB, lw->w, lw->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
347     g_hash_table_insert(window_map, &lw->id, lw);
348     /* new windows are at the top */
349     it = loco_list_prepend(&stacking_top, &stacking_bottom, lw);
350     g_hash_table_insert(stacking_map, &lw->id, it);
351
352     composite_setup_window(lw);
353
354     //print_stacking();
355 }
356
357 static void remove_window(LocoWindow *lw)
358 {
359     printf("remove window 0x%lx\n", lw->id);
360
361     LocoList *pos = find_stacking(lw->id);
362     g_assert(pos);
363
364     loco_list_delete_link(&stacking_top, &stacking_bottom, pos);
365     g_hash_table_remove(stacking_map, &lw->id);
366     g_hash_table_remove(window_map, &lw->id);
367
368     g_free(lw);
369
370     //print_stacking();
371 }
372
373 static void show_window(LocoWindow *lw)
374 {
375     guint32 *type;
376     guint i, ntype;
377
378     lw->visible = TRUE;
379
380     /* get the window's semantic type (for different effects!) */
381     lw->type = 0; /* XXX set this to the default type */
382     if (OBT_PROP_GETA32(lw->id, NET_WM_WINDOW_TYPE, ATOM, &type, &ntype)) {
383         /* use the first value that we know about in the array */
384         for (i = 0; i < ntype; ++i) {
385             /* XXX SET THESE TO AN ENUM'S VALUES */
386             if (type[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP))
387                 lw->type = 1;
388             if (type[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU))
389                 lw->type = 2;
390             /* XXX there are more TYPES that need to be added to prop.h */
391         }
392         g_free(type);
393     }
394
395 }
396
397 static void hide_window(LocoWindow *lw, gboolean destroyed)
398 {
399     /* if destroyed, then the window is no longer available */
400     lw->visible = FALSE;
401     destroy_glxpixmap(lw);
402 }
403
404 void COMPOSTER_RAWR(const XEvent *e, gpointer data)
405 {
406     int redraw_required = 0;
407
408     if (e->type == ConfigureNotify) {
409         LocoWindow *lw;
410 redraw_required = 1;
411         printf("Window 0x%lx moved or something\n", e->xconfigure.window);
412
413         lw = find_window(e->xconfigure.window);
414         if (lw) {
415             LocoList *above, *pos;
416
417             pos = find_stacking(e->xconfigure.window);
418             above = find_stacking(e->xconfigure.above);
419
420             g_assert(pos != NULL && pos->window != NULL);
421             if (e->xconfigure.above && !above)
422                 printf("missing windows from the stacking list!!\n");
423
424             lw->x = e->xconfigure.x;
425             lw->y = e->xconfigure.y;
426             if ((lw->w != e->xconfigure.width) || (lw->h != e->xconfigure.height)) {
427                 lw->w = e->xconfigure.width;
428                 lw->h = e->xconfigure.height;
429                 if (lw->glpixmap != None) {
430                     destroy_glxpixmap(lw);
431                 }
432             }
433             printf("Window 0x%lx above 0x%lx\n", pos->window->id,
434                    above ? above->window->id : 0);
435             loco_list_move_before(&stacking_top, &stacking_bottom, pos, above);
436         }
437         //print_stacking();
438     }
439     else if (e->type == CreateNotify) {
440         add_window(e->xmap.window);
441     }
442     else if (e->type == DestroyNotify) {
443         LocoWindow *lw = find_window(e->xdestroywindow.window);
444         if (lw) {
445             hide_window(lw, TRUE);
446             if (lw->glpixmap != None) {
447                 destroy_glxpixmap(lw);
448             }
449             remove_window(lw);
450         }
451         else
452             printf("destroy notify for unknown window 0x%lx\n",
453                    e->xdestroywindow.window);
454     }
455     else if (e->type == ReparentNotify) {
456         if (e->xreparent.parent == loco_root)
457             add_window(e->xreparent.window);
458         else {
459             LocoWindow *lw = find_window(e->xreparent.window);
460             if (lw) {
461                 hide_window(lw, FALSE);
462                 remove_window(lw);
463             }
464             else
465                 printf("reparent notify away from root for unknown window "
466                        "0x%lx\n", e->xreparent.window);
467         }
468     }
469     else if (e->type == MapNotify) {
470         LocoWindow *lw = find_window(e->xmap.window);
471         if (lw) show_window(lw);
472         else
473             printf("map notify for unknown window 0x%lx\n",
474                    e->xmap.window);
475     }
476     else if (e->type == UnmapNotify) {
477         LocoWindow *lw = find_window(e->xunmap.window);
478         if (lw) hide_window(lw, FALSE);
479         else
480             printf("unmap notify for unknown window 0x%lx\n",
481                    e->xunmap.window);
482     }
483     else if (e->type == obt_display_extension_damage_basep + XDamageNotify) {
484 //        LocoWindow *lw = find_window(e->xdamage.window);
485   //      if (lw->visible)
486             redraw_required = 1;
487     }
488     if (redraw_required)
489         full_composite();
490 }
491
492 static void find_all_windows(gint screen)
493 {
494     guint i, nchild;
495     Window w, *children;
496
497     if (!XQueryTree(obt_display, loco_root, &w, &w, &children, &nchild))
498         nchild = 0;
499
500     for (i = 0; i < nchild; ++i)
501         if (children[i] != None) add_window(children[i]);
502
503     if (children) XFree(children);
504 }
505
506 static guint window_hash(Window *w) { return *w; }
507 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
508
509 void loco_set_mainloop(gint screen_num, ObtMainLoop *loop)
510 {
511     int db, stencil, depth;
512     int i, j, value, count;
513     int w, h;
514     XVisualInfo *vi, tvis, *visinfo;
515     GLXContext cont;
516     int config[] =
517         { GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, GLX_RGBA, None };
518
519     loco_screen_num = screen_num;
520     loco_root = obt_root(screen_num);
521     loco_overlay = XCompositeGetOverlayWindow(obt_display, loco_root);
522 XserverRegion region = XFixesCreateRegion(obt_display, NULL, 0);
523
524     XFixesSetWindowShapeRegion (obt_display,
525                                 loco_overlay,
526                                 ShapeBounding,
527                                 0, 0,
528                                 0);
529     XFixesSetWindowShapeRegion (obt_display,
530                                 loco_overlay,
531                                 ShapeInput,
532                                 0, 0,
533                                 region);
534
535     XFixesDestroyRegion (obt_display, region);
536
537     vi = glXChooseVisual(obt_display, screen_num, config);
538     cont = glXCreateContext(obt_display, vi, NULL, GL_TRUE);
539     if (cont == NULL)
540         printf("context creation failed\n");
541     glXMakeCurrent(obt_display, loco_overlay, cont);
542
543     BindTexImageEXT = glXGetProcAddress("glXBindTexImageEXT");
544     ReleaseTexImageEXT = glXGetProcAddress("glXReleaseTexImageEXT");
545
546     w = WidthOfScreen(ScreenOfDisplay(obt_display, screen_num));
547     h = HeightOfScreen(ScreenOfDisplay(obt_display, screen_num));
548     glViewport(0, 0, w, h);
549     glMatrixMode(GL_PROJECTION);
550     glLoadIdentity();
551 printf("Setting up an orthographic projection of %dx%d\n", w, h);
552     glOrtho(0, w, h, 0.0, -1.0, 100.0);
553     glMatrixMode(GL_MODELVIEW);
554     glLoadIdentity();
555     glClear(GL_COLOR_BUFFER_BIT);
556     glEnable(GL_TEXTURE_2D);
557 glError();
558     glXSwapBuffers(obt_display, loco_overlay);
559     glClearColor(1.0, 0.0, 0.0, 1.0);
560 //    glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
561 //    glEnable(GL_BLEND);
562     db = 32767;
563     stencil = 32767;
564     depth = 32767;
565     for (i = 0; i <= MAX_DEPTH; i++) {
566         tvis.depth = i;
567         visualIDs[i] = 0;
568         visinfo = XGetVisualInfo(obt_display, VisualDepthMask, &tvis, &count);
569         for (j = 0; j < count; j++) {
570             glXGetConfig(obt_display, &visinfo[j], GLX_USE_GL, &value);
571             if (!value)
572                 continue;
573
574             glXGetConfig(obt_display, &visinfo[j], GLX_DOUBLEBUFFER, &value);
575             if (value > db)
576                 continue;
577             db = value;
578
579             glXGetConfig(obt_display, &visinfo[j], GLX_STENCIL_SIZE, &value);
580             if (value > stencil)
581                 continue;
582             stencil = value;
583
584             glXGetConfig(obt_display, &visinfo[j], GLX_DEPTH_SIZE, &value);
585             if (value > depth)
586                 continue;
587             depth = value;
588
589             visualIDs[i] = visinfo[j].visualid;
590         }
591     }
592
593     for (i = 0 ;i <= MAX_DEPTH; i++) {
594         tvis.visualid = visualIDs[i];
595         glxPixmapVisuals[i] = XGetVisualInfo(obt_display, VisualIDMask, &tvis, &count);
596         if (glxPixmapVisuals[i])
597             printf("supporting depth %d\n", i);
598     }
599     obt_main_loop_x_add(loop, COMPOSTER_RAWR, NULL, NULL);
600     window_map = g_hash_table_new((GHashFunc)window_hash,
601                                   (GEqualFunc)window_comp);
602     stacking_map = g_hash_table_new((GHashFunc)window_hash,
603                                     (GEqualFunc)window_comp);
604     stacking_top = stacking_bottom = NULL;
605
606     find_all_windows(screen_num);
607 }
608
609 void loco_shutdown(void)
610 {
611 }