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_linear;
41 static gboolean focus_cycle_iconic_windows;
42 static gboolean focus_cycle_all_desktops;
43 static gboolean focus_cycle_nonhilite_windows;
44 static gboolean focus_cycle_dock_windows;
45 static gboolean focus_cycle_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)
57 void focus_cycle_shutdown(gboolean reconfig)
62 void focus_cycle_addremove(ObClient *c, gboolean redraw)
64 if (!focus_cycle_type)
67 if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
68 if (c && focus_cycle_target == c) {
69 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
73 else if (c && redraw) {
76 v = focus_cycle_valid(c);
77 s = focus_cycle_popup_is_showing(c) || c == focus_cycle_target;
80 focus_cycle_reorder();
83 focus_cycle_reorder();
87 void focus_cycle_reorder()
89 if (focus_cycle_type == OB_CYCLE_NORMAL) {
90 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
93 focus_cycle_update_indicator(focus_cycle_target);
94 if (!focus_cycle_target)
95 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
96 TRUE, OB_FOCUS_CYCLE_POPUP_MODE_NONE,
101 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
102 gboolean nonhilite_windows,
103 gboolean dock_windows, gboolean desktop_windows,
104 gboolean linear, gboolean showbar,
105 ObFocusCyclePopupMode mode,
106 gboolean done, gboolean cancel)
108 static GList *order = NULL;
109 GList *it, *start, *list;
111 ObClient *ret = NULL;
114 focus_cycle_target = NULL;
122 if (linear) list = client_list;
123 else list = focus_order;
125 if (focus_cycle_target == NULL) {
126 focus_cycle_linear = linear;
127 focus_cycle_iconic_windows = TRUE;
128 focus_cycle_all_desktops = all_desktops;
129 focus_cycle_nonhilite_windows = nonhilite_windows;
130 focus_cycle_dock_windows = dock_windows;
131 focus_cycle_desktop_windows = desktop_windows;
132 start = it = g_list_find(list, focus_client);
134 start = it = g_list_find(list, focus_cycle_target);
136 if (!start) /* switched desktops or something? */
137 start = it = forward ? g_list_last(list) : g_list_first(list);
138 if (!start) goto done_cycle;
143 if (it == NULL) it = g_list_first(list);
146 if (it == NULL) it = g_list_last(list);
149 if (focus_cycle_valid(ft)) {
150 if (ft != focus_cycle_target) { /* prevents flicker */
151 focus_cycle_target = ft;
152 focus_cycle_type = OB_CYCLE_NORMAL;
153 focus_cycle_draw_indicator(showbar ? ft : NULL);
155 /* same arguments as focus_target_valid */
156 focus_cycle_popup_show(ft, mode, focus_cycle_linear);
157 return focus_cycle_target;
159 } while (it != start);
162 if (done && !cancel) ret = focus_cycle_target;
164 focus_cycle_target = NULL;
165 focus_cycle_type = OB_CYCLE_NONE;
169 focus_cycle_draw_indicator(NULL);
170 focus_cycle_popup_hide();
175 /* this be mostly ripped from fvwm */
176 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
177 gboolean dock_windows,
178 gboolean desktop_windows)
180 gint my_cx, my_cy, his_cx, his_cy;
183 gint score, best_score;
184 ObClient *best_client, *cur;
190 /* first, find the centre coords of the currently focused window */
191 my_cx = c->frame->area.x + c->frame->area.width / 2;
192 my_cy = c->frame->area.y + c->frame->area.height / 2;
197 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
200 /* the currently selected window isn't interesting */
203 if (!focus_cycle_valid(it->data))
206 /* find the centre coords of this window, from the
207 * currently focused window's point of view */
208 his_cx = (cur->frame->area.x - my_cx)
209 + cur->frame->area.width / 2;
210 his_cy = (cur->frame->area.y - my_cy)
211 + cur->frame->area.height / 2;
213 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
214 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
217 /* Rotate the diagonals 45 degrees counterclockwise.
218 * To do this, multiply the matrix /+h +h\ with the
219 * vector (x y). \-h +h/
220 * h = sqrt(0.5). We can set h := 1 since absolute
221 * distance doesn't matter here. */
222 tx = his_cx + his_cy;
223 his_cy = -his_cx + his_cy;
228 case OB_DIRECTION_NORTH:
229 case OB_DIRECTION_SOUTH:
230 case OB_DIRECTION_NORTHEAST:
231 case OB_DIRECTION_SOUTHWEST:
232 offset = (his_cx < 0) ? -his_cx : his_cx;
233 distance = ((dir == OB_DIRECTION_NORTH ||
234 dir == OB_DIRECTION_NORTHEAST) ?
237 case OB_DIRECTION_EAST:
238 case OB_DIRECTION_WEST:
239 case OB_DIRECTION_SOUTHEAST:
240 case OB_DIRECTION_NORTHWEST:
241 offset = (his_cy < 0) ? -his_cy : his_cy;
242 distance = ((dir == OB_DIRECTION_WEST ||
243 dir == OB_DIRECTION_NORTHWEST) ?
248 /* the target must be in the requested direction */
252 /* Calculate score for this window. The smaller the better. */
253 score = distance + offset;
255 /* windows more than 45 degrees off the direction are
256 * heavily penalized and will only be chosen if nothing
257 * else within a million pixels */
258 if (offset > distance)
261 if (best_score == -1 || score < best_score) {
270 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
271 gboolean desktop_windows,
272 gboolean interactive,
273 gboolean showbar, gboolean dialog,
274 gboolean done, gboolean cancel)
276 static ObClient *first = NULL;
278 ObClient *ret = NULL;
281 focus_cycle_target = NULL;
283 } else if (done && interactive)
289 if (focus_cycle_target == NULL) {
290 focus_cycle_linear = FALSE;
291 focus_cycle_iconic_windows = FALSE;
292 focus_cycle_all_desktops = FALSE;
293 focus_cycle_nonhilite_windows = TRUE;
294 focus_cycle_dock_windows = dock_windows;
295 focus_cycle_desktop_windows = desktop_windows;
298 if (!first) first = focus_client;
300 if (focus_cycle_target)
301 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
304 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
308 for (it = focus_order; it; it = g_list_next(it))
309 if (focus_cycle_valid(it->data)) {
315 if (ft && ft != focus_cycle_target) {/* prevents flicker */
316 focus_cycle_target = ft;
317 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
320 focus_cycle_draw_indicator(showbar ? ft : NULL);
322 if (focus_cycle_target && dialog)
323 /* same arguments as focus_target_valid */
324 focus_cycle_popup_single_show(focus_cycle_target);
325 return focus_cycle_target;
328 if (done && !cancel) ret = focus_cycle_target;
331 focus_cycle_target = NULL;
332 focus_cycle_type = OB_CYCLE_NONE;
334 focus_cycle_draw_indicator(NULL);
335 focus_cycle_popup_single_hide();
340 gboolean focus_cycle_valid(struct _ObClient *client)
342 return focus_valid_target(client, screen_desktop, TRUE,
343 focus_cycle_iconic_windows,
344 focus_cycle_all_desktops,
345 focus_cycle_nonhilite_windows,
346 focus_cycle_dock_windows,
347 focus_cycle_desktop_windows,