]> Dogcows Code - chaz/openbox/blob - openbox/focus.c
dont try change focus while looping thru the focus list
[chaz/openbox] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "grab.h"
4 #include "framerender.h"
5 #include "client.h"
6 #include "config.h"
7 #include "frame.h"
8 #include "screen.h"
9 #include "group.h"
10 #include "prop.h"
11 #include "dispatch.h"
12 #include "focus.h"
13 #include "stacking.h"
14 #include "popup.h"
15
16 #include <X11/Xlib.h>
17 #include <glib.h>
18 #include <assert.h>
19
20 Client *focus_client = NULL;
21 GList **focus_order = NULL; /* these lists are created when screen_startup
22 sets the number of desktops */
23
24 Window focus_backup = None;
25
26 static Client *focus_cycle_target = NULL;
27 static Popup *focus_cycle_popup = NULL;
28
29 void focus_startup()
30 {
31 /* create the window which gets focus when no clients get it. Have to
32 make it override-redirect so we don't try manage it, since it is
33 mapped. */
34 XSetWindowAttributes attrib;
35
36 focus_client = NULL;
37 focus_cycle_popup = popup_new(TRUE);
38
39 attrib.override_redirect = TRUE;
40 focus_backup = XCreateWindow(ob_display, ob_root,
41 -100, -100, 1, 1, 0,
42 CopyFromParent, InputOutput, CopyFromParent,
43 CWOverrideRedirect, &attrib);
44 XMapRaised(ob_display, focus_backup);
45
46 /* start with nothing focused */
47 focus_set_client(NULL);
48 }
49
50 void focus_shutdown()
51 {
52 guint i;
53
54 for (i = 0; i < screen_num_desktops; ++i)
55 g_list_free(focus_order[i]);
56 g_free(focus_order);
57 focus_order = NULL;
58
59 popup_free(focus_cycle_popup);
60 focus_cycle_popup = NULL;
61
62 XDestroyWindow(ob_display, focus_backup);
63
64 /* reset focus to root */
65 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
66 event_lasttime);
67 }
68
69 static void push_to_top(Client *client)
70 {
71 guint desktop;
72
73 desktop = client->desktop;
74 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
75 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
76 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
77 }
78
79 void focus_set_client(Client *client)
80 {
81 Window active;
82 Client *old;
83
84 #ifdef DEBUG_FOCUS
85 g_message("focus_set_client 0x%lx", client ? client->window : 0);
86 #endif
87
88 /* uninstall the old colormap, and install the new one */
89 screen_install_colormap(focus_client, FALSE);
90 screen_install_colormap(client, TRUE);
91
92 if (client == NULL) {
93 /* when nothing will be focused, send focus to the backup target */
94 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
95 event_lasttime);
96 XSync(ob_display, FALSE);
97 }
98
99 /* in the middle of cycling..? kill it. */
100 if (focus_cycle_target)
101 focus_cycle(TRUE, TRUE, TRUE, TRUE);
102
103 old = focus_client;
104 focus_client = client;
105
106 /* move to the top of the list */
107 if (client != NULL)
108 push_to_top(client);
109
110 /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
111 if (ob_state != State_Exiting) {
112 active = client ? client->window : None;
113 PROP_SET32(ob_root, net_active_window, window, active);
114 }
115
116 if (focus_client != NULL)
117 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
118 if (old != NULL)
119 dispatch_client(Event_Client_Unfocus, old, 0, 0);
120 }
121
122 static gboolean focus_under_pointer()
123 {
124 int x, y;
125 GList *it;
126
127 if (ob_pointer_pos(&x, &y)) {
128 for (it = stacking_list; it != NULL; it = it->next) {
129 if (WINDOW_IS_CLIENT(it->data)) {
130 Client *c = WINDOW_AS_CLIENT(it->data);
131 if (c->desktop == screen_desktop &&
132 RECT_CONTAINS(c->frame->area, x, y))
133 break;
134 }
135 }
136 if (it != NULL) {
137 g_assert(WINDOW_IS_CLIENT(it->data));
138 return client_normal(it->data) && client_focus(it->data);
139 }
140 }
141 return FALSE;
142 }
143
144 /* finds the first transient that isn't 'skip' and ensure's that client_normal
145 is true for it */
146 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
147 {
148 GSList *it;
149 Client *ret;
150
151 for (it = c->transients; it; it = it->next) {
152 if (it->data == top) return NULL;
153 ret = find_transient_recursive(it->data, top, skip);
154 if (ret && ret != skip && client_normal(ret)) return ret;
155 if (it->data != skip && client_normal(it->data)) return it->data;
156 }
157 return NULL;
158 }
159
160 static gboolean focus_fallback_transient(Client *top, Client *old)
161 {
162 Client *target = find_transient_recursive(top, top, old);
163 if (!target) {
164 /* make sure client_normal is true always */
165 if (!client_normal(top))
166 return FALSE;
167 target = top; /* no transient, keep the top */
168 }
169 return client_focus(target);
170 }
171
172 void focus_fallback(FallbackType type)
173 {
174 GList *it;
175 Client *old = NULL;
176
177 old = focus_client;
178
179 /* unfocus any focused clients.. they can be focused by Pointer events
180 and such, and then when I try focus them, I won't get a FocusIn event
181 at all for them.
182 */
183 focus_set_client(NULL);
184
185 if (!(type == Fallback_Desktop ?
186 config_focus_last_on_desktop : config_focus_last)) {
187 if (config_focus_follow) focus_under_pointer();
188 return;
189 }
190
191 if (type == Fallback_Unfocusing && old) {
192 /* try for transient relations */
193 if (old->transient_for) {
194 if (old->transient_for == TRAN_GROUP) {
195 for (it = focus_order[screen_desktop]; it; it = it->next) {
196 GSList *sit;
197
198 for (sit = old->group->members; sit; sit = sit->next)
199 if (sit->data == it->data)
200 if (focus_fallback_transient(sit->data, old))
201 return;
202 }
203 } else {
204 if (focus_fallback_transient(old->transient_for, old))
205 return;
206 }
207 }
208
209 /* try for group relations */
210 if (old->group) {
211 GSList *sit;
212
213 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
214 for (sit = old->group->members; sit; sit = sit->next)
215 if (sit->data == it->data)
216 if (sit->data != old && client_normal(sit->data))
217 if (client_can_focus(sit->data)) {
218 gboolean r = client_focus(sit->data);
219 assert(r);
220 return;
221 }
222 }
223 }
224
225 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
226 if (type != Fallback_Unfocusing || it->data != old)
227 if (client_normal(it->data) &&
228 /* dont fall back to 'anonymous' fullscreen windows. theres no
229 checks for this is in transient/group fallbacks, so they can
230 be fallback targets there. */
231 !((Client*)it->data)->fullscreen &&
232 client_can_focus(it->data)) {
233 gboolean r = client_focus(sit->data);
234 assert(r);
235 return;
236 }
237
238 /* nothing to focus */
239 focus_set_client(NULL);
240 }
241
242 static void popup_cycle(Client *c, gboolean show)
243 {
244 if (!show) {
245 popup_hide(focus_cycle_popup);
246 } else {
247 Rect *a;
248
249 a = screen_area(c->desktop);
250 popup_position(focus_cycle_popup, CenterGravity,
251 a->x + a->width / 2, a->y + a->height / 2);
252 /* popup_size(focus_cycle_popup, a->height/2, a->height/16);
253 popup_show(focus_cycle_popup, c->title,
254 client_icon(c, a->height/16, a->height/16));
255 */
256 /* XXX the size and the font extents need to be related on some level
257 */
258 popup_size(focus_cycle_popup, 320, 48);
259
260 /* use the transient's parent's title/icon */
261 while (c->transient_for && c->transient_for != TRAN_GROUP)
262 c = c->transient_for;
263
264 popup_show(focus_cycle_popup, (c->iconic ? c->icon_title : c->title),
265 client_icon(c, 48, 48));
266 }
267 }
268
269 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
270 gboolean cancel)
271 {
272 static Client *first = NULL;
273 static Client *t = NULL;
274 static GList *order = NULL;
275 GList *it, *start, *list;
276 Client *ft;
277
278 if (cancel) {
279 if (focus_cycle_target)
280 frame_adjust_focus(focus_cycle_target->frame, FALSE);
281 if (focus_client)
282 frame_adjust_focus(focus_client->frame, TRUE);
283 goto done_cycle;
284 } else if (done) {
285 if (focus_cycle_target)
286 client_activate(focus_cycle_target);
287 goto done_cycle;
288 }
289 if (!first)
290 grab_pointer(TRUE, None);
291
292 if (!first) first = focus_client;
293 if (!focus_cycle_target) focus_cycle_target = focus_client;
294
295 if (linear) list = client_list;
296 else list = focus_order[screen_desktop];
297
298 start = it = g_list_find(list, focus_cycle_target);
299 if (!start) /* switched desktops or something? */
300 start = it = forward ? g_list_last(list) : g_list_first(list);
301 if (!start) goto done_cycle;
302
303 do {
304 if (forward) {
305 it = it->next;
306 if (it == NULL) it = g_list_first(list);
307 } else {
308 it = it->prev;
309 if (it == NULL) it = g_list_last(list);
310 }
311 /*ft = client_focus_target(it->data);*/
312 ft = it->data;
313 if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
314 (ft->can_focus || ft->focus_notify) &&
315 (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
316 if (ft != focus_cycle_target) { /* prevents flicker */
317 if (focus_cycle_target)
318 frame_adjust_focus(focus_cycle_target->frame, FALSE);
319 focus_cycle_target = ft;
320 frame_adjust_focus(focus_cycle_target->frame, TRUE);
321 }
322 popup_cycle(ft, config_focus_popup);
323 return ft;
324 }
325 } while (it != start);
326
327 done_cycle:
328 t = NULL;
329 first = NULL;
330 focus_cycle_target = NULL;
331 g_list_free(order);
332 order = NULL;
333
334 popup_cycle(ft, FALSE);
335 grab_pointer(FALSE, None);
336
337 return NULL;
338 }
339
340 void focus_order_add_new(Client *c)
341 {
342 guint d, i;
343
344 if (c->iconic)
345 focus_order_to_top(c);
346 else {
347 d = c->desktop;
348 if (d == DESKTOP_ALL) {
349 for (i = 0; i < screen_num_desktops; ++i) {
350 if (focus_order[i] && ((Client*)focus_order[i]->data)->iconic)
351 focus_order[i] = g_list_insert(focus_order[i], c, 0);
352 else
353 focus_order[i] = g_list_insert(focus_order[i], c, 1);
354 }
355 } else
356 if (focus_order[d] && ((Client*)focus_order[d]->data)->iconic)
357 focus_order[d] = g_list_insert(focus_order[d], c, 0);
358 else
359 focus_order[d] = g_list_insert(focus_order[d], c, 1);
360 }
361 }
362
363 void focus_order_remove(Client *c)
364 {
365 guint d, i;
366
367 d = c->desktop;
368 if (d == DESKTOP_ALL) {
369 for (i = 0; i < screen_num_desktops; ++i)
370 focus_order[i] = g_list_remove(focus_order[i], c);
371 } else
372 focus_order[d] = g_list_remove(focus_order[d], c);
373 }
374
375 static void to_top(Client *c, guint d)
376 {
377 focus_order[d] = g_list_remove(focus_order[d], c);
378 if (!c->iconic) {
379 focus_order[d] = g_list_prepend(focus_order[d], c);
380 } else {
381 GList *it;
382
383 /* insert before first iconic window */
384 for (it = focus_order[d];
385 it && !((Client*)it->data)->iconic; it = it->next);
386 g_list_insert_before(focus_order[d], it, c);
387 }
388 }
389
390 void focus_order_to_top(Client *c)
391 {
392 guint d, i;
393
394 d = c->desktop;
395 if (d == DESKTOP_ALL) {
396 for (i = 0; i < screen_num_desktops; ++i)
397 to_top(c, i);
398 } else
399 to_top(c, d);
400 }
401
402 static void to_bottom(Client *c, guint d)
403 {
404 focus_order[d] = g_list_remove(focus_order[d], c);
405 if (c->iconic) {
406 focus_order[d] = g_list_append(focus_order[d], c);
407 } else {
408 GList *it;
409
410 /* insert before first iconic window */
411 for (it = focus_order[d];
412 it && !((Client*)it->data)->iconic; it = it->next);
413 g_list_insert_before(focus_order[d], it, c);
414 }
415 }
416
417 void focus_order_to_bottom(Client *c)
418 {
419 guint d, i;
420
421 d = c->desktop;
422 if (d == DESKTOP_ALL) {
423 for (i = 0; i < screen_num_desktops; ++i)
424 to_bottom(c, i);
425 } else
426 to_bottom(c, d);
427 }
This page took 0.056973 seconds and 5 git commands to generate.