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