]> Dogcows Code - chaz/openbox/blob - openbox/focus.c
add more options for focus fallback, use an enum for all the types of fallbacks.
[chaz/openbox] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "client.h"
4 #include "frame.h"
5 #include "screen.h"
6 #include "group.h"
7 #include "prop.h"
8 #include "dispatch.h"
9 #include "focus.h"
10 #include "parse.h"
11 #include "engine.h"
12
13 #include <X11/Xlib.h>
14 #include <glib.h>
15
16 Client *focus_client = NULL;
17 GList **focus_order = NULL; /* these lists are created when screen_startup
18 sets the number of desktops */
19
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;
25
26 static gboolean noreorder = 0;
27
28 static void parse_assign(char *name, ParseToken *value)
29 {
30 if (!g_ascii_strcasecmp(name, "focusnew")) {
31 if (value->type != TOKEN_BOOL)
32 yyerror("invalid value");
33 else {
34 focus_new = value->data.bool;
35 }
36 } else if (!g_ascii_strcasecmp(name, "followmouse")) {
37 if (value->type != TOKEN_BOOL)
38 yyerror("invalid value");
39 else {
40 focus_follow = value->data.bool;
41 }
42 } else if (!g_ascii_strcasecmp(name, "focuslast")) {
43 if (value->type != TOKEN_BOOL)
44 yyerror("invalid value");
45 else {
46 focus_last = value->data.bool;
47 }
48 } else if (!g_ascii_strcasecmp(name, "focuslastondesktop")) {
49 if (value->type != TOKEN_BOOL)
50 yyerror("invalid value");
51 else {
52 focus_last_on_desktop = value->data.bool;
53 }
54 } else
55 yyerror("invalid option");
56 parse_free_token(value);
57 }
58
59 void focus_startup()
60 {
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
63 mapped. */
64 XSetWindowAttributes attrib;
65
66 focus_client = NULL;
67 focus_new = TRUE;
68 focus_follow = FALSE;
69 focus_last = TRUE;
70 focus_last_on_desktop = TRUE;
71
72 attrib.override_redirect = TRUE;
73 focus_backup = XCreateWindow(ob_display, ob_root,
74 -100, -100, 1, 1, 0,
75 CopyFromParent, InputOutput, CopyFromParent,
76 CWOverrideRedirect, &attrib);
77 XMapRaised(ob_display, focus_backup);
78
79 /* start with nothing focused */
80 focus_set_client(NULL);
81
82 parse_reg_section("focus", NULL, parse_assign);
83 }
84
85 void focus_shutdown()
86 {
87 guint i;
88
89 for (i = 0; i < screen_num_desktops; ++i)
90 g_list_free(focus_order[i]);
91 g_free(focus_order);
92 focus_order = NULL;
93
94 XDestroyWindow(ob_display, focus_backup);
95
96 /* reset focus to root */
97 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
98 event_lasttime);
99 }
100
101 static void push_to_top(Client *client)
102 {
103 guint desktop;
104
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);
109 }
110
111 void focus_set_client(Client *client)
112 {
113 Window active;
114 Client *old;
115
116 /* uninstall the old colormap, and install the new one */
117 screen_install_colormap(focus_client, FALSE);
118 screen_install_colormap(client, TRUE);
119
120 if (client == NULL) {
121 /* when nothing will be focused, send focus to the backup target */
122 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
123 event_lasttime);
124 XSync(ob_display, FALSE);
125 }
126
127 old = focus_client;
128 focus_client = client;
129
130 /* move to the top of the list */
131 if (noreorder)
132 --noreorder;
133 else if (client != NULL)
134 push_to_top(client);
135
136 /* set the NET_ACTIVE_WINDOW hint */
137 active = client ? client->window : None;
138 PROP_SET32(ob_root, net_active_window, window, active);
139
140 if (focus_client != NULL)
141 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
142 if (old != NULL)
143 dispatch_client(Event_Client_Unfocus, old, 0, 0);
144 }
145
146 static gboolean focus_under_pointer()
147 {
148 Window w;
149 int i, x, y;
150 guint u;
151 GList *it;
152
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))
158 break;
159 }
160 if (it != NULL)
161 return client_normal(it->data) && client_focus(it->data);
162 }
163 return FALSE;
164 }
165
166 void focus_fallback(FallbackType type)
167 {
168 GList *it;
169 Client *old = NULL;
170
171 old = focus_client;
172
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
175 at all for them.
176 */
177 focus_set_client(NULL);
178
179 if (!(type == Fallback_Desktop ? focus_last_on_desktop : focus_last)) {
180 if (focus_follow) focus_under_pointer();
181 return;
182 }
183
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) {
187 GSList *sit;
188
189 for (sit = old->group->members; sit; sit = sit->next)
190 if (sit->data == it->data && client_focus(sit->data))
191 return;
192 }
193 } else {
194 if (client_normal(old->transient_for))
195 if (client_focus(old->transient_for))
196 return;
197 }
198 }
199
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))
203 return;
204
205 /* nothing to focus */
206 focus_set_client(NULL);
207 }
208
209 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
210 gboolean cancel)
211 {
212 static Client *first = NULL;
213 static Client *t = NULL;
214 static GList *order = NULL;
215 GList *it, *start, *list;
216 Client *ft;
217
218 if (cancel) {
219 if (first) client_focus(first);
220 goto done_cycle;
221 } else if (done) {
222 if (focus_client) {
223 push_to_top(focus_client); /* move to top of focus_order */
224 stacking_raise(focus_client);
225 }
226 goto done_cycle;
227 }
228 if (!first) first = focus_client;
229
230 if (linear) list = client_list;
231 else list = focus_order[screen_desktop];
232
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;
237
238 do {
239 if (forward) {
240 it = it->next;
241 if (it == NULL) it = list;
242 } else {
243 it = it->prev;
244 if (it == NULL) it = g_list_last(list);
245 }
246 ft = client_focus_target(it->data);
247 if (ft == it->data && focus_client != ft && client_normal(ft) &&
248 client_focus(ft)) {
249 noreorder++; /* avoid reordering the focus_order */
250 return ft;
251 }
252 } while (it != start);
253 return NULL;
254
255 done_cycle:
256 t = NULL;
257 first = NULL;
258 g_list_free(order);
259 order = NULL;
260 return NULL;
261 }
This page took 0.045095 seconds and 5 git commands to generate.