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"
39 ObClient
*focus_cycle_target
= NULL
;
40 static ObCycleType focus_cycle_type
= OB_CYCLE_NONE
;
41 static gboolean focus_cycle_iconic_windows
;
42 static gboolean focus_cycle_all_desktops
;
43 static gboolean focus_cycle_dock_windows
;
44 static gboolean focus_cycle_desktop_windows
;
46 static ObClient
*focus_find_directional(ObClient
*c
,
48 gboolean dock_windows
,
49 gboolean desktop_windows
);
51 void focus_cycle_startup(gboolean reconfig
)
56 void focus_cycle_shutdown(gboolean reconfig
)
61 void focus_cycle_addremove(ObClient
*c
, gboolean redraw
)
63 if (!focus_cycle_type
)
66 if (focus_cycle_type
== OB_CYCLE_DIRECTIONAL
) {
67 if (c
&& focus_cycle_target
== c
) {
68 focus_directional_cycle(0, TRUE
, TRUE
, TRUE
, TRUE
,
72 else if (c
&& redraw
) {
75 v
= focus_cycle_valid(c
);
76 s
= focus_cycle_popup_is_showing(c
);
80 focus_cycle_popup_refresh(focus_cycle_target
, redraw
);
84 focus_cycle_reorder();
88 void focus_cycle_reorder()
90 if (focus_cycle_type
== OB_CYCLE_NORMAL
) {
91 focus_cycle_target
= focus_cycle_popup_refresh(focus_cycle_target
,
93 focus_cycle_update_indicator(focus_cycle_target
);
94 if (!focus_cycle_target
)
95 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
96 TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
100 ObClient
* focus_cycle(gboolean forward
, gboolean all_desktops
,
101 gboolean dock_windows
, gboolean desktop_windows
,
102 gboolean linear
, gboolean interactive
,
103 gboolean showbar
, gboolean dialog
,
104 gboolean done
, gboolean cancel
)
106 static GList
*order
= NULL
;
107 GList
*it
, *start
, *list
;
109 ObClient
*ret
= NULL
;
113 focus_cycle_target
= NULL
;
121 if (linear
) list
= client_list
;
122 else list
= focus_order
;
129 if (focus_cycle_target
== NULL
) {
130 focus_cycle_iconic_windows
= TRUE
;
131 focus_cycle_all_desktops
= all_desktops
;
132 focus_cycle_dock_windows
= dock_windows
;
133 focus_cycle_desktop_windows
= desktop_windows
;
134 start
= it
= g_list_find(list
, focus_client
);
136 start
= it
= g_list_find(list
, focus_cycle_target
);
138 if (!start
) /* switched desktops or something? */
139 start
= it
= forward
? g_list_last(list
) : g_list_first(list
);
140 if (!start
) goto done_cycle
;
145 if (it
== NULL
) it
= g_list_first(list
);
148 if (it
== NULL
) it
= g_list_last(list
);
151 if (focus_cycle_valid(ft
)) {
153 if (ft
!= focus_cycle_target
) { /* prevents flicker */
154 focus_cycle_target
= ft
;
155 focus_cycle_type
= OB_CYCLE_NORMAL
;
156 focus_cycle_draw_indicator(showbar
? ft
: NULL
);
159 /* same arguments as focus_target_valid */
160 focus_cycle_popup_show(ft
,
161 focus_cycle_iconic_windows
,
162 focus_cycle_all_desktops
,
163 focus_cycle_dock_windows
,
164 focus_cycle_desktop_windows
);
165 return focus_cycle_target
;
166 } else if (ft
!= focus_cycle_target
) {
167 focus_cycle_target
= ft
;
168 focus_cycle_type
= OB_CYCLE_NORMAL
;
173 } while (it
!= start
);
176 if (done
&& !cancel
) ret
= focus_cycle_target
;
178 focus_cycle_target
= NULL
;
179 focus_cycle_type
= OB_CYCLE_NONE
;
184 focus_cycle_draw_indicator(NULL
);
185 focus_cycle_popup_hide();
191 /* this be mostly ripped from fvwm */
192 static ObClient
*focus_find_directional(ObClient
*c
, ObDirection dir
,
193 gboolean dock_windows
,
194 gboolean desktop_windows
)
196 gint my_cx
, my_cy
, his_cx
, his_cy
;
199 gint score
, best_score
;
200 ObClient
*best_client
, *cur
;
206 /* first, find the centre coords of the currently focused window */
207 my_cx
= c
->frame
->area
.x
+ c
->frame
->area
.width
/ 2;
208 my_cy
= c
->frame
->area
.y
+ c
->frame
->area
.height
/ 2;
213 for (it
= g_list_first(client_list
); it
; it
= g_list_next(it
)) {
216 /* the currently selected window isn't interesting */
219 if (!focus_cycle_valid(it
->data
))
222 /* find the centre coords of this window, from the
223 * currently focused window's point of view */
224 his_cx
= (cur
->frame
->area
.x
- my_cx
)
225 + cur
->frame
->area
.width
/ 2;
226 his_cy
= (cur
->frame
->area
.y
- my_cy
)
227 + cur
->frame
->area
.height
/ 2;
229 if (dir
== OB_DIRECTION_NORTHEAST
|| dir
== OB_DIRECTION_SOUTHEAST
||
230 dir
== OB_DIRECTION_SOUTHWEST
|| dir
== OB_DIRECTION_NORTHWEST
)
233 /* Rotate the diagonals 45 degrees counterclockwise.
234 * To do this, multiply the matrix /+h +h\ with the
235 * vector (x y). \-h +h/
236 * h = sqrt(0.5). We can set h := 1 since absolute
237 * distance doesn't matter here. */
238 tx
= his_cx
+ his_cy
;
239 his_cy
= -his_cx
+ his_cy
;
244 case OB_DIRECTION_NORTH
:
245 case OB_DIRECTION_SOUTH
:
246 case OB_DIRECTION_NORTHEAST
:
247 case OB_DIRECTION_SOUTHWEST
:
248 offset
= (his_cx
< 0) ? -his_cx
: his_cx
;
249 distance
= ((dir
== OB_DIRECTION_NORTH
||
250 dir
== OB_DIRECTION_NORTHEAST
) ?
253 case OB_DIRECTION_EAST
:
254 case OB_DIRECTION_WEST
:
255 case OB_DIRECTION_SOUTHEAST
:
256 case OB_DIRECTION_NORTHWEST
:
257 offset
= (his_cy
< 0) ? -his_cy
: his_cy
;
258 distance
= ((dir
== OB_DIRECTION_WEST
||
259 dir
== OB_DIRECTION_NORTHWEST
) ?
264 /* the target must be in the requested direction */
268 /* Calculate score for this window. The smaller the better. */
269 score
= distance
+ offset
;
271 /* windows more than 45 degrees off the direction are
272 * heavily penalized and will only be chosen if nothing
273 * else within a million pixels */
274 if (offset
> distance
)
277 if (best_score
== -1 || score
< best_score
) {
286 ObClient
* focus_directional_cycle(ObDirection dir
, gboolean dock_windows
,
287 gboolean desktop_windows
,
288 gboolean interactive
,
289 gboolean showbar
, gboolean dialog
,
290 gboolean done
, gboolean cancel
)
292 static ObClient
*first
= NULL
;
294 ObClient
*ret
= NULL
;
297 focus_cycle_target
= NULL
;
299 } else if (done
&& interactive
)
305 if (focus_cycle_target
== NULL
) {
306 focus_cycle_iconic_windows
= FALSE
;
307 focus_cycle_all_desktops
= FALSE
;
308 focus_cycle_dock_windows
= dock_windows
;
309 focus_cycle_desktop_windows
= desktop_windows
;
312 if (!first
) first
= focus_client
;
314 if (focus_cycle_target
)
315 ft
= focus_find_directional(focus_cycle_target
, dir
, dock_windows
,
318 ft
= focus_find_directional(first
, dir
, dock_windows
, desktop_windows
);
322 for (it
= focus_order
; it
; it
= g_list_next(it
))
323 if (focus_cycle_valid(it
->data
)) {
329 if (ft
&& ft
!= focus_cycle_target
) {/* prevents flicker */
330 focus_cycle_target
= ft
;
331 focus_cycle_type
= OB_CYCLE_DIRECTIONAL
;
334 focus_cycle_draw_indicator(showbar
? ft
: NULL
);
336 if (focus_cycle_target
&& dialog
)
337 /* same arguments as focus_target_valid */
338 focus_cycle_popup_single_show(focus_cycle_target
,
339 focus_cycle_iconic_windows
,
340 focus_cycle_all_desktops
,
341 focus_cycle_dock_windows
,
342 focus_cycle_desktop_windows
);
343 return focus_cycle_target
;
346 if (done
&& !cancel
) ret
= focus_cycle_target
;
349 focus_cycle_target
= NULL
;
350 focus_cycle_type
= OB_CYCLE_NONE
;
352 focus_cycle_draw_indicator(NULL
);
353 focus_cycle_popup_single_hide();
358 gboolean
focus_cycle_valid(struct _ObClient
*client
)
360 return focus_valid_target(client
, TRUE
,
361 focus_cycle_iconic_windows
,
362 focus_cycle_all_desktops
,
363 focus_cycle_dock_windows
,
364 focus_cycle_desktop_windows
,