15 Client *focus_client = NULL;
16 GList **focus_order = NULL; /* these lists are created when screen_startup
17 sets the number of desktops */
19 Window focus_backup = None;
20 gboolean focus_new = TRUE;
21 gboolean focus_follow = FALSE;
22 static gboolean focus_last = TRUE;
23 static gboolean focus_last_on_desktop = TRUE;
25 static gboolean noreorder = 0;
27 static void parse_assign(char *name, ParseToken *value)
29 if (!g_ascii_strcasecmp(name, "focusnew")) {
30 if (value->type != TOKEN_BOOL)
31 yyerror("invalid value");
33 focus_new = value->data.bool;
35 } else if (!g_ascii_strcasecmp(name, "followmouse")) {
36 if (value->type != TOKEN_BOOL)
37 yyerror("invalid value");
39 focus_follow = value->data.bool;
41 } else if (!g_ascii_strcasecmp(name, "focuslast")) {
42 if (value->type != TOKEN_BOOL)
43 yyerror("invalid value");
45 focus_last = value->data.bool;
47 } else if (!g_ascii_strcasecmp(name, "focuslastondesktop")) {
48 if (value->type != TOKEN_BOOL)
49 yyerror("invalid value");
51 focus_last_on_desktop = value->data.bool;
54 yyerror("invalid option");
55 parse_free_token(value);
60 /* create the window which gets focus when no clients get it. Have to
61 make it override-redirect so we don't try manage it, since it is
63 XSetWindowAttributes attrib;
69 focus_last_on_desktop = TRUE;
71 attrib.override_redirect = TRUE;
72 focus_backup = XCreateWindow(ob_display, ob_root,
74 CopyFromParent, InputOutput, CopyFromParent,
75 CWOverrideRedirect, &attrib);
76 XMapRaised(ob_display, focus_backup);
78 /* start with nothing focused */
79 focus_set_client(NULL);
81 parse_reg_section("focus", NULL, parse_assign);
88 for (i = 0; i < screen_num_desktops; ++i)
89 g_list_free(focus_order[i]);
93 XDestroyWindow(ob_display, focus_backup);
95 /* reset focus to root */
96 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
100 static void push_to_top(Client *client)
104 desktop = client->desktop;
105 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
106 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
107 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
110 void focus_set_client(Client *client)
115 /* uninstall the old colormap, and install the new one */
116 screen_install_colormap(focus_client, FALSE);
117 screen_install_colormap(client, TRUE);
119 if (client == NULL) {
120 /* when nothing will be focused, send focus to the backup target */
121 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
123 XSync(ob_display, FALSE);
127 focus_client = client;
129 /* move to the top of the list */
132 else if (client != NULL)
135 /* set the NET_ACTIVE_WINDOW hint */
136 active = client ? client->window : None;
137 PROP_SET32(ob_root, net_active_window, window, active);
139 if (focus_client != NULL)
140 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
142 dispatch_client(Event_Client_Unfocus, old, 0, 0);
145 static gboolean focus_under_pointer()
152 if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
153 for (it = stacking_list; it != NULL; it = it->next) {
154 Client *c = it->data;
155 if (c->desktop == screen_desktop &&
156 RECT_CONTAINS(c->frame->area, x, y))
160 return client_normal(it->data) && client_focus(it->data);
165 void focus_fallback(gboolean switching_desks)
172 /* unfocus any focused clients.. they can be focused by Pointer events
173 and such, and then when I try focus them, I won't get a FocusIn event
176 focus_set_client(NULL);
178 if (switching_desks) {
179 /* don't skip any windows when switching desktops */
183 if (!(switching_desks ? focus_last_on_desktop : focus_last)) {
184 if (focus_follow) focus_under_pointer();
188 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
189 if (it->data != old && client_normal(it->data))
190 if (client_focus(it->data))
193 /* nothing to focus */
194 focus_set_client(NULL);
197 void 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_focusable(ft)) {
236 if (client_focus(ft)) {
237 noreorder++; /* avoid reordering the focus_order */
241 } while (it != start);