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_dock_windows;
43 static gboolean focus_cycle_desktop_windows;
45 static ObClient *focus_find_directional(ObClient *c,
47 gboolean dock_windows,
48 gboolean desktop_windows);
50 void focus_cycle_startup(gboolean reconfig)
55 void focus_cycle_shutdown(gboolean reconfig)
60 void focus_cycle_addremove(ObClient *c, gboolean redraw)
62 if (!focus_cycle_type)
65 if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
66 if (c && focus_cycle_target == c) {
67 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
71 else if (c && redraw) {
74 v = focus_cycle_valid(c);
75 s = focus_cycle_popup_is_showing(c);
78 focus_cycle_reorder();
81 focus_cycle_reorder();
85 void focus_cycle_reorder()
87 if (focus_cycle_type == OB_CYCLE_NORMAL) {
88 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
90 focus_cycle_update_indicator(focus_cycle_target);
91 if (!focus_cycle_target)
92 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE,
93 TRUE, TRUE, TRUE, TRUE, TRUE);
97 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
98 gboolean dock_windows, gboolean desktop_windows,
99 gboolean linear, gboolean interactive,
100 gboolean showbar, ObFocusCyclePopupMode mode,
101 gboolean done, gboolean cancel)
103 static GList *order = NULL;
104 GList *it, *start, *list;
106 ObClient *ret = NULL;
110 focus_cycle_target = NULL;
118 if (linear) list = client_list;
119 else list = focus_order;
126 if (focus_cycle_target == NULL) {
127 focus_cycle_iconic_windows = TRUE;
128 focus_cycle_all_desktops = all_desktops;
129 focus_cycle_dock_windows = dock_windows;
130 focus_cycle_desktop_windows = desktop_windows;
131 start = it = g_list_find(list, focus_client);
133 start = it = g_list_find(list, focus_cycle_target);
135 if (!start) /* switched desktops or something? */
136 start = it = forward ? g_list_last(list) : g_list_first(list);
137 if (!start) goto done_cycle;
142 if (it == NULL) it = g_list_first(list);
145 if (it == NULL) it = g_list_last(list);
148 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,
157 focus_cycle_iconic_windows,
158 focus_cycle_all_desktops,
159 focus_cycle_dock_windows,
160 focus_cycle_desktop_windows,
162 return focus_cycle_target;
163 } else if (ft != focus_cycle_target) {
164 focus_cycle_target = ft;
165 focus_cycle_type = OB_CYCLE_NORMAL;
170 } while (it != start);
173 if (done && !cancel) ret = focus_cycle_target;
175 focus_cycle_target = NULL;
176 focus_cycle_type = OB_CYCLE_NONE;
181 focus_cycle_draw_indicator(NULL);
182 focus_cycle_popup_hide();
188 /* this be mostly ripped from fvwm */
189 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
190 gboolean dock_windows,
191 gboolean desktop_windows)
193 gint my_cx, my_cy, his_cx, his_cy;
196 gint score, best_score;
197 ObClient *best_client, *cur;
203 /* first, find the centre coords of the currently focused window */
204 my_cx = c->frame->area.x + c->frame->area.width / 2;
205 my_cy = c->frame->area.y + c->frame->area.height / 2;
210 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
213 /* the currently selected window isn't interesting */
216 if (!focus_cycle_valid(it->data))
219 /* find the centre coords of this window, from the
220 * currently focused window's point of view */
221 his_cx = (cur->frame->area.x - my_cx)
222 + cur->frame->area.width / 2;
223 his_cy = (cur->frame->area.y - my_cy)
224 + cur->frame->area.height / 2;
226 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
227 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
230 /* Rotate the diagonals 45 degrees counterclockwise.
231 * To do this, multiply the matrix /+h +h\ with the
232 * vector (x y). \-h +h/
233 * h = sqrt(0.5). We can set h := 1 since absolute
234 * distance doesn't matter here. */
235 tx = his_cx + his_cy;
236 his_cy = -his_cx + his_cy;
241 case OB_DIRECTION_NORTH:
242 case OB_DIRECTION_SOUTH:
243 case OB_DIRECTION_NORTHEAST:
244 case OB_DIRECTION_SOUTHWEST:
245 offset = (his_cx < 0) ? -his_cx : his_cx;
246 distance = ((dir == OB_DIRECTION_NORTH ||
247 dir == OB_DIRECTION_NORTHEAST) ?
250 case OB_DIRECTION_EAST:
251 case OB_DIRECTION_WEST:
252 case OB_DIRECTION_SOUTHEAST:
253 case OB_DIRECTION_NORTHWEST:
254 offset = (his_cy < 0) ? -his_cy : his_cy;
255 distance = ((dir == OB_DIRECTION_WEST ||
256 dir == OB_DIRECTION_NORTHWEST) ?
261 /* the target must be in the requested direction */
265 /* Calculate score for this window. The smaller the better. */
266 score = distance + offset;
268 /* windows more than 45 degrees off the direction are
269 * heavily penalized and will only be chosen if nothing
270 * else within a million pixels */
271 if (offset > distance)
274 if (best_score == -1 || score < best_score) {
283 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
284 gboolean desktop_windows,
285 gboolean interactive,
286 gboolean showbar, gboolean dialog,
287 gboolean done, gboolean cancel)
289 static ObClient *first = NULL;
291 ObClient *ret = NULL;
294 focus_cycle_target = NULL;
296 } else if (done && interactive)
302 if (focus_cycle_target == NULL) {
303 focus_cycle_iconic_windows = FALSE;
304 focus_cycle_all_desktops = FALSE;
305 focus_cycle_dock_windows = dock_windows;
306 focus_cycle_desktop_windows = desktop_windows;
309 if (!first) first = focus_client;
311 if (focus_cycle_target)
312 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
315 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
319 for (it = focus_order; it; it = g_list_next(it))
320 if (focus_cycle_valid(it->data)) {
326 if (ft && ft != focus_cycle_target) {/* prevents flicker */
327 focus_cycle_target = ft;
328 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
331 focus_cycle_draw_indicator(showbar ? ft : NULL);
333 if (focus_cycle_target && dialog)
334 /* same arguments as focus_target_valid */
335 focus_cycle_popup_single_show(focus_cycle_target,
336 focus_cycle_iconic_windows,
337 focus_cycle_all_desktops,
338 focus_cycle_dock_windows,
339 focus_cycle_desktop_windows);
340 return focus_cycle_target;
343 if (done && !cancel) ret = focus_cycle_target;
346 focus_cycle_target = NULL;
347 focus_cycle_type = OB_CYCLE_NONE;
349 focus_cycle_draw_indicator(NULL);
350 focus_cycle_popup_single_hide();
355 gboolean focus_cycle_valid(struct _ObClient *client)
357 return focus_valid_target(client, screen_desktop, TRUE,
358 focus_cycle_iconic_windows,
359 focus_cycle_all_desktops,
360 focus_cycle_dock_windows,
361 focus_cycle_desktop_windows,