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