let the frame hold a focus state so that it can lie.
[mikachu/openbox.git] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "client.h"
4 #include "config.h"
5 #include "frame.h"
6 #include "screen.h"
7 #include "group.h"
8 #include "prop.h"
9 #include "dispatch.h"
10 #include "focus.h"
11 #include "parse.h"
12 #include "stacking.h"
13
14 #include <X11/Xlib.h>
15 #include <glib.h>
16
17 Client *focus_client = NULL;
18 GList **focus_order = NULL; /* these lists are created when screen_startup
19                                sets the number of desktops */
20
21 Window focus_backup = None;
22
23 static Client *focus_cycle_target = NULL;
24
25 void focus_startup()
26 {
27     /* create the window which gets focus when no clients get it. Have to
28        make it override-redirect so we don't try manage it, since it is
29        mapped. */
30     XSetWindowAttributes attrib;
31
32     focus_client = NULL;
33
34     attrib.override_redirect = TRUE;
35     focus_backup = XCreateWindow(ob_display, ob_root,
36                                  -100, -100, 1, 1, 0,
37                                  CopyFromParent, InputOutput, CopyFromParent,
38                                  CWOverrideRedirect, &attrib);
39     XMapWindow(ob_display, focus_backup);
40     stacking_raise_internal(focus_backup);
41
42     /* start with nothing focused */
43     focus_set_client(NULL);
44 }
45
46 void focus_shutdown()
47 {
48     guint i;
49
50     for (i = 0; i < screen_num_desktops; ++i)
51         g_list_free(focus_order[i]);
52     g_free(focus_order);
53     focus_order = NULL;
54
55     XDestroyWindow(ob_display, focus_backup);
56
57     /* reset focus to root */
58     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
59                    event_lasttime);
60 }
61
62 static void push_to_top(Client *client)
63 {
64     guint desktop;
65
66     desktop = client->desktop;
67     if (desktop == DESKTOP_ALL) desktop = screen_desktop;
68     focus_order[desktop] = g_list_remove(focus_order[desktop], client);
69     focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
70 }
71
72 void focus_set_client(Client *client)
73 {
74     Window active;
75     Client *old;
76
77     /* uninstall the old colormap, and install the new one */
78     screen_install_colormap(focus_client, FALSE);
79     screen_install_colormap(client, TRUE);
80
81     if (client == NULL) {
82         /* when nothing will be focused, send focus to the backup target */
83         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
84                        event_lasttime);
85         XSync(ob_display, FALSE);
86     }
87
88     /* in the middle of cycling..? kill it. */
89     if (focus_cycle_target)
90         focus_cycle(TRUE, TRUE, TRUE, TRUE);
91
92     old = focus_client;
93     focus_client = client;
94
95     /* move to the top of the list */
96     if (client != NULL)
97         push_to_top(client);
98
99     /* set the NET_ACTIVE_WINDOW hint */
100     active = client ? client->window : None;
101     PROP_SET32(ob_root, net_active_window, window, active);
102
103     if (focus_client != NULL)
104         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
105     if (old != NULL)
106         dispatch_client(Event_Client_Unfocus, old, 0, 0);
107 }
108
109 static gboolean focus_under_pointer()
110 {
111     int x, y;
112     GList *it;
113
114     if (ob_pointer_pos(&x, &y)) {
115         for (it = stacking_list; it != NULL; it = it->next) {
116             Client *c = it->data;
117             if (c->desktop == screen_desktop &&
118                 RECT_CONTAINS(c->frame->area, x, y))
119                 break;
120         }
121         if (it != NULL)
122             return client_normal(it->data) && client_focus(it->data);
123     }
124     return FALSE;
125 }
126
127 /* finds the first transient that isn't 'skip' and ensure's that client_normal
128  is true for it */
129 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
130 {
131     GSList *it;
132     Client *ret;
133
134     for (it = c->transients; it; it = it->next) {
135         g_message("looking");
136         if (it->data == top) return NULL;
137         ret = find_transient_recursive(it->data, top, skip);
138         if (ret && ret != skip && client_normal(ret)) return ret;
139         if (it->data != skip && client_normal(it->data)) return it->data;
140         g_message("not found");
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 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
217                     gboolean cancel)
218 {
219     static Client *first = NULL;
220     static Client *t = NULL;
221     static GList *order = NULL;
222     GList *it, *start, *list;
223     Client *ft;
224
225     if (cancel) {
226         /*if (first) client_focus(first); XXX*/
227         if (focus_cycle_target)
228             frame_adjust_focus(focus_cycle_target->frame, FALSE);
229         if (focus_client)
230             frame_adjust_focus(focus_client->frame, TRUE);
231         goto done_cycle;
232     } else if (done) {
233         if (focus_cycle_target) {
234             client_focus(focus_cycle_target);
235             stacking_raise(focus_cycle_target);
236         }
237         goto done_cycle;
238     }
239     if (!first) first = focus_client;
240     if (!focus_cycle_target) focus_cycle_target = focus_client;
241
242     if (linear) list = client_list;
243     else        list = focus_order[screen_desktop];
244
245     start = it = g_list_find(list, focus_cycle_target);
246     if (!start) /* switched desktops or something? */
247         start = it = forward ? g_list_last(list) : g_list_first(list);
248     if (!start) goto done_cycle;
249
250     do {
251         if (forward) {
252             it = it->next;
253             if (it == NULL) it = list;
254         } else {
255             it = it->prev;
256             if (it == NULL) it = g_list_last(list);
257         }
258         ft = client_focus_target(it->data);
259         if (ft == it->data && client_normal(ft) && client_focusable(ft)) {
260             if (focus_cycle_target)
261                 frame_adjust_focus(focus_cycle_target->frame, FALSE);
262             else if (focus_client)
263                 frame_adjust_focus(focus_client->frame, FALSE);
264             focus_cycle_target = ft;
265             frame_adjust_focus(focus_cycle_target->frame, TRUE);
266             return ft;
267         }
268     } while (it != start);
269     return NULL;
270
271 done_cycle:
272     t = NULL;
273     first = NULL;
274     focus_cycle_target = NULL;
275     g_list_free(order);
276     order = NULL;
277     return NULL;
278 }