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