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"
38 ObClient *focus_cycle_target = NULL;
39 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
40 static gboolean focus_cycle_iconic_windows;
41 static gboolean focus_cycle_all_desktops;
42 static gboolean focus_cycle_nonhilite_windows;
43 static gboolean focus_cycle_dock_windows;
44 static gboolean focus_cycle_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_addremove(ObClient *c, gboolean redraw)
63 if (!focus_cycle_type)
66 if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
67 if (c && focus_cycle_target == c) {
68 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
72 else if (c && redraw) {
75 v = focus_cycle_valid(c);
76 s = focus_cycle_popup_is_showing(c);
79 focus_cycle_reorder();
82 focus_cycle_reorder();
86 void focus_cycle_reorder()
88 if (focus_cycle_type == OB_CYCLE_NORMAL) {
89 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
91 focus_cycle_update_indicator(focus_cycle_target);
92 if (!focus_cycle_target)
93 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
94 TRUE, TRUE, TRUE, TRUE, TRUE);
98 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
99 gboolean nonhilite_windows,
100 gboolean dock_windows, gboolean desktop_windows,
101 gboolean linear, gboolean interactive,
102 gboolean showbar, ObFocusCyclePopupMode mode,
103 gboolean done, gboolean cancel)
105 static GList *order = NULL;
106 GList *it, *start, *list;
108 ObClient *ret = NULL;
112 focus_cycle_target = NULL;
120 if (linear) list = client_list;
121 else list = focus_order;
128 if (focus_cycle_target == NULL) {
129 focus_cycle_iconic_windows = TRUE;
130 focus_cycle_all_desktops = all_desktops;
131 focus_cycle_nonhilite_windows = nonhilite_windows;
132 focus_cycle_dock_windows = dock_windows;
133 focus_cycle_desktop_windows = desktop_windows;
134 start = it = g_list_find(list, focus_client);
136 start = it = g_list_find(list, focus_cycle_target);
138 if (!start) /* switched desktops or something? */
139 start = it = forward ? g_list_last(list) : g_list_first(list);
140 if (!start) goto done_cycle;
145 if (it == NULL) it = g_list_first(list);
148 if (it == NULL) it = g_list_last(list);
151 if (focus_cycle_valid(ft)) {
153 if (ft != focus_cycle_target) { /* prevents flicker */
154 focus_cycle_target = ft;
155 focus_cycle_type = OB_CYCLE_NORMAL;
156 focus_cycle_draw_indicator(showbar ? ft : NULL);
158 /* same arguments as focus_target_valid */
159 focus_cycle_popup_show(ft, mode);
160 return focus_cycle_target;
161 } else if (ft != focus_cycle_target) {
162 focus_cycle_target = ft;
163 focus_cycle_type = OB_CYCLE_NORMAL;
168 } while (it != start);
171 if (done && !cancel) ret = focus_cycle_target;
173 focus_cycle_target = NULL;
174 focus_cycle_type = OB_CYCLE_NONE;
179 focus_cycle_draw_indicator(NULL);
180 focus_cycle_popup_hide();
186 /* this be mostly ripped from fvwm */
187 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
188 gboolean dock_windows,
189 gboolean desktop_windows)
191 gint my_cx, my_cy, his_cx, his_cy;
194 gint score, best_score;
195 ObClient *best_client, *cur;
201 /* first, find the centre coords of the currently focused window */
202 my_cx = c->frame->area.x + c->frame->area.width / 2;
203 my_cy = c->frame->area.y + c->frame->area.height / 2;
208 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
211 /* the currently selected window isn't interesting */
214 if (!focus_cycle_valid(it->data))
217 /* find the centre coords of this window, from the
218 * currently focused window's point of view */
219 his_cx = (cur->frame->area.x - my_cx)
220 + cur->frame->area.width / 2;
221 his_cy = (cur->frame->area.y - my_cy)
222 + cur->frame->area.height / 2;
224 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
225 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
228 /* Rotate the diagonals 45 degrees counterclockwise.
229 * To do this, multiply the matrix /+h +h\ with the
230 * vector (x y). \-h +h/
231 * h = sqrt(0.5). We can set h := 1 since absolute
232 * distance doesn't matter here. */
233 tx = his_cx + his_cy;
234 his_cy = -his_cx + his_cy;
239 case OB_DIRECTION_NORTH:
240 case OB_DIRECTION_SOUTH:
241 case OB_DIRECTION_NORTHEAST:
242 case OB_DIRECTION_SOUTHWEST:
243 offset = (his_cx < 0) ? -his_cx : his_cx;
244 distance = ((dir == OB_DIRECTION_NORTH ||
245 dir == OB_DIRECTION_NORTHEAST) ?
248 case OB_DIRECTION_EAST:
249 case OB_DIRECTION_WEST:
250 case OB_DIRECTION_SOUTHEAST:
251 case OB_DIRECTION_NORTHWEST:
252 offset = (his_cy < 0) ? -his_cy : his_cy;
253 distance = ((dir == OB_DIRECTION_WEST ||
254 dir == OB_DIRECTION_NORTHWEST) ?
259 /* the target must be in the requested direction */
263 /* Calculate score for this window. The smaller the better. */
264 score = distance + offset;
266 /* windows more than 45 degrees off the direction are
267 * heavily penalized and will only be chosen if nothing
268 * else within a million pixels */
269 if (offset > distance)
272 if (best_score == -1 || score < best_score) {
281 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
282 gboolean desktop_windows,
283 gboolean interactive,
284 gboolean showbar, gboolean dialog,
285 gboolean done, gboolean cancel)
287 static ObClient *first = NULL;
289 ObClient *ret = NULL;
292 focus_cycle_target = NULL;
294 } else if (done && interactive)
300 if (focus_cycle_target == NULL) {
301 focus_cycle_iconic_windows = FALSE;
302 focus_cycle_all_desktops = FALSE;
303 focus_cycle_nonhilite_windows = TRUE;
304 focus_cycle_dock_windows = dock_windows;
305 focus_cycle_desktop_windows = desktop_windows;
308 if (!first) first = focus_client;
310 if (focus_cycle_target)
311 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
314 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
318 for (it = focus_order; it; it = g_list_next(it))
319 if (focus_cycle_valid(it->data)) {
325 if (ft && ft != focus_cycle_target) {/* prevents flicker */
326 focus_cycle_target = ft;
327 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
330 focus_cycle_draw_indicator(showbar ? ft : NULL);
332 if (focus_cycle_target && dialog)
333 /* same arguments as focus_target_valid */
334 focus_cycle_popup_single_show(focus_cycle_target);
335 return focus_cycle_target;
338 if (done && !cancel) ret = focus_cycle_target;
341 focus_cycle_target = NULL;
342 focus_cycle_type = OB_CYCLE_NONE;
344 focus_cycle_draw_indicator(NULL);
345 focus_cycle_popup_single_hide();
350 gboolean focus_cycle_valid(struct _ObClient *client)
352 return focus_valid_target(client, screen_desktop, TRUE,
353 focus_cycle_iconic_windows,
354 focus_cycle_all_desktops,
355 focus_cycle_nonhilite_windows,
356 focus_cycle_dock_windows,
357 focus_cycle_desktop_windows,