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"
39 ObClient *focus_cycle_target = NULL;
40 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
41 static gboolean focus_cycle_linear;
42 static gboolean focus_cycle_iconic_windows;
43 static gboolean focus_cycle_all_desktops;
44 static gboolean focus_cycle_nonhilite_windows;
45 static gboolean focus_cycle_dock_windows;
46 static gboolean focus_cycle_desktop_windows;
48 static ObClient *focus_find_directional(ObClient *c,
50 gboolean dock_windows,
51 gboolean desktop_windows);
53 void focus_cycle_startup(gboolean reconfig)
58 void focus_cycle_shutdown(gboolean reconfig)
63 void focus_cycle_addremove(ObClient *c, gboolean redraw)
65 if (!focus_cycle_type)
68 if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
69 if (c && focus_cycle_target == c) {
70 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
74 else if (c && redraw) {
77 v = focus_cycle_valid(c);
78 s = focus_cycle_popup_is_showing(c) || c == focus_cycle_target;
81 focus_cycle_reorder();
84 focus_cycle_reorder();
88 void focus_cycle_reorder()
90 if (focus_cycle_type == OB_CYCLE_NORMAL) {
91 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
94 focus_cycle_update_indicator(focus_cycle_target);
95 if (!focus_cycle_target)
96 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
97 TRUE, OB_FOCUS_CYCLE_POPUP_MODE_NONE,
102 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
103 gboolean nonhilite_windows,
104 gboolean dock_windows, gboolean desktop_windows,
105 gboolean linear, gboolean showbar,
106 ObFocusCyclePopupMode mode,
107 gboolean done, gboolean cancel)
109 static GList *order = NULL;
110 GList *it, *start, *list;
112 ObClient *ret = NULL;
115 focus_cycle_target = NULL;
123 if (linear) list = client_list;
124 else list = focus_order;
126 if (focus_cycle_target == NULL) {
127 focus_cycle_linear = linear;
128 focus_cycle_iconic_windows = TRUE;
129 focus_cycle_all_desktops = all_desktops;
130 focus_cycle_nonhilite_windows = nonhilite_windows;
131 focus_cycle_dock_windows = dock_windows;
132 focus_cycle_desktop_windows = desktop_windows;
133 start = it = g_list_find(list, focus_client);
135 start = it = g_list_find(list, focus_cycle_target);
137 if (!start) /* switched desktops or something? */
138 start = it = forward ? g_list_last(list) : g_list_first(list);
139 if (!start) goto done_cycle;
144 if (it == NULL) it = g_list_first(list);
147 if (it == NULL) it = g_list_last(list);
150 if (focus_cycle_valid(ft)) {
151 if (ft != focus_cycle_target) { /* prevents flicker */
152 focus_cycle_target = ft;
153 focus_cycle_type = OB_CYCLE_NORMAL;
154 focus_cycle_draw_indicator(showbar ? ft : NULL);
156 /* same arguments as focus_target_valid */
157 focus_cycle_popup_show(ft, mode, focus_cycle_linear);
158 return focus_cycle_target;
160 } while (it != start);
163 if (done && !cancel) ret = focus_cycle_target;
165 focus_cycle_target = NULL;
166 focus_cycle_type = OB_CYCLE_NONE;
170 focus_cycle_draw_indicator(NULL);
171 focus_cycle_popup_hide();
176 /* this be mostly ripped from fvwm */
177 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
178 gboolean dock_windows,
179 gboolean desktop_windows)
181 gint my_cx, my_cy, his_cx, his_cy;
184 gint score, best_score;
185 ObClient *best_client, *cur;
191 /* first, find the centre coords of the currently focused window */
192 my_cx = c->frame->area.x + c->frame->area.width / 2;
193 my_cy = c->frame->area.y + c->frame->area.height / 2;
198 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
201 /* the currently selected window isn't interesting */
204 if (!focus_cycle_valid(it->data))
207 /* find the centre coords of this window, from the
208 * currently focused window's point of view */
209 his_cx = (cur->frame->area.x - my_cx)
210 + cur->frame->area.width / 2;
211 his_cy = (cur->frame->area.y - my_cy)
212 + cur->frame->area.height / 2;
214 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
215 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
218 /* Rotate the diagonals 45 degrees counterclockwise.
219 * To do this, multiply the matrix /+h +h\ with the
220 * vector (x y). \-h +h/
221 * h = sqrt(0.5). We can set h := 1 since absolute
222 * distance doesn't matter here. */
223 tx = his_cx + his_cy;
224 his_cy = -his_cx + his_cy;
229 case OB_DIRECTION_NORTH:
230 case OB_DIRECTION_SOUTH:
231 case OB_DIRECTION_NORTHEAST:
232 case OB_DIRECTION_SOUTHWEST:
233 offset = (his_cx < 0) ? -his_cx : his_cx;
234 distance = ((dir == OB_DIRECTION_NORTH ||
235 dir == OB_DIRECTION_NORTHEAST) ?
238 case OB_DIRECTION_EAST:
239 case OB_DIRECTION_WEST:
240 case OB_DIRECTION_SOUTHEAST:
241 case OB_DIRECTION_NORTHWEST:
242 offset = (his_cy < 0) ? -his_cy : his_cy;
243 distance = ((dir == OB_DIRECTION_WEST ||
244 dir == OB_DIRECTION_NORTHWEST) ?
249 /* the target must be in the requested direction */
253 /* Calculate score for this window. The smaller the better. */
254 score = (distance * config_directional_distance_weight
255 + offset * config_directional_angle_weight);
257 /* windows more than 45 degrees off the direction are
258 * heavily penalized and will only be chosen if nothing
259 * else within a million pixels */
260 if (offset > distance)
263 if (best_score == -1 || score < best_score) {
272 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
273 gboolean desktop_windows,
274 gboolean interactive,
275 gboolean showbar, gboolean dialog,
276 gboolean done, gboolean cancel)
278 static ObClient *first = NULL;
280 ObClient *ret = NULL;
283 focus_cycle_target = NULL;
285 } else if (done && interactive)
291 if (focus_cycle_target == NULL) {
292 focus_cycle_linear = FALSE;
293 focus_cycle_iconic_windows = FALSE;
294 focus_cycle_all_desktops = FALSE;
295 focus_cycle_nonhilite_windows = TRUE;
296 focus_cycle_dock_windows = dock_windows;
297 focus_cycle_desktop_windows = desktop_windows;
300 if (!first) first = focus_client;
302 if (focus_cycle_target)
303 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
306 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
310 for (it = focus_order; it; it = g_list_next(it))
311 if (focus_cycle_valid(it->data)) {
317 if (ft && ft != focus_cycle_target) {/* prevents flicker */
318 focus_cycle_target = ft;
319 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
322 focus_cycle_draw_indicator(showbar ? ft : NULL);
324 if (focus_cycle_target && dialog)
325 /* same arguments as focus_target_valid */
326 focus_cycle_popup_single_show(focus_cycle_target);
327 return focus_cycle_target;
330 if (done && !cancel) ret = focus_cycle_target;
333 focus_cycle_target = NULL;
334 focus_cycle_type = OB_CYCLE_NONE;
336 focus_cycle_draw_indicator(NULL);
337 focus_cycle_popup_single_hide();
342 gboolean focus_cycle_valid(struct _ObClient *client)
344 return focus_valid_target(client, screen_desktop, TRUE,
345 focus_cycle_iconic_windows,
346 focus_cycle_all_desktops,
347 focus_cycle_nonhilite_windows,
348 focus_cycle_dock_windows,
349 focus_cycle_desktop_windows,