]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle.c
use focus_cycle_stop to cancel focus cycling
[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 #include "group.h"
30
31 #include <X11/Xlib.h>
32 #include <glib.h>
33
34 ObClient *focus_cycle_target = NULL;
35
36 static void focus_cycle_destroy_notify (ObClient *client, gpointer data);
37 static gboolean focus_target_has_siblings (ObClient *ft,
38 gboolean iconic_windows,
39 gboolean all_desktops);
40 static ObClient *focus_find_directional (ObClient *c,
41 ObDirection dir,
42 gboolean dock_windows,
43 gboolean desktop_windows);
44 static ObClient *focus_find_directional (ObClient *c,
45 ObDirection dir,
46 gboolean dock_windows,
47 gboolean desktop_windows);
48
49 void focus_cycle_startup(gboolean reconfig)
50 {
51 if (reconfig) return;
52
53 client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
54 }
55
56 void focus_cycle_shutdown(gboolean reconfig)
57 {
58 if (reconfig) return;
59
60 client_remove_destroy_notify(focus_cycle_destroy_notify);
61 }
62
63 void focus_cycle_stop()
64 {
65 if (focus_cycle_target) {
66 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
67 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
68 }
69 }
70
71 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
72 {
73 /* end cycling if the target disappears. CurrentTime is fine, time won't
74 be used */
75 if (focus_cycle_target == client)
76 focus_cycle_stop();
77 }
78
79 /*! Returns if a focus target has valid group siblings that can be cycled
80 to in its place */
81 static gboolean focus_target_has_siblings(ObClient *ft,
82 gboolean iconic_windows,
83 gboolean all_desktops)
84
85 {
86 GSList *it;
87
88 if (!ft->group) return FALSE;
89
90 for (it = ft->group->members; it; it = g_slist_next(it)) {
91 ObClient *c = it->data;
92 /* check that it's not a helper window to avoid infinite recursion */
93 if (c != ft && !client_helper(c) &&
94 focus_cycle_target_valid(c, iconic_windows, all_desktops, FALSE,
95 FALSE))
96 {
97 return TRUE;
98 }
99 }
100 return FALSE;
101 }
102
103 gboolean focus_cycle_target_valid(ObClient *ft,
104 gboolean iconic_windows,
105 gboolean all_desktops,
106 gboolean dock_windows,
107 gboolean desktop_windows)
108 {
109 gboolean ok = FALSE;
110
111 /* it's on this desktop unless you want all desktops.
112
113 do this check first because it will usually filter out the most
114 windows */
115 ok = (all_desktops || ft->desktop == screen_desktop ||
116 ft->desktop == DESKTOP_ALL);
117
118 /* the window can receive focus somehow */
119 ok = ok && (ft->can_focus || ft->focus_notify);
120
121 /* the window is not iconic, or we're allowed to go to iconic ones */
122 ok = ok && (iconic_windows || !ft->iconic);
123
124 /* it's the right type of window */
125 if (dock_windows || desktop_windows)
126 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
127 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
128 /* modal windows are important and can always get focus if they are
129 visible and stuff, so don't change 'ok' based on their type */
130 else if (!ft->modal)
131 /* normal non-helper windows are valid targets */
132 ok = ok &&
133 ((client_normal(ft) && !client_helper(ft))
134 ||
135 /* helper windows are valid targets it... */
136 (client_helper(ft) &&
137 /* ...a window in its group already has focus ... */
138 ((focus_client && ft->group == focus_client->group) ||
139 /* ... or if there are no other windows in its group
140 that can be cycled to instead */
141 !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
142
143 /* it's not set to skip the taskbar (unless it is a type that would be
144 expected to set this hint, or modal) */
145 ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
146 ft->type == OB_CLIENT_TYPE_DESKTOP ||
147 ft->type == OB_CLIENT_TYPE_TOOLBAR ||
148 ft->type == OB_CLIENT_TYPE_MENU ||
149 ft->type == OB_CLIENT_TYPE_UTILITY) ||
150 ft->modal ||
151 !ft->skip_taskbar);
152
153 /* it's not going to just send fous off somewhere else (modal window) */
154 ok = ok && ft == client_focus_target(ft);
155
156 return ok;
157 }
158
159 void focus_cycle(gboolean forward, gboolean all_desktops,
160 gboolean dock_windows, gboolean desktop_windows,
161 gboolean linear, gboolean interactive,
162 gboolean dialog, gboolean done, gboolean cancel)
163 {
164 static ObClient *first = NULL;
165 static ObClient *t = NULL;
166 static GList *order = NULL;
167 GList *it, *start, *list;
168 ObClient *ft = NULL;
169
170 if (interactive) {
171 if (cancel) {
172 focus_cycle_target = NULL;
173 goto done_cycle;
174 } else if (done)
175 goto done_cycle;
176
177 if (!focus_order)
178 goto done_cycle;
179
180 if (!first) first = focus_client;
181
182 if (linear) list = client_list;
183 else list = focus_order;
184 } else {
185 if (!focus_order)
186 goto done_cycle;
187 list = client_list;
188 }
189 if (!focus_cycle_target) focus_cycle_target = focus_client;
190
191 start = it = g_list_find(list, focus_cycle_target);
192 if (!start) /* switched desktops or something? */
193 start = it = forward ? g_list_last(list) : g_list_first(list);
194 if (!start) goto done_cycle;
195
196 do {
197 if (forward) {
198 it = it->next;
199 if (it == NULL) it = g_list_first(list);
200 } else {
201 it = it->prev;
202 if (it == NULL) it = g_list_last(list);
203 }
204 ft = it->data;
205 if (focus_cycle_target_valid(ft, TRUE,
206 all_desktops, dock_windows,
207 desktop_windows))
208 {
209 if (interactive) {
210 if (ft != focus_cycle_target) { /* prevents flicker */
211 focus_cycle_target = ft;
212 focus_cycle_draw_indicator(ft);
213 }
214 if (dialog)
215 /* same arguments as focus_target_valid */
216 focus_cycle_popup_show(ft, TRUE,
217 all_desktops, dock_windows,
218 desktop_windows);
219 return;
220 } else if (ft != focus_cycle_target) {
221 focus_cycle_target = ft;
222 done = TRUE;
223 break;
224 }
225 }
226 } while (it != start);
227
228 done_cycle:
229 if (done && focus_cycle_target)
230 client_activate(focus_cycle_target, FALSE, TRUE);
231
232 t = NULL;
233 first = NULL;
234 focus_cycle_target = NULL;
235 g_list_free(order);
236 order = NULL;
237
238 if (interactive) {
239 focus_cycle_draw_indicator(NULL);
240 focus_cycle_popup_hide();
241 }
242
243 return;
244 }
245
246 /* this be mostly ripped from fvwm */
247 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
248 gboolean dock_windows,
249 gboolean desktop_windows)
250 {
251 gint my_cx, my_cy, his_cx, his_cy;
252 gint offset = 0;
253 gint distance = 0;
254 gint score, best_score;
255 ObClient *best_client, *cur;
256 GList *it;
257
258 if(!client_list)
259 return NULL;
260
261 /* first, find the centre coords of the currently focused window */
262 my_cx = c->frame->area.x + c->frame->area.width / 2;
263 my_cy = c->frame->area.y + c->frame->area.height / 2;
264
265 best_score = -1;
266 best_client = NULL;
267
268 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
269 cur = it->data;
270
271 /* the currently selected window isn't interesting */
272 if(cur == c)
273 continue;
274 if (!focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
275 desktop_windows))
276 continue;
277
278 /* find the centre coords of this window, from the
279 * currently focused window's point of view */
280 his_cx = (cur->frame->area.x - my_cx)
281 + cur->frame->area.width / 2;
282 his_cy = (cur->frame->area.y - my_cy)
283 + cur->frame->area.height / 2;
284
285 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
286 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
287 gint tx;
288 /* Rotate the diagonals 45 degrees counterclockwise.
289 * To do this, multiply the matrix /+h +h\ with the
290 * vector (x y). \-h +h/
291 * h = sqrt(0.5). We can set h := 1 since absolute
292 * distance doesn't matter here. */
293 tx = his_cx + his_cy;
294 his_cy = -his_cx + his_cy;
295 his_cx = tx;
296 }
297
298 switch(dir) {
299 case OB_DIRECTION_NORTH:
300 case OB_DIRECTION_SOUTH:
301 case OB_DIRECTION_NORTHEAST:
302 case OB_DIRECTION_SOUTHWEST:
303 offset = (his_cx < 0) ? -his_cx : his_cx;
304 distance = ((dir == OB_DIRECTION_NORTH ||
305 dir == OB_DIRECTION_NORTHEAST) ?
306 -his_cy : his_cy);
307 break;
308 case OB_DIRECTION_EAST:
309 case OB_DIRECTION_WEST:
310 case OB_DIRECTION_SOUTHEAST:
311 case OB_DIRECTION_NORTHWEST:
312 offset = (his_cy < 0) ? -his_cy : his_cy;
313 distance = ((dir == OB_DIRECTION_WEST ||
314 dir == OB_DIRECTION_NORTHWEST) ?
315 -his_cx : his_cx);
316 break;
317 }
318
319 /* the target must be in the requested direction */
320 if(distance <= 0)
321 continue;
322
323 /* Calculate score for this window. The smaller the better. */
324 score = distance + offset;
325
326 /* windows more than 45 degrees off the direction are
327 * heavily penalized and will only be chosen if nothing
328 * else within a million pixels */
329 if(offset > distance)
330 score += 1000000;
331
332 if(best_score == -1 || score < best_score)
333 best_client = cur,
334 best_score = score;
335 }
336
337 return best_client;
338 }
339
340 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
341 gboolean desktop_windows, gboolean interactive,
342 gboolean dialog, gboolean done, gboolean cancel)
343 {
344 static ObClient *first = NULL;
345 ObClient *ft = NULL;
346
347 if (!interactive)
348 return;
349
350 if (cancel) {
351 focus_cycle_target = NULL;
352 goto done_cycle;
353 } else if (done)
354 goto done_cycle;
355
356 if (!focus_order)
357 goto done_cycle;
358
359 if (!first) first = focus_client;
360 if (!focus_cycle_target) focus_cycle_target = focus_client;
361
362 if (focus_cycle_target)
363 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
364 desktop_windows);
365 else {
366 GList *it;
367
368 for (it = focus_order; it; it = g_list_next(it))
369 if (focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
370 desktop_windows))
371 ft = it->data;
372 }
373
374 if (ft) {
375 if (ft != focus_cycle_target) {/* prevents flicker */
376 focus_cycle_target = ft;
377 focus_cycle_draw_indicator(ft);
378 }
379 }
380 if (focus_cycle_target && dialog) {
381 /* same arguments as focus_target_valid */
382 focus_cycle_popup_single_show(focus_cycle_target,
383 FALSE, FALSE, dock_windows,
384 desktop_windows);
385 return;
386 }
387
388 done_cycle:
389 if (done && focus_cycle_target)
390 client_activate(focus_cycle_target, FALSE, TRUE);
391
392 first = NULL;
393 focus_cycle_target = NULL;
394
395 focus_cycle_draw_indicator(NULL);
396 focus_cycle_popup_single_hide();
397
398 return;
399 }
This page took 0.057691 seconds and 5 git commands to generate.