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;
36 static void focus_cycle_destroy_notify (ObClient *client, gpointer data);
37 static gboolean focus_target_has_siblings (ObClient *ft,
38 gboolean iconic_windows,
39 gboolean all_desktops);
40 static ObClient *focus_find_directional (ObClient *c,
42 gboolean dock_windows,
43 gboolean desktop_windows);
44 static ObClient *focus_find_directional (ObClient *c,
46 gboolean dock_windows,
47 gboolean desktop_windows);
49 void focus_cycle_startup(gboolean reconfig)
53 client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
56 void focus_cycle_shutdown(gboolean reconfig)
60 client_remove_destroy_notify(focus_cycle_destroy_notify);
63 void focus_cycle_stop()
65 if (focus_cycle_target)
66 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
69 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
71 /* end cycling if the target disappears. CurrentTime is fine, time won't
74 if (focus_cycle_target == client)
75 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
78 /*! Returns if a focus target has valid group siblings that can be cycled
80 static gboolean focus_target_has_siblings(ObClient *ft,
81 gboolean iconic_windows,
82 gboolean all_desktops)
87 if (!ft->group) return FALSE;
89 for (it = ft->group->members; it; it = g_slist_next(it)) {
90 ObClient *c = it->data;
91 /* check that it's not a helper window to avoid infinite recursion */
92 if (c != ft && !client_helper(c) &&
93 focus_cycle_target_valid(c, iconic_windows, all_desktops, FALSE,
102 gboolean focus_cycle_target_valid(ObClient *ft,
103 gboolean iconic_windows,
104 gboolean all_desktops,
105 gboolean dock_windows,
106 gboolean desktop_windows)
110 /* it's on this desktop unless you want all desktops.
112 do this check first because it will usually filter out the most
114 ok = (all_desktops || ft->desktop == screen_desktop ||
115 ft->desktop == DESKTOP_ALL);
117 /* the window can receive focus somehow */
118 ok = ok && (ft->can_focus || ft->focus_notify);
120 /* the window is not iconic, or we're allowed to go to iconic ones */
121 ok = ok && (iconic_windows || !ft->iconic);
123 /* it's the right type of window */
124 if (dock_windows || desktop_windows)
125 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
126 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
128 /* normal non-helper windows are valid targets */
130 ((client_normal(ft) && !client_helper(ft))
132 /* helper windows are valid targets it... */
133 (client_helper(ft) &&
134 /* ...a window in its group already has focus ... */
135 ((focus_client && ft->group == focus_client->group) ||
136 /* ... or if there are no other windows in its group
137 that can be cycled to instead */
138 !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
140 /* it's not set to skip the taskbar (unless it is a type that would be
141 expected to set this hint */
142 ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
143 ft->type == OB_CLIENT_TYPE_DESKTOP ||
144 ft->type == OB_CLIENT_TYPE_TOOLBAR ||
145 ft->type == OB_CLIENT_TYPE_MENU ||
146 ft->type == OB_CLIENT_TYPE_UTILITY) ||
149 /* it's not going to just send fous off somewhere else (modal window) */
150 ok = ok && ft == client_focus_target(ft);
155 void focus_cycle(gboolean forward, gboolean all_desktops,
156 gboolean dock_windows, gboolean desktop_windows,
157 gboolean linear, gboolean interactive,
158 gboolean dialog, gboolean done, gboolean cancel)
160 static ObClient *first = NULL;
161 static ObClient *t = NULL;
162 static GList *order = NULL;
163 GList *it, *start, *list;
168 focus_cycle_target = NULL;
176 if (!first) first = focus_client;
178 if (linear) list = client_list;
179 else list = focus_order;
185 if (!focus_cycle_target) focus_cycle_target = focus_client;
187 start = it = g_list_find(list, focus_cycle_target);
188 if (!start) /* switched desktops or something? */
189 start = it = forward ? g_list_last(list) : g_list_first(list);
190 if (!start) goto done_cycle;
195 if (it == NULL) it = g_list_first(list);
198 if (it == NULL) it = g_list_last(list);
201 if (focus_cycle_target_valid(ft, TRUE,
202 all_desktops, dock_windows,
206 if (ft != focus_cycle_target) { /* prevents flicker */
207 focus_cycle_target = ft;
208 focus_cycle_draw_indicator(ft);
211 /* same arguments as focus_target_valid */
212 focus_cycle_popup_show(ft, TRUE,
213 all_desktops, dock_windows,
216 } else if (ft != focus_cycle_target) {
217 focus_cycle_target = ft;
222 } while (it != start);
225 if (done && focus_cycle_target)
226 client_activate(focus_cycle_target, FALSE, TRUE);
230 focus_cycle_target = NULL;
235 focus_cycle_draw_indicator(NULL);
236 focus_cycle_popup_hide();
242 /* this be mostly ripped from fvwm */
243 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
244 gboolean dock_windows,
245 gboolean desktop_windows)
247 gint my_cx, my_cy, his_cx, his_cy;
250 gint score, best_score;
251 ObClient *best_client, *cur;
257 /* first, find the centre coords of the currently focused window */
258 my_cx = c->frame->area.x + c->frame->area.width / 2;
259 my_cy = c->frame->area.y + c->frame->area.height / 2;
264 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
267 /* the currently selected window isn't interesting */
270 if (!focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
274 /* find the centre coords of this window, from the
275 * currently focused window's point of view */
276 his_cx = (cur->frame->area.x - my_cx)
277 + cur->frame->area.width / 2;
278 his_cy = (cur->frame->area.y - my_cy)
279 + cur->frame->area.height / 2;
281 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
282 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
284 /* Rotate the diagonals 45 degrees counterclockwise.
285 * To do this, multiply the matrix /+h +h\ with the
286 * vector (x y). \-h +h/
287 * h = sqrt(0.5). We can set h := 1 since absolute
288 * distance doesn't matter here. */
289 tx = his_cx + his_cy;
290 his_cy = -his_cx + his_cy;
295 case OB_DIRECTION_NORTH:
296 case OB_DIRECTION_SOUTH:
297 case OB_DIRECTION_NORTHEAST:
298 case OB_DIRECTION_SOUTHWEST:
299 offset = (his_cx < 0) ? -his_cx : his_cx;
300 distance = ((dir == OB_DIRECTION_NORTH ||
301 dir == OB_DIRECTION_NORTHEAST) ?
304 case OB_DIRECTION_EAST:
305 case OB_DIRECTION_WEST:
306 case OB_DIRECTION_SOUTHEAST:
307 case OB_DIRECTION_NORTHWEST:
308 offset = (his_cy < 0) ? -his_cy : his_cy;
309 distance = ((dir == OB_DIRECTION_WEST ||
310 dir == OB_DIRECTION_NORTHWEST) ?
315 /* the target must be in the requested direction */
319 /* Calculate score for this window. The smaller the better. */
320 score = distance + offset;
322 /* windows more than 45 degrees off the direction are
323 * heavily penalized and will only be chosen if nothing
324 * else within a million pixels */
325 if(offset > distance)
328 if(best_score == -1 || score < best_score)
336 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
337 gboolean desktop_windows, gboolean interactive,
338 gboolean dialog, gboolean done, gboolean cancel)
340 static ObClient *first = NULL;
347 focus_cycle_target = NULL;
355 if (!first) first = focus_client;
356 if (!focus_cycle_target) focus_cycle_target = focus_client;
358 if (focus_cycle_target)
359 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
364 for (it = focus_order; it; it = g_list_next(it))
365 if (focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
371 if (ft != focus_cycle_target) {/* prevents flicker */
372 focus_cycle_target = ft;
373 focus_cycle_draw_indicator(ft);
376 if (focus_cycle_target && dialog) {
377 /* same arguments as focus_target_valid */
378 focus_cycle_popup_single_show(focus_cycle_target,
379 FALSE, FALSE, dock_windows,
385 if (done && focus_cycle_target)
386 client_activate(focus_cycle_target, FALSE, TRUE);
389 focus_cycle_target = NULL;
391 focus_cycle_draw_indicator(NULL);
392 focus_cycle_popup_single_hide();
397 void focus_order_add_new(ObClient *c)
400 focus_order_to_top(c);
402 g_assert(!g_list_find(focus_order, c));
403 /* if there are any iconic windows, put this above them in the order,
404 but if there are not, then put it under the currently focused one */
405 if (focus_order && ((ObClient*)focus_order->data)->iconic)
406 focus_order = g_list_insert(focus_order, c, 0);
408 focus_order = g_list_insert(focus_order, c, 1);