]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
save the focus_client when falling back
[dana/openbox.git] / openbox / focus.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focus.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "event.h"
22 #include "openbox.h"
23 #include "grab.h"
24 #include "client.h"
25 #include "config.h"
26 #include "focus_cycle.h"
27 #include "screen.h"
28 #include "prop.h"
29 #include "keyboard.h"
30 #include "focus.h"
31 #include "stacking.h"
32
33 #include <X11/Xlib.h>
34 #include <glib.h>
35
36 #define FOCUS_INDICATOR_WIDTH 6
37
38 ObClient *focus_client = NULL;
39 GList *focus_order = NULL;
40
41 void focus_startup(gboolean reconfig)
42 {
43     if (reconfig) return;
44
45     /* start with nothing focused */
46     focus_nothing();
47 }
48
49 void focus_shutdown(gboolean reconfig)
50 {
51     if (reconfig) return;
52
53     /* reset focus to root */
54     XSetInputFocus(ob_display, PointerRoot, RevertToNone, CurrentTime);
55 }
56
57 static void push_to_top(ObClient *client)
58 {
59     focus_order = g_list_remove(focus_order, client);
60     focus_order = g_list_prepend(focus_order, client);
61 }
62
63 void focus_set_client(ObClient *client)
64 {
65     Window active;
66
67     ob_debug_type(OB_DEBUG_FOCUS,
68                   "focus_set_client 0x%lx\n", client ? client->window : 0);
69
70     if (focus_client == client)
71         return;
72
73     /* uninstall the old colormap, and install the new one */
74     screen_install_colormap(focus_client, FALSE);
75     screen_install_colormap(client, TRUE);
76
77     /* in the middle of cycling..? kill it. */
78     focus_cycle_stop();
79
80     focus_client = client;
81
82     if (client != NULL) {
83         /* move to the top of the list */
84         push_to_top(client);
85         /* remove hiliting from the window when it gets focused */
86         client_hilite(client, FALSE);
87     }
88
89     /* set the NET_ACTIVE_WINDOW hint, but preserve it on shutdown */
90     if (ob_state() != OB_STATE_EXITING) {
91         active = client ? client->window : None;
92         PROP_SET32(RootWindow(ob_display, ob_screen),
93                    net_active_window, window, active);
94     }
95 }
96
97 static ObClient* focus_fallback_target(gboolean allow_refocus, ObClient *old)
98 {
99     GList *it;
100     ObClient *c;
101
102     ob_debug_type(OB_DEBUG_FOCUS, "trying pointer stuff\n");
103     if (config_focus_follow && !config_focus_last)
104         if ((c = client_under_pointer()) &&
105             (allow_refocus || c != old) &&
106             (client_normal(c) &&
107              client_focus(c)))
108         {
109             ob_debug_type(OB_DEBUG_FOCUS, "found in pointer stuff\n");
110             return c;
111         }
112
113     ob_debug_type(OB_DEBUG_FOCUS, "trying omnipresentness\n");
114     if (allow_refocus && old &&
115         old->desktop == DESKTOP_ALL &&
116         client_normal(old) &&
117         client_focus(old))
118     {
119         ob_debug_type(OB_DEBUG_FOCUS, "found in omnipresentness\n");
120         return old;
121     }
122
123
124     ob_debug_type(OB_DEBUG_FOCUS, "trying the focus order\n");
125     for (it = focus_order; it; it = g_list_next(it)) {
126         c = it->data;
127         /* fallback focus to a window if:
128            1. it is on the current desktop. this ignores omnipresent
129            windows, which are problematic in their own rite.
130            2. it is a normal type window, don't fall back onto a dock or
131            a splashscreen or a desktop window (save the desktop as a
132            backup fallback though)
133         */
134         if (c->desktop == screen_desktop &&
135             client_normal(c) &&
136             (allow_refocus || c != old) &&
137             client_focus(c))
138         {
139             ob_debug_type(OB_DEBUG_FOCUS, "found in focus order\n");
140             return c;
141         }
142     }
143
144     ob_debug_type(OB_DEBUG_FOCUS, "trying a desktop window\n");
145     for (it = focus_order; it; it = g_list_next(it)) {
146         c = it->data;
147         /* fallback focus to a window if:
148            1. it is on the current desktop. this ignores omnipresent
149            windows, which are problematic in their own rite.
150            2. it is a normal type window, don't fall back onto a dock or
151            a splashscreen or a desktop window (save the desktop as a
152            backup fallback though)
153         */
154         if (c->type == OB_CLIENT_TYPE_DESKTOP &&
155             (allow_refocus || c != old) &&
156             client_focus(c))
157         {
158             ob_debug_type(OB_DEBUG_FOCUS, "found a desktop window\n");
159             return c;
160         }
161     }
162
163     return NULL;
164 }
165
166 ObClient* focus_fallback(gboolean allow_refocus)
167 {
168     ObClient *new;
169     ObClient *old = focus_client;
170
171     /* unfocus any focused clients.. they can be focused by Pointer events
172        and such, and then when we try focus them, we won't get a FocusIn
173        event at all for them. */
174     focus_nothing();
175
176     new = focus_fallback_target(allow_refocus, old);
177
178     return new;
179 }
180
181 void focus_nothing()
182 {
183     /* Install our own colormap */
184     if (focus_client != NULL) {
185         screen_install_colormap(focus_client, FALSE);
186         screen_install_colormap(NULL, TRUE);
187     }
188
189     /* nothing is focused, update the colormap and _the root property_ */
190     focus_set_client(NULL);
191
192     /* if there is a grab going on, then we need to cancel it. if we move
193        focus during the grab, applications will get NotifyWhileGrabbed events
194        and ignore them !
195
196        actions should not rely on being able to move focus during an
197        interactive grab.
198     */
199     if (keyboard_interactively_grabbed())
200         keyboard_interactive_cancel();
201
202     /* when nothing will be focused, send focus to the backup target */
203     XSetInputFocus(ob_display, screen_support_win, RevertToPointerRoot,
204                    event_curtime);
205 }
206
207 void focus_order_remove(ObClient *c)
208 {
209     focus_order = g_list_remove(focus_order, c);
210 }
211
212 void focus_order_to_top(ObClient *c)
213 {
214     focus_order = g_list_remove(focus_order, c);
215     if (!c->iconic) {
216         focus_order = g_list_prepend(focus_order, c);
217     } else {
218         GList *it;
219
220         /* insert before first iconic window */
221         for (it = focus_order;
222              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
223         focus_order = g_list_insert_before(focus_order, it, c);
224     }
225 }
226
227 void focus_order_to_bottom(ObClient *c)
228 {
229     focus_order = g_list_remove(focus_order, c);
230     if (c->iconic) {
231         focus_order = g_list_append(focus_order, c);
232     } else {
233         GList *it;
234
235         /* insert before first iconic window */
236         for (it = focus_order;
237              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
238         focus_order = g_list_insert_before(focus_order, it, c);
239     }
240 }
241
242 ObClient *focus_order_find_first(guint desktop)
243 {
244     GList *it;
245     for (it = focus_order; it; it = g_list_next(it)) {
246         ObClient *c = it->data;
247         if (c->desktop == desktop || c->desktop == DESKTOP_ALL)
248             return c;
249     }
250     return NULL;
251 }