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