fallback to transients' parents when possible.
[dana/openbox.git] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "client.h"
4 #include "frame.h"
5 #include "screen.h"
6 #include "group.h"
7 #include "prop.h"
8 #include "dispatch.h"
9 #include "focus.h"
10 #include "parse.h"
11 #include "engine.h"
12
13 #include <X11/Xlib.h>
14 #include <glib.h>
15
16 Client *focus_client = NULL;
17 GList **focus_order = NULL; /* these lists are created when screen_startup
18                                sets the number of desktops */
19
20 Window focus_backup = None;
21 gboolean focus_new = TRUE;
22 gboolean focus_follow = FALSE;
23 static gboolean focus_last = TRUE;
24 static gboolean focus_last_on_desktop = TRUE;
25
26 static gboolean noreorder = 0;
27
28 static void parse_assign(char *name, ParseToken *value)
29 {
30     if (!g_ascii_strcasecmp(name, "focusnew")) {
31         if (value->type != TOKEN_BOOL)
32             yyerror("invalid value");
33         else {
34             focus_new = value->data.bool;
35         }
36     } else if (!g_ascii_strcasecmp(name, "followmouse")) {
37         if (value->type != TOKEN_BOOL)
38             yyerror("invalid value");
39         else {
40             focus_follow = value->data.bool;
41         }
42     } else if (!g_ascii_strcasecmp(name, "focuslast")) {
43         if (value->type != TOKEN_BOOL)
44             yyerror("invalid value");
45         else {
46             focus_last = value->data.bool;
47         }
48     } else if (!g_ascii_strcasecmp(name, "focuslastondesktop")) {
49         if (value->type != TOKEN_BOOL)
50             yyerror("invalid value");
51         else {
52             focus_last_on_desktop = value->data.bool;
53         }
54     } else
55         yyerror("invalid option");
56     parse_free_token(value);
57 }
58
59 void focus_startup()
60 {
61     /* create the window which gets focus when no clients get it. Have to
62        make it override-redirect so we don't try manage it, since it is
63        mapped. */
64     XSetWindowAttributes attrib;
65
66     focus_client = NULL;
67     focus_new = TRUE;
68     focus_follow = FALSE;
69     focus_last = TRUE;
70     focus_last_on_desktop = TRUE;
71
72     attrib.override_redirect = TRUE;
73     focus_backup = XCreateWindow(ob_display, ob_root,
74                                  -100, -100, 1, 1, 0,
75                                  CopyFromParent, InputOutput, CopyFromParent,
76                                  CWOverrideRedirect, &attrib);
77     XMapRaised(ob_display, focus_backup);
78
79     /* start with nothing focused */
80     focus_set_client(NULL);
81
82     parse_reg_section("focus", NULL, parse_assign);
83 }
84
85 void focus_shutdown()
86 {
87     guint i;
88
89     for (i = 0; i < screen_num_desktops; ++i)
90         g_list_free(focus_order[i]);
91     g_free(focus_order);
92     focus_order = NULL;
93
94     XDestroyWindow(ob_display, focus_backup);
95
96     /* reset focus to root */
97     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
98                    event_lasttime);
99 }
100
101 static void push_to_top(Client *client)
102 {
103     guint desktop;
104
105     desktop = client->desktop;
106     if (desktop == DESKTOP_ALL) desktop = screen_desktop;
107     focus_order[desktop] = g_list_remove(focus_order[desktop], client);
108     focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
109 }
110
111 void focus_set_client(Client *client)
112 {
113     Window active;
114     Client *old;
115
116     /* uninstall the old colormap, and install the new one */
117     screen_install_colormap(focus_client, FALSE);
118     screen_install_colormap(client, TRUE);
119
120     if (client == NULL) {
121         /* when nothing will be focused, send focus to the backup target */
122         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
123                        event_lasttime);
124         XSync(ob_display, FALSE);
125     }
126
127     old = focus_client;
128     focus_client = client;
129
130     /* move to the top of the list */
131     if (noreorder)
132         --noreorder;
133     else if (client != NULL)
134         push_to_top(client);
135
136     /* set the NET_ACTIVE_WINDOW hint */
137     active = client ? client->window : None;
138     PROP_SET32(ob_root, net_active_window, window, active);
139
140     if (focus_client != NULL)
141         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
142     if (old != NULL)
143         dispatch_client(Event_Client_Unfocus, old, 0, 0);
144 }
145
146 static gboolean focus_under_pointer()
147 {
148     Window w;
149     int i, x, y;
150     guint u;
151     GList *it;
152
153     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
154         for (it = stacking_list; it != NULL; it = it->next) {
155             Client *c = it->data;
156             if (c->desktop == screen_desktop &&
157                 RECT_CONTAINS(c->frame->area, x, y))
158                 break;
159         }
160         if (it != NULL)
161             return client_normal(it->data) && client_focus(it->data);
162     }
163     return FALSE;
164 }
165
166 void focus_fallback(gboolean switching_desks)
167 {
168     GList *it;
169     Client *old = NULL;
170
171     old = focus_client;
172
173     /* unfocus any focused clients.. they can be focused by Pointer events
174        and such, and then when I try focus them, I won't get a FocusIn event
175        at all for them.
176     */
177     focus_set_client(NULL);
178
179     if (switching_desks) {
180         /* don't skip any windows when switching desktops */
181         old = NULL;
182     }
183
184     if (!(switching_desks ? focus_last_on_desktop : focus_last)) {
185         if (focus_follow) focus_under_pointer();
186         return;
187     }
188
189     if (old && old->transient_for) {
190         if (old->transient_for == TRAN_GROUP) {
191             for (it = focus_order[screen_desktop]; it != NULL; it = it->next) {
192                 GSList *sit;
193
194                 for (sit = old->group->members; sit; sit = sit->next)
195                     if (sit->data == it->data && client_focus(sit->data))
196                         return;
197             }
198         } else {
199             if (client_focus(old->transient_for))
200                 return;
201         }
202     }
203
204     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
205         if (it->data != old && client_normal(it->data))
206             if (client_focus(it->data))
207                 return;
208
209     /* nothing to focus */
210     focus_set_client(NULL);
211 }
212
213 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
214                  gboolean cancel)
215 {
216     static Client *first = NULL;
217     static Client *t = NULL;
218     static GList *order = NULL;
219     GList *it, *start, *list;
220     Client *ft;
221
222     if (cancel) {
223         if (first) client_focus(first);
224         goto done_cycle;
225     } else if (done) {
226         if (focus_client) {
227             push_to_top(focus_client); /* move to top of focus_order */
228             stacking_raise(focus_client);
229         }
230         goto done_cycle;
231     }
232     if (!first) first = focus_client;
233
234     if (linear) list = client_list;
235     else        list = focus_order[screen_desktop];
236
237     start = it = g_list_find(list, focus_client);
238     if (!start) /* switched desktops or something? */
239         start = it = forward ? g_list_last(list) : g_list_first(list);
240     if (!start) goto done_cycle;
241
242     do {
243         if (forward) {
244             it = it->next;
245             if (it == NULL) it = list;
246         } else {
247             it = it->prev;
248             if (it == NULL) it = g_list_last(list);
249         }
250         ft = client_focus_target(it->data);
251         if (ft == it->data && focus_client != ft && client_focusable(ft)) {
252             if (client_focus(ft)) {
253                 noreorder++; /* avoid reordering the focus_order */
254                 return ft;
255             }
256         }
257     } while (it != start);
258     return NULL;
259
260 done_cycle:
261     t = NULL;
262     first = NULL;
263     g_list_free(order);
264     order = NULL;
265     return NULL;
266 }