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 void focus_cycle_destroy_notify (ObClient *client, gpointer data);
40 static gboolean focus_target_has_siblings (ObClient *ft,
41 gboolean iconic_windows,
42 gboolean all_desktops);
43 static ObClient *focus_find_directional (ObClient *c,
45 gboolean dock_windows,
46 gboolean desktop_windows);
47 static ObClient *focus_find_directional (ObClient *c,
49 gboolean dock_windows,
50 gboolean desktop_windows);
52 void focus_cycle_startup(gboolean reconfig)
56 client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
59 void focus_cycle_shutdown(gboolean reconfig)
63 client_remove_destroy_notify(focus_cycle_destroy_notify);
66 void focus_cycle_stop()
68 if (focus_cycle_target) {
69 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
70 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
74 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
76 /* end cycling if the target disappears. CurrentTime is fine, time won't
78 if (focus_cycle_target == client)
82 /*! Returns if a focus target has valid group siblings that can be cycled
84 static gboolean focus_target_has_siblings(ObClient *ft,
85 gboolean iconic_windows,
86 gboolean all_desktops)
91 if (!ft->group) return FALSE;
93 for (it = ft->group->members; it; it = g_slist_next(it)) {
94 ObClient *c = it->data;
95 /* check that it's not a helper window to avoid infinite recursion */
96 if (c != ft && !client_helper(c) &&
97 focus_cycle_target_valid(c, iconic_windows, all_desktops, FALSE,
106 gboolean focus_cycle_target_valid(ObClient *ft,
107 gboolean iconic_windows,
108 gboolean all_desktops,
109 gboolean dock_windows,
110 gboolean desktop_windows)
114 /* it's on this desktop unless you want all desktops.
116 do this check first because it will usually filter out the most
118 ok = (all_desktops || ft->desktop == screen_desktop ||
119 ft->desktop == DESKTOP_ALL);
121 /* the window can receive focus somehow */
122 ok = ok && (ft->can_focus || ft->focus_notify);
124 /* the window is not iconic, or we're allowed to go to iconic ones */
125 ok = ok && (iconic_windows || !ft->iconic);
127 /* it's the right type of window */
128 if (dock_windows || desktop_windows)
129 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
130 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
131 /* modal windows are important and can always get focus if they are
132 visible and stuff, so don't change 'ok' based on their type */
134 /* normal non-helper windows are valid targets */
136 ((client_normal(ft) && !client_helper(ft))
138 /* helper windows are valid targets it... */
139 (client_helper(ft) &&
140 /* ...a window in its group already has focus ... */
141 ((focus_client && ft->group == focus_client->group) ||
142 /* ... or if there are no other windows in its group
143 that can be cycled to instead */
144 !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
146 /* it's not set to skip the taskbar (unless it is a type that would be
147 expected to set this hint, or modal) */
148 ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
149 ft->type == OB_CLIENT_TYPE_DESKTOP ||
150 ft->type == OB_CLIENT_TYPE_TOOLBAR ||
151 ft->type == OB_CLIENT_TYPE_MENU ||
152 ft->type == OB_CLIENT_TYPE_UTILITY) ||
156 /* it's not going to just send fous off somewhere else (modal window) */
157 ok = ok && ft == client_focus_target(ft);
162 void focus_cycle(gboolean forward, gboolean all_desktops,
163 gboolean dock_windows, gboolean desktop_windows,
164 gboolean linear, gboolean interactive,
165 gboolean dialog, gboolean done, gboolean cancel)
167 static ObClient *first = NULL;
168 static ObClient *t = NULL;
169 static GList *order = NULL;
170 GList *it, *start, *list;
175 focus_cycle_target = NULL;
183 if (!first) first = focus_client;
185 if (linear) list = client_list;
186 else list = focus_order;
194 if (focus_cycle_target == NULL) {
195 focus_cycle_all_desktops = FALSE;
196 focus_cycle_dock_windows = dock_windows;
197 focus_cycle_desktop_windows = desktop_windows;
198 focus_cycle_target = focus_client;
201 start = it = g_list_find(list, focus_cycle_target);
202 if (!start) /* switched desktops or something? */
203 start = it = forward ? g_list_last(list) : g_list_first(list);
204 if (!start) goto done_cycle;
209 if (it == NULL) it = g_list_first(list);
212 if (it == NULL) it = g_list_last(list);
215 if (focus_cycle_target_valid(ft, TRUE,
216 focus_cycle_all_desktops,
217 focus_cycle_dock_windows,
218 focus_cycle_desktop_windows))
221 if (ft != focus_cycle_target) { /* prevents flicker */
222 focus_cycle_target = ft;
223 focus_cycle_draw_indicator(ft);
226 /* same arguments as focus_target_valid */
227 focus_cycle_popup_show(ft, TRUE,
228 focus_cycle_all_desktops,
229 focus_cycle_dock_windows,
230 focus_cycle_desktop_windows);
232 } else if (ft != focus_cycle_target) {
233 focus_cycle_target = ft;
238 } while (it != start);
241 if (done && focus_cycle_target)
242 client_activate(focus_cycle_target, FALSE, TRUE);
246 focus_cycle_target = NULL;
251 focus_cycle_draw_indicator(NULL);
252 focus_cycle_popup_hide();
258 /* this be mostly ripped from fvwm */
259 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
260 gboolean dock_windows,
261 gboolean desktop_windows)
263 gint my_cx, my_cy, his_cx, his_cy;
266 gint score, best_score;
267 ObClient *best_client, *cur;
273 /* first, find the centre coords of the currently focused window */
274 my_cx = c->frame->area.x + c->frame->area.width / 2;
275 my_cy = c->frame->area.y + c->frame->area.height / 2;
280 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
283 /* the currently selected window isn't interesting */
286 if (!focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
290 /* find the centre coords of this window, from the
291 * currently focused window's point of view */
292 his_cx = (cur->frame->area.x - my_cx)
293 + cur->frame->area.width / 2;
294 his_cy = (cur->frame->area.y - my_cy)
295 + cur->frame->area.height / 2;
297 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
298 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
300 /* Rotate the diagonals 45 degrees counterclockwise.
301 * To do this, multiply the matrix /+h +h\ with the
302 * vector (x y). \-h +h/
303 * h = sqrt(0.5). We can set h := 1 since absolute
304 * distance doesn't matter here. */
305 tx = his_cx + his_cy;
306 his_cy = -his_cx + his_cy;
311 case OB_DIRECTION_NORTH:
312 case OB_DIRECTION_SOUTH:
313 case OB_DIRECTION_NORTHEAST:
314 case OB_DIRECTION_SOUTHWEST:
315 offset = (his_cx < 0) ? -his_cx : his_cx;
316 distance = ((dir == OB_DIRECTION_NORTH ||
317 dir == OB_DIRECTION_NORTHEAST) ?
320 case OB_DIRECTION_EAST:
321 case OB_DIRECTION_WEST:
322 case OB_DIRECTION_SOUTHEAST:
323 case OB_DIRECTION_NORTHWEST:
324 offset = (his_cy < 0) ? -his_cy : his_cy;
325 distance = ((dir == OB_DIRECTION_WEST ||
326 dir == OB_DIRECTION_NORTHWEST) ?
331 /* the target must be in the requested direction */
335 /* Calculate score for this window. The smaller the better. */
336 score = distance + offset;
338 /* windows more than 45 degrees off the direction are
339 * heavily penalized and will only be chosen if nothing
340 * else within a million pixels */
341 if(offset > distance)
344 if(best_score == -1 || score < best_score)
352 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
353 gboolean desktop_windows, gboolean interactive,
354 gboolean dialog, gboolean done, gboolean cancel)
356 static ObClient *first = NULL;
363 focus_cycle_target = NULL;
371 if (focus_cycle_target == NULL) {
372 focus_cycle_all_desktops = FALSE;
373 focus_cycle_dock_windows = dock_windows;
374 focus_cycle_desktop_windows = desktop_windows;
375 focus_cycle_target = focus_client;
378 if (!first) first = focus_client;
380 if (focus_cycle_target)
381 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
386 for (it = focus_order; it; it = g_list_next(it))
387 if (focus_cycle_target_valid(it->data, FALSE, FALSE,
388 focus_cycle_dock_windows,
389 focus_cycle_desktop_windows))
394 if (ft != focus_cycle_target) {/* prevents flicker */
395 focus_cycle_target = ft;
396 focus_cycle_draw_indicator(ft);
399 if (focus_cycle_target && dialog) {
400 /* same arguments as focus_target_valid */
401 focus_cycle_popup_single_show(focus_cycle_target,
403 focus_cycle_dock_windows,
404 focus_cycle_desktop_windows);
409 if (done && focus_cycle_target)
410 client_activate(focus_cycle_target, FALSE, TRUE);
413 focus_cycle_target = NULL;
415 focus_cycle_draw_indicator(NULL);
416 focus_cycle_popup_single_hide();