]> Dogcows Code - chaz/openbox/blob - openbox/focus.c
remove a todo item that seems to be.. done!
[chaz/openbox] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "grab.h"
4 #include "framerender.h"
5 #include "client.h"
6 #include "config.h"
7 #include "frame.h"
8 #include "screen.h"
9 #include "group.h"
10 #include "prop.h"
11 #include "dispatch.h"
12 #include "focus.h"
13 #include "parse.h"
14 #include "stacking.h"
15 #include "popup.h"
16
17 #include <X11/Xlib.h>
18 #include <glib.h>
19
20 Client *focus_client = NULL;
21 GList **focus_order = NULL; /* these lists are created when screen_startup
22 sets the number of desktops */
23
24 Window focus_backup = None;
25
26 static Client *focus_cycle_target = NULL;
27 static Popup *focus_cycle_popup = NULL;
28
29 void focus_startup()
30 {
31 /* create the window which gets focus when no clients get it. Have to
32 make it override-redirect so we don't try manage it, since it is
33 mapped. */
34 XSetWindowAttributes attrib;
35
36 focus_client = NULL;
37 focus_cycle_popup = popup_new(TRUE);
38
39 attrib.override_redirect = TRUE;
40 focus_backup = XCreateWindow(ob_display, ob_root,
41 -100, -100, 1, 1, 0,
42 CopyFromParent, InputOutput, CopyFromParent,
43 CWOverrideRedirect, &attrib);
44 XMapRaised(ob_display, 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 if (WINDOW_IS_CLIENT(it->data)) {
126 Client *c = WINDOW_AS_CLIENT(it->data);
127 if (c->desktop == screen_desktop &&
128 RECT_CONTAINS(c->frame->area, x, y))
129 break;
130 }
131 }
132 if (it != NULL) {
133 g_assert(WINDOW_IS_CLIENT(it->data));
134 return client_normal(it->data) && client_focus(it->data);
135 }
136 }
137 return FALSE;
138 }
139
140 /* finds the first transient that isn't 'skip' and ensure's that client_normal
141 is true for it */
142 static Client *find_transient_recursive(Client *c, Client *top, Client *skip)
143 {
144 GSList *it;
145 Client *ret;
146
147 for (it = c->transients; it; it = it->next) {
148 if (it->data == top) return NULL;
149 ret = find_transient_recursive(it->data, top, skip);
150 if (ret && ret != skip && client_normal(ret)) return ret;
151 if (it->data != skip && client_normal(it->data)) return it->data;
152 }
153 return NULL;
154 }
155
156 static gboolean focus_fallback_transient(Client *top, Client *old)
157 {
158 Client *target = find_transient_recursive(top, top, old);
159 if (!target) {
160 /* make sure client_normal is true always */
161 if (!client_normal(top))
162 return FALSE;
163 target = top; /* no transient, keep the top */
164 }
165 return client_focus(target);
166 }
167
168 void focus_fallback(FallbackType type)
169 {
170 GList *it;
171 Client *old = NULL;
172
173 old = focus_client;
174
175 /* unfocus any focused clients.. they can be focused by Pointer events
176 and such, and then when I try focus them, I won't get a FocusIn event
177 at all for them.
178 */
179 focus_set_client(NULL);
180
181 if (!(type == Fallback_Desktop ?
182 config_focus_last_on_desktop : config_focus_last)) {
183 if (config_focus_follow) focus_under_pointer();
184 return;
185 }
186
187 if (type == Fallback_Unfocusing && old) {
188 /* try for transient relations */
189 if (old->transient_for) {
190 if (old->transient_for == TRAN_GROUP) {
191 for (it = focus_order[screen_desktop]; it; it = it->next) {
192 GSList *sit;
193
194 for (sit = old->group->members; sit; sit = sit->next)
195 if (sit->data == it->data)
196 if (focus_fallback_transient(sit->data, old))
197 return;
198 }
199 } else {
200 if (focus_fallback_transient(old->transient_for, old))
201 return;
202 }
203 }
204
205 /* try for group relations */
206 if (old->group) {
207 GSList *sit;
208
209 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
210 for (sit = old->group->members; sit; sit = sit->next)
211 if (sit->data == it->data)
212 if (sit->data != old && client_normal(sit->data))
213 if (client_focus(sit->data))
214 return;
215 }
216 }
217
218 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
219 if (type != Fallback_Unfocusing || it->data != old)
220 if (client_normal(it->data) &&
221 /* dont fall back to 'anonymous' fullscreen windows. theres no
222 checks for this is in transient/group fallbacks. */
223 !((Client*)it->data)->fullscreen &&
224 client_focus(it->data))
225 return;
226
227 /* nothing to focus */
228 focus_set_client(NULL);
229 }
230
231 static void popup_cycle(Client *c, gboolean show)
232 {
233 if (!show) {
234 popup_hide(focus_cycle_popup);
235 } else {
236 Rect *a;
237
238 a = screen_area(c->desktop);
239 popup_position(focus_cycle_popup, CenterGravity,
240 a->x + a->width / 2, a->y + a->height / 2);
241 /* popup_size(focus_cycle_popup, a->height/2, a->height/16);
242 popup_show(focus_cycle_popup, c->title,
243 client_icon(c, a->height/16, a->height/16));
244 */
245 /* XXX the size and the font extents need to be related on some level
246 */
247 popup_size(focus_cycle_popup, 320, 48);
248
249 /* use the transient's parent's title/icon */
250 while (c->transient_for && c->transient_for != TRAN_GROUP)
251 c = c->transient_for;
252
253 popup_show(focus_cycle_popup, (c->iconic ? c->icon_title : c->title),
254 client_icon(c, 48, 48));
255 }
256 }
257
258 Client *focus_cycle(gboolean forward, gboolean linear, gboolean done,
259 gboolean cancel)
260 {
261 static Client *first = NULL;
262 static Client *t = NULL;
263 static GList *order = NULL;
264 GList *it, *start, *list;
265 Client *ft;
266
267 if (cancel) {
268 if (focus_cycle_target)
269 frame_adjust_focus(focus_cycle_target->frame, FALSE);
270 if (focus_client)
271 frame_adjust_focus(focus_client->frame, TRUE);
272 goto done_cycle;
273 } else if (done) {
274 if (focus_cycle_target)
275 client_activate(focus_cycle_target);
276 goto done_cycle;
277 }
278 if (!first)
279 grab_pointer(TRUE, None);
280
281 if (!first) first = focus_client;
282 if (!focus_cycle_target) focus_cycle_target = focus_client;
283
284 if (linear) list = client_list;
285 else list = focus_order[screen_desktop];
286
287 start = it = g_list_find(list, focus_cycle_target);
288 if (!start) /* switched desktops or something? */
289 start = it = forward ? g_list_last(list) : g_list_first(list);
290 if (!start) goto done_cycle;
291
292 do {
293 if (forward) {
294 it = it->next;
295 if (it == NULL) it = g_list_first(list);
296 } else {
297 it = it->prev;
298 if (it == NULL) it = g_list_last(list);
299 }
300 /*ft = client_focus_target(it->data);*/
301 ft = it->data;
302 if (ft->transients == NULL && /*ft == it->data &&*/client_normal(ft) &&
303 (ft->can_focus || ft->focus_notify) &&
304 (ft->desktop == screen_desktop || ft->desktop == DESKTOP_ALL)) {
305 if (ft != focus_cycle_target) { /* prevents flicker */
306 if (focus_cycle_target)
307 frame_adjust_focus(focus_cycle_target->frame, FALSE);
308 focus_cycle_target = ft;
309 frame_adjust_focus(focus_cycle_target->frame, TRUE);
310 }
311 popup_cycle(ft, config_focus_popup);
312 return ft;
313 }
314 } while (it != start);
315
316 done_cycle:
317 t = NULL;
318 first = NULL;
319 focus_cycle_target = NULL;
320 g_list_free(order);
321 order = NULL;
322
323 popup_cycle(ft, FALSE);
324 grab_pointer(FALSE, None);
325
326 return NULL;
327 }
328
329 void focus_order_add_new(Client *c)
330 {
331 guint d, i;
332
333 if (c->iconic)
334 focus_order_to_top(c);
335 else {
336 d = c->desktop;
337 if (d == DESKTOP_ALL) {
338 for (i = 0; i < screen_num_desktops; ++i) {
339 if (focus_order[i] && ((Client*)focus_order[i]->data)->iconic)
340 focus_order[i] = g_list_insert(focus_order[i], c, 0);
341 else
342 focus_order[i] = g_list_insert(focus_order[i], c, 1);
343 }
344 } else
345 if (focus_order[d] && ((Client*)focus_order[d]->data)->iconic)
346 focus_order[d] = g_list_insert(focus_order[d], c, 0);
347 else
348 focus_order[d] = g_list_insert(focus_order[d], c, 1);
349 }
350 }
351
352 void focus_order_remove(Client *c)
353 {
354 guint d, i;
355
356 d = c->desktop;
357 if (d == DESKTOP_ALL) {
358 for (i = 0; i < screen_num_desktops; ++i)
359 focus_order[i] = g_list_remove(focus_order[i], c);
360 } else
361 focus_order[d] = g_list_remove(focus_order[d], c);
362 }
363
364 static void to_top(Client *c, guint d)
365 {
366 focus_order[d] = g_list_remove(focus_order[d], c);
367 if (!c->iconic) {
368 focus_order[d] = g_list_prepend(focus_order[d], c);
369 } else {
370 GList *it;
371
372 /* insert before first iconic window */
373 for (it = focus_order[d];
374 it && !((Client*)it->data)->iconic; it = it->next);
375 g_list_insert_before(focus_order[d], it, c);
376 }
377 }
378
379 void focus_order_to_top(Client *c)
380 {
381 guint d, i;
382
383 d = c->desktop;
384 if (d == DESKTOP_ALL) {
385 for (i = 0; i < screen_num_desktops; ++i)
386 to_top(c, i);
387 } else
388 to_top(c, d);
389 }
390
391 static void to_bottom(Client *c, guint d)
392 {
393 focus_order[d] = g_list_remove(focus_order[d], c);
394 if (c->iconic) {
395 focus_order[d] = g_list_append(focus_order[d], c);
396 } else {
397 GList *it;
398
399 /* insert before first iconic window */
400 for (it = focus_order[d];
401 it && !((Client*)it->data)->iconic; it = it->next);
402 g_list_insert_before(focus_order[d], it, c);
403 }
404 }
405
406 void focus_order_to_bottom(Client *c)
407 {
408 guint d, i;
409
410 d = c->desktop;
411 if (d == DESKTOP_ALL) {
412 for (i = 0; i < screen_num_desktops; ++i)
413 to_bottom(c, i);
414 } else
415 to_bottom(c, d);
416 }
This page took 0.058101 seconds and 4 git commands to generate.