]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle.c
move focus_cycle_indicator 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 "client.h"
23 #include "frame.h"
24 #include "focus.h"
25 #include "screen.h"
26 #include "openbox.h"
27 #include "popup.h"
28 #include "debug.h"
29 #include "group.h"
30
31 #include <X11/Xlib.h>
32 #include <glib.h>
33
34 #define FOCUS_CYCLE_ICON_SIZE 48
35
36 struct _ObFocusCyclePopup
37 {
38 ObWindow obwin;
39 Window bg;
40
41 Window text;
42
43 RrAppearance *a_bg;
44 RrAppearance *a_text;
45 gint textw;
46 gint h;
47 gint minw;
48 gint maxw;
49 gboolean mapped;
50 };
51
52 ObClient *focus_cycle_target = NULL;
53
54 static ObIconPopup *focus_cycle_popup;
55
56 static void focus_cycle_destroy_notify (ObClient *client, gpointer data);
57 static gboolean focus_target_valid (ObClient *ft,
58 gboolean all_desktops,
59 gboolean dock_windows,
60 gboolean desktop_windows);
61 static gboolean focus_target_has_siblings (ObClient *ft,
62 gboolean all_desktops);
63 static gchar *popup_get_name (ObClient *c,
64 ObClient **nametarget);
65 static void popup_cycle (ObClient *c,
66 gboolean show,
67 gboolean all_desktops,
68 gboolean dock_windows,
69 gboolean desktop_windows);
70 static ObClient *focus_find_directional (ObClient *c,
71 ObDirection dir,
72 gboolean dock_windows,
73 gboolean desktop_windows);
74 static ObClient *focus_find_directional (ObClient *c,
75 ObDirection dir,
76 gboolean dock_windows,
77 gboolean desktop_windows);
78
79 void focus_cycle_startup(gboolean reconfig)
80 {
81 focus_cycle_popup = icon_popup_new(TRUE);
82
83 if (!reconfig)
84 client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
85 }
86
87 void focus_cycle_shutdown(gboolean reconfig)
88 {
89 icon_popup_free(focus_cycle_popup);
90
91 if (!reconfig)
92 client_remove_destroy_notify(focus_cycle_destroy_notify);
93 }
94
95 void focus_cycle_stop()
96 {
97 if (focus_cycle_target)
98 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
99 }
100
101
102 static gchar *popup_get_name(ObClient *c, ObClient **nametarget)
103 {
104 ObClient *p;
105 gchar *title = NULL;
106 const gchar *desk = NULL;
107 gchar *ret;
108
109 /* find our highest direct parent, including non-normal windows */
110 for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
111 p = p->transient_for);
112
113 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
114 desk = screen_desktop_names[c->desktop];
115
116 /* use the transient's parent's title/icon if we don't have one */
117 if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
118 title = g_strdup(p->iconic ? p->icon_title : p->title);
119
120 if (title == NULL)
121 title = g_strdup(c->iconic ? c->icon_title : c->title);
122
123 if (desk)
124 ret = g_strdup_printf("%s [%s]", title, desk);
125 else {
126 ret = title;
127 title = NULL;
128 }
129 g_free(title);
130
131 /* set this only if we're returning true and they asked for it */
132 if (ret && nametarget) *nametarget = p;
133 return ret;
134 }
135
136 static void popup_cycle(ObClient *c, gboolean show,
137 gboolean all_desktops, gboolean dock_windows,
138 gboolean desktop_windows)
139 {
140 gchar *showtext = NULL;
141 ObClient *showtarget;
142
143 if (!show) {
144 icon_popup_hide(focus_cycle_popup);
145 return;
146 }
147
148 /* do this stuff only when the dialog is first showing */
149 if (!focus_cycle_popup->popup->mapped &&
150 !focus_cycle_popup->popup->delay_mapped)
151 {
152 Rect *a;
153 gchar **names;
154 GList *targets = NULL, *it;
155 gint n = 0, i;
156
157 /* position the popup */
158 a = screen_physical_area_monitor(0);
159 icon_popup_position(focus_cycle_popup, CenterGravity,
160 a->x + a->width / 2, a->y + a->height / 2);
161 icon_popup_height(focus_cycle_popup, POPUP_HEIGHT);
162 icon_popup_min_width(focus_cycle_popup, POPUP_WIDTH);
163 icon_popup_max_width(focus_cycle_popup,
164 MAX(a->width/3, POPUP_WIDTH));
165
166
167 /* make its width to be the width of all the possible titles */
168
169 /* build a list of all the valid focus targets */
170 for (it = focus_order; it; it = g_list_next(it)) {
171 ObClient *ft = it->data;
172 if (focus_target_valid(ft, all_desktops, dock_windows
173 , desktop_windows))
174 {
175 targets = g_list_prepend(targets, ft);
176 ++n;
177 }
178 }
179 /* make it null terminated so we can use g_strfreev */
180 names = g_new(char*, n+1);
181 for (it = targets, i = 0; it; it = g_list_next(it), ++i) {
182 ObClient *ft = it->data, *t;
183 names[i] = popup_get_name(ft, &t);
184
185 /* little optimization.. save this text and client, so we dont
186 have to get it again */
187 if (ft == c) {
188 showtext = g_strdup(names[i]);
189 showtarget = t;
190 }
191 }
192 names[n] = NULL;
193
194 icon_popup_text_width_to_strings(focus_cycle_popup, names, n);
195 g_strfreev(names);
196 }
197
198
199 if (!showtext) showtext = popup_get_name(c, &showtarget);
200 icon_popup_show(focus_cycle_popup, showtext,
201 client_icon(showtarget, 48, 48));
202 g_free(showtext);
203 }
204
205 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
206 {
207 /* end cycling if the target disappears. CurrentTime is fine, time won't
208 be used
209 */
210 if (focus_cycle_target == client)
211 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
212 }
213
214 /*! Returns if a focus target has valid group siblings that can be cycled
215 to in its place */
216 static gboolean focus_target_has_siblings(ObClient *ft, gboolean all_desktops)
217
218 {
219 GSList *it;
220
221 if (!ft->group) return FALSE;
222
223 for (it = ft->group->members; it; it = g_slist_next(it)) {
224 ObClient *c = it->data;
225 /* check that it's not a helper window to avoid infinite recursion */
226 if (c != ft && !client_helper(c) &&
227 focus_target_valid(c, all_desktops, FALSE, FALSE))
228 {
229 return TRUE;
230 }
231 }
232 return FALSE;
233 }
234
235 /*! @param allow_helpers This is used for calling itself recursively while
236 checking helper windows. */
237 static gboolean focus_target_valid(ObClient *ft,
238 gboolean all_desktops,
239 gboolean dock_windows,
240 gboolean desktop_windows)
241 {
242 gboolean ok = FALSE;
243
244 /* it's on this desktop unless you want all desktops.
245
246 do this check first because it will usually filter out the most
247 windows */
248 ok = (all_desktops || ft->desktop == screen_desktop ||
249 ft->desktop == DESKTOP_ALL);
250
251 /* the window can receive focus somehow */
252 ok = ok && (ft->can_focus || ft->focus_notify);
253
254 /* it's the right type of window */
255 if (dock_windows || desktop_windows)
256 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
257 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
258 else
259 /* normal non-helper windows are valid targets */
260 ok = ok &&
261 ((client_normal(ft) && !client_helper(ft))
262 ||
263 /* helper windows are valid targets it... */
264 (client_helper(ft) &&
265 /* ...a window in its group already has focus ... */
266 ((focus_client && ft->group == focus_client->group) ||
267 /* ... or if there are no other windows in its group
268 that can be cycled to instead */
269 !focus_target_has_siblings(ft, all_desktops))));
270
271 /* it's not set to skip the taskbar (unless it is a type that would be
272 expected to set this hint */
273 ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
274 ft->type == OB_CLIENT_TYPE_DESKTOP ||
275 ft->type == OB_CLIENT_TYPE_TOOLBAR ||
276 ft->type == OB_CLIENT_TYPE_MENU ||
277 ft->type == OB_CLIENT_TYPE_UTILITY) ||
278 !ft->skip_taskbar);
279
280 /* it's not going to just send fous off somewhere else (modal window) */
281 ok = ok && ft == client_focus_target(ft);
282
283 return ok;
284 }
285
286 void focus_cycle(gboolean forward, gboolean all_desktops,
287 gboolean dock_windows, gboolean desktop_windows,
288 gboolean linear, gboolean interactive,
289 gboolean dialog, gboolean done, gboolean cancel)
290 {
291 static ObClient *first = NULL;
292 static ObClient *t = NULL;
293 static GList *order = NULL;
294 GList *it, *start, *list;
295 ObClient *ft = NULL;
296
297 if (interactive) {
298 if (cancel) {
299 focus_cycle_target = NULL;
300 goto done_cycle;
301 } else if (done)
302 goto done_cycle;
303
304 if (!focus_order)
305 goto done_cycle;
306
307 if (!first) first = focus_client;
308
309 if (linear) list = client_list;
310 else list = focus_order;
311 } else {
312 if (!focus_order)
313 goto done_cycle;
314 list = client_list;
315 }
316 if (!focus_cycle_target) focus_cycle_target = focus_client;
317
318 start = it = g_list_find(list, focus_cycle_target);
319 if (!start) /* switched desktops or something? */
320 start = it = forward ? g_list_last(list) : g_list_first(list);
321 if (!start) goto done_cycle;
322
323 do {
324 if (forward) {
325 it = it->next;
326 if (it == NULL) it = g_list_first(list);
327 } else {
328 it = it->prev;
329 if (it == NULL) it = g_list_last(list);
330 }
331 ft = it->data;
332 if (focus_target_valid(ft, all_desktops, dock_windows,
333 desktop_windows))
334 {
335 if (interactive) {
336 if (ft != focus_cycle_target) { /* prevents flicker */
337 focus_cycle_target = ft;
338 focus_cycle_draw_indicator(ft);
339 }
340 /* same arguments as focus_target_valid */
341 popup_cycle(ft, dialog, all_desktops, dock_windows,
342 desktop_windows);
343 return;
344 } else if (ft != focus_cycle_target) {
345 focus_cycle_target = ft;
346 done = TRUE;
347 break;
348 }
349 }
350 } while (it != start);
351
352 done_cycle:
353 if (done && focus_cycle_target)
354 client_activate(focus_cycle_target, FALSE, TRUE);
355
356 t = NULL;
357 first = NULL;
358 focus_cycle_target = NULL;
359 g_list_free(order);
360 order = NULL;
361
362 if (interactive) {
363 focus_cycle_draw_indicator(NULL);
364 popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
365 }
366
367 return;
368 }
369
370 /* this be mostly ripped from fvwm */
371 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
372 gboolean dock_windows,
373 gboolean desktop_windows)
374 {
375 gint my_cx, my_cy, his_cx, his_cy;
376 gint offset = 0;
377 gint distance = 0;
378 gint score, best_score;
379 ObClient *best_client, *cur;
380 GList *it;
381
382 if(!client_list)
383 return NULL;
384
385 /* first, find the centre coords of the currently focused window */
386 my_cx = c->frame->area.x + c->frame->area.width / 2;
387 my_cy = c->frame->area.y + c->frame->area.height / 2;
388
389 best_score = -1;
390 best_client = NULL;
391
392 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
393 cur = it->data;
394
395 /* the currently selected window isn't interesting */
396 if(cur == c)
397 continue;
398 if (cur->type == OB_CLIENT_TYPE_DOCK && !dock_windows)
399 continue;
400 if (cur->type == OB_CLIENT_TYPE_DESKTOP && !desktop_windows)
401 continue;
402 if (!client_normal(cur) &&
403 cur->type != OB_CLIENT_TYPE_DOCK &&
404 cur->type != OB_CLIENT_TYPE_DESKTOP)
405 continue;
406 /* using c->desktop instead of screen_desktop doesn't work if the
407 * current window was omnipresent, hope this doesn't have any other
408 * side effects */
409 if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
410 continue;
411 if(cur->iconic)
412 continue;
413 if(!(client_focus_target(cur) == cur &&
414 client_can_focus(cur)))
415 continue;
416
417 /* find the centre coords of this window, from the
418 * currently focused window's point of view */
419 his_cx = (cur->frame->area.x - my_cx)
420 + cur->frame->area.width / 2;
421 his_cy = (cur->frame->area.y - my_cy)
422 + cur->frame->area.height / 2;
423
424 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
425 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
426 gint tx;
427 /* Rotate the diagonals 45 degrees counterclockwise.
428 * To do this, multiply the matrix /+h +h\ with the
429 * vector (x y). \-h +h/
430 * h = sqrt(0.5). We can set h := 1 since absolute
431 * distance doesn't matter here. */
432 tx = his_cx + his_cy;
433 his_cy = -his_cx + his_cy;
434 his_cx = tx;
435 }
436
437 switch(dir) {
438 case OB_DIRECTION_NORTH:
439 case OB_DIRECTION_SOUTH:
440 case OB_DIRECTION_NORTHEAST:
441 case OB_DIRECTION_SOUTHWEST:
442 offset = (his_cx < 0) ? -his_cx : his_cx;
443 distance = ((dir == OB_DIRECTION_NORTH ||
444 dir == OB_DIRECTION_NORTHEAST) ?
445 -his_cy : his_cy);
446 break;
447 case OB_DIRECTION_EAST:
448 case OB_DIRECTION_WEST:
449 case OB_DIRECTION_SOUTHEAST:
450 case OB_DIRECTION_NORTHWEST:
451 offset = (his_cy < 0) ? -his_cy : his_cy;
452 distance = ((dir == OB_DIRECTION_WEST ||
453 dir == OB_DIRECTION_NORTHWEST) ?
454 -his_cx : his_cx);
455 break;
456 }
457
458 /* the target must be in the requested direction */
459 if(distance <= 0)
460 continue;
461
462 /* Calculate score for this window. The smaller the better. */
463 score = distance + offset;
464
465 /* windows more than 45 degrees off the direction are
466 * heavily penalized and will only be chosen if nothing
467 * else within a million pixels */
468 if(offset > distance)
469 score += 1000000;
470
471 if(best_score == -1 || score < best_score)
472 best_client = cur,
473 best_score = score;
474 }
475
476 return best_client;
477 }
478
479 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
480 gboolean desktop_windows, gboolean interactive,
481 gboolean dialog, gboolean done, gboolean cancel)
482 {
483 static ObClient *first = NULL;
484 ObClient *ft = NULL;
485
486 if (!interactive)
487 return;
488
489 if (cancel) {
490 focus_cycle_target = NULL;
491 goto done_cycle;
492 } else if (done)
493 goto done_cycle;
494
495 if (!focus_order)
496 goto done_cycle;
497
498 if (!first) first = focus_client;
499 if (!focus_cycle_target) focus_cycle_target = focus_client;
500
501 if (focus_cycle_target)
502 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
503 desktop_windows);
504 else {
505 GList *it;
506
507 for (it = focus_order; it; it = g_list_next(it))
508 if (focus_target_valid(it->data, FALSE, dock_windows,
509 desktop_windows))
510 ft = it->data;
511 }
512
513 if (ft) {
514 if (ft != focus_cycle_target) {/* prevents flicker */
515 focus_cycle_target = ft;
516 focus_cycle_draw_indicator(ft);
517 }
518 }
519 if (focus_cycle_target) {
520 /* same arguments as focus_target_valid */
521 popup_cycle(focus_cycle_target, dialog, FALSE, dock_windows,
522 desktop_windows);
523 if (dialog)
524 return;
525 }
526
527
528 done_cycle:
529 if (done && focus_cycle_target)
530 client_activate(focus_cycle_target, FALSE, TRUE);
531
532 first = NULL;
533 focus_cycle_target = NULL;
534
535 focus_cycle_draw_indicator(NULL);
536 popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
537
538 return;
539 }
540
541 void focus_order_add_new(ObClient *c)
542 {
543 if (c->iconic)
544 focus_order_to_top(c);
545 else {
546 g_assert(!g_list_find(focus_order, c));
547 /* if there are any iconic windows, put this above them in the order,
548 but if there are not, then put it under the currently focused one */
549 if (focus_order && ((ObClient*)focus_order->data)->iconic)
550 focus_order = g_list_insert(focus_order, c, 0);
551 else
552 focus_order = g_list_insert(focus_order, c, 1);
553 }
554 }
555
This page took 0.063684 seconds and 5 git commands to generate.