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