4 #include "framerender.h"
19 Client *focus_client = NULL;
20 GList **focus_order = NULL; /* these lists are created when screen_startup
21 sets the number of desktops */
23 Window focus_backup = None;
25 static Client *focus_cycle_target = NULL;
26 static Popup *focus_cycle_popup = NULL;
30 /* create the window which gets focus when no clients get it. Have to
31 make it override-redirect so we don't try manage it, since it is
33 XSetWindowAttributes attrib;
36 focus_cycle_popup = popup_new(TRUE);
38 attrib.override_redirect = TRUE;
39 focus_backup = XCreateWindow(ob_display, ob_root,
41 CopyFromParent, InputOutput, CopyFromParent,
42 CWOverrideRedirect, &attrib);
43 XMapRaised(ob_display, focus_backup);
45 /* start with nothing focused */
46 focus_set_client(NULL);
53 for (i = 0; i < screen_num_desktops; ++i)
54 g_list_free(focus_order[i]);
58 popup_free(focus_cycle_popup);
59 focus_cycle_popup = NULL;
61 XDestroyWindow(ob_display, focus_backup);
63 /* reset focus to root */
64 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
68 static void push_to_top(Client *client)
72 desktop = client->desktop;
73 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
74 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
75 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
78 void focus_set_client(Client *client)
84 g_message("focus_set_client 0x%lx", client ? client->window : 0);
87 /* uninstall the old colormap, and install the new one */
88 screen_install_colormap(focus_client, FALSE);
89 screen_install_colormap(client, TRUE);
92 /* when nothing will be focused, send focus to the backup target */
93 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
95 XSync(ob_display, FALSE);
98 /* in the middle of cycling..? kill it. */
99 if (focus_cycle_target)
100 focus_cycle(TRUE, TRUE, TRUE, TRUE);
103 focus_client = client;
105 /* move to the top of the list */
109 /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
110 if (ob_state != State_Exiting) {
111 active = client ? client->window : None;
112 PROP_SET32(ob_root, net_active_window, window, active);
115 if (focus_client != NULL)
116 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
118 dispatch_client(Event_Client_Unfocus, old, 0, 0);
121 static gboolean focus_under_pointer()
126 if (ob_pointer_pos(&x, &y)) {
127 for (it = stacking_list; it != NULL; it = it->next) {
128 if (WINDOW_IS_CLIENT(it->data)) {
129 Client *c = WINDOW_AS_CLIENT(it->data);
130 if (c->desktop == screen_desktop &&
131 RECT_CONTAINS(c->frame->area, x, y))
136 g_assert(WINDOW_IS_CLIENT(it->data));
137 return client_normal(it->data) && client_focus(it->data);
143 /* finds the first transient that isn't 'skip' and ensure's that client_normal
145 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
150 for (it = c->transients; it; it = it->next) {
151 if (it->data == top) return NULL;
152 ret = find_transient_recursive(it->data, top, skip);
153 if (ret && ret != skip && client_normal(ret)) return ret;
154 if (it->data != skip && client_normal(it->data)) return it->data;
159 static gboolean focus_fallback_transient(Client *top, Client *old)
161 Client *target = find_transient_recursive(top, top, old);
163 /* make sure client_normal is true always */
164 if (!client_normal(top))
166 target = top; /* no transient, keep the top */
168 return client_focus(target);
171 void focus_fallback(FallbackType type)
178 /* unfocus any focused clients.. they can be focused by Pointer events
179 and such, and then when I try focus them, I won't get a FocusIn event
182 focus_set_client(NULL);
184 if (!(type == Fallback_Desktop ?
185 config_focus_last_on_desktop : config_focus_last)) {
186 if (config_focus_follow) focus_under_pointer();
190 if (type == Fallback_Unfocusing && old) {
191 /* try for transient relations */
192 if (old->transient_for) {
193 if (old->transient_for == TRAN_GROUP) {
194 for (it = focus_order[screen_desktop]; it; it = it->next) {
197 for (sit = old->group->members; sit; sit = sit->next)
198 if (sit->data == it->data)
199 if (focus_fallback_transient(sit->data, old))
203 if (focus_fallback_transient(old->transient_for, old))
208 /* try for group relations */
212 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
213 for (sit = old->group->members; sit; sit = sit->next)
214 if (sit->data == it->data)
215 if (sit->data != old && client_normal(sit->data))
216 if (client_focus(sit->data))
221 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
222 if (type != Fallback_Unfocusing || it->data != old)
223 if (client_normal(it->data) &&
224 /* dont fall back to 'anonymous' fullscreen windows. theres no
225 checks for this is in transient/group fallbacks, so they can
226 be fallback targets there. */
227 !((Client*)it->data)->fullscreen &&
228 client_focus(it->data))
231 /* nothing to focus */
232 focus_set_client(NULL);
235 static void popup_cycle(Client *c, gboolean show)
238 popup_hide(focus_cycle_popup);
242 a = screen_area(c->desktop);
243 popup_position(focus_cycle_popup, CenterGravity,
244 a->x + a->width / 2, a->y + a->height / 2);
245 /* popup_size(focus_cycle_popup, a->height/2, a->height/16);
246 popup_show(focus_cycle_popup, c->title,
247 client_icon(c, a->height/16, a->height/16));
249 /* XXX the size and the font extents need to be related on some level
251 popup_size(focus_cycle_popup, 320, 48);
253 /* use the transient's parent's title/icon */
254 while (c->transient_for && c->transient_for != TRAN_GROUP)
255 c = c->transient_for;
257 popup_show(focus_cycle_popup, (c->iconic ? c->icon_title : c->title),
258 client_icon(c, 48, 48));
262 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
265 static Client *first = NULL;
266 static Client *t = NULL;
267 static GList *order = NULL;
268 GList *it, *start, *list;
272 if (focus_cycle_target)
273 frame_adjust_focus(focus_cycle_target->frame, FALSE);
275 frame_adjust_focus(focus_client->frame, TRUE);
278 if (focus_cycle_target)
279 client_activate(focus_cycle_target);
283 grab_pointer(TRUE, None);
285 if (!first) first = focus_client;
286 if (!focus_cycle_target) focus_cycle_target = focus_client;
288 if (linear) list = client_list;
289 else list = focus_order[screen_desktop];
291 start = it = g_list_find(list, focus_cycle_target);
292 if (!start) /* switched desktops or something? */
293 start = it = forward ? g_list_last(list) : g_list_first(list);
294 if (!start) goto done_cycle;
299 if (it == NULL) it = g_list_first(list);
302 if (it == NULL) it = g_list_last(list);
304 /*ft = client_focus_target(it->data);*/
306 if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
307 (ft->can_focus || ft->focus_notify) &&
308 (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
309 if (ft != focus_cycle_target) { /* prevents flicker */
310 if (focus_cycle_target)
311 frame_adjust_focus(focus_cycle_target->frame, FALSE);
312 focus_cycle_target = ft;
313 frame_adjust_focus(focus_cycle_target->frame, TRUE);
315 popup_cycle(ft, config_focus_popup);
318 } while (it != start);
323 focus_cycle_target = NULL;
327 popup_cycle(ft, FALSE);
328 grab_pointer(FALSE, None);
333 void focus_order_add_new(Client *c)
338 focus_order_to_top(c);
341 if (d == DESKTOP_ALL) {
342 for (i = 0; i < screen_num_desktops; ++i) {
343 if (focus_order[i] && ((Client*)focus_order[i]->data)->iconic)
344 focus_order[i] = g_list_insert(focus_order[i], c, 0);
346 focus_order[i] = g_list_insert(focus_order[i], c, 1);
349 if (focus_order[d] && ((Client*)focus_order[d]->data)->iconic)
350 focus_order[d] = g_list_insert(focus_order[d], c, 0);
352 focus_order[d] = g_list_insert(focus_order[d], c, 1);
356 void focus_order_remove(Client *c)
361 if (d == DESKTOP_ALL) {
362 for (i = 0; i < screen_num_desktops; ++i)
363 focus_order[i] = g_list_remove(focus_order[i], c);
365 focus_order[d] = g_list_remove(focus_order[d], c);
368 static void to_top(Client *c, guint d)
370 focus_order[d] = g_list_remove(focus_order[d], c);
372 focus_order[d] = g_list_prepend(focus_order[d], c);
376 /* insert before first iconic window */
377 for (it = focus_order[d];
378 it && !((Client*)it->data)->iconic; it = it->next);
379 g_list_insert_before(focus_order[d], it, c);
383 void focus_order_to_top(Client *c)
388 if (d == DESKTOP_ALL) {
389 for (i = 0; i < screen_num_desktops; ++i)
395 static void to_bottom(Client *c, guint d)
397 focus_order[d] = g_list_remove(focus_order[d], c);
399 focus_order[d] = g_list_append(focus_order[d], c);
403 /* insert before first iconic window */
404 for (it = focus_order[d];
405 it && !((Client*)it->data)->iconic; it = it->next);
406 g_list_insert_before(focus_order[d], it, c);
410 void focus_order_to_bottom(Client *c)
415 if (d == DESKTOP_ALL) {
416 for (i = 0; i < screen_num_desktops; ++i)