]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
Allow skip_taskbar windows to be focused on map when the user requests it in rc.xml...
[dana/openbox.git] / openbox / focus.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focus.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "event.h"
22 #include "openbox.h"
23 #include "grab.h"
24 #include "client.h"
25 #include "config.h"
26 #include "group.h"
27 #include "focus_cycle.h"
28 #include "screen.h"
29 #include "prop.h"
30 #include "keyboard.h"
31 #include "focus.h"
32 #include "stacking.h"
33
34 #include <X11/Xlib.h>
35 #include <glib.h>
36
37 #define FOCUS_INDICATOR_WIDTH 6
38
39 ObClient *focus_client = NULL;
40 GList *focus_order = NULL;
41
42 void focus_startup(gboolean reconfig)
43 {
44     if (reconfig) return;
45
46     /* start with nothing focused */
47     focus_nothing();
48 }
49
50 void focus_shutdown(gboolean reconfig)
51 {
52     if (reconfig) return;
53
54     /* reset focus to root */
55     XSetInputFocus(ob_display, PointerRoot, RevertToNone, CurrentTime);
56 }
57
58 static void push_to_top(ObClient *client)
59 {
60     ObClient *p;
61
62     /* if it is modal for a single window, then put that window at the top
63        of the focus order first, so it will be right after ours. the same is
64        done with stacking */
65     if (client->modal && (p = client_direct_parent(client)))
66         push_to_top(p);
67
68     focus_order = g_list_remove(focus_order, client);
69     focus_order = g_list_prepend(focus_order, client);
70 }
71
72 void focus_set_client(ObClient *client)
73 {
74     Window active;
75
76     ob_debug_type(OB_DEBUG_FOCUS,
77                   "focus_set_client 0x%lx\n", client ? client->window : 0);
78
79     if (focus_client == client)
80         return;
81
82     /* uninstall the old colormap, and install the new one */
83     screen_install_colormap(focus_client, FALSE);
84     screen_install_colormap(client, TRUE);
85
86     focus_client = client;
87
88     if (client != NULL) {
89         /* move to the top of the list */
90         push_to_top(client);
91         /* remove hiliting from the window when it gets focused */
92         client_hilite(client, FALSE);
93     }
94
95     /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
96     if (ob_state() != OB_STATE_EXITING) {
97         active = client ? client->window : None;
98         PROP_SET32(RootWindow(ob_display, ob_screen),
99                    net_active_window, window, active);
100     }
101 }
102
103 static ObClient* focus_fallback_target(gboolean allow_refocus,
104                                        gboolean allow_pointer,
105                                        gboolean allow_omnipresent,
106                                        ObClient *old)
107 {
108     GList *it;
109     ObClient *c;
110
111     ob_debug_type(OB_DEBUG_FOCUS, "trying pointer stuff\n");
112     if (allow_pointer && config_focus_follow)
113         if ((c = client_under_pointer()) &&
114             (allow_refocus || client_focus_target(c) != old) &&
115             (client_normal(c) &&
116              client_focus(c)))
117         {
118             ob_debug_type(OB_DEBUG_FOCUS, "found in pointer stuff\n");
119             return c;
120         }
121
122     ob_debug_type(OB_DEBUG_FOCUS, "trying the focus order\n");
123     for (it = focus_order; it; it = g_list_next(it)) {
124         c = it->data;
125         /* fallback focus to a window if:
126            1. it is on the current desktop. this ignores omnipresent
127            windows, which are problematic in their own rite, unless they are
128            specifically allowed
129            2. it is a valid auto-focus target
130            3. it is not shaded
131         */
132         if ((allow_omnipresent || c->desktop == screen_desktop) &&
133             focus_valid_target(c, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) &&
134             !c->shaded &&
135             (allow_refocus || client_focus_target(c) != old) &&
136             client_focus(c))
137         {
138             ob_debug_type(OB_DEBUG_FOCUS, "found in focus order\n");
139             return c;
140         }
141     }
142
143     ob_debug_type(OB_DEBUG_FOCUS, "trying a desktop window\n");
144     for (it = focus_order; it; it = g_list_next(it)) {
145         c = it->data;
146         /* fallback focus to a window if:
147            1. it is on the current desktop. this ignores omnipresent
148            windows, which are problematic in their own rite.
149            2. it is a normal type window, don't fall back onto a dock or
150            a splashscreen or a desktop window (save the desktop as a
151            backup fallback though)
152         */
153         if (focus_valid_target(c, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) &&
154             (allow_refocus || client_focus_target(c) != old) &&
155             client_focus(c))
156         {
157             ob_debug_type(OB_DEBUG_FOCUS, "found a desktop window\n");
158             return c;
159         }
160     }
161
162     return NULL;
163 }
164
165 ObClient* focus_fallback(gboolean allow_refocus, gboolean allow_pointer,
166                          gboolean allow_omnipresent, gboolean focus_lost)
167 {
168     ObClient *new;
169     ObClient *old = focus_client;
170
171     /* unfocus any focused clients.. they can be focused by Pointer events
172        and such, and then when we try focus them, we won't get a FocusIn
173        event at all for them. */
174     if (focus_lost)
175         focus_nothing();
176
177     new = focus_fallback_target(allow_refocus, allow_pointer,
178                                 allow_omnipresent, old);
179     /* get what was really focused */
180     if (new) new = client_focus_target(new);
181
182     return new;
183 }
184
185 void focus_nothing(void)
186 {
187     /* Install our own colormap */
188     if (focus_client != NULL) {
189         screen_install_colormap(focus_client, FALSE);
190         screen_install_colormap(NULL, TRUE);
191     }
192
193     /* nothing is focused, update the colormap and _the root property_ */
194     focus_set_client(NULL);
195
196     /* when nothing will be focused, send focus to the backup target */
197     XSetInputFocus(ob_display, screen_support_win, RevertToPointerRoot,
198                    event_curtime);
199 }
200
201 void focus_order_add_new(ObClient *c)
202 {
203     if (c->iconic)
204         focus_order_to_top(c);
205     else {
206         g_assert(!g_list_find(focus_order, c));
207         /* if there are any iconic windows, put this above them in the order,
208            but if there are not, then put it under the currently focused one */
209         if (focus_order && ((ObClient*)focus_order->data)->iconic)
210             focus_order = g_list_insert(focus_order, c, 0);
211         else
212             focus_order = g_list_insert(focus_order, c, 1);
213     }
214
215     /* in the middle of cycling..? kill it. */
216     focus_cycle_stop(c);
217 }
218
219 void focus_order_remove(ObClient *c)
220 {
221     focus_order = g_list_remove(focus_order, c);
222
223     /* in the middle of cycling..? kill it. */
224     focus_cycle_stop(c);
225 }
226
227 void focus_order_to_top(ObClient *c)
228 {
229     focus_order = g_list_remove(focus_order, c);
230     if (!c->iconic) {
231         focus_order = g_list_prepend(focus_order, c);
232     } else {
233         GList *it;
234
235         /* insert before first iconic window */
236         for (it = focus_order;
237              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
238         focus_order = g_list_insert_before(focus_order, it, c);
239     }
240 }
241
242 void focus_order_to_bottom(ObClient *c)
243 {
244     focus_order = g_list_remove(focus_order, c);
245     if (c->iconic) {
246         focus_order = g_list_append(focus_order, c);
247     } else {
248         GList *it;
249
250         /* insert before first iconic window */
251         for (it = focus_order;
252              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
253         focus_order = g_list_insert_before(focus_order, it, c);
254     }
255 }
256
257 ObClient *focus_order_find_first(guint desktop)
258 {
259     GList *it;
260     for (it = focus_order; it; it = g_list_next(it)) {
261         ObClient *c = it->data;
262         if (c->desktop == desktop || c->desktop == DESKTOP_ALL)
263             return c;
264     }
265     return NULL;
266 }
267
268 /*! Returns if a focus target has valid group siblings that can be cycled
269   to in its place */
270 static gboolean focus_target_has_siblings(ObClient *ft,
271                                           gboolean iconic_windows,
272                                           gboolean all_desktops)
273
274 {
275     GSList *it;
276
277     if (!ft->group) return FALSE;
278
279     for (it = ft->group->members; it; it = g_slist_next(it)) {
280         ObClient *c = it->data;
281         /* check that it's not a helper window to avoid infinite recursion */
282         if (c != ft && c->type == OB_CLIENT_TYPE_NORMAL &&
283             focus_valid_target(c, TRUE, iconic_windows, all_desktops,
284                                FALSE, FALSE, FALSE))
285         {
286             return TRUE;
287         }
288     }
289     return FALSE;
290 }
291
292 gboolean focus_valid_target(ObClient *ft,
293                             gboolean helper_windows,
294                             gboolean iconic_windows,
295                             gboolean all_desktops,
296                             gboolean dock_windows,
297                             gboolean desktop_windows,
298                             gboolean user_request)
299 {
300     gboolean ok = FALSE;
301
302     /* it's on this desktop unless you want all desktops.
303
304        do this check first because it will usually filter out the most
305        windows */
306     ok = (all_desktops || ft->desktop == screen_desktop ||
307           ft->desktop == DESKTOP_ALL);
308
309     /* the window can receive focus somehow */
310     ok = ok && (ft->can_focus || ft->focus_notify);
311
312     /* the window is not iconic, or we're allowed to go to iconic ones */
313     ok = ok && (iconic_windows || !ft->iconic);
314
315     /* it's the right type of window */
316     if (dock_windows || desktop_windows)
317         ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
318                     (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
319     /* modal windows are important and can always get focus if they are
320        visible and stuff, so don't change 'ok' based on their type */
321     else if (!ft->modal)
322         /* normal non-helper windows are valid targets */
323         ok = ok &&
324             ((client_normal(ft) && !client_helper(ft))
325              ||
326              /* helper windows are valid targets if... */
327              (client_helper(ft) &&
328               /* ...a window in its group already has focus and we want to
329                  include helper windows ... */
330               ((focus_client && ft->group == focus_client->group &&
331                 helper_windows) ||
332                /* ... or if there are no other windows in its group
333                   that can be focused instead */
334                !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
335
336     /* it's not set to skip the taskbar (but this only applies to normal typed
337        windows, and is overridden if the window is modal or if the user asked
338        for this window to be focused) */
339     ok = ok && (ft->type != OB_CLIENT_TYPE_NORMAL ||
340                 ft->modal ||
341                 user_request ||
342                 !ft->skip_taskbar);
343
344     /* it's not going to just send focus off somewhere else (modal window),
345        unless that modal window is not one of our valid targets, then let
346        you choose this window and bring the modal one here */
347     {
348         ObClient *cft = client_focus_target(ft);
349         ok = ok && (ft == cft || !focus_valid_target(cft,
350                                                      TRUE,
351                                                      iconic_windows,
352                                                      all_desktops,
353                                                      dock_windows,
354                                                      desktop_windows,
355                                                      FALSE));
356     }
357
358     return ok;
359 }