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