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