Correct typo occured -> occurred in function name
[openbox.git] / openbox / focus.c
blob8c023618e71188b85a5b02580733bf08b13c8567
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
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"
34 #include <X11/Xlib.h>
35 #include <glib.h>
37 #define FOCUS_INDICATOR_WIDTH 6
39 ObClient *focus_client = NULL;
40 GList *focus_order = NULL;
42 void focus_startup(gboolean reconfig)
44 if (reconfig) return;
46 /* start with nothing focused */
47 focus_nothing();
50 void focus_shutdown(gboolean reconfig)
52 if (reconfig) return;
54 /* reset focus to root */
55 XSetInputFocus(obt_display, PointerRoot, RevertToNone, CurrentTime);
58 static void push_to_top(ObClient *client)
60 ObClient *p;
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);
68 focus_order = g_list_remove(focus_order, client);
69 focus_order = g_list_prepend(focus_order, client);
72 void focus_set_client(ObClient *client)
74 Window active;
76 ob_debug_type(OB_DEBUG_FOCUS,
77 "focus_set_client 0x%lx", client ? client->window : 0);
79 if (focus_client == client)
80 return;
82 /* uninstall the old colormap, and install the new one */
83 screen_install_colormap(focus_client, FALSE);
84 screen_install_colormap(client, TRUE);
86 focus_client = client;
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);
94 /* make sure the focus cycle popup shows things in the right order */
95 focus_cycle_reorder();
98 /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
99 if (ob_state() != OB_STATE_EXITING) {
100 active = client ? client->window : None;
101 OBT_PROP_SET32(obt_root(ob_screen), NET_ACTIVE_WINDOW, WINDOW, active);
105 static ObClient* focus_fallback_target(gboolean allow_refocus,
106 gboolean allow_pointer,
107 gboolean allow_omnipresent,
108 ObClient *old)
110 GList *it;
111 ObClient *c;
113 ob_debug_type(OB_DEBUG_FOCUS, "trying pointer stuff");
114 if (allow_pointer && config_focus_follow)
115 if ((c = client_under_pointer()) &&
116 (allow_refocus || client_focus_target(c) != old) &&
117 (client_normal(c) &&
118 client_focus(c)))
120 ob_debug_type(OB_DEBUG_FOCUS, "found in pointer stuff");
121 return c;
124 ob_debug_type(OB_DEBUG_FOCUS, "trying the focus order");
125 for (it = focus_order; it; it = g_list_next(it)) {
126 c = it->data;
127 /* fallback focus to a window if:
128 1. it is on the current desktop. this ignores omnipresent
129 windows, which are problematic in their own rite, unless they are
130 specifically allowed
131 2. it is a valid auto-focus target
132 3. it is not shaded
134 if ((allow_omnipresent || c->desktop == screen_desktop) &&
135 focus_valid_target(c, screen_desktop,
136 TRUE, FALSE, FALSE, TRUE, FALSE, FALSE,
137 FALSE) &&
138 !c->shaded &&
139 (allow_refocus || client_focus_target(c) != old) &&
140 client_focus(c))
142 ob_debug_type(OB_DEBUG_FOCUS, "found in focus order");
143 return c;
147 ob_debug_type(OB_DEBUG_FOCUS, "trying a desktop window");
148 for (it = focus_order; it; it = g_list_next(it)) {
149 c = it->data;
150 /* fallback focus to a window if:
151 1. it is on the current desktop. this ignores omnipresent
152 windows, which are problematic in their own rite.
153 2. it is a normal type window, don't fall back onto a dock or
154 a splashscreen or a desktop window (save the desktop as a
155 backup fallback though)
157 if (focus_valid_target(c, screen_desktop,
158 TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE) &&
159 (allow_refocus || client_focus_target(c) != old) &&
160 client_focus(c))
162 ob_debug_type(OB_DEBUG_FOCUS, "found a desktop window");
163 return c;
167 return NULL;
170 ObClient* focus_fallback(gboolean allow_refocus, gboolean allow_pointer,
171 gboolean allow_omnipresent, gboolean focus_lost)
173 ObClient *new;
174 ObClient *old = focus_client;
176 /* unfocus any focused clients.. they can be focused by Pointer events
177 and such, and then when we try focus them, we won't get a FocusIn
178 event at all for them. */
179 if (focus_lost)
180 focus_nothing();
182 new = focus_fallback_target(allow_refocus, allow_pointer,
183 allow_omnipresent, old);
184 /* get what was really focused */
185 if (new) new = client_focus_target(new);
187 return new;
190 void focus_nothing(void)
192 /* nothing is focused, update the colormap and _the root property_ */
193 focus_set_client(NULL);
195 /* when nothing will be focused, send focus to the backup target */
196 XSetInputFocus(obt_display, screen_support_win, RevertToPointerRoot,
197 event_time());
200 void focus_order_add_new(ObClient *c)
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 only 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);
214 focus_cycle_addremove(c, TRUE);
217 void focus_order_remove(ObClient *c)
219 focus_order = g_list_remove(focus_order, c);
221 focus_cycle_addremove(c, TRUE);
224 void focus_order_like_new(struct _ObClient *c)
226 focus_order = g_list_remove(focus_order, c);
227 focus_order_add_new(c);
230 void focus_order_to_top(ObClient *c)
232 focus_order = g_list_remove(focus_order, c);
233 if (!c->iconic) {
234 focus_order = g_list_prepend(focus_order, c);
235 } else {
236 GList *it;
238 /* insert before first iconic window */
239 for (it = focus_order;
240 it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
241 focus_order = g_list_insert_before(focus_order, it, c);
244 focus_cycle_reorder();
247 void focus_order_to_bottom(ObClient *c)
249 focus_order = g_list_remove(focus_order, c);
250 if (c->iconic) {
251 focus_order = g_list_append(focus_order, c);
252 } else {
253 GList *it;
255 /* insert before first iconic window */
256 for (it = focus_order;
257 it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
258 focus_order = g_list_insert_before(focus_order, it, c);
261 focus_cycle_reorder();
264 ObClient *focus_order_find_first(guint desktop)
266 GList *it;
267 for (it = focus_order; it; it = g_list_next(it)) {
268 ObClient *c = it->data;
269 if (c->desktop == desktop || c->desktop == DESKTOP_ALL)
270 return c;
272 return NULL;
275 /*! Returns if a focus target has valid group siblings that can be cycled
276 to in its place */
277 static gboolean focus_target_has_siblings(ObClient *ft,
278 gboolean iconic_windows,
279 gboolean all_desktops)
282 GSList *it;
284 if (!ft->group) return FALSE;
286 for (it = ft->group->members; it; it = g_slist_next(it)) {
287 ObClient *c = it->data;
288 /* check that it's not a helper window to avoid infinite recursion */
289 if (c != ft && c->type == OB_CLIENT_TYPE_NORMAL &&
290 focus_valid_target(c, screen_desktop,
291 TRUE, iconic_windows, all_desktops,
292 TRUE, FALSE, FALSE, FALSE))
294 return TRUE;
297 return FALSE;
300 gboolean focus_valid_target(ObClient *ft,
301 guint desktop,
302 gboolean helper_windows,
303 gboolean iconic_windows,
304 gboolean all_desktops,
305 gboolean nonhilite_windows,
306 gboolean dock_windows,
307 gboolean desktop_windows,
308 gboolean user_request)
310 /* NOTE: if any of these things change on a client, then they should call
311 focus_cycle_addremove() to make sure the client is not shown/hidden
312 when it should not be */
314 gboolean ok = FALSE;
316 /* see if the window is still managed or is going away */
317 if (!ft->managed) return FALSE;
319 /* it's on this desktop unless you want all desktops.
321 do this check first because it will usually filter out the most
322 windows */
323 ok = (all_desktops || ft->desktop == desktop ||
324 ft->desktop == DESKTOP_ALL);
326 /* if we only include hilited windows, check if the window is */
327 ok = ok && (nonhilite_windows || ft->demands_attention);
329 /* the window can receive focus somehow */
330 ok = ok && (ft->can_focus || ft->focus_notify);
332 /* the window is not iconic, or we're allowed to go to iconic ones */
333 ok = ok && (iconic_windows || !ft->iconic);
335 /* it's the right type of window */
336 if (dock_windows || desktop_windows)
337 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
338 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
339 /* modal windows are important and can always get focus if they are
340 visible and stuff, so don't change 'ok' based on their type */
341 else if (!ft->modal)
342 /* normal non-helper windows are valid targets */
343 ok = ok &&
344 ((client_normal(ft) && !client_helper(ft))
346 /* helper windows are valid targets if... */
347 (client_helper(ft) &&
348 /* ...a window in its group already has focus and we want to
349 include helper windows ... */
350 ((focus_client && ft->group == focus_client->group &&
351 helper_windows) ||
352 /* ... or if there are no other windows in its group
353 that can be focused instead */
354 !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
356 /* it's not set to skip the taskbar (but this is overridden if the
357 window is modal or if the user asked for this window to be focused,
358 or if the window is iconified (and does not have any parents with
359 which to uniconify it), and it is not used for windows which are
360 hilited, or dialog windows as these need user interaction and should
361 not be long-lasting windows */
362 ok = ok && (!ft->skip_taskbar ||
363 (ft->modal || user_request ||
364 (ft->iconic && !ft->parents) ||
365 ft->demands_attention ||
366 ft->type == OB_CLIENT_TYPE_DIALOG));
368 /* it's not going to just send focus off somewhere else (modal window),
369 unless that modal window is not one of our valid targets, then let
370 you choose this window and bring the modal one here */
372 ObClient *cft = client_focus_target(ft);
373 ok = ok && (ft == cft || !focus_valid_target(cft,
374 screen_desktop,
375 TRUE,
376 iconic_windows,
377 all_desktops,
378 nonhilite_windows,
379 dock_windows,
380 desktop_windows,
381 FALSE));
384 return ok;