17 Client *focus_client = NULL;
18 GList **focus_order = NULL; /* these lists are created when screen_startup
19 sets the number of desktops */
21 Window focus_backup = None;
23 static gboolean noreorder = 0;
27 /* create the window which gets focus when no clients get it. Have to
28 make it override-redirect so we don't try manage it, since it is
30 XSetWindowAttributes attrib;
34 attrib.override_redirect = TRUE;
35 focus_backup = XCreateWindow(ob_display, ob_root,
37 CopyFromParent, InputOutput, CopyFromParent,
38 CWOverrideRedirect, &attrib);
39 XMapWindow(ob_display, focus_backup);
40 stacking_raise_internal(focus_backup);
42 /* start with nothing focused */
43 focus_set_client(NULL);
50 for (i = 0; i < screen_num_desktops; ++i)
51 g_list_free(focus_order[i]);
55 XDestroyWindow(ob_display, focus_backup);
57 /* reset focus to root */
58 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
62 static void push_to_top(Client *client)
66 desktop = client->desktop;
67 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
68 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
69 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
72 void focus_set_client(Client *client)
77 /* uninstall the old colormap, and install the new one */
78 screen_install_colormap(focus_client, FALSE);
79 screen_install_colormap(client, TRUE);
82 /* when nothing will be focused, send focus to the backup target */
83 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
85 XSync(ob_display, FALSE);
89 focus_client = client;
91 /* move to the top of the list */
94 else if (client != NULL)
97 /* set the NET_ACTIVE_WINDOW hint */
98 active = client ? client->window : None;
99 PROP_SET32(ob_root, net_active_window, window, active);
101 if (focus_client != NULL)
102 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
104 dispatch_client(Event_Client_Unfocus, old, 0, 0);
107 static gboolean focus_under_pointer()
112 if (ob_pointer_pos(&x, &y)) {
113 for (it = stacking_list; it != NULL; it = it->next) {
114 Client *c = it->data;
115 if (c->desktop == screen_desktop &&
116 RECT_CONTAINS(c->frame->area, x, y))
120 return client_normal(it->data) && client_focus(it->data);
125 /* finds the first transient that isn't 'skip' */
126 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
131 for (it = c->transients; it; it = it->next) {
132 g_message("looking");
133 if (it->data == top) return NULL;
134 ret = find_transient_recursive(it->data, top, skip);
135 if (ret && ret != skip) return ret;
136 if (it->data != skip) return it->data;
137 g_message("not found");
142 void focus_fallback(FallbackType type)
149 /* unfocus any focused clients.. they can be focused by Pointer events
150 and such, and then when I try focus them, I won't get a FocusIn event
153 focus_set_client(NULL);
155 if (!(type == Fallback_Desktop ?
156 config_focus_last_on_desktop : config_focus_last)) {
157 if (config_focus_follow) focus_under_pointer();
161 if (type == Fallback_Unfocusing && old && old->transient_for) {
164 if (old->transient_for == TRAN_GROUP) {
165 for (it = focus_order[screen_desktop]; it != NULL; it = it->next) {
168 for (sit = old->group->members; sit; sit = sit->next)
169 if (sit->data == it->data && client_normal(sit->data) &&
170 client_focusable(sit->data)) {
176 if (client_normal(old->transient_for))
177 c = old->transient_for;
181 Client *t = find_transient_recursive(c, c, old);
182 if (!t) t = c; /* no transient, keep the top */
188 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
189 if (type != Fallback_Unfocusing || it->data != old)
190 if (client_normal(it->data) && client_focus(it->data))
193 /* nothing to focus */
194 focus_set_client(NULL);
197 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
200 static Client *first = NULL;
201 static Client *t = NULL;
202 static GList *order = NULL;
203 GList *it, *start, *list;
207 if (first) client_focus(first);
211 push_to_top(focus_client); /* move to top of focus_order */
212 stacking_raise(focus_client);
216 if (!first) first = focus_client;
218 if (linear) list = client_list;
219 else list = focus_order[screen_desktop];
221 start = it = g_list_find(list, focus_client);
222 if (!start) /* switched desktops or something? */
223 start = it = forward ? g_list_last(list) : g_list_first(list);
224 if (!start) goto done_cycle;
229 if (it == NULL) it = list;
232 if (it == NULL) it = g_list_last(list);
234 ft = client_focus_target(it->data);
235 if (ft == it->data && focus_client != ft && client_normal(ft) &&
237 noreorder++; /* avoid reordering the focus_order */
240 } while (it != start);