]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/focus.c
add a client_activate function, use it for net_wm_active mesgs and for focus cycling.
[mikachu/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     XMapWindow(ob_display, focus_backup);
44     stacking_raise_internal(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     /* uninstall the old colormap, and install the new one */
85     screen_install_colormap(focus_client, FALSE);
86     screen_install_colormap(client, TRUE);
87
88     if (client == NULL) {
89         /* when nothing will be focused, send focus to the backup target */
90         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
91                        event_lasttime);
92         XSync(ob_display, FALSE);
93     }
94
95     /* in the middle of cycling..? kill it. */
96     if (focus_cycle_target)
97         focus_cycle(TRUE, TRUE, TRUE, TRUE);
98
99     old = focus_client;
100     focus_client = client;
101
102     /* move to the top of the list */
103     if (client != NULL)
104         push_to_top(client);
105
106     /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
107     if (ob_state != State_Exiting) {
108         active = client ? client->window : None;
109         PROP_SET32(ob_root, net_active_window, window, active);
110     }
111
112     if (focus_client != NULL)
113         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
114     if (old != NULL)
115         dispatch_client(Event_Client_Unfocus, old, 0, 0);
116 }
117
118 static gboolean focus_under_pointer()
119 {
120     int x, y;
121     GList *it;
122
123     if (ob_pointer_pos(&x, &y)) {
124         for (it = stacking_list; it != NULL; it = it->next) {
125             Client *c = it->data;
126             if (c->desktop == screen_desktop &&
127                 RECT_CONTAINS(c->frame->area, x, y))
128                 break;
129         }
130         if (it != NULL)
131             return client_normal(it->data) && client_focus(it->data);
132     }
133     return FALSE;
134 }
135
136 /* finds the first transient that isn't 'skip' and ensure's that client_normal
137  is true for it */
138 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
139 {
140     GSList *it;
141     Client *ret;
142
143     for (it = c->transients; it; it = it->next) {
144         if (it->data == top) return NULL;
145         ret = find_transient_recursive(it->data, top, skip);
146         if (ret && ret != skip && client_normal(ret)) return ret;
147         if (it->data != skip && client_normal(it->data)) return it->data;
148     }
149     return NULL;
150 }
151
152 static gboolean focus_fallback_transient(Client *top, Client *old)
153 {
154     Client *target = find_transient_recursive(top, top, old);
155     if (!target) {
156         /* make sure client_normal is true always */
157         if (!client_normal(top))
158             return FALSE;
159         target = top; /* no transient, keep the top */
160     }
161     return client_focus(target);
162 }
163
164 void focus_fallback(FallbackType type)
165 {
166     GList *it;
167     Client *old = NULL;
168
169     old = focus_client;
170
171     /* unfocus any focused clients.. they can be focused by Pointer events
172        and such, and then when I try focus them, I won't get a FocusIn event
173        at all for them.
174     */
175     focus_set_client(NULL);
176
177     if (!(type == Fallback_Desktop ?
178           config_focus_last_on_desktop : config_focus_last)) {
179         if (config_focus_follow) focus_under_pointer();
180         return;
181     }
182
183     if (type == Fallback_Unfocusing && old) {
184         /* try for transient relations */
185         if (old->transient_for) {
186             if (old->transient_for == TRAN_GROUP) {
187                 for (it = focus_order[screen_desktop]; it; it = it->next) {
188                     GSList *sit;
189
190                     for (sit = old->group->members; sit; sit = sit->next)
191                         if (sit->data == it->data)
192                             if (focus_fallback_transient(sit->data, old))
193                                 return;
194                 }
195             } else {
196                 if (focus_fallback_transient(old->transient_for, old))
197                     return;
198             }
199         }
200
201         /* try for group relations */
202         if (old->group) {
203             GSList *sit;
204
205             for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
206                 for (sit = old->group->members; sit; sit = sit->next)
207                     if (sit->data == it->data)
208                         if (sit->data != old && client_normal(sit->data))
209                             if (client_focus(sit->data))
210                                 return;
211         }
212     }
213
214     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
215         if (type != Fallback_Unfocusing || it->data != old)
216             if (client_normal(it->data) &&
217                 /* dont fall back to 'anonymous' fullscreen windows. theres no
218                    checks for this is in transient/group fallbacks. */
219                 !((Client*)it->data)->fullscreen &&
220                 client_focus(it->data))
221                 return;
222
223     /* nothing to focus */
224     focus_set_client(NULL);
225 }
226
227 static void popup_cycle(Client *c, gboolean show)
228 {
229     if (!show) {
230         popup_hide(focus_cycle_popup);
231     } else {
232         Rect *a;
233
234         a = screen_area(c->desktop);
235         popup_position(focus_cycle_popup, CenterGravity,
236                        a->x + a->width / 2, a->y + a->height / 2);
237 /*        popup_size(focus_cycle_popup, a->height/2, a->height/16);
238         popup_show(focus_cycle_popup, c->title,
239                    client_icon(c, a->height/16, a->height/16));
240 */
241         /* XXX the size and the font extents need to be related on some level
242          */
243         popup_size(focus_cycle_popup, 320, 48);
244
245         /* use the transient's parent's title/icon */
246         while (c->transient_for && c->transient_for != TRAN_GROUP)
247             c = c->transient_for;
248
249         popup_show(focus_cycle_popup, (c->iconic ? c->icon_title : c->title),
250                    client_icon(c, 48, 48));
251     }
252 }
253
254 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
255                     gboolean cancel)
256 {
257     static Client *first = NULL;
258     static Client *t = NULL;
259     static GList *order = NULL;
260     GList *it, *start, *list;
261     Client *ft;
262
263     if (cancel) {
264         /*if (first) client_focus(first); XXX*/
265         if (focus_cycle_target)
266             frame_adjust_focus(focus_cycle_target->frame, FALSE);
267         if (focus_client)
268             frame_adjust_focus(focus_client->frame, TRUE);
269         goto done_cycle;
270     } else if (done) {
271         if (focus_cycle_target)
272             client_activate(focus_cycle_target);
273         goto done_cycle;
274     }
275     if (!first) first = focus_client;
276     if (!focus_cycle_target) focus_cycle_target = focus_client;
277
278     if (linear) list = client_list;
279     else        list = focus_order[screen_desktop];
280
281     start = it = g_list_find(list, focus_cycle_target);
282     if (!start) /* switched desktops or something? */
283         start = it = forward ? g_list_last(list) : g_list_first(list);
284     if (!start) goto done_cycle;
285
286     do {
287         if (forward) {
288             it = it->next;
289             if (it == NULL) it = g_list_first(list);
290         } else {
291             it = it->prev;
292             if (it == NULL) it = g_list_last(list);
293         }
294         /*ft = client_focus_target(it->data);*/
295         ft = it->data;
296         if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
297             (ft->can_focus || ft->focus_notify) &&
298             (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
299             if (ft != focus_cycle_target) { /* prevents flicker */
300                 if (focus_cycle_target)
301                     frame_adjust_focus(focus_cycle_target->frame, FALSE);
302                 focus_cycle_target = ft;
303                 frame_adjust_focus(focus_cycle_target->frame, TRUE);
304             }
305             popup_cycle(ft, config_focus_popup);
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 }