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