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