]> icculus.org git repositories - dana/openbox.git/blob - loco/loco.c
I fergit
[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         glBindTexture(GL_TEXTURE_2D, lw->texname);
204
205         ReleaseTexImageEXT(obt_display, lw->glpixmap, GLX_FRONT_LEFT_EXT);
206
207         glBindTexture(GL_TEXTURE_2D, 0);
208     }
209 }
210
211 static void full_composite(void)
212 {
213     int ret;
214     LocoList *it;
215     glClear(GL_COLOR_BUFFER_BIT);
216     for (it = stacking_bottom; it != stacking_top; it = it->prev) {
217         if (!it->window->visible)
218             continue;
219         if ((it->window->w > 1000) || (it->window->h > 1000)) {
220             printf("I am afraid of window 0x%x because it is very large\n", it->window->id);
221             continue;
222         }
223         ret = bindPixmapToTexture(it->window);
224         glBegin(GL_QUADS);
225         glColor3f(1.0, 1.0, 1.0);
226         glVertex2i(it->window->x, it->window->y);
227         glTexCoord2f(1, 0);
228         glVertex2i(it->window->x + it->window->w, it->window->y);
229         glTexCoord2f(1, 1);
230         glVertex2i(it->window->x + it->window->w, it->window->y + it->window->h);
231         glTexCoord2f(0, 1);
232         glVertex2i(it->window->x, it->window->y + it->window->h);
233         glTexCoord2f(0, 0);
234         glEnd();
235 //      releasePixmapFromTexture(it->window);
236     }
237     glXSwapBuffers(obt_display, loco_overlay);
238 }
239
240 static LocoList* loco_list_prepend(LocoList **top, LocoList **bottom,
241                                    LocoWindow *window)
242 {
243     LocoList *n = g_new(LocoList, 1);
244     n->window = window;
245
246     n->prev = NULL;
247     n->next = *top;
248     if (n->next) n->next->prev = n;
249
250     *top = n;
251     if (!*bottom) *bottom = n;
252     return n;
253 }
254
255 static void loco_list_delete_link(LocoList **top, LocoList **bottom,
256                                   LocoList *pos)
257 {
258     LocoList *prev = pos->prev;
259     LocoList *next = pos->next;
260
261     if (next)
262         next->prev = prev;
263     if (prev)
264         prev->next = next;
265     if (!next)
266         *bottom = prev;
267     if (!prev)
268         *top = next;
269
270     g_free(pos);
271 }
272
273 static void loco_list_move_before(LocoList **top, LocoList **bottom,
274                                   LocoList *move, LocoList *before)
275 {
276     LocoList *prev, *next;
277
278     /* these won't move it anywhere */
279     if (move == before || move->next == before) return;
280
281     prev = move->prev;
282     next = move->next;
283
284     /* remove it from the list */
285     if (next) next->prev = prev;
286     else      *bottom = prev;
287     if (prev) prev->next = next;
288     else      *top = next;
289
290     /* reinsert it */
291     if (before) {
292         move->next = before;
293         move->prev = before->prev;
294         move->next->prev = move;
295         if (move->prev) move->prev->next = move;
296     }
297     else {
298         /* after the bottom */
299         move->prev = *bottom;
300         move->next = NULL;
301         if (move->prev) move->prev->next = move;
302         *bottom = move;
303     }
304
305     if (!move->prev) *top = move;
306 }
307
308 /* Returns a LocoWindow structure */
309 static LocoWindow* find_window(Window window)
310 {
311     return g_hash_table_lookup(window_map, &window);
312 }
313
314 /* Returns a node from the stacking_top/bottom list */
315 static LocoList* find_stacking(Window window)
316 {
317     return g_hash_table_lookup(stacking_map, &window);
318 }
319
320 void composite_setup_window(LocoWindow *win)
321 {
322     if (win->input_only) return;
323
324 //    XCompositeRedirectWindow(obt_display, win->id, CompositeRedirectManual);
325 //    XCompositeRedirectWindow(obt_display, win->id, CompositeRedirectManual);
326     /*something useful = */XDamageCreate(obt_display, win->id, XDamageReportRawRectangles);
327 }
328
329 static void add_window(Window window)
330 {
331     LocoWindow *lw;
332     LocoList *it;
333     XWindowAttributes attrib;
334
335     printf("add window 0x%lx\n", window);
336
337     if (!XGetWindowAttributes(obt_display, window, &attrib))
338         return;
339
340     lw = g_new0(LocoWindow, 1);
341     lw->id = window;
342     lw->input_only = attrib.class == InputOnly;
343     lw->x = attrib.x;
344     lw->y = attrib.y;
345     lw->w = attrib.width;
346     lw->h = attrib.height;
347     lw->depth = attrib.depth;
348     lw->glpixmap = None;
349     glGenTextures(1, &lw->texname);
350   //  glTexImage2D(TARGET, 0, GL_RGB, lw->w, lw->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
351     g_hash_table_insert(window_map, &lw->id, lw);
352     /* new windows are at the top */
353     it = loco_list_prepend(&stacking_top, &stacking_bottom, lw);
354     g_hash_table_insert(stacking_map, &lw->id, it);
355
356     composite_setup_window(lw);
357
358     //print_stacking();
359 }
360
361 static void remove_window(LocoWindow *lw)
362 {
363     printf("remove window 0x%lx\n", lw->id);
364
365     LocoList *pos = find_stacking(lw->id);
366     g_assert(pos);
367
368     loco_list_delete_link(&stacking_top, &stacking_bottom, pos);
369     g_hash_table_remove(stacking_map, &lw->id);
370     g_hash_table_remove(window_map, &lw->id);
371
372     g_free(lw);
373
374     //print_stacking();
375 }
376
377 static void show_window(LocoWindow *lw)
378 {
379     guint32 *type;
380     guint i, ntype;
381
382     lw->visible = TRUE;
383
384     /* get the window's semantic type (for different effects!) */
385     lw->type = 0; /* XXX set this to the default type */
386     if (OBT_PROP_GETA32(lw->id, NET_WM_WINDOW_TYPE, ATOM, &type, &ntype)) {
387         /* use the first value that we know about in the array */
388         for (i = 0; i < ntype; ++i) {
389             /* XXX SET THESE TO AN ENUM'S VALUES */
390             if (type[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP))
391                 lw->type = 1;
392             if (type[i] == OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU))
393                 lw->type = 2;
394             /* XXX there are more TYPES that need to be added to prop.h */
395         }
396         g_free(type);
397     }
398
399 }
400
401 static void hide_window(LocoWindow *lw, gboolean destroyed)
402 {
403     /* if destroyed, then the window is no longer available */
404     lw->visible = FALSE;
405     destroy_glxpixmap(lw);
406 }
407
408 void COMPOSTER_RAWR(const XEvent *e, gpointer data)
409 {
410     int redraw_required = 0;
411
412     if (e->type == ConfigureNotify) {
413         LocoWindow *lw;
414 redraw_required = 1;
415         printf("Window 0x%lx moved or something\n", e->xconfigure.window);
416
417         lw = find_window(e->xconfigure.window);
418         if (lw) {
419             LocoList *above, *pos;
420
421             pos = find_stacking(e->xconfigure.window);
422             above = find_stacking(e->xconfigure.above);
423
424             g_assert(pos != NULL && pos->window != NULL);
425             if (e->xconfigure.above && !above)
426                 printf("missing windows from the stacking list!!\n");
427
428             lw->x = e->xconfigure.x;
429             lw->y = e->xconfigure.y;
430             if ((lw->w != e->xconfigure.width) || (lw->h != e->xconfigure.height)) {
431                 lw->w = e->xconfigure.width;
432                 lw->h = e->xconfigure.height;
433                 if (lw->glpixmap != None) {
434                     destroy_glxpixmap(lw);
435                 }
436             }
437             printf("Window 0x%lx above 0x%lx\n", pos->window->id,
438                    above ? above->window->id : 0);
439             loco_list_move_before(&stacking_top, &stacking_bottom, pos, above);
440         }
441         //print_stacking();
442     }
443     else if (e->type == CreateNotify) {
444         add_window(e->xmap.window);
445     }
446     else if (e->type == DestroyNotify) {
447         LocoWindow *lw = find_window(e->xdestroywindow.window);
448         if (lw) {
449             hide_window(lw, TRUE);
450             if (lw->glpixmap != None) {
451                 destroy_glxpixmap(lw);
452             }
453             remove_window(lw);
454         }
455         else
456             printf("destroy notify for unknown window 0x%lx\n",
457                    e->xdestroywindow.window);
458     }
459     else if (e->type == ReparentNotify) {
460         if (e->xreparent.parent == loco_root)
461             add_window(e->xreparent.window);
462         else {
463             LocoWindow *lw = find_window(e->xreparent.window);
464             if (lw) {
465                 hide_window(lw, FALSE);
466                 remove_window(lw);
467             }
468             else
469                 printf("reparent notify away from root for unknown window "
470                        "0x%lx\n", e->xreparent.window);
471         }
472     }
473     else if (e->type == MapNotify) {
474         LocoWindow *lw = find_window(e->xmap.window);
475         if (lw) show_window(lw);
476         else
477             printf("map notify for unknown window 0x%lx\n",
478                    e->xmap.window);
479     }
480     else if (e->type == UnmapNotify) {
481         LocoWindow *lw = find_window(e->xunmap.window);
482         if (lw) hide_window(lw, FALSE);
483         else
484             printf("unmap notify for unknown window 0x%lx\n",
485                    e->xunmap.window);
486     }
487     else if (e->type == obt_display_extension_damage_basep + XDamageNotify) {
488 //        LocoWindow *lw = find_window(e->xdamage.window);
489   //      if (lw->visible)
490             redraw_required = 1;
491     }
492     if (redraw_required)
493         full_composite();
494 }
495
496 static void find_all_windows(gint screen)
497 {
498     guint i, nchild;
499     Window w, *children;
500
501     if (!XQueryTree(obt_display, loco_root, &w, &w, &children, &nchild))
502         nchild = 0;
503
504     for (i = 0; i < nchild; ++i)
505         if (children[i] != None) add_window(children[i]);
506
507     if (children) XFree(children);
508 }
509
510 static guint window_hash(Window *w) { return *w; }
511 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
512
513 void loco_set_mainloop(gint screen_num, ObtMainLoop *loop)
514 {
515     int db, stencil, depth;
516     int i, j, value, count;
517     int w, h;
518     XVisualInfo *vi, tvis, *visinfo;
519     GLXContext cont;
520     int config[] =
521         { GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, GLX_RGBA, None };
522
523     loco_screen_num = screen_num;
524     loco_root = obt_root(screen_num);
525     loco_overlay = XCompositeGetOverlayWindow(obt_display, loco_root);
526 XserverRegion region = XFixesCreateRegion(obt_display, NULL, 0);
527
528     XFixesSetWindowShapeRegion (obt_display,
529                                 loco_overlay,
530                                 ShapeBounding,
531                                 0, 0,
532                                 0);
533     XFixesSetWindowShapeRegion (obt_display,
534                                 loco_overlay,
535                                 ShapeInput,
536                                 0, 0,
537                                 region);
538
539     XFixesDestroyRegion (obt_display, region);
540
541     vi = glXChooseVisual(obt_display, screen_num, config);
542     cont = glXCreateContext(obt_display, vi, NULL, GL_TRUE);
543     if (cont == NULL)
544         printf("context creation failed\n");
545     glXMakeCurrent(obt_display, loco_overlay, cont);
546
547     BindTexImageEXT = glXGetProcAddress("glXBindTexImageEXT");
548     ReleaseTexImageEXT = glXGetProcAddress("glXReleaseTexImageEXT");
549
550     w = WidthOfScreen(ScreenOfDisplay(obt_display, screen_num));
551     h = HeightOfScreen(ScreenOfDisplay(obt_display, screen_num));
552     glViewport(0, 0, w, h);
553     glMatrixMode(GL_PROJECTION);
554     glLoadIdentity();
555 printf("Setting up an orthographic projection of %dx%d\n", w, h);
556     glOrtho(0, w, h, 0.0, -1.0, 100.0);
557     glMatrixMode(GL_MODELVIEW);
558     glLoadIdentity();
559     glClear(GL_COLOR_BUFFER_BIT);
560     glEnable(GL_TEXTURE_2D);
561 glError();
562     glXSwapBuffers(obt_display, loco_overlay);
563     glClearColor(1.0, 0.0, 0.0, 1.0);
564 //    glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
565 //    glEnable(GL_BLEND);
566     db = 32767;
567     stencil = 32767;
568     depth = 32767;
569     for (i = 0; i <= MAX_DEPTH; i++) {
570         tvis.depth = i;
571         visualIDs[i] = 0;
572         visinfo = XGetVisualInfo(obt_display, VisualDepthMask, &tvis, &count);
573         for (j = 0; j < count; j++) {
574             glXGetConfig(obt_display, &visinfo[j], GLX_USE_GL, &value);
575             if (!value)
576                 continue;
577
578             glXGetConfig(obt_display, &visinfo[j], GLX_DOUBLEBUFFER, &value);
579             if (value > db)
580                 continue;
581             db = value;
582
583             glXGetConfig(obt_display, &visinfo[j], GLX_STENCIL_SIZE, &value);
584             if (value > stencil)
585                 continue;
586             stencil = value;
587
588             glXGetConfig(obt_display, &visinfo[j], GLX_DEPTH_SIZE, &value);
589             if (value > depth)
590                 continue;
591             depth = value;
592
593             visualIDs[i] = visinfo[j].visualid;
594         }
595     }
596
597     for (i = 0 ;i <= MAX_DEPTH; i++) {
598         tvis.visualid = visualIDs[i];
599         glxPixmapVisuals[i] = XGetVisualInfo(obt_display, VisualIDMask, &tvis, &count);
600         if (glxPixmapVisuals[i])
601             printf("supporting depth %d\n", i);
602     }
603     obt_main_loop_x_add(loop, COMPOSTER_RAWR, NULL, NULL);
604     window_map = g_hash_table_new((GHashFunc)window_hash,
605                                   (GEqualFunc)window_comp);
606     stacking_map = g_hash_table_new((GHashFunc)window_hash,
607                                     (GEqualFunc)window_comp);
608     stacking_top = stacking_bottom = NULL;
609
610     XCompositeRedirectSubwindows(obt_display, loco_root, CompositeRedirectManual);
611     find_all_windows(screen_num);
612 }
613
614 void loco_shutdown(void)
615 {
616 }