1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus_cycle.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "focus_cycle.h"
21 #include "focus_cycle_indicator.h"
22 #include "focus_cycle_popup.h"
33 ObClient
*focus_cycle_target
= NULL
;
34 static gboolean focus_cycle_iconic_windows
;
35 static gboolean focus_cycle_all_desktops
;
36 static gboolean focus_cycle_dock_windows
;
37 static gboolean focus_cycle_desktop_windows
;
39 static ObClient
*focus_find_directional(ObClient
*c
,
41 gboolean dock_windows
,
42 gboolean desktop_windows
);
44 void focus_cycle_startup(gboolean reconfig
)
49 void focus_cycle_shutdown(gboolean reconfig
)
54 void focus_cycle_stop(ObClient
*ifclient
)
56 /* stop focus cycling if the given client is a valid focus target,
57 and so the cycling is being disrupted */
58 if (focus_cycle_target
&& ifclient
&&
59 /* shortcut check, it is what we are pointing at right now */
60 (ifclient
== focus_cycle_target
||
61 /* it's shown but it shouldn't be anymore */
62 focus_cycle_popup_is_showing(ifclient
) ||
63 /* it's not shown but it should be */
64 focus_valid_target(ifclient
, TRUE
,
65 focus_cycle_iconic_windows
,
66 focus_cycle_all_desktops
,
67 focus_cycle_dock_windows
,
68 focus_cycle_desktop_windows
,
71 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,TRUE
);
72 focus_directional_cycle(0, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
76 ObClient
* focus_cycle(gboolean forward
, gboolean all_desktops
,
77 gboolean dock_windows
, gboolean desktop_windows
,
78 gboolean linear
, gboolean interactive
,
79 gboolean showbar
, gboolean dialog
,
80 gboolean done
, gboolean cancel
)
82 static GList
*order
= NULL
;
83 GList
*it
, *start
, *list
;
89 focus_cycle_target
= NULL
;
97 if (linear
) list
= client_list
;
98 else list
= focus_order
;
105 if (focus_cycle_target
== NULL
) {
106 focus_cycle_iconic_windows
= TRUE
;
107 focus_cycle_all_desktops
= all_desktops
;
108 focus_cycle_dock_windows
= dock_windows
;
109 focus_cycle_desktop_windows
= desktop_windows
;
110 start
= it
= g_list_find(list
, focus_client
);
112 start
= it
= g_list_find(list
, focus_cycle_target
);
114 if (!start
) /* switched desktops or something? */
115 start
= it
= forward
? g_list_last(list
) : g_list_first(list
);
116 if (!start
) goto done_cycle
;
121 if (it
== NULL
) it
= g_list_first(list
);
124 if (it
== NULL
) it
= g_list_last(list
);
127 if (focus_valid_target(ft
, TRUE
,
128 focus_cycle_iconic_windows
,
129 focus_cycle_all_desktops
,
130 focus_cycle_dock_windows
,
131 focus_cycle_desktop_windows
,
135 if (ft
!= focus_cycle_target
) { /* prevents flicker */
136 focus_cycle_target
= ft
;
137 focus_cycle_draw_indicator(showbar
? ft
: NULL
);
140 /* same arguments as focus_target_valid */
141 focus_cycle_popup_show(ft
,
142 focus_cycle_iconic_windows
,
143 focus_cycle_all_desktops
,
144 focus_cycle_dock_windows
,
145 focus_cycle_desktop_windows
);
146 return focus_cycle_target
;
147 } else if (ft
!= focus_cycle_target
) {
148 focus_cycle_target
= ft
;
153 } while (it
!= start
);
156 if (done
&& !cancel
) ret
= focus_cycle_target
;
158 focus_cycle_target
= NULL
;
163 focus_cycle_draw_indicator(NULL
);
164 focus_cycle_popup_hide();
170 /* this be mostly ripped from fvwm */
171 static ObClient
*focus_find_directional(ObClient
*c
, ObDirection dir
,
172 gboolean dock_windows
,
173 gboolean desktop_windows
)
175 gint my_cx
, my_cy
, his_cx
, his_cy
;
178 gint score
, best_score
;
179 ObClient
*best_client
, *cur
;
185 /* first, find the centre coords of the currently focused window */
186 my_cx
= c
->frame
->area
.x
+ c
->frame
->area
.width
/ 2;
187 my_cy
= c
->frame
->area
.y
+ c
->frame
->area
.height
/ 2;
192 for (it
= g_list_first(client_list
); it
; it
= g_list_next(it
)) {
195 /* the currently selected window isn't interesting */
198 if (!focus_valid_target(it
->data
, TRUE
, FALSE
, FALSE
, dock_windows
,
199 desktop_windows
, FALSE
))
202 /* find the centre coords of this window, from the
203 * currently focused window's point of view */
204 his_cx
= (cur
->frame
->area
.x
- my_cx
)
205 + cur
->frame
->area
.width
/ 2;
206 his_cy
= (cur
->frame
->area
.y
- my_cy
)
207 + cur
->frame
->area
.height
/ 2;
209 if (dir
== OB_DIRECTION_NORTHEAST
|| dir
== OB_DIRECTION_SOUTHEAST
||
210 dir
== OB_DIRECTION_SOUTHWEST
|| dir
== OB_DIRECTION_NORTHWEST
)
213 /* Rotate the diagonals 45 degrees counterclockwise.
214 * To do this, multiply the matrix /+h +h\ with the
215 * vector (x y). \-h +h/
216 * h = sqrt(0.5). We can set h := 1 since absolute
217 * distance doesn't matter here. */
218 tx
= his_cx
+ his_cy
;
219 his_cy
= -his_cx
+ his_cy
;
224 case OB_DIRECTION_NORTH
:
225 case OB_DIRECTION_SOUTH
:
226 case OB_DIRECTION_NORTHEAST
:
227 case OB_DIRECTION_SOUTHWEST
:
228 offset
= (his_cx
< 0) ? -his_cx
: his_cx
;
229 distance
= ((dir
== OB_DIRECTION_NORTH
||
230 dir
== OB_DIRECTION_NORTHEAST
) ?
233 case OB_DIRECTION_EAST
:
234 case OB_DIRECTION_WEST
:
235 case OB_DIRECTION_SOUTHEAST
:
236 case OB_DIRECTION_NORTHWEST
:
237 offset
= (his_cy
< 0) ? -his_cy
: his_cy
;
238 distance
= ((dir
== OB_DIRECTION_WEST
||
239 dir
== OB_DIRECTION_NORTHWEST
) ?
244 /* the target must be in the requested direction */
248 /* Calculate score for this window. The smaller the better. */
249 score
= distance
+ offset
;
251 /* windows more than 45 degrees off the direction are
252 * heavily penalized and will only be chosen if nothing
253 * else within a million pixels */
254 if (offset
> distance
)
257 if (best_score
== -1 || score
< best_score
) {
266 ObClient
* focus_directional_cycle(ObDirection dir
, gboolean dock_windows
,
267 gboolean desktop_windows
,
268 gboolean interactive
,
269 gboolean showbar
, gboolean dialog
,
270 gboolean done
, gboolean cancel
)
272 static ObClient
*first
= NULL
;
274 ObClient
*ret
= NULL
;
277 focus_cycle_target
= NULL
;
279 } else if (done
&& interactive
)
285 if (focus_cycle_target
== NULL
) {
286 focus_cycle_iconic_windows
= FALSE
;
287 focus_cycle_all_desktops
= FALSE
;
288 focus_cycle_dock_windows
= dock_windows
;
289 focus_cycle_desktop_windows
= desktop_windows
;
292 if (!first
) first
= focus_client
;
294 if (focus_cycle_target
)
295 ft
= focus_find_directional(focus_cycle_target
, dir
, dock_windows
,
298 ft
= focus_find_directional(first
, dir
, dock_windows
, desktop_windows
);
302 for (it
= focus_order
; it
; it
= g_list_next(it
))
303 if (focus_valid_target(it
->data
, TRUE
,
304 focus_cycle_iconic_windows
,
305 focus_cycle_all_desktops
,
306 focus_cycle_dock_windows
,
307 focus_cycle_desktop_windows
, FALSE
))
311 if (ft
&& ft
!= focus_cycle_target
) {/* prevents flicker */
312 focus_cycle_target
= ft
;
315 focus_cycle_draw_indicator(showbar
? ft
: NULL
);
317 if (focus_cycle_target
&& dialog
)
318 /* same arguments as focus_target_valid */
319 focus_cycle_popup_single_show(focus_cycle_target
,
320 focus_cycle_iconic_windows
,
321 focus_cycle_all_desktops
,
322 focus_cycle_dock_windows
,
323 focus_cycle_desktop_windows
);
324 return focus_cycle_target
;
327 if (done
&& !cancel
) ret
= focus_cycle_target
;
330 focus_cycle_target
= NULL
;
332 focus_cycle_draw_indicator(NULL
);
333 focus_cycle_popup_single_hide();