]> icculus.org git repositories - manmower/openbox.git/blob - openbox/focus.c
Clients Menus and Slits are all 'ObWindow's now.
[manmower/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) first = focus_client;
275     if (!focus_cycle_target) focus_cycle_target = focus_client;
276
277     if (linear) list = client_list;
278     else        list = focus_order[screen_desktop];
279
280     start = it = g_list_find(list, focus_cycle_target);
281     if (!start) /* switched desktops or something? */
282         start = it = forward ? g_list_last(list) : g_list_first(list);
283     if (!start) goto done_cycle;
284
285     do {
286         if (forward) {
287             it = it->next;
288             if (it == NULL) it = g_list_first(list);
289         } else {
290             it = it->prev;
291             if (it == NULL) it = g_list_last(list);
292         }
293         /*ft = client_focus_target(it->data);*/
294         ft = it->data;
295         if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
296             (ft->can_focus || ft->focus_notify) &&
297             (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
298             if (ft != focus_cycle_target) { /* prevents flicker */
299                 if (focus_cycle_target)
300                     frame_adjust_focus(focus_cycle_target->frame, FALSE);
301                 focus_cycle_target = ft;
302                 frame_adjust_focus(focus_cycle_target->frame, TRUE);
303             }
304             popup_cycle(ft, config_focus_popup);
305             return ft;
306         }
307     } while (it != start);
308
309 done_cycle:
310     t = NULL;
311     first = NULL;
312     focus_cycle_target = NULL;
313     g_list_free(order);
314     order = NULL;
315     popup_cycle(ft, FALSE);
316     return NULL;
317 }
318
319 void focus_order_add_new(Client *c)
320 {
321     guint d, i;
322
323     if (c->iconic)
324         focus_order_to_top(c);
325     else {
326         d = c->desktop;
327         if (d == DESKTOP_ALL) {
328             for (i = 0; i < screen_num_desktops; ++i) {
329                 if (focus_order[i] && ((Client*)focus_order[i]->data)->iconic)
330                     focus_order[i] = g_list_insert(focus_order[i], c, 0);
331                 else
332                     focus_order[i] = g_list_insert(focus_order[i], c, 1);
333             }
334         } else
335              if (focus_order[d] && ((Client*)focus_order[d]->data)->iconic)
336                 focus_order[d] = g_list_insert(focus_order[d], c, 0);
337             else
338                 focus_order[d] = g_list_insert(focus_order[d], c, 1);
339     }
340 }
341
342 void focus_order_remove(Client *c)
343 {
344     guint d, i;
345
346     d = c->desktop;
347     if (d == DESKTOP_ALL) {
348         for (i = 0; i < screen_num_desktops; ++i)
349             focus_order[i] = g_list_remove(focus_order[i], c);
350     } else
351         focus_order[d] = g_list_remove(focus_order[d], c);
352 }
353
354 static void to_top(Client *c, guint d)
355 {
356     focus_order[d] = g_list_remove(focus_order[d], c);
357     if (!c->iconic) {
358         focus_order[d] = g_list_prepend(focus_order[d], c);
359     } else {
360         GList *it;
361
362         /* insert before first iconic window */
363         for (it = focus_order[d];
364              it && !((Client*)it->data)->iconic; it = it->next);
365         g_list_insert_before(focus_order[d], it, c);
366     }
367 }
368
369 void focus_order_to_top(Client *c)
370 {
371     guint d, i;
372
373     d = c->desktop;
374     if (d == DESKTOP_ALL) {
375         for (i = 0; i < screen_num_desktops; ++i)
376             to_top(c, i);
377     } else
378         to_top(c, d);
379 }
380
381 static void to_bottom(Client *c, guint d)
382 {
383     focus_order[d] = g_list_remove(focus_order[d], c);
384     if (c->iconic) {
385         focus_order[d] = g_list_append(focus_order[d], c);
386     } else {
387         GList *it;
388
389         /* insert before first iconic window */
390         for (it = focus_order[d];
391              it && !((Client*)it->data)->iconic; it = it->next);
392         g_list_insert_before(focus_order[d], it, c);
393     }
394 }
395
396 void focus_order_to_bottom(Client *c)
397 {
398     guint d, i;
399
400     d = c->desktop;
401     if (d == DESKTOP_ALL) {
402         for (i = 0; i < screen_num_desktops; ++i)
403             to_bottom(c, i);
404     } else
405         to_bottom(c, d);
406 }