]> icculus.org git repositories - dana/openbox.git/blob - openbox/window.c
Basic compositing with GL
[dana/openbox.git] / openbox / window.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    window.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "window.h"
20 #include "menuframe.h"
21 #include "config.h"
22 #include "dock.h"
23 #include "client.h"
24 #include "frame.h"
25 #include "openbox.h"
26 #include "prompt.h"
27 #include "debug.h"
28 #include "grab.h"
29 #include "obt/xqueue.h"
30
31 static GHashTable *window_map;
32
33 static guint window_hash(Window *w) { return *w; }
34 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
35
36 void window_startup(gboolean reconfig)
37 {
38     if (reconfig) return;
39
40     window_map = g_hash_table_new((GHashFunc)window_hash,
41                                   (GEqualFunc)window_comp);
42 }
43
44 void window_shutdown(gboolean reconfig)
45 {
46     if (reconfig) return;
47
48     g_hash_table_destroy(window_map);
49 }
50
51 Window window_top(ObWindow *self)
52 {
53     switch (self->type) {
54     case OB_WINDOW_CLASS_MENUFRAME:
55         return WINDOW_AS_MENUFRAME(self)->window;
56     case OB_WINDOW_CLASS_DOCK:
57         return WINDOW_AS_DOCK(self)->frame;
58     case OB_WINDOW_CLASS_CLIENT:
59         return WINDOW_AS_CLIENT(self)->frame->window;
60     case OB_WINDOW_CLASS_INTERNAL:
61         return WINDOW_AS_INTERNAL(self)->window;
62     case OB_WINDOW_CLASS_PROMPT:
63         return WINDOW_AS_PROMPT(self)->super.window;
64     }
65     g_assert_not_reached();
66     return None;
67 }
68
69 ObStackingLayer window_layer(ObWindow *self)
70 {
71     switch (self->type) {
72     case OB_WINDOW_CLASS_DOCK:
73         return config_dock_layer;
74     case OB_WINDOW_CLASS_CLIENT:
75         return ((ObClient*)self)->layer;
76     case OB_WINDOW_CLASS_MENUFRAME:
77     case OB_WINDOW_CLASS_INTERNAL:
78         return OB_STACKING_LAYER_INTERNAL;
79     case OB_WINDOW_CLASS_PROMPT:
80         /* not used directly for stacking, prompts are managed as clients */
81         g_assert_not_reached();
82         break;
83     }
84     g_assert_not_reached();
85     return None;
86 }
87
88 ObWindow* window_find(Window xwin)
89 {
90     return g_hash_table_lookup(window_map, &xwin);
91 }
92
93 void window_add(Window *xwin, ObWindow *win)
94 {
95     g_assert(xwin != NULL);
96     g_assert(win != NULL);
97 #ifdef USE_COMPOSITING
98     XWindowAttributes wattrib;
99     Status ret;
100
101     if (win->type != OB_WINDOW_CLASS_PROMPT) {
102         win->damage = XDamageCreate(obt_display, window_top(win), XDamageReportNonEmpty);
103
104         XCompositeRedirectWindow(obt_display, window_top(win), CompositeRedirectManual);
105
106         win->pixmap = None;
107         glGenTextures(1, &win->texture);
108         ret = XGetWindowAttributes(obt_display, window_top(win), &wattrib);
109         g_assert(ret != BadDrawable);
110         g_assert(ret != BadWindow);
111
112         win->depth = wattrib.depth;
113     }
114 #endif
115     g_hash_table_insert(window_map, xwin, win);
116 }
117
118 void window_remove(Window xwin)
119 {
120     g_assert(xwin != None);
121 #ifdef USE_COMPOSITING
122     ObWindow *win;
123     win = window_find(xwin);
124     if (!win) {
125         printf("Compositor tried to clean up a window, but it was not there.\n");
126         return;
127     }
128     if (win->type != OB_WINDOW_CLASS_PROMPT) {
129         if (win->damage)
130             XDamageDestroy(obt_display, win->damage);
131         if (win->gpixmap)
132             XFreePixmap(obt_display, win->gpixmap);
133         if (win->pixmap)
134             XFreePixmap(obt_display, win->pixmap);
135         if (win->texture)
136             glDeleteTextures(1, &win->texture);
137     }
138 #endif
139     g_hash_table_remove(window_map, &xwin);
140 }
141
142 void window_manage_all(void)
143 {
144     guint i, j, nchild;
145     Window w, *children;
146     XWMHints *wmhints;
147     XWindowAttributes attrib;
148
149     if (!XQueryTree(obt_display, RootWindow(obt_display, ob_screen),
150                     &w, &w, &children, &nchild)) {
151         ob_debug("XQueryTree failed in window_manage_all");
152         nchild = 0;
153     }
154
155     /* remove all icon windows from the list */
156     for (i = 0; i < nchild; i++) {
157         if (children[i] == None) continue;
158         wmhints = XGetWMHints(obt_display, children[i]);
159         if (wmhints) {
160             if ((wmhints->flags & IconWindowHint) &&
161                 (wmhints->icon_window != children[i]))
162                 for (j = 0; j < nchild; j++)
163                     if (children[j] == wmhints->icon_window) {
164                         /* XXX watch the window though */
165                         children[j] = None;
166                         break;
167                     }
168             XFree(wmhints);
169         }
170     }
171
172     for (i = 0; i < nchild; ++i) {
173         if (children[i] == None) continue;
174         if (window_find(children[i])) continue; /* skip our own windows */
175         if (XGetWindowAttributes(obt_display, children[i], &attrib)) {
176             if (attrib.map_state == IsUnmapped)
177                 ;
178             else
179                 window_manage(children[i]);
180         }
181     }
182
183     if (children) XFree(children);
184 }
185
186 static gboolean check_unmap(XEvent *e, gpointer data)
187 {
188     const Window win = *(Window*)data;
189     return ((e->type == DestroyNotify && e->xdestroywindow.window == win) ||
190             (e->type == UnmapNotify && e->xunmap.window == win));
191 }
192
193 void window_manage(Window win)
194 {
195     XWindowAttributes attrib;
196     gboolean no_manage = FALSE;
197     gboolean is_dockapp = FALSE;
198     Window icon_win = None;
199
200     grab_server(TRUE);
201
202     /* check if it has already been unmapped by the time we started
203        mapping. the grab does a sync so we don't have to here */
204     if (xqueue_exists_local(check_unmap, &win)) {
205         ob_debug("Trying to manage unmapped window. Aborting that.");
206         no_manage = TRUE;
207     }
208     else if (!XGetWindowAttributes(obt_display, win, &attrib))
209         no_manage = TRUE;
210     else {
211         XWMHints *wmhints;
212
213         /* is the window a docking app */
214         is_dockapp = FALSE;
215         if ((wmhints = XGetWMHints(obt_display, win))) {
216             if ((wmhints->flags & StateHint) &&
217                 wmhints->initial_state == WithdrawnState)
218             {
219                 if (wmhints->flags & IconWindowHint)
220                     icon_win = wmhints->icon_window;
221                 is_dockapp = TRUE;
222             }
223             XFree(wmhints);
224         }
225     }
226
227     if (!no_manage) {
228         if (attrib.override_redirect) {
229             ob_debug("not managing override redirect window 0x%x", win);
230             grab_server(FALSE);
231         }
232         else if (is_dockapp) {
233             if (!icon_win)
234                 icon_win = win;
235             dock_manage(icon_win, win);
236         }
237         else
238             client_manage(win, NULL);
239     }
240     else {
241         grab_server(FALSE);
242         ob_debug("FAILED to manage window 0x%x", win);
243     }
244 }
245
246 void window_unmanage_all(void)
247 {
248     dock_unmanage_all();
249     client_unmanage_all();
250 }