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.
27 #include "focus_cycle.h"
37 #define FOCUS_INDICATOR_WIDTH 6
39 ObClient
*focus_client
= NULL
;
40 GList
*focus_order
= NULL
;
42 void focus_startup(gboolean reconfig
)
46 /* start with nothing focused */
50 void focus_shutdown(gboolean reconfig
)
54 /* reset focus to root */
55 XSetInputFocus(obt_display
, PointerRoot
, RevertToNone
, CurrentTime
);
58 static void push_to_top(ObClient
*client
)
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
65 if (client
->modal
&& (p
= client_direct_parent(client
)))
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
)
76 ob_debug_type(OB_DEBUG_FOCUS
,
77 "focus_set_client 0x%lx", client
? client
->window
: 0);
79 if (focus_client
== client
)
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
;
89 /* move to the top of the list */
91 /* remove hiliting from the window when it gets focused */
92 client_hilite(client
, FALSE
);
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
);
102 static ObClient
* focus_fallback_target(gboolean allow_refocus
,
103 gboolean allow_pointer
,
104 gboolean allow_omnipresent
,
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
) &&
117 ob_debug_type(OB_DEBUG_FOCUS
, "found in pointer stuff");
121 ob_debug_type(OB_DEBUG_FOCUS
, "trying the focus order");
122 for (it
= focus_order
; it
; it
= g_list_next(it
)) {
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
128 2. it is a valid auto-focus target
131 if ((allow_omnipresent
|| c
->desktop
== screen_desktop
) &&
132 focus_valid_target(c
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
) &&
134 (allow_refocus
|| client_focus_target(c
) != old
) &&
137 ob_debug_type(OB_DEBUG_FOCUS
, "found in focus order");
142 ob_debug_type(OB_DEBUG_FOCUS
, "trying a desktop window");
143 for (it
= focus_order
; it
; it
= g_list_next(it
)) {
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)
152 if (focus_valid_target(c
, TRUE
, FALSE
, FALSE
, FALSE
, TRUE
, FALSE
) &&
153 (allow_refocus
|| client_focus_target(c
) != old
) &&
156 ob_debug_type(OB_DEBUG_FOCUS
, "found a desktop window");
164 ObClient
* focus_fallback(gboolean allow_refocus
, gboolean allow_pointer
,
165 gboolean allow_omnipresent
, gboolean focus_lost
)
168 ObClient
*old
= focus_client
;
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. */
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);
184 void focus_nothing(void)
186 /* nothing is focused, update the colormap and _the root property_ */
187 focus_set_client(NULL
);
189 /* when nothing will be focused, send focus to the backup target */
190 XSetInputFocus(obt_display
, screen_support_win
, RevertToPointerRoot
,
194 void focus_order_add_new(ObClient
*c
)
197 focus_order_to_top(c
);
199 g_assert(!g_list_find(focus_order
, c
));
200 /* if there are any iconic windows, put this above them in the order,
201 but if there are not, then put it under the currently focused one */
202 if (focus_order
&& ((ObClient
*)focus_order
->data
)->iconic
)
203 focus_order
= g_list_insert(focus_order
, c
, 0);
205 focus_order
= g_list_insert(focus_order
, c
, 1);
208 /* in the middle of cycling..? kill it. */
212 void focus_order_remove(ObClient
*c
)
214 focus_order
= g_list_remove(focus_order
, c
);
216 /* in the middle of cycling..? kill it. */
220 void focus_order_to_top(ObClient
*c
)
222 focus_order
= g_list_remove(focus_order
, c
);
224 focus_order
= g_list_prepend(focus_order
, c
);
228 /* insert before first iconic window */
229 for (it
= focus_order
;
230 it
&& !((ObClient
*)it
->data
)->iconic
; it
= g_list_next(it
));
231 focus_order
= g_list_insert_before(focus_order
, it
, c
);
235 void focus_order_to_bottom(ObClient
*c
)
237 focus_order
= g_list_remove(focus_order
, c
);
239 focus_order
= g_list_append(focus_order
, c
);
243 /* insert before first iconic window */
244 for (it
= focus_order
;
245 it
&& !((ObClient
*)it
->data
)->iconic
; it
= g_list_next(it
));
246 focus_order
= g_list_insert_before(focus_order
, it
, c
);
250 ObClient
*focus_order_find_first(guint desktop
)
253 for (it
= focus_order
; it
; it
= g_list_next(it
)) {
254 ObClient
*c
= it
->data
;
255 if (c
->desktop
== desktop
|| c
->desktop
== DESKTOP_ALL
)
261 /*! Returns if a focus target has valid group siblings that can be cycled
263 static gboolean
focus_target_has_siblings(ObClient
*ft
,
264 gboolean iconic_windows
,
265 gboolean all_desktops
)
270 if (!ft
->group
) return FALSE
;
272 for (it
= ft
->group
->members
; it
; it
= g_slist_next(it
)) {
273 ObClient
*c
= it
->data
;
274 /* check that it's not a helper window to avoid infinite recursion */
275 if (c
!= ft
&& c
->type
== OB_CLIENT_TYPE_NORMAL
&&
276 focus_valid_target(c
, TRUE
, iconic_windows
, all_desktops
,
277 FALSE
, FALSE
, FALSE
))
285 gboolean
focus_valid_target(ObClient
*ft
,
286 gboolean helper_windows
,
287 gboolean iconic_windows
,
288 gboolean all_desktops
,
289 gboolean dock_windows
,
290 gboolean desktop_windows
,
291 gboolean user_request
)
295 /* it's on this desktop unless you want all desktops.
297 do this check first because it will usually filter out the most
299 ok
= (all_desktops
|| ft
->desktop
== screen_desktop
||
300 ft
->desktop
== DESKTOP_ALL
);
302 /* the window can receive focus somehow */
303 ok
= ok
&& (ft
->can_focus
|| ft
->focus_notify
);
305 /* the window is not iconic, or we're allowed to go to iconic ones */
306 ok
= ok
&& (iconic_windows
|| !ft
->iconic
);
308 /* it's the right type of window */
309 if (dock_windows
|| desktop_windows
)
310 ok
= ok
&& ((dock_windows
&& ft
->type
== OB_CLIENT_TYPE_DOCK
) ||
311 (desktop_windows
&& ft
->type
== OB_CLIENT_TYPE_DESKTOP
));
312 /* modal windows are important and can always get focus if they are
313 visible and stuff, so don't change 'ok' based on their type */
315 /* normal non-helper windows are valid targets */
317 ((client_normal(ft
) && !client_helper(ft
))
319 /* helper windows are valid targets if... */
320 (client_helper(ft
) &&
321 /* ...a window in its group already has focus and we want to
322 include helper windows ... */
323 ((focus_client
&& ft
->group
== focus_client
->group
&&
325 /* ... or if there are no other windows in its group
326 that can be focused instead */
327 !focus_target_has_siblings(ft
, iconic_windows
, all_desktops
))));
329 /* it's not set to skip the taskbar (but this only applies to normal typed
330 windows, and is overridden if the window is modal or if the user asked
331 for this window to be focused, or if the window is iconified) */
332 ok
= ok
&& (ft
->type
!= OB_CLIENT_TYPE_NORMAL
||
338 /* it's not going to just send focus off somewhere else (modal window),
339 unless that modal window is not one of our valid targets, then let
340 you choose this window and bring the modal one here */
342 ObClient
*cft
= client_focus_target(ft
);
343 ok
= ok
&& (ft
== cft
|| !focus_valid_target(cft
,