]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle.c
Merge branch 'master' into chaz
[chaz/openbox] / openbox / focus_cycle.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 focus_cycle.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 "focus_cycle.h"
21 #include "focus_cycle_indicator.h"
22 #include "client.h"
23 #include "frame.h"
24 #include "focus.h"
25 #include "screen.h"
26 #include "openbox.h"
27 #include "debug.h"
28
29 #include <X11/Xlib.h>
30 #include <glib.h>
31
32 typedef enum {
33 OB_CYCLE_NONE = 0,
34 OB_CYCLE_NORMAL,
35 OB_CYCLE_DIRECTIONAL
36 } ObCycleType;
37
38 ObClient *focus_cycle_target = NULL;
39 static ObCycleType focus_cycle_type = OB_CYCLE_NONE;
40 static gboolean focus_cycle_linear;
41 static gboolean focus_cycle_iconic_windows;
42 static gboolean focus_cycle_all_desktops;
43 static gboolean focus_cycle_nonhilite_windows;
44 static gboolean focus_cycle_dock_windows;
45 static gboolean focus_cycle_desktop_windows;
46
47 static ObClient *focus_find_directional(ObClient *c,
48 ObDirection dir,
49 gboolean dock_windows,
50 gboolean desktop_windows);
51
52 void focus_cycle_startup(gboolean reconfig)
53 {
54 if (reconfig) return;
55 }
56
57 void focus_cycle_shutdown(gboolean reconfig)
58 {
59 if (reconfig) return;
60 }
61
62 void focus_cycle_addremove(ObClient *c, gboolean redraw)
63 {
64 if (!focus_cycle_type)
65 return;
66
67 if (focus_cycle_type == OB_CYCLE_DIRECTIONAL) {
68 if (c && focus_cycle_target == c) {
69 focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE,
70 TRUE, TRUE, TRUE);
71 }
72 }
73 else if (c && redraw) {
74 gboolean v, s;
75
76 v = focus_cycle_valid(c);
77 s = focus_cycle_popup_is_showing(c) || c == focus_cycle_target;
78
79 if (v != s)
80 focus_cycle_reorder();
81 }
82 else if (redraw) {
83 focus_cycle_reorder();
84 }
85 }
86
87 void focus_cycle_reorder()
88 {
89 if (focus_cycle_type == OB_CYCLE_NORMAL) {
90 focus_cycle_target = focus_cycle_popup_refresh(focus_cycle_target,
91 TRUE,
92 focus_cycle_linear);
93 focus_cycle_update_indicator(focus_cycle_target);
94 if (!focus_cycle_target)
95 focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
96 TRUE, OB_FOCUS_CYCLE_POPUP_MODE_NONE,
97 TRUE, TRUE);
98 }
99 }
100
101 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
102 gboolean nonhilite_windows,
103 gboolean dock_windows, gboolean desktop_windows,
104 gboolean linear, gboolean showbar,
105 ObFocusCyclePopupMode mode,
106 gboolean done, gboolean cancel)
107 {
108 static GList *order = NULL;
109 GList *it, *start, *list;
110 ObClient *ft = NULL;
111 ObClient *ret = NULL;
112
113 if (cancel) {
114 focus_cycle_target = NULL;
115 goto done_cycle;
116 } else if (done)
117 goto done_cycle;
118
119 if (!focus_order)
120 goto done_cycle;
121
122 if (linear) list = client_list;
123 else list = focus_order;
124
125 if (focus_cycle_target == NULL) {
126 focus_cycle_linear = linear;
127 focus_cycle_iconic_windows = TRUE;
128 focus_cycle_all_desktops = all_desktops;
129 focus_cycle_nonhilite_windows = nonhilite_windows;
130 focus_cycle_dock_windows = dock_windows;
131 focus_cycle_desktop_windows = desktop_windows;
132 start = it = g_list_find(list, focus_client);
133 } else
134 start = it = g_list_find(list, focus_cycle_target);
135
136 if (!start) /* switched desktops or something? */
137 start = it = forward ? g_list_last(list) : g_list_first(list);
138 if (!start) goto done_cycle;
139
140 do {
141 if (forward) {
142 it = it->next;
143 if (it == NULL) it = g_list_first(list);
144 } else {
145 it = it->prev;
146 if (it == NULL) it = g_list_last(list);
147 }
148 ft = it->data;
149 if (focus_cycle_valid(ft)) {
150 if (ft != focus_cycle_target) { /* prevents flicker */
151 focus_cycle_target = ft;
152 focus_cycle_type = OB_CYCLE_NORMAL;
153 focus_cycle_draw_indicator(showbar ? ft : NULL);
154 }
155 /* same arguments as focus_target_valid */
156 focus_cycle_popup_show(ft, mode, focus_cycle_linear);
157 return focus_cycle_target;
158 }
159 } while (it != start);
160
161 done_cycle:
162 if (done && !cancel) ret = focus_cycle_target;
163
164 focus_cycle_target = NULL;
165 focus_cycle_type = OB_CYCLE_NONE;
166 g_list_free(order);
167 order = NULL;
168
169 focus_cycle_draw_indicator(NULL);
170 focus_cycle_popup_hide();
171
172 return ret;
173 }
174
175 /* this be mostly ripped from fvwm */
176 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
177 gboolean dock_windows,
178 gboolean desktop_windows)
179 {
180 gint my_cx, my_cy, his_cx, his_cy;
181 gint offset = 0;
182 gint distance = 0;
183 gint score, best_score;
184 ObClient *best_client, *cur;
185 GList *it;
186
187 if (!client_list)
188 return NULL;
189
190 /* first, find the centre coords of the currently focused window */
191 my_cx = c->frame->area.x + c->frame->area.width / 2;
192 my_cy = c->frame->area.y + c->frame->area.height / 2;
193
194 best_score = -1;
195 best_client = c;
196
197 for (it = g_list_first(client_list); it; it = g_list_next(it)) {
198 cur = it->data;
199
200 /* the currently selected window isn't interesting */
201 if (cur == c)
202 continue;
203 if (!focus_cycle_valid(it->data))
204 continue;
205
206 /* find the centre coords of this window, from the
207 * currently focused window's point of view */
208 his_cx = (cur->frame->area.x - my_cx)
209 + cur->frame->area.width / 2;
210 his_cy = (cur->frame->area.y - my_cy)
211 + cur->frame->area.height / 2;
212
213 if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
214 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
215 {
216 gint tx;
217 /* Rotate the diagonals 45 degrees counterclockwise.
218 * To do this, multiply the matrix /+h +h\ with the
219 * vector (x y). \-h +h/
220 * h = sqrt(0.5). We can set h := 1 since absolute
221 * distance doesn't matter here. */
222 tx = his_cx + his_cy;
223 his_cy = -his_cx + his_cy;
224 his_cx = tx;
225 }
226
227 switch (dir) {
228 case OB_DIRECTION_NORTH:
229 case OB_DIRECTION_SOUTH:
230 case OB_DIRECTION_NORTHEAST:
231 case OB_DIRECTION_SOUTHWEST:
232 offset = (his_cx < 0) ? -his_cx : his_cx;
233 distance = ((dir == OB_DIRECTION_NORTH ||
234 dir == OB_DIRECTION_NORTHEAST) ?
235 -his_cy : his_cy);
236 break;
237 case OB_DIRECTION_EAST:
238 case OB_DIRECTION_WEST:
239 case OB_DIRECTION_SOUTHEAST:
240 case OB_DIRECTION_NORTHWEST:
241 offset = (his_cy < 0) ? -his_cy : his_cy;
242 distance = ((dir == OB_DIRECTION_WEST ||
243 dir == OB_DIRECTION_NORTHWEST) ?
244 -his_cx : his_cx);
245 break;
246 }
247
248 /* the target must be in the requested direction */
249 if (distance <= 0)
250 continue;
251
252 /* Calculate score for this window. The smaller the better. */
253 score = distance + offset;
254
255 /* windows more than 45 degrees off the direction are
256 * heavily penalized and will only be chosen if nothing
257 * else within a million pixels */
258 if (offset > distance)
259 score += 1000000;
260
261 if (best_score == -1 || score < best_score) {
262 best_client = cur;
263 best_score = score;
264 }
265 }
266
267 return best_client;
268 }
269
270 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
271 gboolean desktop_windows,
272 gboolean interactive,
273 gboolean showbar, gboolean dialog,
274 gboolean done, gboolean cancel)
275 {
276 static ObClient *first = NULL;
277 ObClient *ft = NULL;
278 ObClient *ret = NULL;
279
280 if (cancel) {
281 focus_cycle_target = NULL;
282 goto done_cycle;
283 } else if (done && interactive)
284 goto done_cycle;
285
286 if (!focus_order)
287 goto done_cycle;
288
289 if (focus_cycle_target == NULL) {
290 focus_cycle_linear = FALSE;
291 focus_cycle_iconic_windows = FALSE;
292 focus_cycle_all_desktops = FALSE;
293 focus_cycle_nonhilite_windows = TRUE;
294 focus_cycle_dock_windows = dock_windows;
295 focus_cycle_desktop_windows = desktop_windows;
296 }
297
298 if (!first) first = focus_client;
299
300 if (focus_cycle_target)
301 ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
302 desktop_windows);
303 else if (first)
304 ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
305 else {
306 GList *it;
307
308 for (it = focus_order; it; it = g_list_next(it))
309 if (focus_cycle_valid(it->data)) {
310 ft = it->data;
311 break;
312 }
313 }
314
315 if (ft && ft != focus_cycle_target) {/* prevents flicker */
316 focus_cycle_target = ft;
317 focus_cycle_type = OB_CYCLE_DIRECTIONAL;
318 if (!interactive)
319 goto done_cycle;
320 focus_cycle_draw_indicator(showbar ? ft : NULL);
321 }
322 if (focus_cycle_target && dialog)
323 /* same arguments as focus_target_valid */
324 focus_cycle_popup_single_show(focus_cycle_target);
325 return focus_cycle_target;
326
327 done_cycle:
328 if (done && !cancel) ret = focus_cycle_target;
329
330 first = NULL;
331 focus_cycle_target = NULL;
332 focus_cycle_type = OB_CYCLE_NONE;
333
334 focus_cycle_draw_indicator(NULL);
335 focus_cycle_popup_single_hide();
336
337 return ret;
338 }
339
340 gboolean focus_cycle_valid(struct _ObClient *client)
341 {
342 return focus_valid_target(client, screen_desktop, TRUE,
343 focus_cycle_iconic_windows,
344 focus_cycle_all_desktops,
345 focus_cycle_nonhilite_windows,
346 focus_cycle_dock_windows,
347 focus_cycle_desktop_windows,
348 FALSE);
349 }
This page took 0.048495 seconds and 4 git commands to generate.