]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
grab the mouse during focus cycling. this removes the bug of focusing following the...
[dana/openbox.git] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "framerender.h"
4 #include "client.h"
5 #include "config.h"
6 #include "frame.h"
7 #include "screen.h"
8 #include "group.h"
9 #include "prop.h"
10 #include "dispatch.h"
11 #include "focus.h"
12 #include "parse.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     /* uninstall the old colormap, and install the new one */
84     screen_install_colormap(focus_client, FALSE);
85     screen_install_colormap(client, TRUE);
86
87     if (client == NULL) {
88         /* when nothing will be focused, send focus to the backup target */
89         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
90                        event_lasttime);
91         XSync(ob_display, FALSE);
92     }
93
94     /* in the middle of cycling..? kill it. */
95     if (focus_cycle_target)
96         focus_cycle(TRUE, TRUE, TRUE, TRUE);
97
98     old = focus_client;
99     focus_client = client;
100
101     /* move to the top of the list */
102     if (client != NULL)
103         push_to_top(client);
104
105     /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
106     if (ob_state != State_Exiting) {
107         active = client ? client->window : None;
108         PROP_SET32(ob_root, net_active_window, window, active);
109     }
110
111     if (focus_client != NULL)
112         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
113     if (old != NULL)
114         dispatch_client(Event_Client_Unfocus, old, 0, 0);
115 }
116
117 static gboolean focus_under_pointer()
118 {
119     int x, y;
120     GList *it;
121
122     if (ob_pointer_pos(&x, &y)) {
123         for (it = stacking_list; it != NULL; it = it->next) {
124             Client *c = it->data;
125             if (c->desktop == screen_desktop &&
126                 RECT_CONTAINS(c->frame->area, x, y))
127                 break;
128         }
129         if (it != NULL)
130             return client_normal(it->data) && client_focus(it->data);
131     }
132     return FALSE;
133 }
134
135 /* finds the first transient that isn't 'skip' and ensure's that client_normal
136  is true for it */
137 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
138 {
139     GSList *it;
140     Client *ret;
141
142     for (it = c->transients; it; it = it->next) {
143         if (it->data == top) return NULL;
144         ret = find_transient_recursive(it->data, top, skip);
145         if (ret && ret != skip && client_normal(ret)) return ret;
146         if (it->data != skip && client_normal(it->data)) return it->data;
147     }
148     return NULL;
149 }
150
151 static gboolean focus_fallback_transient(Client *top, Client *old)
152 {
153     Client *target = find_transient_recursive(top, top, old);
154     if (!target) {
155         /* make sure client_normal is true always */
156         if (!client_normal(top))
157             return FALSE;
158         target = top; /* no transient, keep the top */
159     }
160     return client_focus(target);
161 }
162
163 void focus_fallback(FallbackType type)
164 {
165     GList *it;
166     Client *old = NULL;
167
168     old = focus_client;
169
170     /* unfocus any focused clients.. they can be focused by Pointer events
171        and such, and then when I try focus them, I won't get a FocusIn event
172        at all for them.
173     */
174     focus_set_client(NULL);
175
176     if (!(type == Fallback_Desktop ?
177           config_focus_last_on_desktop : config_focus_last)) {
178         if (config_focus_follow) focus_under_pointer();
179         return;
180     }
181
182     if (type == Fallback_Unfocusing && old) {
183         /* try for transient relations */
184         if (old->transient_for) {
185             if (old->transient_for == TRAN_GROUP) {
186                 for (it = focus_order[screen_desktop]; it; it = it->next) {
187                     GSList *sit;
188
189                     for (sit = old->group->members; sit; sit = sit->next)
190                         if (sit->data == it->data)
191                             if (focus_fallback_transient(sit->data, old))
192                                 return;
193                 }
194             } else {
195                 if (focus_fallback_transient(old->transient_for, old))
196                     return;
197             }
198         }
199
200         /* try for group relations */
201         if (old->group) {
202             GSList *sit;
203
204             for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
205                 for (sit = old->group->members; sit; sit = sit->next)
206                     if (sit->data == it->data)
207                         if (sit->data != old && client_normal(sit->data))
208                             if (client_focus(sit->data))
209                                 return;
210         }
211     }
212
213     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
214         if (type != Fallback_Unfocusing || it->data != old)
215             if (client_normal(it->data) &&
216                 /* dont fall back to 'anonymous' fullscreen windows. theres no
217                    checks for this is in transient/group fallbacks. */
218                 !((Client*)it->data)->fullscreen &&
219                 client_focus(it->data))
220                 return;
221
222     /* nothing to focus */
223     focus_set_client(NULL);
224 }
225
226 static void popup_cycle(Client *c, gboolean show)
227 {
228     if (!show) {
229         popup_hide(focus_cycle_popup);
230     } else {
231         Rect *a;
232
233         a = screen_area(c->desktop);
234         popup_position(focus_cycle_popup, CenterGravity,
235                        a->x + a->width / 2, a->y + a->height / 2);
236 /*        popup_size(focus_cycle_popup, a->height/2, a->height/16);
237         popup_show(focus_cycle_popup, c->title,
238                    client_icon(c, a->height/16, a->height/16));
239 */
240         /* XXX the size and the font extents need to be related on some level
241          */
242         popup_size(focus_cycle_popup, 320, 48);
243
244         /* use the transient's parent's title/icon */
245         while (c->transient_for && c->transient_for != TRAN_GROUP)
246             c = c->transient_for;
247
248         popup_show(focus_cycle_popup, (c->iconic ? c->icon_title : c->title),
249                    client_icon(c, 48, 48));
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             client_activate(focus_cycle_target);
272         goto done_cycle;
273     }
274     if (!first)
275         grab_pointer(TRUE, None);
276
277     if (!first) first = focus_client;
278     if (!focus_cycle_target) focus_cycle_target = focus_client;
279
280     if (linear) list = client_list;
281     else        list = focus_order[screen_desktop];
282
283     start = it = g_list_find(list, focus_cycle_target);
284     if (!start) /* switched desktops or something? */
285         start = it = forward ? g_list_last(list) : g_list_first(list);
286     if (!start) goto done_cycle;
287
288     do {
289         if (forward) {
290             it = it->next;
291             if (it == NULL) it = g_list_first(list);
292         } else {
293             it = it->prev;
294             if (it == NULL) it = g_list_last(list);
295         }
296         /*ft = client_focus_target(it->data);*/
297         ft = it->data;
298         if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
299             (ft->can_focus || ft->focus_notify) &&
300             (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
301             if (ft != focus_cycle_target) { /* prevents flicker */
302                 if (focus_cycle_target)
303                     frame_adjust_focus(focus_cycle_target->frame, FALSE);
304                 focus_cycle_target = ft;
305                 frame_adjust_focus(focus_cycle_target->frame, TRUE);
306             }
307             popup_cycle(ft, config_focus_popup);
308             return ft;
309         }
310     } while (it != start);
311
312 done_cycle:
313     t = NULL;
314     first = NULL;
315     focus_cycle_target = NULL;
316     g_list_free(order);
317     order = NULL;
318
319     popup_cycle(ft, FALSE);
320     grab_pointer(FALSE, None);
321
322     return NULL;
323 }
324
325 void focus_order_add_new(Client *c)
326 {
327     guint d, i;
328
329     if (c->iconic)
330         focus_order_to_top(c);
331     else {
332         d = c->desktop;
333         if (d == DESKTOP_ALL) {
334             for (i = 0; i < screen_num_desktops; ++i) {
335                 if (focus_order[i] && ((Client*)focus_order[i]->data)->iconic)
336                     focus_order[i] = g_list_insert(focus_order[i], c, 0);
337                 else
338                     focus_order[i] = g_list_insert(focus_order[i], c, 1);
339             }
340         } else
341              if (focus_order[d] && ((Client*)focus_order[d]->data)->iconic)
342                 focus_order[d] = g_list_insert(focus_order[d], c, 0);
343             else
344                 focus_order[d] = g_list_insert(focus_order[d], c, 1);
345     }
346 }
347
348 void focus_order_remove(Client *c)
349 {
350     guint d, i;
351
352     d = c->desktop;
353     if (d == DESKTOP_ALL) {
354         for (i = 0; i < screen_num_desktops; ++i)
355             focus_order[i] = g_list_remove(focus_order[i], c);
356     } else
357         focus_order[d] = g_list_remove(focus_order[d], c);
358 }
359
360 static void to_top(Client *c, guint d)
361 {
362     focus_order[d] = g_list_remove(focus_order[d], c);
363     if (!c->iconic) {
364         focus_order[d] = g_list_prepend(focus_order[d], c);
365     } else {
366         GList *it;
367
368         /* insert before first iconic window */
369         for (it = focus_order[d];
370              it && !((Client*)it->data)->iconic; it = it->next);
371         g_list_insert_before(focus_order[d], it, c);
372     }
373 }
374
375 void focus_order_to_top(Client *c)
376 {
377     guint d, i;
378
379     d = c->desktop;
380     if (d == DESKTOP_ALL) {
381         for (i = 0; i < screen_num_desktops; ++i)
382             to_top(c, i);
383     } else
384         to_top(c, d);
385 }
386
387 static void to_bottom(Client *c, guint d)
388 {
389     focus_order[d] = g_list_remove(focus_order[d], c);
390     if (c->iconic) {
391         focus_order[d] = g_list_append(focus_order[d], c);
392     } else {
393         GList *it;
394
395         /* insert before first iconic window */
396         for (it = focus_order[d];
397              it && !((Client*)it->data)->iconic; it = it->next);
398         g_list_insert_before(focus_order[d], it, c);
399     }
400 }
401
402 void focus_order_to_bottom(Client *c)
403 {
404     guint d, i;
405
406     d = c->desktop;
407     if (d == DESKTOP_ALL) {
408         for (i = 0; i < screen_num_desktops; ++i)
409             to_bottom(c, i);
410     } else
411         to_bottom(c, d);
412 }