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