]> Dogcows Code - chaz/openbox/blob - openbox/focus.c
cf6e7b514aef8be24bff6196ac49051bb7481851
[chaz/openbox] / openbox / focus.c
1 #include "event.h"
2 #include "openbox.h"
3 #include "client.h"
4 #include "frame.h"
5 #include "screen.h"
6 #include "prop.h"
7 #include "dispatch.h"
8 #include "focus.h"
9 #include "parse.h"
10 #include "engine.h"
11
12 #include <X11/Xlib.h>
13 #include <glib.h>
14
15 Client *focus_client = NULL;
16 GList **focus_order = NULL; /* these lists are created when screen_startup
17 sets the number of desktops */
18
19 Window focus_backup = None;
20 gboolean focus_new = TRUE;
21 gboolean focus_follow = FALSE;
22 static gboolean focus_last = TRUE;
23 static gboolean focus_last_on_desktop = TRUE;
24
25 static gboolean noreorder = 0;
26
27 static void parse_assign(char *name, ParseToken *value)
28 {
29 if (!g_ascii_strcasecmp(name, "focusnew")) {
30 if (value->type != TOKEN_BOOL)
31 yyerror("invalid value");
32 else {
33 focus_new = value->data.bool;
34 }
35 } else if (!g_ascii_strcasecmp(name, "followmouse")) {
36 if (value->type != TOKEN_BOOL)
37 yyerror("invalid value");
38 else {
39 focus_follow = value->data.bool;
40 }
41 } else if (!g_ascii_strcasecmp(name, "focuslast")) {
42 if (value->type != TOKEN_BOOL)
43 yyerror("invalid value");
44 else {
45 focus_last = value->data.bool;
46 }
47 } else if (!g_ascii_strcasecmp(name, "focuslastondesktop")) {
48 if (value->type != TOKEN_BOOL)
49 yyerror("invalid value");
50 else {
51 focus_last_on_desktop = value->data.bool;
52 }
53 } else
54 yyerror("invalid option");
55 parse_free_token(value);
56 }
57
58 void focus_startup()
59 {
60 /* create the window which gets focus when no clients get it. Have to
61 make it override-redirect so we don't try manage it, since it is
62 mapped. */
63 XSetWindowAttributes attrib;
64
65 focus_client = NULL;
66 focus_new = TRUE;
67 focus_follow = FALSE;
68 focus_last = TRUE;
69 focus_last_on_desktop = TRUE;
70
71 attrib.override_redirect = TRUE;
72 focus_backup = XCreateWindow(ob_display, ob_root,
73 -100, -100, 1, 1, 0,
74 CopyFromParent, InputOutput, CopyFromParent,
75 CWOverrideRedirect, &attrib);
76 XMapRaised(ob_display, focus_backup);
77
78 /* start with nothing focused */
79 focus_set_client(NULL);
80
81 parse_reg_section("focus", NULL, parse_assign);
82 }
83
84 void focus_shutdown()
85 {
86 guint i;
87
88 for (i = 0; i < screen_num_desktops; ++i)
89 g_list_free(focus_order[i]);
90 g_free(focus_order);
91 focus_order = NULL;
92
93 XDestroyWindow(ob_display, focus_backup);
94
95 /* reset focus to root */
96 XSetInputFocus(ob_display, PointerRoot, RevertToPointerRoot,
97 event_lasttime);
98 }
99
100 static void push_to_top(Client *client)
101 {
102 guint desktop;
103
104 desktop = client->desktop;
105 if (desktop == DESKTOP_ALL) desktop = screen_desktop;
106 focus_order[desktop] = g_list_remove(focus_order[desktop], client);
107 focus_order[desktop] = g_list_prepend(focus_order[desktop], client);
108 }
109
110 void focus_set_client(Client *client)
111 {
112 Window active;
113 Client *old;
114
115 /* uninstall the old colormap, and install the new one */
116 screen_install_colormap(focus_client, FALSE);
117 screen_install_colormap(client, TRUE);
118
119 if (client == NULL) {
120 /* when nothing will be focused, send focus to the backup target */
121 XSetInputFocus(ob_display, focus_backup, RevertToPointerRoot,
122 event_lasttime);
123 XSync(ob_display, FALSE);
124 }
125
126 old = focus_client;
127 focus_client = client;
128
129 /* move to the top of the list */
130 if (noreorder)
131 --noreorder;
132 else if (client != NULL)
133 push_to_top(client);
134
135 /* set the NET_ACTIVE_WINDOW hint */
136 active = client ? client->window : None;
137 PROP_SET32(ob_root, net_active_window, window, active);
138
139 if (focus_client != NULL)
140 dispatch_client(Event_Client_Focus, focus_client, 0, 0);
141 if (old != NULL)
142 dispatch_client(Event_Client_Unfocus, old, 0, 0);
143 }
144
145 static gboolean focus_under_pointer()
146 {
147 Window w;
148 int i, x, y;
149 guint u;
150 GList *it;
151
152 if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u)) {
153 for (it = stacking_list; it != NULL; it = it->next) {
154 Client *c = it->data;
155 if (c->desktop == screen_desktop &&
156 RECT_CONTAINS(c->frame->area, x, y))
157 break;
158 }
159 if (it != NULL)
160 return client_normal(it->data) && client_focus(it->data);
161 }
162 return FALSE;
163 }
164
165 void focus_fallback(gboolean switching_desks)
166 {
167 GList *it;
168 Client *old = NULL;
169
170 old = focus_client;
171
172 /* unfocus any focused clients.. they can be focused by Pointer events
173 and such, and then when I try focus them, I won't get a FocusIn event
174 at all for them.
175 */
176 focus_set_client(NULL);
177
178 if (switching_desks) {
179 /* don't skip any windows when switching desktops */
180 old = NULL;
181 }
182
183 if (!(switching_desks ? focus_last_on_desktop : focus_last)) {
184 if (focus_follow) focus_under_pointer();
185 return;
186 }
187
188 for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
189 if (it->data != old && client_normal(it->data))
190 if (client_focus(it->data))
191 return;
192
193 /* nothing to focus */
194 focus_set_client(NULL);
195 }
196
197 void focus_cycle(gboolean forward, gboolean linear, gboolean done,
198 gboolean cancel)
199 {
200 static Client *first = NULL;
201 static Client *t = NULL;
202 static GList *order = NULL;
203 GList *it, *start, *list;
204 Client *ft;
205
206 if (cancel) {
207 if (first) client_focus(first);
208 goto done_cycle;
209 } else if (done) {
210 if (focus_client) {
211 push_to_top(focus_client); /* move to top of focus_order */
212 stacking_raise(focus_client);
213 }
214 goto done_cycle;
215 }
216 if (!first) first = focus_client;
217
218 if (linear) list = client_list;
219 else list = focus_order[screen_desktop];
220
221 start = it = g_list_find(list, focus_client);
222 if (!start) /* switched desktops or something? */
223 start = it = forward ? g_list_last(list) : g_list_first(list);
224 if (!start) goto done_cycle;
225
226 do {
227 if (forward) {
228 it = it->next;
229 if (it == NULL) it = list;
230 } else {
231 it = it->prev;
232 if (it == NULL) it = g_list_last(list);
233 }
234 ft = client_focus_target(it->data);
235 if (ft == it->data && focus_client != ft && client_focusable(ft)) {
236 if (client_focus(ft)) {
237 noreorder++; /* avoid reordering the focus_order */
238 break;
239 }
240 }
241 } while (it != start);
242 return;
243
244 done_cycle:
245 t = NULL;
246 first = NULL;
247 g_list_free(order);
248 order = NULL;
249 }
This page took 0.041818 seconds and 3 git commands to generate.