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"
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
;
38 static ObClient
*focus_find_directional(ObClient
*c
,
40 gboolean dock_windows
,
41 gboolean desktop_windows
);
43 void focus_cycle_startup(gboolean reconfig
)
48 void focus_cycle_shutdown(gboolean reconfig
)
53 void focus_cycle_stop(ObClient
*ifclient
)
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 (ifclient
== focus_cycle_target
||
59 focus_cycle_popup_is_showing(ifclient
)))
61 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,TRUE
);
62 focus_directional_cycle(0, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
66 ObClient
* focus_cycle(gboolean forward
, gboolean all_desktops
,
67 gboolean dock_windows
, gboolean desktop_windows
,
68 gboolean linear
, gboolean interactive
,
69 gboolean showbar
, ObFocusCyclePopupMode mode
,
70 gboolean done
, gboolean cancel
)
72 static GList
*order
= NULL
;
73 GList
*it
, *start
, *list
;
79 focus_cycle_target
= NULL
;
87 if (linear
) list
= client_list
;
88 else list
= focus_order
;
95 if (focus_cycle_target
== NULL
) {
96 focus_cycle_iconic_windows
= TRUE
;
97 focus_cycle_all_desktops
= all_desktops
;
98 focus_cycle_dock_windows
= dock_windows
;
99 focus_cycle_desktop_windows
= desktop_windows
;
100 start
= it
= g_list_find(list
, focus_client
);
102 start
= it
= g_list_find(list
, focus_cycle_target
);
104 if (!start
) /* switched desktops or something? */
105 start
= it
= forward
? g_list_last(list
) : g_list_first(list
);
106 if (!start
) goto done_cycle
;
111 if (it
== NULL
) it
= g_list_first(list
);
114 if (it
== NULL
) it
= g_list_last(list
);
117 if (focus_valid_target(ft
, TRUE
,
118 focus_cycle_iconic_windows
,
119 focus_cycle_all_desktops
,
120 focus_cycle_dock_windows
,
121 focus_cycle_desktop_windows
,
125 if (ft
!= focus_cycle_target
) { /* prevents flicker */
126 focus_cycle_target
= ft
;
127 focus_cycle_draw_indicator(showbar
? ft
: NULL
);
129 /* same arguments as focus_target_valid */
130 focus_cycle_popup_show(ft
,
131 focus_cycle_iconic_windows
,
132 focus_cycle_all_desktops
,
133 focus_cycle_dock_windows
,
134 focus_cycle_desktop_windows
,
136 return focus_cycle_target
;
137 } else if (ft
!= focus_cycle_target
) {
138 focus_cycle_target
= ft
;
143 } while (it
!= start
);
146 if (done
&& !cancel
) ret
= focus_cycle_target
;
148 focus_cycle_target
= NULL
;
153 focus_cycle_draw_indicator(NULL
);
154 focus_cycle_popup_hide();
160 /* this be mostly ripped from fvwm */
161 static ObClient
*focus_find_directional(ObClient
*c
, ObDirection dir
,
162 gboolean dock_windows
,
163 gboolean desktop_windows
)
165 gint my_cx
, my_cy
, his_cx
, his_cy
;
168 gint score
, best_score
;
169 ObClient
*best_client
, *cur
;
175 /* first, find the centre coords of the currently focused window */
176 my_cx
= c
->frame
->area
.x
+ c
->frame
->area
.width
/ 2;
177 my_cy
= c
->frame
->area
.y
+ c
->frame
->area
.height
/ 2;
182 for (it
= g_list_first(client_list
); it
; it
= g_list_next(it
)) {
185 /* the currently selected window isn't interesting */
188 if (!focus_valid_target(it
->data
, TRUE
, FALSE
, FALSE
, dock_windows
,
189 desktop_windows
, FALSE
))
192 /* find the centre coords of this window, from the
193 * currently focused window's point of view */
194 his_cx
= (cur
->frame
->area
.x
- my_cx
)
195 + cur
->frame
->area
.width
/ 2;
196 his_cy
= (cur
->frame
->area
.y
- my_cy
)
197 + cur
->frame
->area
.height
/ 2;
199 if (dir
== OB_DIRECTION_NORTHEAST
|| dir
== OB_DIRECTION_SOUTHEAST
||
200 dir
== OB_DIRECTION_SOUTHWEST
|| dir
== OB_DIRECTION_NORTHWEST
)
203 /* Rotate the diagonals 45 degrees counterclockwise.
204 * To do this, multiply the matrix /+h +h\ with the
205 * vector (x y). \-h +h/
206 * h = sqrt(0.5). We can set h := 1 since absolute
207 * distance doesn't matter here. */
208 tx
= his_cx
+ his_cy
;
209 his_cy
= -his_cx
+ his_cy
;
214 case OB_DIRECTION_NORTH
:
215 case OB_DIRECTION_SOUTH
:
216 case OB_DIRECTION_NORTHEAST
:
217 case OB_DIRECTION_SOUTHWEST
:
218 offset
= (his_cx
< 0) ? -his_cx
: his_cx
;
219 distance
= ((dir
== OB_DIRECTION_NORTH
||
220 dir
== OB_DIRECTION_NORTHEAST
) ?
223 case OB_DIRECTION_EAST
:
224 case OB_DIRECTION_WEST
:
225 case OB_DIRECTION_SOUTHEAST
:
226 case OB_DIRECTION_NORTHWEST
:
227 offset
= (his_cy
< 0) ? -his_cy
: his_cy
;
228 distance
= ((dir
== OB_DIRECTION_WEST
||
229 dir
== OB_DIRECTION_NORTHWEST
) ?
234 /* the target must be in the requested direction */
238 /* Calculate score for this window. The smaller the better. */
239 score
= distance
+ offset
;
241 /* windows more than 45 degrees off the direction are
242 * heavily penalized and will only be chosen if nothing
243 * else within a million pixels */
244 if (offset
> distance
)
247 if (best_score
== -1 || score
< best_score
) {
256 ObClient
* focus_directional_cycle(ObDirection dir
, gboolean dock_windows
,
257 gboolean desktop_windows
,
258 gboolean interactive
,
259 gboolean showbar
, gboolean dialog
,
260 gboolean done
, gboolean cancel
)
262 static ObClient
*first
= NULL
;
264 ObClient
*ret
= NULL
;
267 focus_cycle_target
= NULL
;
269 } else if (done
&& interactive
)
275 if (focus_cycle_target
== NULL
) {
276 focus_cycle_iconic_windows
= FALSE
;
277 focus_cycle_all_desktops
= FALSE
;
278 focus_cycle_dock_windows
= dock_windows
;
279 focus_cycle_desktop_windows
= desktop_windows
;
282 if (!first
) first
= focus_client
;
284 if (focus_cycle_target
)
285 ft
= focus_find_directional(focus_cycle_target
, dir
, dock_windows
,
288 ft
= focus_find_directional(first
, dir
, dock_windows
, desktop_windows
);
292 for (it
= focus_order
; it
; it
= g_list_next(it
))
293 if (focus_valid_target(it
->data
, TRUE
,
294 focus_cycle_iconic_windows
,
295 focus_cycle_all_desktops
,
296 focus_cycle_dock_windows
,
297 focus_cycle_desktop_windows
, FALSE
))
301 if (ft
&& ft
!= focus_cycle_target
) {/* prevents flicker */
302 focus_cycle_target
= ft
;
305 focus_cycle_draw_indicator(showbar
? ft
: NULL
);
307 if (focus_cycle_target
&& dialog
)
308 /* same arguments as focus_target_valid */
309 focus_cycle_popup_single_show(focus_cycle_target
,
310 focus_cycle_iconic_windows
,
311 focus_cycle_all_desktops
,
312 focus_cycle_dock_windows
,
313 focus_cycle_desktop_windows
);
314 return focus_cycle_target
;
317 if (done
&& !cancel
) ret
= focus_cycle_target
;
320 focus_cycle_target
= NULL
;
322 focus_cycle_draw_indicator(NULL
);
323 focus_cycle_popup_single_hide();