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