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