]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus.c
put the actions back so they don't have to be smart.
[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                                        gboolean send_focus)
99 {
100     GList *it;
101     ObClient *c;
102
103     ob_debug_type(OB_DEBUG_FOCUS, "trying pointer stuff\n");
104     if (config_focus_follow && !config_focus_last)
105         if ((c = client_under_pointer()) &&
106             (allow_refocus || c != old) &&
107             client_normal(c) &&
108             /* if we're sending focus then try to */
109             ((send_focus && client_focus(c)) ||
110              /* if not just see if we could try, or it's already focused */
111              (!send_focus && (c == old || client_can_focus(c)))))
112         {
113             ob_debug_type(OB_DEBUG_FOCUS, "found in pointer stuff (%d)\n",
114                           send_focus);
115             return c;
116         }
117
118     ob_debug_type(OB_DEBUG_FOCUS, "trying omnipresentness\n");
119     if (allow_refocus && old &&
120         old->desktop == DESKTOP_ALL &&
121         client_normal(old) &&
122         /* this one is only for when not sending focus, to keep it there */
123         !send_focus)
124     {
125         ob_debug_type(OB_DEBUG_FOCUS, "found in omnipresentness (%d)\n",
126                       send_focus);
127         return old;
128     }
129
130
131     ob_debug_type(OB_DEBUG_FOCUS, "trying the focus order\n");
132     for (it = focus_order; it; it = g_list_next(it)) {
133         c = it->data;
134         /* fallback focus to a window if:
135            1. it is on the current desktop. this ignores omnipresent
136            windows, which are problematic in their own rite.
137            2. it is a normal type window, don't fall back onto a dock or
138            a splashscreen or a desktop window (save the desktop as a
139            backup fallback though)
140         */
141         if (c->desktop == screen_desktop &&
142             client_normal(c) &&
143             (allow_refocus || c != old) &&
144             /* if we're sending focus then try to */
145             ((send_focus && client_focus(c)) ||
146              /* if not just see if we could try, or it's already focused */
147              (!send_focus && (c == old || client_can_focus(c)))))
148         {
149             ob_debug_type(OB_DEBUG_FOCUS, "found in focus order (%d)\n",
150                           send_focus);
151             return c;
152         }
153     }
154
155     ob_debug_type(OB_DEBUG_FOCUS, "trying a desktop window\n");
156     for (it = focus_order; it; it = g_list_next(it)) {
157         c = it->data;
158         /* fallback focus to a window if:
159            1. it is on the current desktop. this ignores omnipresent
160            windows, which are problematic in their own rite.
161            2. it is a normal type window, don't fall back onto a dock or
162            a splashscreen or a desktop window (save the desktop as a
163            backup fallback though)
164         */
165         if (c->type == OB_CLIENT_TYPE_DESKTOP &&
166             (allow_refocus || c != old) &&
167             /* if we're sending focus then try to */
168             ((send_focus && client_focus(c)) ||
169              /* if not just see if we could try, or it's already focused */
170              (!send_focus && (c == old || client_can_focus(c)))))
171         {
172             ob_debug_type(OB_DEBUG_FOCUS, "found a desktop window (%d)\n",
173                           send_focus);
174             return c;
175         }
176     }
177
178     return NULL;
179 }
180
181 ObClient* focus_fallback(gboolean allow_refocus)
182 {
183     ObClient *new;
184     ObClient *old = focus_client;
185
186     new = focus_fallback_target(allow_refocus, old, FALSE);
187     if (new == old) return;
188
189     /* unfocus any focused clients.. they can be focused by Pointer events
190        and such, and then when we try focus them, we won't get a FocusIn
191        event at all for them. */
192     focus_nothing();
193
194     new = focus_fallback_target(allow_refocus, old, TRUE);
195
196     return new;
197 }
198
199 void focus_nothing()
200 {
201     /* Install our own colormap */
202     if (focus_client != NULL) {
203         screen_install_colormap(focus_client, FALSE);
204         screen_install_colormap(NULL, TRUE);
205     }
206
207     /* nothing is focused, update the colormap and _the root property_ */
208     focus_set_client(NULL);
209
210     /* if there is a grab going on, then we need to cancel it. if we move
211        focus during the grab, applications will get NotifyWhileGrabbed events
212        and ignore them !
213
214        actions should not rely on being able to move focus during an
215        interactive grab.
216     */
217     if (keyboard_interactively_grabbed())
218         keyboard_interactive_cancel();
219
220     /* when nothing will be focused, send focus to the backup target */
221     XSetInputFocus(ob_display, screen_support_win, RevertToPointerRoot,
222                    event_curtime);
223 }
224
225 void focus_order_remove(ObClient *c)
226 {
227     focus_order = g_list_remove(focus_order, c);
228 }
229
230 void focus_order_to_top(ObClient *c)
231 {
232     focus_order = g_list_remove(focus_order, c);
233     if (!c->iconic) {
234         focus_order = g_list_prepend(focus_order, c);
235     } else {
236         GList *it;
237
238         /* insert before first iconic window */
239         for (it = focus_order;
240              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
241         focus_order = g_list_insert_before(focus_order, it, c);
242     }
243 }
244
245 void focus_order_to_bottom(ObClient *c)
246 {
247     focus_order = g_list_remove(focus_order, c);
248     if (c->iconic) {
249         focus_order = g_list_append(focus_order, c);
250     } else {
251         GList *it;
252
253         /* insert before first iconic window */
254         for (it = focus_order;
255              it && !((ObClient*)it->data)->iconic; it = g_list_next(it));
256         focus_order = g_list_insert_before(focus_order, it, c);
257     }
258 }
259
260 ObClient *focus_order_find_first(guint desktop)
261 {
262     GList *it;
263     for (it = focus_order; it; it = g_list_next(it)) {
264         ObClient *c = it->data;
265         if (c->desktop == desktop || c->desktop == DESKTOP_ALL)
266             return c;
267     }
268     return NULL;
269 }