4 #include "framerender.h"
20 Client *focus_client = NULL;
21 GList **focus_order = NULL; /* these lists are created when screen_startup
22 sets the number of desktops */
24 Window focus_backup = None;
26 static Client *focus_cycle_target = NULL;
27 static Popup *focus_cycle_popup = NULL;
31 /* create the window which gets focus when no clients get it. Have to
32 make it override-redirect so we don't try manage it, since it is
34 XSetWindowAttributes attrib;
37 focus_cycle_popup = popup_new(TRUE);
39 attrib.override_redirect = TRUE;
40 focus_backup = XCreateWindow(ob_display, ob_root,
42 CopyFromParent, InputOutput, CopyFromParent,
43 CWOverrideRedirect, &attrib);
44 XMapRaised(ob_display, focus_backup);
46 /* start with nothing focused */
47 focus_set_client(NULL);
54 for (i = 0; i < screen_num_desktops; ++i)
55 g_list_free(focus_order[i]);
59 popup_free(focus_cycle_popup);
60 focus_cycle_popup = NULL;
62 XDestroyWindow(ob_display, focus_backup);
64 /* reset focus to root */
65 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
69 static void push_to_top(Client *client)
73 desktop = client->desktop;
74 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
75 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
76 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
79 void focus_set_client(Client *client)
84 /* uninstall the old colormap, and install the new one */
85 screen_install_colormap(focus_client, FALSE);
86 screen_install_colormap(client, TRUE);
89 /* when nothing will be focused, send focus to the backup target */
90 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
92 XSync(ob_display, FALSE);
95 /* in the middle of cycling..? kill it. */
96 if (focus_cycle_target)
97 focus_cycle(TRUE, TRUE, TRUE, TRUE);
100 focus_client = client;
102 /* move to the top of the list */
106 /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
107 if (ob_state != State_Exiting) {
108 active = client ? client->window : None;
109 PROP_SET32(ob_root, net_active_window, window, active);
112 if (focus_client != NULL)
113 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
115 dispatch_client(Event_Client_Unfocus, old, 0, 0);
118 static gboolean focus_under_pointer()
123 if (ob_pointer_pos(&x, &y)) {
124 for (it = stacking_list; it != NULL; it = it->next) {
125 if (WINDOW_IS_CLIENT(it->data)) {
126 Client *c = WINDOW_AS_CLIENT(it->data);
127 if (c->desktop == screen_desktop &&
128 RECT_CONTAINS(c->frame->area, x, y))
133 g_assert(WINDOW_IS_CLIENT(it->data));
134 return client_normal(it->data) && client_focus(it->data);
140 /* finds the first transient that isn't 'skip' and ensure's that client_normal
142 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
147 for (it = c->transients; it; it = it->next) {
148 if (it->data == top) return NULL;
149 ret = find_transient_recursive(it->data, top, skip);
150 if (ret && ret != skip && client_normal(ret)) return ret;
151 if (it->data != skip && client_normal(it->data)) return it->data;
156 static gboolean focus_fallback_transient(Client *top, Client *old)
158 Client *target = find_transient_recursive(top, top, old);
160 /* make sure client_normal is true always */
161 if (!client_normal(top))
163 target = top; /* no transient, keep the top */
165 return client_focus(target);
168 void focus_fallback(FallbackType type)
175 /* unfocus any focused clients.. they can be focused by Pointer events
176 and such, and then when I try focus them, I won't get a FocusIn event
179 focus_set_client(NULL);
181 if (!(type == Fallback_Desktop ?
182 config_focus_last_on_desktop : config_focus_last)) {
183 if (config_focus_follow) focus_under_pointer();
187 if (type == Fallback_Unfocusing && old) {
188 /* try for transient relations */
189 if (old->transient_for) {
190 if (old->transient_for == TRAN_GROUP) {
191 for (it = focus_order[screen_desktop]; it; it = it->next) {
194 for (sit = old->group->members; sit; sit = sit->next)
195 if (sit->data == it->data)
196 if (focus_fallback_transient(sit->data, old))
200 if (focus_fallback_transient(old->transient_for, old))
205 /* try for group relations */
209 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
210 for (sit = old->group->members; sit; sit = sit->next)
211 if (sit->data == it->data)
212 if (sit->data != old && client_normal(sit->data))
213 if (client_focus(sit->data))
218 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
219 if (type != Fallback_Unfocusing || it->data != old)
220 if (client_normal(it->data) &&
221 /* dont fall back to 'anonymous' fullscreen windows. theres no
222 checks for this is in transient/group fallbacks, so they can
223 be fallback targets there. */
224 !((Client*)it->data)->fullscreen &&
225 client_focus(it->data))
228 /* nothing to focus */
229 focus_set_client(NULL);
232 static void popup_cycle(Client *c, gboolean show)
235 popup_hide(focus_cycle_popup);
239 a = screen_area(c->desktop);
240 popup_position(focus_cycle_popup, CenterGravity,
241 a->x + a->width / 2, a->y + a->height / 2);
242 /* popup_size(focus_cycle_popup, a->height/2, a->height/16);
243 popup_show(focus_cycle_popup, c->title,
244 client_icon(c, a->height/16, a->height/16));
246 /* XXX the size and the font extents need to be related on some level
248 popup_size(focus_cycle_popup, 320, 48);
250 /* use the transient's parent's title/icon */
251 while (c->transient_for && c->transient_for != TRAN_GROUP)
252 c = c->transient_for;
254 popup_show(focus_cycle_popup, (c->iconic ? c->icon_title : c->title),
255 client_icon(c, 48, 48));
259 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
262 static Client *first = NULL;
263 static Client *t = NULL;
264 static GList *order = NULL;
265 GList *it, *start, *list;
269 if (focus_cycle_target)
270 frame_adjust_focus(focus_cycle_target->frame, FALSE);
272 frame_adjust_focus(focus_client->frame, TRUE);
275 if (focus_cycle_target)
276 client_activate(focus_cycle_target);
280 grab_pointer(TRUE, None);
282 if (!first) first = focus_client;
283 if (!focus_cycle_target) focus_cycle_target = focus_client;
285 if (linear) list = client_list;
286 else list = focus_order[screen_desktop];
288 start = it = g_list_find(list, focus_cycle_target);
289 if (!start) /* switched desktops or something? */
290 start = it = forward ? g_list_last(list) : g_list_first(list);
291 if (!start) goto done_cycle;
296 if (it == NULL) it = g_list_first(list);
299 if (it == NULL) it = g_list_last(list);
301 /*ft = client_focus_target(it->data);*/
303 if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
304 (ft->can_focus || ft->focus_notify) &&
305 (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
306 if (ft != focus_cycle_target) { /* prevents flicker */
307 if (focus_cycle_target)
308 frame_adjust_focus(focus_cycle_target->frame, FALSE);
309 focus_cycle_target = ft;
310 frame_adjust_focus(focus_cycle_target->frame, TRUE);
312 popup_cycle(ft, config_focus_popup);
315 } while (it != start);
320 focus_cycle_target = NULL;
324 popup_cycle(ft, FALSE);
325 grab_pointer(FALSE, None);
330 void focus_order_add_new(Client *c)
335 focus_order_to_top(c);
338 if (d == DESKTOP_ALL) {
339 for (i = 0; i < screen_num_desktops; ++i) {
340 if (focus_order[i] && ((Client*)focus_order[i]->data)->iconic)
341 focus_order[i] = g_list_insert(focus_order[i], c, 0);
343 focus_order[i] = g_list_insert(focus_order[i], c, 1);
346 if (focus_order[d] && ((Client*)focus_order[d]->data)->iconic)
347 focus_order[d] = g_list_insert(focus_order[d], c, 0);
349 focus_order[d] = g_list_insert(focus_order[d], c, 1);
353 void focus_order_remove(Client *c)
358 if (d == DESKTOP_ALL) {
359 for (i = 0; i < screen_num_desktops; ++i)
360 focus_order[i] = g_list_remove(focus_order[i], c);
362 focus_order[d] = g_list_remove(focus_order[d], c);
365 static void to_top(Client *c, guint d)
367 focus_order[d] = g_list_remove(focus_order[d], c);
369 focus_order[d] = g_list_prepend(focus_order[d], c);
373 /* insert before first iconic window */
374 for (it = focus_order[d];
375 it && !((Client*)it->data)->iconic; it = it->next);
376 g_list_insert_before(focus_order[d], it, c);
380 void focus_order_to_top(Client *c)
385 if (d == DESKTOP_ALL) {
386 for (i = 0; i < screen_num_desktops; ++i)
392 static void to_bottom(Client *c, guint d)
394 focus_order[d] = g_list_remove(focus_order[d], c);
396 focus_order[d] = g_list_append(focus_order[d], c);
400 /* insert before first iconic window */
401 for (it = focus_order[d];
402 it && !((Client*)it->data)->iconic; it = it->next);
403 g_list_insert_before(focus_order[d], it, c);
407 void focus_order_to_bottom(Client *c)
412 if (d == DESKTOP_ALL) {
413 for (i = 0; i < screen_num_desktops; ++i)