1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus_cycle.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 "focus_cycle.h"
21 #include "focus_cycle_indicator.h"
22 #include "focus_cycle_popup.h"
34 ObClient *focus_cycle_target = NULL;
35 static gboolean focus_cycle_all_desktops;
36 static gboolean focus_cycle_dock_windows;
37 static gboolean focus_cycle_desktop_windows;
39 static gboolean focus_target_has_siblings (ObClient *ft,
40 gboolean iconic_windows,
41 gboolean all_desktops);
42 static ObClient *focus_find_directional (ObClient *c,
44 gboolean dock_windows,
45 gboolean desktop_windows);
46 static ObClient *focus_find_directional (ObClient *c,
48 gboolean dock_windows,
49 gboolean desktop_windows);
51 void focus_cycle_startup(gboolean reconfig)
56 void focus_cycle_shutdown(gboolean reconfig)
61 void focus_cycle_stop()
63 if (focus_cycle_target) {
64 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
65 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
69 /*! Returns if a focus target has valid group siblings that can be cycled
71 static gboolean focus_target_has_siblings(ObClient *ft,
72 gboolean iconic_windows,
73 gboolean all_desktops)
78 if (!ft->group) return FALSE;
80 for (it = ft->group->members; it; it = g_slist_next(it)) {
81 ObClient *c = it->data;
82 /* check that it's not a helper window to avoid infinite recursion */
83 if (c != ft && !client_helper(c) &&
84 focus_cycle_target_valid(c, iconic_windows, all_desktops, FALSE,
93 gboolean focus_cycle_target_valid(ObClient *ft,
94 gboolean iconic_windows,
95 gboolean all_desktops,
96 gboolean dock_windows,
97 gboolean desktop_windows)
101 /* it's on this desktop unless you want all desktops.
103 do this check first because it will usually filter out the most
105 ok = (all_desktops || ft->desktop == screen_desktop ||
106 ft->desktop == DESKTOP_ALL);
108 /* the window can receive focus somehow */
109 ok = ok && (ft->can_focus || ft->focus_notify);
111 /* the window is not iconic, or we're allowed to go to iconic ones */
112 ok = ok && (iconic_windows || !ft->iconic);
114 /* it's the right type of window */
115 if (dock_windows || desktop_windows)
116 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
117 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
118 /* modal windows are important and can always get focus if they are
119 visible and stuff, so don't change 'ok' based on their type */
121 /* normal non-helper windows are valid targets */
123 ((client_normal(ft) && !client_helper(ft))
125 /* helper windows are valid targets it... */
126 (client_helper(ft) &&
127 /* ...a window in its group already has focus ... */
128 ((focus_client && ft->group == focus_client->group) ||
129 /* ... or if there are no other windows in its group
130 that can be cycled to instead */
131 !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
133 /* it's not set to skip the taskbar (unless it is a type that would be
134 expected to set this hint, or modal) */
135 ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
136 ft->type == OB_CLIENT_TYPE_DESKTOP ||
137 ft->type == OB_CLIENT_TYPE_TOOLBAR ||
138 ft->type == OB_CLIENT_TYPE_MENU ||
139 ft->type == OB_CLIENT_TYPE_UTILITY) ||
143 /* it's not going to just send fous off somewhere else (modal window) */
144 ok = ok && ft == client_focus_target(ft);
149 void focus_cycle(gboolean forward, gboolean all_desktops,
150 gboolean dock_windows, gboolean desktop_windows,
151 gboolean linear, gboolean interactive,
152 gboolean dialog, gboolean done, gboolean cancel)
154 static ObClient *first = NULL;
155 static ObClient *t = NULL;
156 static GList *order = NULL;
157 GList *it, *start, *list;
162 focus_cycle_target = NULL;
170 if (!first) first = focus_client;
172 if (linear) list = client_list;
173 else list = focus_order;
181 if (focus_cycle_target == NULL) {
182 focus_cycle_all_desktops = FALSE;
183 focus_cycle_dock_windows = dock_windows;
184 focus_cycle_desktop_windows = desktop_windows;
185 focus_cycle_target = focus_client;
188 start = it = g_list_find(list, focus_cycle_target);
189 if (!start) /* switched desktops or something? */
190 start = it = forward ? g_list_last(list) : g_list_first(list);
191 if (!start) goto done_cycle;
196 if (it == NULL) it = g_list_first(list);
199 if (it == NULL) it = g_list_last(list);
202 if (focus_cycle_target_valid(ft, TRUE,
203 focus_cycle_all_desktops,
204 focus_cycle_dock_windows,
205 focus_cycle_desktop_windows))
208 if (ft != focus_cycle_target) { /* prevents flicker */
209 focus_cycle_target = ft;
210 focus_cycle_draw_indicator(ft);
213 /* same arguments as focus_target_valid */
214 focus_cycle_popup_show(ft, TRUE,
215 focus_cycle_all_desktops,
216 focus_cycle_dock_windows,
217 focus_cycle_desktop_windows);
219 } else if (ft != focus_cycle_target) {
220 focus_cycle_target = ft;
225 } while (it != start);
228 if (done && focus_cycle_target)
229 client_activate(focus_cycle_target, FALSE, TRUE);
233 focus_cycle_target = NULL;
238 focus_cycle_draw_indicator(NULL);
239 focus_cycle_popup_hide();
245 /* this be mostly ripped from fvwm */
246 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
247 gboolean dock_windows,
248 gboolean desktop_windows)
250 gint my_cx, my_cy, his_cx, his_cy;
253 gint score, best_score;
254 ObClient *best_client, *cur;
260 /* first, find the centre coords of the currently focused window */
261 my_cx = c->frame->area.x + c->frame->area.width / 2;
262 my_cy = c->frame->area.y + c->frame->area.height / 2;
267 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
270 /* the currently selected window isn't interesting */
273 if (!focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
277 /* find the centre coords of this window, from the
278 * currently focused window's point of view */
279 his_cx = (cur->frame->area.x - my_cx)
280 + cur->frame->area.width / 2;
281 his_cy = (cur->frame->area.y - my_cy)
282 + cur->frame->area.height / 2;
284 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
285 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
287 /* Rotate the diagonals 45 degrees counterclockwise.
288 * To do this, multiply the matrix /+h +h\ with the
289 * vector (x y). \-h +h/
290 * h = sqrt(0.5). We can set h := 1 since absolute
291 * distance doesn't matter here. */
292 tx = his_cx + his_cy;
293 his_cy = -his_cx + his_cy;
298 case OB_DIRECTION_NORTH:
299 case OB_DIRECTION_SOUTH:
300 case OB_DIRECTION_NORTHEAST:
301 case OB_DIRECTION_SOUTHWEST:
302 offset = (his_cx < 0) ? -his_cx : his_cx;
303 distance = ((dir == OB_DIRECTION_NORTH ||
304 dir == OB_DIRECTION_NORTHEAST) ?
307 case OB_DIRECTION_EAST:
308 case OB_DIRECTION_WEST:
309 case OB_DIRECTION_SOUTHEAST:
310 case OB_DIRECTION_NORTHWEST:
311 offset = (his_cy < 0) ? -his_cy : his_cy;
312 distance = ((dir == OB_DIRECTION_WEST ||
313 dir == OB_DIRECTION_NORTHWEST) ?
318 /* the target must be in the requested direction */
322 /* Calculate score for this window. The smaller the better. */
323 score = distance + offset;
325 /* windows more than 45 degrees off the direction are
326 * heavily penalized and will only be chosen if nothing
327 * else within a million pixels */
328 if(offset > distance)
331 if(best_score == -1 || score < best_score)
339 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
340 gboolean desktop_windows, gboolean interactive,
341 gboolean dialog, gboolean done, gboolean cancel)
343 static ObClient *first = NULL;
350 focus_cycle_target = NULL;
358 if (focus_cycle_target == NULL) {
359 focus_cycle_all_desktops = FALSE;
360 focus_cycle_dock_windows = dock_windows;
361 focus_cycle_desktop_windows = desktop_windows;
362 focus_cycle_target = focus_client;
365 if (!first) first = focus_client;
367 if (focus_cycle_target)
368 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
373 for (it = focus_order; it; it = g_list_next(it))
374 if (focus_cycle_target_valid(it->data, FALSE, FALSE,
375 focus_cycle_dock_windows,
376 focus_cycle_desktop_windows))
381 if (ft != focus_cycle_target) {/* prevents flicker */
382 focus_cycle_target = ft;
383 focus_cycle_draw_indicator(ft);
386 if (focus_cycle_target && dialog) {
387 /* same arguments as focus_target_valid */
388 focus_cycle_popup_single_show(focus_cycle_target,
390 focus_cycle_dock_windows,
391 focus_cycle_desktop_windows);
396 if (done && focus_cycle_target)
397 client_activate(focus_cycle_target, FALSE, TRUE);
400 focus_cycle_target = NULL;
402 focus_cycle_draw_indicator(NULL);
403 focus_cycle_popup_single_hide();