1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus_cycle.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"
21 #include "focus_cycle_indicator.h"
34 #define FOCUS_CYCLE_ICON_SIZE 48
36 struct _ObFocusCyclePopup
52 ObClient *focus_cycle_target = NULL;
54 static ObIconPopup *focus_cycle_popup;
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,
67 gboolean all_desktops,
68 gboolean dock_windows,
69 gboolean desktop_windows);
70 static ObClient *focus_find_directional (ObClient *c,
72 gboolean dock_windows,
73 gboolean desktop_windows);
74 static ObClient *focus_find_directional (ObClient *c,
76 gboolean dock_windows,
77 gboolean desktop_windows);
79 void focus_cycle_startup(gboolean reconfig)
81 focus_cycle_popup = icon_popup_new(TRUE);
84 client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
87 void focus_cycle_shutdown(gboolean reconfig)
89 icon_popup_free(focus_cycle_popup);
92 client_remove_destroy_notify(focus_cycle_destroy_notify);
95 void focus_cycle_stop()
97 if (focus_cycle_target)
98 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
102 static gchar *popup_get_name(ObClient *c, ObClient **nametarget)
106 const gchar *desk = NULL;
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);
113 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
114 desk = screen_desktop_names[c->desktop];
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);
121 title = g_strdup(c->iconic ? c->icon_title : c->title);
124 ret = g_strdup_printf("%s [%s]", title, desk);
131 /* set this only if we're returning true and they asked for it */
132 if (ret && nametarget) *nametarget = p;
136 static void popup_cycle(ObClient *c, gboolean show,
137 gboolean all_desktops, gboolean dock_windows,
138 gboolean desktop_windows)
140 gchar *showtext = NULL;
141 ObClient *showtarget;
144 icon_popup_hide(focus_cycle_popup);
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)
154 GList *targets = NULL, *it;
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));
167 /* make its width to be the width of all the possible titles */
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
175 targets = g_list_prepend(targets, ft);
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);
185 /* little optimization.. save this text and client, so we dont
186 have to get it again */
188 showtext = g_strdup(names[i]);
194 icon_popup_text_width_to_strings(focus_cycle_popup, names, n);
199 if (!showtext) showtext = popup_get_name(c, &showtarget);
200 icon_popup_show(focus_cycle_popup, showtext,
201 client_icon(showtarget, 48, 48));
205 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
207 /* end cycling if the target disappears. CurrentTime is fine, time won't
210 if (focus_cycle_target == client)
211 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
214 /*! Returns if a focus target has valid group siblings that can be cycled
216 static gboolean focus_target_has_siblings(ObClient *ft, gboolean all_desktops)
221 if (!ft->group) return FALSE;
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))
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)
244 /* it's on this desktop unless you want all desktops.
246 do this check first because it will usually filter out the most
248 ok = (all_desktops || ft->desktop == screen_desktop ||
249 ft->desktop == DESKTOP_ALL);
251 /* the window can receive focus somehow */
252 ok = ok && (ft->can_focus || ft->focus_notify);
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));
259 /* normal non-helper windows are valid targets */
261 ((client_normal(ft) && !client_helper(ft))
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))));
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) ||
280 /* it's not going to just send fous off somewhere else (modal window) */
281 ok = ok && ft == client_focus_target(ft);
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)
291 static ObClient *first = NULL;
292 static ObClient *t = NULL;
293 static GList *order = NULL;
294 GList *it, *start, *list;
299 focus_cycle_target = NULL;
307 if (!first) first = focus_client;
309 if (linear) list = client_list;
310 else list = focus_order;
316 if (!focus_cycle_target) focus_cycle_target = focus_client;
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;
326 if (it == NULL) it = g_list_first(list);
329 if (it == NULL) it = g_list_last(list);
332 if (focus_target_valid(ft, all_desktops, dock_windows,
336 if (ft != focus_cycle_target) { /* prevents flicker */
337 focus_cycle_target = ft;
338 focus_cycle_draw_indicator(ft);
340 /* same arguments as focus_target_valid */
341 popup_cycle(ft, dialog, all_desktops, dock_windows,
344 } else if (ft != focus_cycle_target) {
345 focus_cycle_target = ft;
350 } while (it != start);
353 if (done && focus_cycle_target)
354 client_activate(focus_cycle_target, FALSE, TRUE);
358 focus_cycle_target = NULL;
363 focus_cycle_draw_indicator(NULL);
364 popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
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)
375 gint my_cx, my_cy, his_cx, his_cy;
378 gint score, best_score;
379 ObClient *best_client, *cur;
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;
392 for(it = g_list_first(client_list); it; it = g_list_next(it)) {
395 /* the currently selected window isn't interesting */
398 if (cur->type == OB_CLIENT_TYPE_DOCK && !dock_windows)
400 if (cur->type == OB_CLIENT_TYPE_DESKTOP && !desktop_windows)
402 if (!client_normal(cur) &&
403 cur->type != OB_CLIENT_TYPE_DOCK &&
404 cur->type != OB_CLIENT_TYPE_DESKTOP)
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
409 if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
413 if(!(client_focus_target(cur) == cur &&
414 client_can_focus(cur)))
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;
424 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
425 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
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;
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) ?
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) ?
458 /* the target must be in the requested direction */
462 /* Calculate score for this window. The smaller the better. */
463 score = distance + offset;
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)
471 if(best_score == -1 || score < best_score)
479 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
480 gboolean desktop_windows, gboolean interactive,
481 gboolean dialog, gboolean done, gboolean cancel)
483 static ObClient *first = NULL;
490 focus_cycle_target = NULL;
498 if (!first) first = focus_client;
499 if (!focus_cycle_target) focus_cycle_target = focus_client;
501 if (focus_cycle_target)
502 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
507 for (it = focus_order; it; it = g_list_next(it))
508 if (focus_target_valid(it->data, FALSE, dock_windows,
514 if (ft != focus_cycle_target) {/* prevents flicker */
515 focus_cycle_target = ft;
516 focus_cycle_draw_indicator(ft);
519 if (focus_cycle_target) {
520 /* same arguments as focus_target_valid */
521 popup_cycle(focus_cycle_target, dialog, FALSE, dock_windows,
529 if (done && focus_cycle_target)
530 client_activate(focus_cycle_target, FALSE, TRUE);
533 focus_cycle_target = NULL;
535 focus_cycle_draw_indicator(NULL);
536 popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
541 void focus_order_add_new(ObClient *c)
544 focus_order_to_top(c);
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);
552 focus_order = g_list_insert(focus_order, c, 1);