16 Client *focus_client = NULL;
17 GList **focus_order = NULL; /* these lists are created when screen_startup
18 sets the number of desktops */
20 Window focus_backup = None;
21 gboolean focus_new = TRUE;
22 gboolean focus_follow = FALSE;
23 static gboolean focus_last = TRUE;
24 static gboolean focus_last_on_desktop = TRUE;
26 static gboolean noreorder = 0;
28 static void parse_assign(char *name, ParseToken *value)
30 if (!g_ascii_strcasecmp(name, "focusnew")) {
31 if (value->type != TOKEN_BOOL)
32 yyerror("invalid value");
34 focus_new = value->data.bool;
36 } else if (!g_ascii_strcasecmp(name, "followmouse")) {
37 if (value->type != TOKEN_BOOL)
38 yyerror("invalid value");
40 focus_follow = value->data.bool;
42 } else if (!g_ascii_strcasecmp(name, "focuslast")) {
43 if (value->type != TOKEN_BOOL)
44 yyerror("invalid value");
46 focus_last = value->data.bool;
48 } else if (!g_ascii_strcasecmp(name, "focuslastondesktop")) {
49 if (value->type != TOKEN_BOOL)
50 yyerror("invalid value");
52 focus_last_on_desktop = value->data.bool;
55 yyerror("invalid option");
56 parse_free_token(value);
61 /* create the window which gets focus when no clients get it. Have to
62 make it override-redirect so we don't try manage it, since it is
64 XSetWindowAttributes attrib;
70 focus_last_on_desktop = TRUE;
72 attrib.override_redirect = TRUE;
73 focus_backup = XCreateWindow(ob_display, ob_root,
75 CopyFromParent, InputOutput, CopyFromParent,
76 CWOverrideRedirect, &attrib);
77 XMapRaised(ob_display, focus_backup);
79 /* start with nothing focused */
80 focus_set_client(NULL);
82 parse_reg_section("focus", NULL, parse_assign);
89 for (i = 0; i < screen_num_desktops; ++i)
90 g_list_free(focus_order[i]);
94 XDestroyWindow(ob_display, focus_backup);
96 /* reset focus to root */
97 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
101 static void push_to_top(Client *client)
105 desktop = client->desktop;
106 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
107 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
108 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
111 void focus_set_client(Client *client)
116 /* uninstall the old colormap, and install the new one */
117 screen_install_colormap(focus_client, FALSE);
118 screen_install_colormap(client, TRUE);
120 if (client == NULL) {
121 /* when nothing will be focused, send focus to the backup target */
122 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
124 XSync(ob_display, FALSE);
128 focus_client = client;
130 /* move to the top of the list */
133 else if (client != NULL)
136 /* set the NET_ACTIVE_WINDOW hint */
137 active = client ? client->window : None;
138 PROP_SET32(ob_root, net_active_window, window, active);
140 if (focus_client != NULL)
141 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
143 dispatch_client(Event_Client_Unfocus, old, 0, 0);
146 static gboolean focus_under_pointer()
153 if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
154 for (it = stacking_list; it != NULL; it = it->next) {
155 Client *c = it->data;
156 if (c->desktop == screen_desktop &&
157 RECT_CONTAINS(c->frame->area, x, y))
161 return client_normal(it->data) && client_focus(it->data);
166 void focus_fallback(FallbackType type)
173 /* unfocus any focused clients.. they can be focused by Pointer events
174 and such, and then when I try focus them, I won't get a FocusIn event
177 focus_set_client(NULL);
179 if (!(type == Fallback_Desktop ? focus_last_on_desktop : focus_last)) {
180 if (focus_follow) focus_under_pointer();
184 if (type == Fallback_Unfocusing && old && old->transient_for) {
185 if (old->transient_for == TRAN_GROUP) {
186 for (it = focus_order[screen_desktop]; it != NULL; it = it->next) {
189 for (sit = old->group->members; sit; sit = sit->next)
190 if (sit->data == it->data && client_focus(sit->data))
194 if (client_normal(old->transient_for))
195 if (client_focus(old->transient_for))
200 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
201 if (type != Fallback_Unfocusing || it->data != old)
202 if (client_normal(it->data) && client_focus(it->data))
205 /* nothing to focus */
206 focus_set_client(NULL);
209 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
212 static Client *first = NULL;
213 static Client *t = NULL;
214 static GList *order = NULL;
215 GList *it, *start, *list;
219 if (first) client_focus(first);
223 push_to_top(focus_client); /* move to top of focus_order */
224 stacking_raise(focus_client);
228 if (!first) first = focus_client;
230 if (linear) list = client_list;
231 else list = focus_order[screen_desktop];
233 start = it = g_list_find(list, focus_client);
234 if (!start) /* switched desktops or something? */
235 start = it = forward ? g_list_last(list) : g_list_first(list);
236 if (!start) goto done_cycle;
241 if (it == NULL) it = list;
244 if (it == NULL) it = g_list_last(list);
246 ft = client_focus_target(it->data);
247 if (ft == it->data && focus_client != ft && client_normal(ft) &&
249 noreorder++; /* avoid reordering the focus_order */
252 } while (it != start);