1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "focus_cycle.h"
30 #include "render/render.h"
35 #define FOCUS_INDICATOR_WIDTH 6
41 InternalWindow bottom;
44 ObClient *focus_cycle_target = NULL;
46 static RrAppearance *a_focus_indicator;
47 static RrColor *color_white;
48 static ObIconPopup *focus_cycle_popup;
50 static void focus_cycle_destroy_notify (ObClient *client, gpointer data);
51 static Window create_window (Window parent, gulong mask,
52 XSetWindowAttributes *attrib);
53 static gboolean focus_target_valid (ObClient *ft,
54 gboolean all_desktops,
55 gboolean dock_windows,
56 gboolean desktop_windows);
57 static gboolean focus_target_has_siblings (ObClient *ft,
58 gboolean all_desktops);
59 static gchar *popup_get_name (ObClient *c,
60 ObClient **nametarget);
61 static void popup_cycle (ObClient *c,
63 gboolean all_desktops,
64 gboolean dock_windows,
65 gboolean desktop_windows);
66 static ObClient *focus_find_directional (ObClient *c,
68 gboolean dock_windows,
69 gboolean desktop_windows);
70 static ObClient *focus_find_directional (ObClient *c,
72 gboolean dock_windows,
73 gboolean desktop_windows);
75 void focus_cycle_startup(gboolean reconfig)
77 focus_cycle_popup = icon_popup_new(TRUE);
80 XSetWindowAttributes attr;
82 client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
84 focus_indicator.top.obwin.type = Window_Internal;
85 focus_indicator.left.obwin.type = Window_Internal;
86 focus_indicator.right.obwin.type = Window_Internal;
87 focus_indicator.bottom.obwin.type = Window_Internal;
89 attr.override_redirect = True;
90 attr.background_pixel = BlackPixel(ob_display, ob_screen);
91 focus_indicator.top.win =
92 create_window(RootWindow(ob_display, ob_screen),
93 CWOverrideRedirect | CWBackPixel, &attr);
94 focus_indicator.left.win =
95 create_window(RootWindow(ob_display, ob_screen),
96 CWOverrideRedirect | CWBackPixel, &attr);
97 focus_indicator.right.win =
98 create_window(RootWindow(ob_display, ob_screen),
99 CWOverrideRedirect | CWBackPixel, &attr);
100 focus_indicator.bottom.win =
101 create_window(RootWindow(ob_display, ob_screen),
102 CWOverrideRedirect | CWBackPixel, &attr);
104 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.top));
105 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.left));
106 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.right));
107 stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.bottom));
109 color_white = RrColorNew(ob_rr_inst, 0xff, 0xff, 0xff);
111 a_focus_indicator = RrAppearanceNew(ob_rr_inst, 4);
112 a_focus_indicator->surface.grad = RR_SURFACE_SOLID;
113 a_focus_indicator->surface.relief = RR_RELIEF_FLAT;
114 a_focus_indicator->surface.primary = RrColorNew(ob_rr_inst,
116 a_focus_indicator->texture[0].type = RR_TEXTURE_LINE_ART;
117 a_focus_indicator->texture[0].data.lineart.color = color_white;
118 a_focus_indicator->texture[1].type = RR_TEXTURE_LINE_ART;
119 a_focus_indicator->texture[1].data.lineart.color = color_white;
120 a_focus_indicator->texture[2].type = RR_TEXTURE_LINE_ART;
121 a_focus_indicator->texture[2].data.lineart.color = color_white;
122 a_focus_indicator->texture[3].type = RR_TEXTURE_LINE_ART;
123 a_focus_indicator->texture[3].data.lineart.color = color_white;
127 void focus_cycle_shutdown(gboolean reconfig)
129 icon_popup_free(focus_cycle_popup);
132 client_remove_destroy_notify(focus_cycle_destroy_notify);
134 RrColorFree(color_white);
136 RrAppearanceFree(a_focus_indicator);
138 XDestroyWindow(ob_display, focus_indicator.top.win);
139 XDestroyWindow(ob_display, focus_indicator.left.win);
140 XDestroyWindow(ob_display, focus_indicator.right.win);
141 XDestroyWindow(ob_display, focus_indicator.bottom.win);
145 void focus_cycle_stop()
147 if (focus_cycle_target)
148 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
152 static Window create_window(Window parent, gulong mask,
153 XSetWindowAttributes *attrib)
155 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
156 RrDepth(ob_rr_inst), InputOutput,
157 RrVisual(ob_rr_inst), mask, attrib);
161 static gchar *popup_get_name(ObClient *c, ObClient **nametarget)
165 const gchar *desk = NULL;
168 /* find our highest direct parent, including non-normal windows */
169 for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
170 p = p->transient_for);
172 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
173 desk = screen_desktop_names[c->desktop];
175 /* use the transient's parent's title/icon if we don't have one */
176 if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
177 title = g_strdup(p->iconic ? p->icon_title : p->title);
180 title = g_strdup(c->iconic ? c->icon_title : c->title);
183 ret = g_strdup_printf("%s [%s]", title, desk);
190 /* set this only if we're returning true and they asked for it */
191 if (ret && nametarget) *nametarget = p;
195 static void popup_cycle(ObClient *c, gboolean show,
196 gboolean all_desktops, gboolean dock_windows,
197 gboolean desktop_windows)
199 gchar *showtext = NULL;
200 ObClient *showtarget;
203 icon_popup_hide(focus_cycle_popup);
207 /* do this stuff only when the dialog is first showing */
208 if (!focus_cycle_popup->popup->mapped &&
209 !focus_cycle_popup->popup->delay_mapped)
213 GList *targets = NULL, *it;
216 /* position the popup */
217 a = screen_physical_area_monitor(0);
218 icon_popup_position(focus_cycle_popup, CenterGravity,
219 a->x + a->width / 2, a->y + a->height / 2);
220 icon_popup_height(focus_cycle_popup, POPUP_HEIGHT);
221 icon_popup_min_width(focus_cycle_popup, POPUP_WIDTH);
222 icon_popup_max_width(focus_cycle_popup,
223 MAX(a->width/3, POPUP_WIDTH));
226 /* make its width to be the width of all the possible titles */
228 /* build a list of all the valid focus targets */
229 for (it = focus_order; it; it = g_list_next(it)) {
230 ObClient *ft = it->data;
231 if (focus_target_valid(ft, all_desktops, dock_windows
234 targets = g_list_prepend(targets, ft);
238 /* make it null terminated so we can use g_strfreev */
239 names = g_new(char*, n+1);
240 for (it = targets, i = 0; it; it = g_list_next(it), ++i) {
241 ObClient *ft = it->data, *t;
242 names[i] = popup_get_name(ft, &t);
244 /* little optimization.. save this text and client, so we dont
245 have to get it again */
247 showtext = g_strdup(names[i]);
253 icon_popup_text_width_to_strings(focus_cycle_popup, names, n);
258 if (!showtext) showtext = popup_get_name(c, &showtarget);
259 icon_popup_show(focus_cycle_popup, showtext,
260 client_icon(showtarget, 48, 48));
264 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
266 /* end cycling if the target disappears. CurrentTime is fine, time won't
269 if (focus_cycle_target == client)
270 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
273 void focus_cycle_draw_indicator()
275 if (!focus_cycle_target) {
276 XUnmapWindow(ob_display, focus_indicator.top.win);
277 XUnmapWindow(ob_display, focus_indicator.left.win);
278 XUnmapWindow(ob_display, focus_indicator.right.win);
279 XUnmapWindow(ob_display, focus_indicator.bottom.win);
281 /* kill enter events cause by this unmapping */
282 event_ignore_all_queued_enters();
285 if (focus_cycle_target)
286 frame_adjust_focus(focus_cycle_target->frame, FALSE);
287 frame_adjust_focus(focus_cycle_target->frame, TRUE);
292 wt = wl = wr = wb = FOCUS_INDICATOR_WIDTH;
294 x = focus_cycle_target->frame->area.x;
295 y = focus_cycle_target->frame->area.y;
296 w = focus_cycle_target->frame->area.width;
299 XMoveResizeWindow(ob_display, focus_indicator.top.win,
301 a_focus_indicator->texture[0].data.lineart.x1 = 0;
302 a_focus_indicator->texture[0].data.lineart.y1 = h-1;
303 a_focus_indicator->texture[0].data.lineart.x2 = 0;
304 a_focus_indicator->texture[0].data.lineart.y2 = 0;
305 a_focus_indicator->texture[1].data.lineart.x1 = 0;
306 a_focus_indicator->texture[1].data.lineart.y1 = 0;
307 a_focus_indicator->texture[1].data.lineart.x2 = w-1;
308 a_focus_indicator->texture[1].data.lineart.y2 = 0;
309 a_focus_indicator->texture[2].data.lineart.x1 = w-1;
310 a_focus_indicator->texture[2].data.lineart.y1 = 0;
311 a_focus_indicator->texture[2].data.lineart.x2 = w-1;
312 a_focus_indicator->texture[2].data.lineart.y2 = h-1;
313 a_focus_indicator->texture[3].data.lineart.x1 = (wl-1);
314 a_focus_indicator->texture[3].data.lineart.y1 = h-1;
315 a_focus_indicator->texture[3].data.lineart.x2 = w - wr;
316 a_focus_indicator->texture[3].data.lineart.y2 = h-1;
317 RrPaint(a_focus_indicator, focus_indicator.top.win,
320 x = focus_cycle_target->frame->area.x;
321 y = focus_cycle_target->frame->area.y;
323 h = focus_cycle_target->frame->area.height;
325 XMoveResizeWindow(ob_display, focus_indicator.left.win,
327 a_focus_indicator->texture[0].data.lineart.x1 = w-1;
328 a_focus_indicator->texture[0].data.lineart.y1 = 0;
329 a_focus_indicator->texture[0].data.lineart.x2 = 0;
330 a_focus_indicator->texture[0].data.lineart.y2 = 0;
331 a_focus_indicator->texture[1].data.lineart.x1 = 0;
332 a_focus_indicator->texture[1].data.lineart.y1 = 0;
333 a_focus_indicator->texture[1].data.lineart.x2 = 0;
334 a_focus_indicator->texture[1].data.lineart.y2 = h-1;
335 a_focus_indicator->texture[2].data.lineart.x1 = 0;
336 a_focus_indicator->texture[2].data.lineart.y1 = h-1;
337 a_focus_indicator->texture[2].data.lineart.x2 = w-1;
338 a_focus_indicator->texture[2].data.lineart.y2 = h-1;
339 a_focus_indicator->texture[3].data.lineart.x1 = w-1;
340 a_focus_indicator->texture[3].data.lineart.y1 = wt-1;
341 a_focus_indicator->texture[3].data.lineart.x2 = w-1;
342 a_focus_indicator->texture[3].data.lineart.y2 = h - wb;
343 RrPaint(a_focus_indicator, focus_indicator.left.win,
346 x = focus_cycle_target->frame->area.x +
347 focus_cycle_target->frame->area.width - wr;
348 y = focus_cycle_target->frame->area.y;
350 h = focus_cycle_target->frame->area.height ;
352 XMoveResizeWindow(ob_display, focus_indicator.right.win,
354 a_focus_indicator->texture[0].data.lineart.x1 = 0;
355 a_focus_indicator->texture[0].data.lineart.y1 = 0;
356 a_focus_indicator->texture[0].data.lineart.x2 = w-1;
357 a_focus_indicator->texture[0].data.lineart.y2 = 0;
358 a_focus_indicator->texture[1].data.lineart.x1 = w-1;
359 a_focus_indicator->texture[1].data.lineart.y1 = 0;
360 a_focus_indicator->texture[1].data.lineart.x2 = w-1;
361 a_focus_indicator->texture[1].data.lineart.y2 = h-1;
362 a_focus_indicator->texture[2].data.lineart.x1 = w-1;
363 a_focus_indicator->texture[2].data.lineart.y1 = h-1;
364 a_focus_indicator->texture[2].data.lineart.x2 = 0;
365 a_focus_indicator->texture[2].data.lineart.y2 = h-1;
366 a_focus_indicator->texture[3].data.lineart.x1 = 0;
367 a_focus_indicator->texture[3].data.lineart.y1 = wt-1;
368 a_focus_indicator->texture[3].data.lineart.x2 = 0;
369 a_focus_indicator->texture[3].data.lineart.y2 = h - wb;
370 RrPaint(a_focus_indicator, focus_indicator.right.win,
373 x = focus_cycle_target->frame->area.x;
374 y = focus_cycle_target->frame->area.y +
375 focus_cycle_target->frame->area.height - wb;
376 w = focus_cycle_target->frame->area.width;
379 XMoveResizeWindow(ob_display, focus_indicator.bottom.win,
381 a_focus_indicator->texture[0].data.lineart.x1 = 0;
382 a_focus_indicator->texture[0].data.lineart.y1 = 0;
383 a_focus_indicator->texture[0].data.lineart.x2 = 0;
384 a_focus_indicator->texture[0].data.lineart.y2 = h-1;
385 a_focus_indicator->texture[1].data.lineart.x1 = 0;
386 a_focus_indicator->texture[1].data.lineart.y1 = h-1;
387 a_focus_indicator->texture[1].data.lineart.x2 = w-1;
388 a_focus_indicator->texture[1].data.lineart.y2 = h-1;
389 a_focus_indicator->texture[2].data.lineart.x1 = w-1;
390 a_focus_indicator->texture[2].data.lineart.y1 = h-1;
391 a_focus_indicator->texture[2].data.lineart.x2 = w-1;
392 a_focus_indicator->texture[2].data.lineart.y2 = 0;
393 a_focus_indicator->texture[3].data.lineart.x1 = wl-1;
394 a_focus_indicator->texture[3].data.lineart.y1 = 0;
395 a_focus_indicator->texture[3].data.lineart.x2 = w - wr;
396 a_focus_indicator->texture[3].data.lineart.y2 = 0;
397 RrPaint(a_focus_indicator, focus_indicator.bottom.win,
400 XMapWindow(ob_display, focus_indicator.top.win);
401 XMapWindow(ob_display, focus_indicator.left.win);
402 XMapWindow(ob_display, focus_indicator.right.win);
403 XMapWindow(ob_display, focus_indicator.bottom.win);
407 /*! Returns if a focus target has valid group siblings that can be cycled
409 static gboolean focus_target_has_siblings(ObClient *ft, gboolean all_desktops)
414 if (!ft->group) return FALSE;
416 for (it = ft->group->members; it; it = g_slist_next(it)) {
417 ObClient *c = it->data;
418 /* check that it's not a helper window to avoid infinite recursion */
419 if (c != ft && !client_helper(c) &&
420 focus_target_valid(c, all_desktops, FALSE, FALSE))
428 /*! @param allow_helpers This is used for calling itself recursively while
429 checking helper windows. */
430 static gboolean focus_target_valid(ObClient *ft,
431 gboolean all_desktops,
432 gboolean dock_windows,
433 gboolean desktop_windows)
437 /* it's on this desktop unless you want all desktops.
439 do this check first because it will usually filter out the most
441 ok = (all_desktops || ft->desktop == screen_desktop ||
442 ft->desktop == DESKTOP_ALL);
444 /* the window can receive focus somehow */
445 ok = ok && (ft->can_focus || ft->focus_notify);
447 /* it's the right type of window */
448 if (dock_windows || desktop_windows)
449 ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
450 (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
452 /* normal non-helper windows are valid targets */
454 ((client_normal(ft) && !client_helper(ft))
456 /* helper windows are valid targets it... */
457 (client_helper(ft) &&
458 /* ...a window in its group already has focus ... */
459 ((focus_client && ft->group == focus_client->group) ||
460 /* ... or if there are no other windows in its group
461 that can be cycled to instead */
462 !focus_target_has_siblings(ft, all_desktops))));
464 /* it's not set to skip the taskbar (unless it is a type that would be
465 expected to set this hint */
466 ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
467 ft->type == OB_CLIENT_TYPE_DESKTOP ||
468 ft->type == OB_CLIENT_TYPE_TOOLBAR ||
469 ft->type == OB_CLIENT_TYPE_MENU ||
470 ft->type == OB_CLIENT_TYPE_UTILITY) ||
473 /* it's not going to just send fous off somewhere else (modal window) */
474 ok = ok && ft == client_focus_target(ft);
479 void focus_cycle(gboolean forward, gboolean all_desktops,
480 gboolean dock_windows, gboolean desktop_windows,
481 gboolean linear, gboolean interactive,
482 gboolean dialog, gboolean done, gboolean cancel)
484 static ObClient *first = NULL;
485 static ObClient *t = NULL;
486 static GList *order = NULL;
487 GList *it, *start, *list;
492 focus_cycle_target = NULL;
500 if (!first) first = focus_client;
502 if (linear) list = client_list;
503 else list = focus_order;
509 if (!focus_cycle_target) focus_cycle_target = focus_client;
511 start = it = g_list_find(list, focus_cycle_target);
512 if (!start) /* switched desktops or something? */
513 start = it = forward ? g_list_last(list) : g_list_first(list);
514 if (!start) goto done_cycle;
519 if (it == NULL) it = g_list_first(list);
522 if (it == NULL) it = g_list_last(list);
525 if (focus_target_valid(ft, all_desktops, dock_windows,
529 if (ft != focus_cycle_target) { /* prevents flicker */
530 focus_cycle_target = ft;
531 focus_cycle_draw_indicator();
533 /* same arguments as focus_target_valid */
534 popup_cycle(ft, dialog, all_desktops, dock_windows,
537 } else if (ft != focus_cycle_target) {
538 focus_cycle_target = ft;
543 } while (it != start);
546 if (done && focus_cycle_target)
547 client_activate(focus_cycle_target, FALSE, TRUE);
551 focus_cycle_target = NULL;
556 focus_cycle_draw_indicator();
557 popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
563 /* this be mostly ripped from fvwm */
564 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
565 gboolean dock_windows,
566 gboolean desktop_windows)
568 gint my_cx, my_cy, his_cx, his_cy;
571 gint score, best_score;
572 ObClient *best_client, *cur;
578 /* first, find the centre coords of the currently focused window */
579 my_cx = c->frame->area.x + c->frame->area.width / 2;
580 my_cy = c->frame->area.y + c->frame->area.height / 2;
585 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
588 /* the currently selected window isn't interesting */
591 if (cur->type == OB_CLIENT_TYPE_DOCK && !dock_windows)
593 if (cur->type == OB_CLIENT_TYPE_DESKTOP && !desktop_windows)
595 if (!client_normal(cur) &&
596 cur->type != OB_CLIENT_TYPE_DOCK &&
597 cur->type != OB_CLIENT_TYPE_DESKTOP)
599 /* using c->desktop instead of screen_desktop doesn't work if the
600 * current window was omnipresent, hope this doesn't have any other
602 if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
606 if(!(client_focus_target(cur) == cur &&
607 client_can_focus(cur)))
610 /* find the centre coords of this window, from the
611 * currently focused window's point of view */
612 his_cx = (cur->frame->area.x - my_cx)
613 + cur->frame->area.width / 2;
614 his_cy = (cur->frame->area.y - my_cy)
615 + cur->frame->area.height / 2;
617 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
618 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
620 /* Rotate the diagonals 45 degrees counterclockwise.
621 * To do this, multiply the matrix /+h +h\ with the
622 * vector (x y). \-h +h/
623 * h = sqrt(0.5). We can set h := 1 since absolute
624 * distance doesn't matter here. */
625 tx = his_cx + his_cy;
626 his_cy = -his_cx + his_cy;
631 case OB_DIRECTION_NORTH:
632 case OB_DIRECTION_SOUTH:
633 case OB_DIRECTION_NORTHEAST:
634 case OB_DIRECTION_SOUTHWEST:
635 offset = (his_cx < 0) ? -his_cx : his_cx;
636 distance = ((dir == OB_DIRECTION_NORTH ||
637 dir == OB_DIRECTION_NORTHEAST) ?
640 case OB_DIRECTION_EAST:
641 case OB_DIRECTION_WEST:
642 case OB_DIRECTION_SOUTHEAST:
643 case OB_DIRECTION_NORTHWEST:
644 offset = (his_cy < 0) ? -his_cy : his_cy;
645 distance = ((dir == OB_DIRECTION_WEST ||
646 dir == OB_DIRECTION_NORTHWEST) ?
651 /* the target must be in the requested direction */
655 /* Calculate score for this window. The smaller the better. */
656 score = distance + offset;
658 /* windows more than 45 degrees off the direction are
659 * heavily penalized and will only be chosen if nothing
660 * else within a million pixels */
661 if(offset > distance)
664 if(best_score == -1 || score < best_score)
672 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
673 gboolean desktop_windows, gboolean interactive,
674 gboolean dialog, gboolean done, gboolean cancel)
676 static ObClient *first = NULL;
683 focus_cycle_target = NULL;
691 if (!first) first = focus_client;
692 if (!focus_cycle_target) focus_cycle_target = focus_client;
694 if (focus_cycle_target)
695 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
700 for (it = focus_order; it; it = g_list_next(it))
701 if (focus_target_valid(it->data, FALSE, dock_windows,
707 if (ft != focus_cycle_target) {/* prevents flicker */
708 focus_cycle_target = ft;
709 focus_cycle_draw_indicator();
712 if (focus_cycle_target) {
713 /* same arguments as focus_target_valid */
714 popup_cycle(focus_cycle_target, dialog, FALSE, dock_windows,
722 if (done && focus_cycle_target)
723 client_activate(focus_cycle_target, FALSE, TRUE);
726 focus_cycle_target = NULL;
728 focus_cycle_draw_indicator();
729 popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
734 void focus_order_add_new(ObClient *c)
737 focus_order_to_top(c);
739 g_assert(!g_list_find(focus_order, c));
740 /* if there are any iconic windows, put this above them in the order,
741 but if there are not, then put it under the currently focused one */
742 if (focus_order && ((ObClient*)focus_order->data)->iconic)
743 focus_order = g_list_insert(focus_order, c, 0);
745 focus_order = g_list_insert(focus_order, c, 1);