Merge branch 'backport' into work
[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 "keyboard.h"
30 #include "focus.h"
31 #include "stacking.h"
32 #include "obt/prop.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(obt_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", 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         OBT_PROP_SET32(obt_root(ob_screen), NET_ACTIVE_WINDOW, WINDOW, active);
99     }
100 }
101
102 static ObClient* focus_fallback_target(gboolean allow_refocus,
103                                        gboolean allow_pointer,
104                                        gboolean allow_omnipresent,
105                                        ObClient *old)
106 {
107     GList *it;
108     ObClient *c;
109
110     ob_debug_type(OB_DEBUG_FOCUS, "trying pointer stuff");
111     if (allow_pointer && config_focus_follow)
112         if ((c = client_under_pointer()) &&
113             (allow_refocus || client_focus_target(c) != old) &&
114             (client_normal(c) &&
115              client_focus(c)))
116         {
117             ob_debug_type(OB_DEBUG_FOCUS, "found in pointer stuff");
118             return c;
119         }
120
121     ob_debug_type(OB_DEBUG_FOCUS, "trying the focus order");
122     for (it = focus_order; it; it = g_list_next(it)) {
123         c = it->data;
124         /* fallback focus to a window if:
125            1. it is on the current desktop. this ignores omnipresent
126            windows, which are problematic in their own rite, unless they are
127            specifically allowed
128            2. it is a valid auto-focus target
129            3. it is not shaded
130         */
131         if ((allow_omnipresent || c->desktop == screen_desktop) &&
132             focus_valid_target(c, TRUE, FALSE, FALSE, FALSE, FALSE) &&
133             !c->shaded &&
134             (allow_refocus || client_focus_target(c) != old) &&
135             client_focus(c))
136         {
137             ob_debug_type(OB_DEBUG_FOCUS, "found in focus order");
138             return c;
139         }
140     }
141
142     ob_debug_type(OB_DEBUG_FOCUS, "trying a desktop window");
143     for (it = focus_order; it; it = g_list_next(it)) {
144         c = it->data;
145         /* fallback focus to a window if:
146            1. it is on the current desktop. this ignores omnipresent
147            windows, which are problematic in their own rite.
148            2. it is a normal type window, don't fall back onto a dock or
149            a splashscreen or a desktop window (save the desktop as a
150            backup fallback though)
151         */
152         if (focus_valid_target(c, TRUE, FALSE, FALSE, FALSE, TRUE) &&
153             (allow_refocus || client_focus_target(c) != old) &&
154             client_focus(c))
155         {
156             ob_debug_type(OB_DEBUG_FOCUS, "found a desktop window");
157             return c;
158         }
159     }
160
161     return NULL;
162 }
163
164 ObClient* focus_fallback(gboolean allow_refocus, gboolean allow_pointer,
165                          gboolean allow_omnipresent, gboolean focus_lost)
166 {
167     ObClient *new;
168     ObClient *old = focus_client;
169
170     /* unfocus any focused clients.. they can be focused by Pointer events
171        and such, and then when we try focus them, we won't get a FocusIn
172        event at all for them. */
173     if (focus_lost)
174         focus_nothing();
175
176     new = focus_fallback_target(allow_refocus, allow_pointer,
177                                 allow_omnipresent, old);
178     /* get what was really focused */
179     if (new) new = client_focus_target(new);
180
181     return new;
182 }
183
184 void focus_nothing(void)
185 {
186     /* Install our own colormap */
187     if (focus_client != NULL) {
188         screen_install_colormap(focus_client, FALSE);
189         screen_install_colormap(NULL, TRUE);
190     }
191
192     /* nothing is focused, update the colormap and _the root property_ */
193     focus_set_client(NULL);
194
195     /* when nothing will be focused, send focus to the backup target */
196     XSetInputFocus(obt_display, screen_support_win, RevertToPointerRoot,
197                    event_curtime);
198 }
199
200 void focus_order_add_new(ObClient *c)
201 {
202     if (c->iconic)
203         focus_order_to_top(c);
204     else {
205         g_assert(!g_list_find(focus_order, c));
206         /* if there are any iconic windows, put this above them in the order,
207            but if there are not, then put it under the currently focused one */
208         if (focus_order && ((ObClient*)focus_order->data)->iconic)
209             focus_order = g_list_insert(focus_order, c, 0);
210         else
211             focus_order = g_list_insert(focus_order, c, 1);
212     }
213
214     /* in the middle of cycling..? kill it. */
215     focus_cycle_stop(c);
216 }
217
218 void focus_order_remove(ObClient *c)
219 {
220     focus_order = g_list_remove(focus_order, c);
221
222     /* in the middle of cycling..? kill it. */
223     focus_cycle_stop(c);
224 }
225
226 void focus_order_to_top(ObClient *c)
227 {
228     focus_order = g_list_remove(focus_order, c);
229     if (!c->iconic) {
230         focus_order = g_list_prepend(focus_order, c);
231     } else {
232         GList *it;
233
234         /* insert before first iconic window */
235         for (it = focus_order;
236              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
237         focus_order = g_list_insert_before(focus_order, it, c);
238     }
239 }
240
241 void focus_order_to_bottom(ObClient *c)
242 {
243     focus_order = g_list_remove(focus_order, c);
244     if (c->iconic) {
245         focus_order = g_list_append(focus_order, c);
246     } else {
247         GList *it;
248
249         /* insert before first iconic window */
250         for (it = focus_order;
251              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
252         focus_order = g_list_insert_before(focus_order, it, c);
253     }
254 }
255
256 ObClient *focus_order_find_first(guint desktop)
257 {
258     GList *it;
259     for (it = focus_order; it; it = g_list_next(it)) {
260         ObClient *c = it->data;
261         if (c->desktop == desktop || c->desktop == DESKTOP_ALL)
262             return c;
263     }
264     return NULL;
265 }
266
267 /*! Returns if a focus target has valid group siblings that can be cycled
268   to in its place */
269 static gboolean focus_target_has_siblings(ObClient *ft,
270                                           gboolean iconic_windows,
271                                           gboolean all_desktops)
272
273 {
274     GSList *it;
275
276     if (!ft->group) return FALSE;
277
278     for (it = ft->group->members; it; it = g_slist_next(it)) {
279         ObClient *c = it->data;
280         /* check that it's not a helper window to avoid infinite recursion */
281         if (c != ft && c->type == OB_CLIENT_TYPE_NORMAL &&
282             focus_valid_target(c, TRUE, iconic_windows, all_desktops,
283                                FALSE, FALSE))
284         {
285             return TRUE;
286         }
287     }
288     return FALSE;
289 }
290
291 gboolean focus_valid_target(ObClient *ft,
292                             gboolean helper_windows,
293                             gboolean iconic_windows,
294                             gboolean all_desktops,
295                             gboolean dock_windows,
296                             gboolean desktop_windows)
297 {
298     gboolean ok = FALSE;
299
300     /* it's on this desktop unless you want all desktops.
301
302        do this check first because it will usually filter out the most
303        windows */
304     ok = (all_desktops || ft->desktop == screen_desktop ||
305           ft->desktop == DESKTOP_ALL);
306
307     /* the window can receive focus somehow */
308     ok = ok && (ft->can_focus || ft->focus_notify);
309
310     /* the window is not iconic, or we're allowed to go to iconic ones */
311     ok = ok && (iconic_windows || !ft->iconic);
312
313     /* it's the right type of window */
314     if (dock_windows || desktop_windows)
315         ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
316                     (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
317     /* modal windows are important and can always get focus if they are
318        visible and stuff, so don't change 'ok' based on their type */
319     else if (!ft->modal)
320         /* normal non-helper windows are valid targets */
321         ok = ok &&
322             ((client_normal(ft) && !client_helper(ft))
323              ||
324              /* helper windows are valid targets if... */
325              (client_helper(ft) &&
326               /* ...a window in its group already has focus and we want to
327                  include helper windows ... */
328               ((focus_client && ft->group == focus_client->group &&
329                 helper_windows) ||
330                /* ... or if there are no other windows in its group
331                   that can be focused instead */
332                !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
333
334     /* it's not set to skip the taskbar (but this only applies to normal typed
335        windows, and is overridden if the window is modal) */
336     ok = ok && (ft->type != OB_CLIENT_TYPE_NORMAL ||
337                 ft->modal ||
338                 !ft->skip_taskbar);
339
340     /* it's not going to just send focus off somewhere else (modal window),
341        unless that modal window is not one of our valid targets, then let
342        you choose this window and bring the modal one here */
343     {
344         ObClient *cft = client_focus_target(ft);
345         ok = ok && (ft == cft || !focus_valid_target(cft,
346                                                      TRUE,
347                                                      iconic_windows,
348                                                      all_desktops,
349                                                      dock_windows,
350                                                      desktop_windows));
351     }
352
353     return ok;
354 }