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"
23 #include "client_set.h"
39 ObClient *focus_cycle_target = NULL;
40 ObClientSet *focus_cycle_set = NULL;
41 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
42 static gboolean focus_cycle_linear;
43 static gboolean focus_cycle_iconic_windows;
44 static gboolean focus_cycle_dock_windows;
45 static gboolean focus_cycle_desktop_windows;
47 static ObClient *focus_find_directional(ObClient *c,
49 const ObClientSet *set,
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) {
71 focus_directional_cycle(0, NULL, TRUE, TRUE, TRUE, TRUE,
75 else if (c && redraw) {
78 v = focus_cycle_valid(c);
79 s = focus_cycle_popup_is_showing(c);
82 focus_cycle_reorder();
85 focus_cycle_reorder();
89 void focus_cycle_reorder()
91 if (focus_cycle_type == OB_CYCLE_NORMAL) {
92 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_set,
96 focus_cycle_update_indicator(focus_cycle_target);
97 if (!focus_cycle_target)
98 focus_cycle(NULL, TRUE, TRUE, TRUE, TRUE,
99 TRUE, TRUE, OB_FOCUS_CYCLE_POPUP_MODE_NONE,
104 ObClient* focus_cycle(const ObClientSet *set, gboolean forward,
105 gboolean dock_windows, gboolean desktop_windows,
106 gboolean linear, gboolean interactive,
107 gboolean showbar, ObFocusCyclePopupMode mode,
108 gboolean done, gboolean cancel)
110 static GList *order = NULL;
111 GList *it, *start, *list;
113 ObClient *ret = NULL;
117 focus_cycle_target = NULL;
125 if (linear) list = client_list;
126 else list = focus_order;
133 if (focus_cycle_target == NULL) {
134 focus_cycle_set = client_set_clone(set);
135 focus_cycle_linear = linear;
136 focus_cycle_iconic_windows = TRUE;
137 focus_cycle_dock_windows = dock_windows;
138 focus_cycle_desktop_windows = desktop_windows;
139 start = it = g_list_find(list, focus_client);
141 start = it = g_list_find(list, focus_cycle_target);
143 if (!start) /* switched desktops or something? */
144 start = it = forward ? g_list_last(list) : g_list_first(list);
145 if (!start) goto done_cycle;
150 if (it == NULL) it = g_list_first(list);
153 if (it == NULL) it = g_list_last(list);
156 if (focus_cycle_valid(ft)) {
158 if (ft != focus_cycle_target) { /* prevents flicker */
159 focus_cycle_target = ft;
160 focus_cycle_type = OB_CYCLE_NORMAL;
161 focus_cycle_draw_indicator(showbar ? ft : NULL);
163 /* same arguments as focus_target_valid */
164 focus_cycle_popup_show(focus_cycle_set, ft, mode,
166 return focus_cycle_target;
167 } else if (ft != focus_cycle_target) {
168 focus_cycle_target = ft;
169 focus_cycle_type = OB_CYCLE_NORMAL;
174 } while (it != start);
177 if (done && !cancel) ret = focus_cycle_target;
179 focus_cycle_target = NULL;
180 client_set_destroy(focus_cycle_set);
181 focus_cycle_set = NULL;
182 focus_cycle_type = OB_CYCLE_NONE;
187 focus_cycle_draw_indicator(NULL);
188 focus_cycle_popup_hide();
194 /* this be mostly ripped from fvwm */
195 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
196 const ObClientSet *set,
197 gboolean dock_windows,
198 gboolean desktop_windows)
200 gint my_cx, my_cy, his_cx, his_cy;
203 gint score, best_score;
204 ObClient *best_client, *cur;
210 /* first, find the centre coords of the currently focused window */
211 my_cx = c->frame->area.x + c->frame->area.width / 2;
212 my_cy = c->frame->area.y + c->frame->area.height / 2;
217 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
220 /* the currently selected window isn't interesting */
223 if (!focus_cycle_valid(it->data))
226 /* find the centre coords of this window, from the
227 * currently focused window's point of view */
228 his_cx = (cur->frame->area.x - my_cx)
229 + cur->frame->area.width / 2;
230 his_cy = (cur->frame->area.y - my_cy)
231 + cur->frame->area.height / 2;
233 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
234 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
237 /* Rotate the diagonals 45 degrees counterclockwise.
238 * To do this, multiply the matrix /+h +h\ with the
239 * vector (x y). \-h +h/
240 * h = sqrt(0.5). We can set h := 1 since absolute
241 * distance doesn't matter here. */
242 tx = his_cx + his_cy;
243 his_cy = -his_cx + his_cy;
248 case OB_DIRECTION_NORTH:
249 case OB_DIRECTION_SOUTH:
250 case OB_DIRECTION_NORTHEAST:
251 case OB_DIRECTION_SOUTHWEST:
252 offset = (his_cx < 0) ? -his_cx : his_cx;
253 distance = ((dir == OB_DIRECTION_NORTH ||
254 dir == OB_DIRECTION_NORTHEAST) ?
257 case OB_DIRECTION_EAST:
258 case OB_DIRECTION_WEST:
259 case OB_DIRECTION_SOUTHEAST:
260 case OB_DIRECTION_NORTHWEST:
261 offset = (his_cy < 0) ? -his_cy : his_cy;
262 distance = ((dir == OB_DIRECTION_WEST ||
263 dir == OB_DIRECTION_NORTHWEST) ?
268 /* the target must be in the requested direction */
272 /* Calculate score for this window. The smaller the better. */
273 score = distance + offset;
275 /* windows more than 45 degrees off the direction are
276 * heavily penalized and will only be chosen if nothing
277 * else within a million pixels */
278 if (offset > distance)
281 if (best_score == -1 || score < best_score) {
290 ObClient* focus_directional_cycle(ObDirection dir, const ObClientSet *set,
291 gboolean dock_windows,
292 gboolean desktop_windows,
293 gboolean interactive,
294 gboolean showbar, gboolean dialog,
295 gboolean done, gboolean cancel)
297 static ObClient *first = NULL;
299 ObClient *ret = NULL;
302 focus_cycle_target = NULL;
304 } else if (done && interactive)
310 if (focus_cycle_target == NULL) {
311 focus_cycle_set = client_set_clone(set);
312 focus_cycle_linear = FALSE;
313 focus_cycle_iconic_windows = FALSE;
314 focus_cycle_dock_windows = dock_windows;
315 focus_cycle_desktop_windows = desktop_windows;
318 if (!first) first = focus_client;
320 if (focus_cycle_target)
321 ft = focus_find_directional(focus_cycle_target, dir, set, dock_windows,
324 ft = focus_find_directional(first, dir, set, dock_windows,
329 for (it = focus_order; it; it = g_list_next(it))
330 if (focus_cycle_valid(it->data)) {
336 if (ft && ft != focus_cycle_target) {/* prevents flicker */
337 focus_cycle_target = ft;
338 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
341 focus_cycle_draw_indicator(showbar ? ft : NULL);
343 if (focus_cycle_target && dialog)
344 /* same arguments as focus_target_valid */
345 focus_cycle_popup_single_show(focus_cycle_target);
346 return focus_cycle_target;
349 if (done && !cancel) ret = focus_cycle_target;
352 focus_cycle_target = NULL;
353 client_set_destroy(focus_cycle_set);
354 focus_cycle_set = NULL;
355 focus_cycle_type = OB_CYCLE_NONE;
357 focus_cycle_draw_indicator(NULL);
358 focus_cycle_popup_single_hide();
363 gboolean focus_cycle_valid(struct _ObClient *client)
366 client_set_contains(focus_cycle_set, client) &&
367 focus_valid_target(client, screen_desktop, TRUE,
368 focus_cycle_iconic_windows,
369 TRUE, /* all desktops */
370 TRUE, /* non-hilite windows */
371 focus_cycle_dock_windows,
372 focus_cycle_desktop_windows,