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