Merge branch 'backport' into work
[openbox.git] / openbox / window.c
blob19b362edd14e26fda80d5a1ee6fc5c2d1e680e65
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 window.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
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"
30 static GHashTable *window_map;
32 static guint window_hash(Window *w) { return *w; }
33 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
35 void window_startup(gboolean reconfig)
37 if (reconfig) return;
39 window_map = g_hash_table_new((GHashFunc)window_hash,
40 (GEqualFunc)window_comp);
43 void window_shutdown(gboolean reconfig)
45 if (reconfig) return;
47 g_hash_table_destroy(window_map);
50 Window window_top(ObWindow *self)
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;
64 g_assert_not_reached();
65 return None;
68 ObStackingLayer window_layer(ObWindow *self)
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 return OB_STACKING_LAYER_INTERNAL;
78 case OB_WINDOW_CLASS_PROMPT:
79 /* not used directly for stacking, prompts are managed as clients */
80 g_assert_not_reached();
81 break;
83 g_assert_not_reached();
84 return None;
87 ObWindow* window_find(Window xwin)
89 return g_hash_table_lookup(window_map, &xwin);
92 void window_add(Window *xwin, ObWindow *win)
94 g_assert(xwin != NULL);
95 g_assert(win != NULL);
96 g_hash_table_insert(window_map, xwin, win);
99 void window_remove(Window xwin)
101 g_assert(xwin != None);
102 g_hash_table_remove(window_map, &xwin);
105 void window_manage_all(void)
107 guint i, j, nchild;
108 Window w, *children;
109 XWMHints *wmhints;
110 XWindowAttributes attrib;
112 if (!XQueryTree(obt_display, RootWindow(obt_display, ob_screen),
113 &w, &w, &children, &nchild)) {
114 ob_debug("XQueryTree failed in window_manage_all");
115 nchild = 0;
118 /* remove all icon windows from the list */
119 for (i = 0; i < nchild; i++) {
120 if (children[i] == None) continue;
121 wmhints = XGetWMHints(obt_display, children[i]);
122 if (wmhints) {
123 if ((wmhints->flags & IconWindowHint) &&
124 (wmhints->icon_window != children[i]))
125 for (j = 0; j < nchild; j++)
126 if (children[j] == wmhints->icon_window) {
127 /* XXX watch the window though */
128 children[j] = None;
129 break;
131 XFree(wmhints);
135 for (i = 0; i < nchild; ++i) {
136 if (children[i] == None) continue;
137 if (window_find(children[i])) continue; /* skip our own windows */
138 if (XGetWindowAttributes(obt_display, children[i], &attrib)) {
139 if (attrib.map_state == IsUnmapped)
141 else
142 window_manage(children[i]);
146 if (children) XFree(children);
149 void window_manage(Window win)
151 XEvent e;
152 XWindowAttributes attrib;
153 gboolean no_manage = FALSE;
154 gboolean is_dockapp = FALSE;
155 Window icon_win = None;
157 grab_server(TRUE);
159 /* check if it has already been unmapped by the time we started
160 mapping. the grab does a sync so we don't have to here */
161 if (XCheckTypedWindowEvent(obt_display, win, DestroyNotify, &e) ||
162 XCheckTypedWindowEvent(obt_display, win, UnmapNotify, &e))
164 XPutBackEvent(obt_display, &e);
165 ob_debug("Trying to manage unmapped window. Aborting that.");
166 no_manage = TRUE;
169 if (!XGetWindowAttributes(obt_display, win, &attrib))
170 no_manage = TRUE;
171 else {
172 XWMHints *wmhints;
174 /* is the window a docking app */
175 is_dockapp = FALSE;
176 if ((wmhints = XGetWMHints(obt_display, win))) {
177 if ((wmhints->flags & StateHint) &&
178 wmhints->initial_state == WithdrawnState)
180 if (wmhints->flags & IconWindowHint)
181 icon_win = wmhints->icon_window;
182 is_dockapp = TRUE;
184 XFree(wmhints);
188 if (!no_manage) {
189 if (attrib.override_redirect) {
190 ob_debug("not managing override redirect window 0x%x", win);
191 grab_server(FALSE);
193 else if (is_dockapp) {
194 if (!icon_win)
195 icon_win = win;
196 dock_manage(icon_win, win);
198 else
199 client_manage(win, NULL);
201 else {
202 grab_server(FALSE);
203 ob_debug("FAILED to manage window 0x%x", win);
207 void window_unmanage_all(void)
209 dock_unmanage_all();
210 client_unmanage_all();