]> icculus.org git repositories - dana/openbox.git/blob - openbox/window.c
generalize the window managing process into window_manage, which handles dock apps...
[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 "debug.h"
27 #include "grab.h"
28
29 static GHashTable *window_map;
30
31 static guint window_hash(Window *w) { return *w; }
32 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
33
34 void window_startup(gboolean reconfig)
35 {
36     if (reconfig) return;
37
38     window_map = g_hash_table_new((GHashFunc)window_hash,
39                                   (GEqualFunc)window_comp);
40 }
41
42 void window_shutdown(gboolean reconfig)
43 {
44     if (reconfig) return;
45
46     g_hash_table_destroy(window_map);
47 }
48
49 Window window_top(ObWindow *self)
50 {
51     switch (self->type) {
52     case OB_WINDOW_CLASS_MENUFRAME:
53         return WINDOW_AS_MENUFRAME(self)->window;
54     case OB_WINDOW_CLASS_DOCK:
55         return WINDOW_AS_DOCK(self)->frame;
56     case OB_WINDOW_CLASS_CLIENT:
57         return WINDOW_AS_CLIENT(self)->frame->window;
58     case OB_WINDOW_CLASS_INTERNAL:
59         return WINDOW_AS_INTERNAL(self)->window;
60     }
61     g_assert_not_reached();
62     return None;
63 }
64
65 ObStackingLayer window_layer(ObWindow *self)
66 {
67     switch (self->type) {
68     case OB_WINDOW_CLASS_DOCK:
69         return config_dock_layer;
70     case OB_WINDOW_CLASS_CLIENT:
71         return ((ObClient*)self)->layer;
72     case OB_WINDOW_CLASS_MENUFRAME:
73     case OB_WINDOW_CLASS_INTERNAL:
74         return OB_STACKING_LAYER_INTERNAL;
75     }
76     g_assert_not_reached();
77     return None;
78 }
79
80 ObWindow* window_find(Window xwin)
81 {
82     return g_hash_table_lookup(window_map, &xwin);
83 }
84
85 void window_add(Window *xwin, ObWindow *win)
86 {
87     g_assert(xwin != NULL);
88     g_assert(win != NULL);
89     g_hash_table_insert(window_map, xwin, win);
90 }
91
92 void window_remove(Window xwin)
93 {
94     g_assert(xwin != None);
95     g_hash_table_remove(window_map, &xwin);
96 }
97
98 void window_manage_all(void)
99 {
100     guint i, j, nchild;
101     Window w, *children;
102     XWMHints *wmhints;
103     XWindowAttributes attrib;
104
105     if (!XQueryTree(obt_display, RootWindow(obt_display, ob_screen),
106                     &w, &w, &children, &nchild)) {
107         ob_debug("XQueryTree failed in window_manage_all");
108         nchild = 0;
109     }
110
111     /* remove all icon windows from the list */
112     for (i = 0; i < nchild; i++) {
113         if (children[i] == None) continue;
114         wmhints = XGetWMHints(obt_display, children[i]);
115         if (wmhints) {
116             if ((wmhints->flags & IconWindowHint) &&
117                 (wmhints->icon_window != children[i]))
118                 for (j = 0; j < nchild; j++)
119                     if (children[j] == wmhints->icon_window) {
120                         /* XXX watch the window though */
121                         children[j] = None;
122                         break;
123                     }
124             XFree(wmhints);
125         }
126     }
127
128     for (i = 0; i < nchild; ++i) {
129         if (children[i] == None) continue;
130         if (window_find(children[i])) continue; /* skip our own windows */
131         if (XGetWindowAttributes(obt_display, children[i], &attrib)) {
132             if (attrib.map_state == IsUnmapped)
133                 ;
134             else
135                 window_manage(children[i]);
136         }
137     }
138
139     if (children) XFree(children);
140 }
141
142 void window_manage(Window win)
143 {
144     XEvent e;
145     XWindowAttributes attrib;
146     gboolean no_manage = FALSE;
147     gboolean is_dockapp = FALSE;
148     Window icon_win = None;
149
150     grab_server(TRUE);
151
152     /* check if it has already been unmapped by the time we started
153        mapping. the grab does a sync so we don't have to here */
154     if (XCheckTypedWindowEvent(obt_display, win, DestroyNotify, &e) ||
155         XCheckTypedWindowEvent(obt_display, win, UnmapNotify, &e))
156     {
157         XPutBackEvent(obt_display, &e);
158         ob_debug("Trying to manage unmapped window. Aborting that.\n");
159         no_manage = TRUE;
160     }
161
162     if (!XGetWindowAttributes(obt_display, win, &attrib))
163         no_manage = TRUE;
164     else {
165         XWMHints *wmhints;
166
167         /* is the window a docking app */
168         is_dockapp = FALSE;
169         if ((wmhints = XGetWMHints(obt_display, win))) {
170             if ((wmhints->flags & StateHint) &&
171                 wmhints->initial_state == WithdrawnState)
172             {
173                 if (wmhints->flags & IconWindowHint)
174                     icon_win = wmhints->icon_window;
175                 is_dockapp = TRUE;
176             }
177             XFree(wmhints);
178         }
179     }
180
181     if (!no_manage) {
182         if (attrib.override_redirect) {
183             ob_debug("not managing override redirect window 0x%x\n", win);
184             grab_server(FALSE);
185         }
186         else if (is_dockapp) {
187             if (!icon_win)
188                 icon_win = win;
189             dock_manage(icon_win, win);
190         }
191         else
192             client_manage(win);
193     }
194     else {
195         grab_server(FALSE);
196         ob_debug("FAILED to manage window 0x%x\n", win);
197     }
198 }
199
200 void window_unmanage_all(void)
201 {
202     dock_unmanage_all();
203     client_unmanage_all();
204 }