Allow escaping _ in menu labels by putting __
[openbox.git] / openbox / focus_cycle.c
blobe30672dce89e5a35a8b69d01c75e230c2f0428b7
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"
22 #include "focus_cycle_popup.h"
23 #include "client.h"
24 #include "frame.h"
25 #include "focus.h"
26 #include "screen.h"
27 #include "openbox.h"
28 #include "debug.h"
30 #include <X11/Xlib.h>
31 #include <glib.h>
33 ObClient *focus_cycle_target = NULL;
34 static gboolean focus_cycle_iconic_windows;
35 static gboolean focus_cycle_all_desktops;
36 static gboolean focus_cycle_dock_windows;
37 static gboolean focus_cycle_desktop_windows;
39 static ObClient *focus_find_directional(ObClient *c,
40 ObDirection dir,
41 gboolean dock_windows,
42 gboolean desktop_windows);
44 void focus_cycle_startup(gboolean reconfig)
46 if (reconfig) return;
49 void focus_cycle_shutdown(gboolean reconfig)
51 if (reconfig) return;
54 void focus_cycle_stop(ObClient *ifclient)
56 /* stop focus cycling if the given client is a valid focus target,
57 and so the cycling is being disrupted */
58 if (focus_cycle_target && ifclient &&
59 focus_valid_target(ifclient, TRUE,
60 focus_cycle_iconic_windows,
61 focus_cycle_all_desktops,
62 focus_cycle_dock_windows,
63 focus_cycle_desktop_windows))
65 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,TRUE);
66 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
70 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
71 gboolean dock_windows, gboolean desktop_windows,
72 gboolean linear, gboolean interactive,
73 gboolean showbar, gboolean dialog,
74 gboolean done, gboolean cancel)
76 static GList *order = NULL;
77 GList *it, *start, *list;
78 ObClient *ft = NULL;
79 ObClient *ret = NULL;
81 if (interactive) {
82 if (cancel) {
83 focus_cycle_target = NULL;
84 goto done_cycle;
85 } else if (done)
86 goto done_cycle;
88 if (!focus_order)
89 goto done_cycle;
91 if (linear) list = client_list;
92 else list = focus_order;
93 } else {
94 if (!focus_order)
95 goto done_cycle;
96 list = client_list;
99 if (focus_cycle_target == NULL) {
100 focus_cycle_iconic_windows = TRUE;
101 focus_cycle_all_desktops = all_desktops;
102 focus_cycle_dock_windows = dock_windows;
103 focus_cycle_desktop_windows = desktop_windows;
104 start = it = g_list_find(list, focus_client);
105 } else
106 start = it = g_list_find(list, focus_cycle_target);
108 if (!start) /* switched desktops or something? */
109 start = it = forward ? g_list_last(list) : g_list_first(list);
110 if (!start) goto done_cycle;
112 do {
113 if (forward) {
114 it = it->next;
115 if (it == NULL) it = g_list_first(list);
116 } else {
117 it = it->prev;
118 if (it == NULL) it = g_list_last(list);
120 ft = it->data;
121 if (focus_valid_target(ft, TRUE,
122 focus_cycle_iconic_windows,
123 focus_cycle_all_desktops,
124 focus_cycle_dock_windows,
125 focus_cycle_desktop_windows))
127 if (interactive) {
128 if (ft != focus_cycle_target) { /* prevents flicker */
129 focus_cycle_target = ft;
130 focus_cycle_draw_indicator(showbar ? ft : NULL);
132 if (dialog)
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);
139 return focus_cycle_target;
140 } else if (ft != focus_cycle_target) {
141 focus_cycle_target = ft;
142 done = TRUE;
143 break;
146 } while (it != start);
148 done_cycle:
149 if (done && !cancel) ret = focus_cycle_target;
151 focus_cycle_target = NULL;
152 g_list_free(order);
153 order = NULL;
155 if (interactive) {
156 focus_cycle_draw_indicator(NULL);
157 focus_cycle_popup_hide();
160 return ret;
163 /* this be mostly ripped from fvwm */
164 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
165 gboolean dock_windows,
166 gboolean desktop_windows)
168 gint my_cx, my_cy, his_cx, his_cy;
169 gint offset = 0;
170 gint distance = 0;
171 gint score, best_score;
172 ObClient *best_client, *cur;
173 GList *it;
175 if (!client_list)
176 return NULL;
178 /* first, find the centre coords of the currently focused window */
179 my_cx = c->frame->area.x + c->frame->area.width / 2;
180 my_cy = c->frame->area.y + c->frame->area.height / 2;
182 best_score = -1;
183 best_client = c;
185 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
186 cur = it->data;
188 /* the currently selected window isn't interesting */
189 if (cur == c)
190 continue;
191 if (!focus_valid_target(it->data, TRUE, FALSE, FALSE, dock_windows,
192 desktop_windows))
193 continue;
195 /* find the centre coords of this window, from the
196 * currently focused window's point of view */
197 his_cx = (cur->frame->area.x - my_cx)
198 + cur->frame->area.width / 2;
199 his_cy = (cur->frame->area.y - my_cy)
200 + cur->frame->area.height / 2;
202 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
203 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
205 gint tx;
206 /* Rotate the diagonals 45 degrees counterclockwise.
207 * To do this, multiply the matrix /+h +h\ with the
208 * vector (x y). \-h +h/
209 * h = sqrt(0.5). We can set h := 1 since absolute
210 * distance doesn't matter here. */
211 tx = his_cx + his_cy;
212 his_cy = -his_cx + his_cy;
213 his_cx = tx;
216 switch (dir) {
217 case OB_DIRECTION_NORTH:
218 case OB_DIRECTION_SOUTH:
219 case OB_DIRECTION_NORTHEAST:
220 case OB_DIRECTION_SOUTHWEST:
221 offset = (his_cx < 0) ? -his_cx : his_cx;
222 distance = ((dir == OB_DIRECTION_NORTH ||
223 dir == OB_DIRECTION_NORTHEAST) ?
224 -his_cy : his_cy);
225 break;
226 case OB_DIRECTION_EAST:
227 case OB_DIRECTION_WEST:
228 case OB_DIRECTION_SOUTHEAST:
229 case OB_DIRECTION_NORTHWEST:
230 offset = (his_cy < 0) ? -his_cy : his_cy;
231 distance = ((dir == OB_DIRECTION_WEST ||
232 dir == OB_DIRECTION_NORTHWEST) ?
233 -his_cx : his_cx);
234 break;
237 /* the target must be in the requested direction */
238 if (distance <= 0)
239 continue;
241 /* Calculate score for this window. The smaller the better. */
242 score = distance + offset;
244 /* windows more than 45 degrees off the direction are
245 * heavily penalized and will only be chosen if nothing
246 * else within a million pixels */
247 if (offset > distance)
248 score += 1000000;
250 if (best_score == -1 || score < best_score) {
251 best_client = cur;
252 best_score = score;
256 return best_client;
259 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
260 gboolean desktop_windows,
261 gboolean interactive,
262 gboolean showbar, gboolean dialog,
263 gboolean done, gboolean cancel)
265 static ObClient *first = NULL;
266 ObClient *ft = NULL;
267 ObClient *ret = NULL;
269 if (cancel) {
270 focus_cycle_target = NULL;
271 goto done_cycle;
272 } else if (done && interactive)
273 goto done_cycle;
275 if (!focus_order)
276 goto done_cycle;
278 if (focus_cycle_target == NULL) {
279 focus_cycle_iconic_windows = FALSE;
280 focus_cycle_all_desktops = FALSE;
281 focus_cycle_dock_windows = dock_windows;
282 focus_cycle_desktop_windows = desktop_windows;
285 if (!first) first = focus_client;
287 if (focus_cycle_target)
288 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
289 desktop_windows);
290 else if (first)
291 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
292 else {
293 GList *it;
295 for (it = focus_order; it; it = g_list_next(it))
296 if (focus_valid_target(it->data, TRUE,
297 focus_cycle_iconic_windows,
298 focus_cycle_all_desktops,
299 focus_cycle_dock_windows,
300 focus_cycle_desktop_windows))
301 ft = it->data;
304 if (ft && ft != focus_cycle_target) {/* prevents flicker */
305 focus_cycle_target = ft;
306 if (!interactive)
307 goto done_cycle;
308 focus_cycle_draw_indicator(showbar ? ft : NULL);
310 if (focus_cycle_target && dialog)
311 /* same arguments as focus_target_valid */
312 focus_cycle_popup_single_show(focus_cycle_target,
313 focus_cycle_iconic_windows,
314 focus_cycle_all_desktops,
315 focus_cycle_dock_windows,
316 focus_cycle_desktop_windows);
317 return focus_cycle_target;
319 done_cycle:
320 if (done && !cancel) ret = focus_cycle_target;
322 first = NULL;
323 focus_cycle_target = NULL;
325 focus_cycle_draw_indicator(NULL);
326 focus_cycle_popup_single_hide();
328 return ret;