]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle.c
93a9a9930ee1884743372b1ac2ff314f8500fb1e
[chaz/openbox] / openbox / focus_cycle.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 focus_cycle.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
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.
11
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.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "focus_cycle.h"
21 #include "focus_cycle_indicator.h"
22 #include "client.h"
23 #include "frame.h"
24 #include "focus.h"
25 #include "screen.h"
26 #include "openbox.h"
27 #include "debug.h"
28
29 #include <X11/Xlib.h>
30 #include <glib.h>
31
32 typedef enum {
33 OB_CYCLE_NONE = 0,
34 OB_CYCLE_NORMAL,
35 OB_CYCLE_DIRECTIONAL
36 } ObCycleType;
37
38 ObClient *focus_cycle_target = NULL;
39 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
40 static gboolean focus_cycle_linear;
41 static gboolean focus_cycle_iconic_windows;
42 static gboolean focus_cycle_all_desktops;
43 static gboolean focus_cycle_nonhilite_windows;
44 static gboolean focus_cycle_dock_windows;
45 static gboolean focus_cycle_desktop_windows;
46
47 static ObClient *focus_find_directional(ObClient *c,
48 ObDirection dir,
49 gboolean dock_windows,
50 gboolean desktop_windows);
51
52 void focus_cycle_startup(gboolean reconfig)
53 {
54 if (reconfig) return;
55 }
56
57 void focus_cycle_shutdown(gboolean reconfig)
58 {
59 if (reconfig) return;
60 }
61
62 void focus_cycle_addremove(ObClient *c, gboolean redraw)
63 {
64 if (!focus_cycle_type)
65 return;
66
67 if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
68 if (c && focus_cycle_target == c) {
69 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
70 TRUE, TRUE, TRUE);
71 }
72 }
73 else if (c && redraw) {
74 gboolean v, s;
75
76 v = focus_cycle_valid(c);
77 s = focus_cycle_popup_is_showing(c) || c == focus_cycle_target;
78
79 if (v != s)
80 focus_cycle_reorder();
81 }
82 else if (redraw) {
83 focus_cycle_reorder();
84 }
85 }
86
87 void focus_cycle_reorder()
88 {
89 if (focus_cycle_type == OB_CYCLE_NORMAL) {
90 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
91 TRUE,
92 focus_cycle_linear);
93 focus_cycle_update_indicator(focus_cycle_target);
94 if (!focus_cycle_target)
95 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
96 TRUE, TRUE, OB_FOCUS_CYCLE_POPUP_MODE_NONE,
97 TRUE, TRUE);
98 }
99 }
100
101 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
102 gboolean nonhilite_windows,
103 gboolean dock_windows, gboolean desktop_windows,
104 gboolean linear, gboolean interactive,
105 gboolean showbar, ObFocusCyclePopupMode mode,
106 gboolean done, gboolean cancel)
107 {
108 static GList *order = NULL;
109 GList *it, *start, *list;
110 ObClient *ft = NULL;
111 ObClient *ret = NULL;
112
113 if (interactive) {
114 if (cancel) {
115 focus_cycle_target = NULL;
116 goto done_cycle;
117 } else if (done)
118 goto done_cycle;
119
120 if (!focus_order)
121 goto done_cycle;
122
123 if (linear) list = client_list;
124 else list = focus_order;
125 } else {
126 if (!focus_order)
127 goto done_cycle;
128 list = client_list;
129 }
130
131 if (focus_cycle_target == NULL) {
132 focus_cycle_linear = linear;
133 focus_cycle_iconic_windows = TRUE;
134 focus_cycle_all_desktops = all_desktops;
135 focus_cycle_nonhilite_windows = nonhilite_windows;
136 focus_cycle_dock_windows = dock_windows;
137 focus_cycle_desktop_windows = desktop_windows;
138 start = it = g_list_find(list, focus_client);
139 } else
140 start = it = g_list_find(list, focus_cycle_target);
141
142 if (!start) /* switched desktops or something? */
143 start = it = forward ? g_list_last(list) : g_list_first(list);
144 if (!start) goto done_cycle;
145
146 do {
147 if (forward) {
148 it = it->next;
149 if (it == NULL) it = g_list_first(list);
150 } else {
151 it = it->prev;
152 if (it == NULL) it = g_list_last(list);
153 }
154 ft = it->data;
155 if (focus_cycle_valid(ft)) {
156 if (interactive) {
157 if (ft != focus_cycle_target) { /* prevents flicker */
158 focus_cycle_target = ft;
159 focus_cycle_type = OB_CYCLE_NORMAL;
160 focus_cycle_draw_indicator(showbar ? ft : NULL);
161 }
162 /* same arguments as focus_target_valid */
163 focus_cycle_popup_show(ft, mode, focus_cycle_linear);
164 return focus_cycle_target;
165 } else if (ft != focus_cycle_target) {
166 focus_cycle_target = ft;
167 focus_cycle_type = OB_CYCLE_NORMAL;
168 done = TRUE;
169 break;
170 }
171 }
172 } while (it != start);
173
174 done_cycle:
175 if (done && !cancel) ret = focus_cycle_target;
176
177 focus_cycle_target = NULL;
178 focus_cycle_type = OB_CYCLE_NONE;
179 g_list_free(order);
180 order = NULL;
181
182 if (interactive) {
183 focus_cycle_draw_indicator(NULL);
184 focus_cycle_popup_hide();
185 }
186
187 return ret;
188 }
189
190 /* this be mostly ripped from fvwm */
191 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
192 gboolean dock_windows,
193 gboolean desktop_windows)
194 {
195 gint my_cx, my_cy, his_cx, his_cy;
196 gint offset = 0;
197 gint distance = 0;
198 gint score, best_score;
199 ObClient *best_client, *cur;
200 GList *it;
201
202 if (!client_list)
203 return NULL;
204
205 /* first, find the centre coords of the currently focused window */
206 my_cx = c->frame->area.x + c->frame->area.width / 2;
207 my_cy = c->frame->area.y + c->frame->area.height / 2;
208
209 best_score = -1;
210 best_client = c;
211
212 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
213 cur = it->data;
214
215 /* the currently selected window isn't interesting */
216 if (cur == c)
217 continue;
218 if (!focus_cycle_valid(it->data))
219 continue;
220
221 /* find the centre coords of this window, from the
222 * currently focused window's point of view */
223 his_cx = (cur->frame->area.x - my_cx)
224 + cur->frame->area.width / 2;
225 his_cy = (cur->frame->area.y - my_cy)
226 + cur->frame->area.height / 2;
227
228 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
229 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
230 {
231 gint tx;
232 /* Rotate the diagonals 45 degrees counterclockwise.
233 * To do this, multiply the matrix /+h +h\ with the
234 * vector (x y). \-h +h/
235 * h = sqrt(0.5). We can set h := 1 since absolute
236 * distance doesn't matter here. */
237 tx = his_cx + his_cy;
238 his_cy = -his_cx + his_cy;
239 his_cx = tx;
240 }
241
242 switch (dir) {
243 case OB_DIRECTION_NORTH:
244 case OB_DIRECTION_SOUTH:
245 case OB_DIRECTION_NORTHEAST:
246 case OB_DIRECTION_SOUTHWEST:
247 offset = (his_cx < 0) ? -his_cx : his_cx;
248 distance = ((dir == OB_DIRECTION_NORTH ||
249 dir == OB_DIRECTION_NORTHEAST) ?
250 -his_cy : his_cy);
251 break;
252 case OB_DIRECTION_EAST:
253 case OB_DIRECTION_WEST:
254 case OB_DIRECTION_SOUTHEAST:
255 case OB_DIRECTION_NORTHWEST:
256 offset = (his_cy < 0) ? -his_cy : his_cy;
257 distance = ((dir == OB_DIRECTION_WEST ||
258 dir == OB_DIRECTION_NORTHWEST) ?
259 -his_cx : his_cx);
260 break;
261 }
262
263 /* the target must be in the requested direction */
264 if (distance <= 0)
265 continue;
266
267 /* Calculate score for this window. The smaller the better. */
268 score = distance + offset;
269
270 /* windows more than 45 degrees off the direction are
271 * heavily penalized and will only be chosen if nothing
272 * else within a million pixels */
273 if (offset > distance)
274 score += 1000000;
275
276 if (best_score == -1 || score < best_score) {
277 best_client = cur;
278 best_score = score;
279 }
280 }
281
282 return best_client;
283 }
284
285 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
286 gboolean desktop_windows,
287 gboolean interactive,
288 gboolean showbar, gboolean dialog,
289 gboolean done, gboolean cancel)
290 {
291 static ObClient *first = NULL;
292 ObClient *ft = NULL;
293 ObClient *ret = NULL;
294
295 if (cancel) {
296 focus_cycle_target = NULL;
297 goto done_cycle;
298 } else if (done && interactive)
299 goto done_cycle;
300
301 if (!focus_order)
302 goto done_cycle;
303
304 if (focus_cycle_target == NULL) {
305 focus_cycle_linear = FALSE;
306 focus_cycle_iconic_windows = FALSE;
307 focus_cycle_all_desktops = FALSE;
308 focus_cycle_nonhilite_windows = TRUE;
309 focus_cycle_dock_windows = dock_windows;
310 focus_cycle_desktop_windows = desktop_windows;
311 }
312
313 if (!first) first = focus_client;
314
315 if (focus_cycle_target)
316 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
317 desktop_windows);
318 else if (first)
319 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
320 else {
321 GList *it;
322
323 for (it = focus_order; it; it = g_list_next(it))
324 if (focus_cycle_valid(it->data)) {
325 ft = it->data;
326 break;
327 }
328 }
329
330 if (ft && ft != focus_cycle_target) {/* prevents flicker */
331 focus_cycle_target = ft;
332 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
333 if (!interactive)
334 goto done_cycle;
335 focus_cycle_draw_indicator(showbar ? ft : NULL);
336 }
337 if (focus_cycle_target && dialog)
338 /* same arguments as focus_target_valid */
339 focus_cycle_popup_single_show(focus_cycle_target);
340 return focus_cycle_target;
341
342 done_cycle:
343 if (done && !cancel) ret = focus_cycle_target;
344
345 first = NULL;
346 focus_cycle_target = NULL;
347 focus_cycle_type = OB_CYCLE_NONE;
348
349 focus_cycle_draw_indicator(NULL);
350 focus_cycle_popup_single_hide();
351
352 return ret;
353 }
354
355 gboolean focus_cycle_valid(struct _ObClient *client)
356 {
357 return focus_valid_target(client, screen_desktop, TRUE,
358 focus_cycle_iconic_windows,
359 focus_cycle_all_desktops,
360 focus_cycle_nonhilite_windows,
361 focus_cycle_dock_windows,
362 focus_cycle_desktop_windows,
363 FALSE);
364 }
This page took 0.042761 seconds and 3 git commands to generate.