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 "engine_interface.h"
32 //ObClient *focus_cycle_target = NULL;
33 static gboolean focus_cycle_iconic_windows;
34 static gboolean focus_cycle_all_desktops;
35 static gboolean focus_cycle_dock_windows;
36 static gboolean focus_cycle_desktop_windows;
38 static ObClient *focus_find_directional(ObClient *c,
40 gboolean dock_windows,
41 gboolean desktop_windows);
43 void focus_cycle_startup(gboolean reconfig)
48 void focus_cycle_shutdown(gboolean reconfig)
53 void focus_cycle_stop(ObClient *ifclient)
55 /* stop focus cycling if the given client is a valid focus target,
56 and so the cycling is being disrupted */
57 if (frame_engine->focus_cycle_target && ifclient &&
58 focus_valid_target(ifclient, TRUE,
59 focus_cycle_iconic_windows,
60 focus_cycle_all_desktops,
61 focus_cycle_dock_windows,
62 focus_cycle_desktop_windows))
64 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,TRUE);
65 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
69 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
70 gboolean dock_windows, gboolean desktop_windows,
71 gboolean linear, gboolean interactive,
72 gboolean showbar, ObFocusCyclePopupMode mode,
73 gboolean done, gboolean cancel)
75 static ObClient *t = NULL;
76 static GList *order = NULL;
77 GList *it, *start, *list;
83 frame_engine->focus_cycle_target = NULL;
91 if (linear) list = client_list;
92 else list = focus_order;
100 if (frame_engine->focus_cycle_target == NULL) {
101 focus_cycle_iconic_windows = TRUE;
102 focus_cycle_all_desktops = all_desktops;
103 focus_cycle_dock_windows = dock_windows;
104 focus_cycle_desktop_windows = desktop_windows;
105 start = it = g_list_find(list, focus_client);
107 start = it = g_list_find(list, frame_engine->focus_cycle_target);
109 if (!start) /* switched desktops or something? */
110 start = it = forward ? g_list_last(list) : g_list_first(list);
111 if (!start) goto done_cycle;
116 if (it == NULL) it = g_list_first(list);
119 if (it == NULL) it = g_list_last(list);
122 if (focus_valid_target(ft, TRUE,
123 focus_cycle_iconic_windows,
124 focus_cycle_all_desktops,
125 focus_cycle_dock_windows,
126 focus_cycle_desktop_windows))
129 if (ft != frame_engine->focus_cycle_target) { /* prevents flicker */
130 frame_engine->focus_cycle_target = ft;
131 focus_cycle_draw_indicator(showbar ? ft : NULL);
133 /* same arguments as focus_target_valid */
134 focus_cycle_popup_show(ft,
135 focus_cycle_iconic_windows,
136 focus_cycle_all_desktops,
137 focus_cycle_dock_windows,
138 focus_cycle_desktop_windows,
140 return frame_engine->focus_cycle_target;
141 } else if (ft != frame_engine->focus_cycle_target) {
142 frame_engine->focus_cycle_target = ft;
147 } while (it != start);
150 if (done && !cancel) ret = frame_engine->focus_cycle_target;
153 frame_engine->focus_cycle_target = NULL;
158 focus_cycle_draw_indicator(NULL);
159 focus_cycle_popup_hide();
165 /* this be mostly ripped from fvwm */
166 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
167 gboolean dock_windows,
168 gboolean desktop_windows)
170 gint my_cx, my_cy, his_cx, his_cy;
173 gint score, best_score;
174 ObClient *best_client, *cur;
181 frame_engine->frame_get_window_area(c->frame, &area);
182 /* first, find the centre coords of the currently focused window */
183 my_cx = area.x + area.width / 2;
184 my_cy = area.y + area.height / 2;
189 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
192 /* the currently selected window isn't interesting */
195 if (!focus_valid_target(it->data, TRUE, FALSE, FALSE, dock_windows,
200 frame_engine->frame_get_window_area(cur->frame, &cur_area);
201 /* find the centre coords of this window, from the
202 * currently focused window's point of view */
203 his_cx = (cur_area.x - my_cx)
204 + cur_area.width / 2;
205 his_cy = (cur_area.y - my_cy)
206 + cur_area.height / 2;
208 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
209 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
212 /* Rotate the diagonals 45 degrees counterclockwise.
213 * To do this, multiply the matrix /+h +h\ with the
214 * vector (x y). \-h +h/
215 * h = sqrt(0.5). We can set h := 1 since absolute
216 * distance doesn't matter here. */
217 tx = his_cx + his_cy;
218 his_cy = -his_cx + his_cy;
223 case OB_DIRECTION_NORTH:
224 case OB_DIRECTION_SOUTH:
225 case OB_DIRECTION_NORTHEAST:
226 case OB_DIRECTION_SOUTHWEST:
227 offset = (his_cx < 0) ? -his_cx : his_cx;
228 distance = ((dir == OB_DIRECTION_NORTH ||
229 dir == OB_DIRECTION_NORTHEAST) ?
232 case OB_DIRECTION_EAST:
233 case OB_DIRECTION_WEST:
234 case OB_DIRECTION_SOUTHEAST:
235 case OB_DIRECTION_NORTHWEST:
236 offset = (his_cy < 0) ? -his_cy : his_cy;
237 distance = ((dir == OB_DIRECTION_WEST ||
238 dir == OB_DIRECTION_NORTHWEST) ?
243 /* the target must be in the requested direction */
247 /* Calculate score for this window. The smaller the better. */
248 score = distance + offset;
250 /* windows more than 45 degrees off the direction are
251 * heavily penalized and will only be chosen if nothing
252 * else within a million pixels */
253 if (offset > distance)
256 if (best_score == -1 || score < best_score) {
265 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
266 gboolean desktop_windows,
267 gboolean interactive,
268 gboolean showbar, gboolean dialog,
269 gboolean done, gboolean cancel)
271 static ObClient *first = NULL;
273 ObClient *ret = NULL;
276 frame_engine->focus_cycle_target = NULL;
278 } else if (done && interactive)
284 if (frame_engine->focus_cycle_target == NULL) {
285 focus_cycle_iconic_windows = FALSE;
286 focus_cycle_all_desktops = FALSE;
287 focus_cycle_dock_windows = dock_windows;
288 focus_cycle_desktop_windows = desktop_windows;
291 if (!first) first = focus_client;
293 if (frame_engine->focus_cycle_target)
294 ft = focus_find_directional(frame_engine->focus_cycle_target, dir, dock_windows,
297 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
301 for (it = focus_order; it; it = g_list_next(it))
302 if (focus_valid_target(it->data, TRUE,
303 focus_cycle_iconic_windows,
304 focus_cycle_all_desktops,
305 focus_cycle_dock_windows,
306 focus_cycle_desktop_windows))
310 if (ft && ft != frame_engine->focus_cycle_target) {/* prevents flicker */
311 frame_engine->focus_cycle_target = ft;
314 focus_cycle_draw_indicator(showbar ? ft : NULL);
316 if (frame_engine->focus_cycle_target && dialog)
317 /* same arguments as focus_target_valid */
318 focus_cycle_popup_single_show(frame_engine->focus_cycle_target,
319 focus_cycle_iconic_windows,
320 focus_cycle_all_desktops,
321 focus_cycle_dock_windows,
322 focus_cycle_desktop_windows);
323 return frame_engine->focus_cycle_target;
326 if (done && !cancel) ret = frame_engine->focus_cycle_target;
329 frame_engine->focus_cycle_target = NULL;
331 focus_cycle_draw_indicator(NULL);
332 focus_cycle_popup_single_hide();