move config option loading for the kernel into config.c/h
[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 "engine.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 gboolean noreorder = 0;
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     XMapRaised(ob_display, focus_backup);
40
41     /* start with nothing focused */
42     focus_set_client(NULL);
43 }
44
45 void focus_shutdown()
46 {
47     guint i;
48
49     for (i = 0; i < screen_num_desktops; ++i)
50         g_list_free(focus_order[i]);
51     g_free(focus_order);
52     focus_order = NULL;
53
54     XDestroyWindow(ob_display, focus_backup);
55
56     /* reset focus to root */
57     XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
58                    event_lasttime);
59 }
60
61 static void push_to_top(Client *client)
62 {
63     guint desktop;
64
65     desktop = client->desktop;
66     if (desktop == DESKTOP_ALL) desktop = screen_desktop;
67     focus_order[desktop] = g_list_remove(focus_order[desktop], client);
68     focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
69 }
70
71 void focus_set_client(Client *client)
72 {
73     Window active;
74     Client *old;
75
76     /* uninstall the old colormap, and install the new one */
77     screen_install_colormap(focus_client, FALSE);
78     screen_install_colormap(client, TRUE);
79
80     if (client == NULL) {
81         /* when nothing will be focused, send focus to the backup target */
82         XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
83                        event_lasttime);
84         XSync(ob_display, FALSE);
85     }
86
87     old = focus_client;
88     focus_client = client;
89
90     /* move to the top of the list */
91     if (noreorder)
92         --noreorder;
93     else if (client != NULL)
94         push_to_top(client);
95
96     /* set the NET_ACTIVE_WINDOW hint */
97     active = client ? client->window : None;
98     PROP_SET32(ob_root, net_active_window, window, active);
99
100     if (focus_client != NULL)
101         dispatch_client(Event_Client_Focus, focus_client, 0, 0);
102     if (old != NULL)
103         dispatch_client(Event_Client_Unfocus, old, 0, 0);
104 }
105
106 static gboolean focus_under_pointer()
107 {
108     Window w;
109     int i, x, y;
110     guint u;
111     GList *it;
112
113     if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
114         for (it = stacking_list; it != NULL; it = it->next) {
115             Client *c = it->data;
116             if (c->desktop == screen_desktop &&
117                 RECT_CONTAINS(c->frame->area, x, y))
118                 break;
119         }
120         if (it != NULL)
121             return client_normal(it->data) && client_focus(it->data);
122     }
123     return FALSE;
124 }
125
126 void focus_fallback(FallbackType type)
127 {
128     GList *it;
129     Client *old = NULL;
130
131     old = focus_client;
132
133     /* unfocus any focused clients.. they can be focused by Pointer events
134        and such, and then when I try focus them, I won't get a FocusIn event
135        at all for them.
136     */
137     focus_set_client(NULL);
138
139     if (!(type == Fallback_Desktop ?
140           config_focus_last_on_desktop : config_focus_last)) {
141         if (config_focus_follow) focus_under_pointer();
142         return;
143     }
144
145     if (type == Fallback_Unfocusing && old && old->transient_for) {
146         if (old->transient_for == TRAN_GROUP) {
147             for (it = focus_order[screen_desktop]; it != NULL; it = it->next) {
148                 GSList *sit;
149
150                 for (sit = old->group->members; sit; sit = sit->next)
151                     if (sit->data == it->data && client_focus(sit->data))
152                         return;
153             }
154         } else {
155             if (client_normal(old->transient_for))
156                 if (client_focus(old->transient_for))
157                     return;
158         }
159     }
160
161     for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
162         if (type != Fallback_Unfocusing || it->data != old)
163             if (client_normal(it->data) && client_focus(it->data))
164                 return;
165
166     /* nothing to focus */
167     focus_set_client(NULL);
168 }
169
170 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
171                  gboolean cancel)
172 {
173     static Client *first = NULL;
174     static Client *t = NULL;
175     static GList *order = NULL;
176     GList *it, *start, *list;
177     Client *ft;
178
179     if (cancel) {
180         if (first) client_focus(first);
181         goto done_cycle;
182     } else if (done) {
183         if (focus_client) {
184             push_to_top(focus_client); /* move to top of focus_order */
185             stacking_raise(focus_client);
186         }
187         goto done_cycle;
188     }
189     if (!first) first = focus_client;
190
191     if (linear) list = client_list;
192     else        list = focus_order[screen_desktop];
193
194     start = it = g_list_find(list, focus_client);
195     if (!start) /* switched desktops or something? */
196         start = it = forward ? g_list_last(list) : g_list_first(list);
197     if (!start) goto done_cycle;
198
199     do {
200         if (forward) {
201             it = it->next;
202             if (it == NULL) it = list;
203         } else {
204             it = it->prev;
205             if (it == NULL) it = g_list_last(list);
206         }
207         ft = client_focus_target(it->data);
208         if (ft == it->data && focus_client != ft && client_normal(ft) &&
209             client_focus(ft)) {
210             noreorder++; /* avoid reordering the focus_order */
211             return ft;
212         }
213     } while (it != start);
214     return NULL;
215
216 done_cycle:
217     t = NULL;
218     first = NULL;
219     g_list_free(order);
220     order = NULL;
221     return NULL;
222 }