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 focus_valid_target(ifclient
,
60 focus_cycle_iconic_windows
,
61 focus_cycle_all_desktops
,
62 focus_cycle_dock_windows
,
63 focus_cycle_desktop_windows
))
65 focus_cycle(TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
66 focus_directional_cycle(0, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
);
70 ObClient
* focus_cycle(gboolean forward
, gboolean all_desktops
,
71 gboolean dock_windows
, gboolean desktop_windows
,
72 gboolean linear
, gboolean interactive
,
73 gboolean dialog
, gboolean done
, gboolean cancel
)
75 static ObClient
*t
= NULL
;
76 static GList
*order
= NULL
;
77 GList
*it
, *start
, *list
;
83 focus_cycle_target
= NULL
;
91 if (linear
) list
= client_list
;
92 else list
= focus_order
;
100 if (focus_cycle_target
== NULL
) {
101 focus_cycle_iconic_windows
= TRUE
;
102 focus_cycle_all_desktops
= all_desktops
;
103 focus_cycle_dock_windows
= dock_windows
;
104 focus_cycle_desktop_windows
= desktop_windows
;
105 start
= it
= g_list_find(list
, focus_client
);
107 start
= it
= g_list_find(list
, focus_cycle_target
);
109 if (!start
) /* switched desktops or something? */
110 start
= it
= forward
? g_list_last(list
) : g_list_first(list
);
111 if (!start
) goto done_cycle
;
116 if (it
== NULL
) it
= g_list_first(list
);
119 if (it
== NULL
) it
= g_list_last(list
);
122 if (focus_valid_target(ft
,
123 focus_cycle_iconic_windows
,
124 focus_cycle_all_desktops
,
125 focus_cycle_dock_windows
,
126 focus_cycle_desktop_windows
))
129 if (ft
!= focus_cycle_target
) { /* prevents flicker */
130 focus_cycle_target
= ft
;
131 focus_cycle_draw_indicator(ft
);
134 /* same arguments as focus_target_valid */
135 focus_cycle_popup_show(ft
,
136 focus_cycle_iconic_windows
,
137 focus_cycle_all_desktops
,
138 focus_cycle_dock_windows
,
139 focus_cycle_desktop_windows
);
141 } else if (ft
!= focus_cycle_target
) {
142 focus_cycle_target
= ft
;
147 } while (it
!= start
);
150 if (done
&& !cancel
) ret
= focus_cycle_target
;
153 focus_cycle_target
= NULL
;
158 focus_cycle_draw_indicator(NULL
);
159 focus_cycle_popup_hide();
165 /* this be mostly ripped from fvwm */
166 static ObClient
*focus_find_directional(ObClient
*c
, ObDirection dir
,
167 gboolean dock_windows
,
168 gboolean desktop_windows
)
170 gint my_cx
, my_cy
, his_cx
, his_cy
;
173 gint score
, best_score
;
174 ObClient
*best_client
, *cur
;
180 /* first, find the centre coords of the currently focused window */
181 my_cx
= c
->frame
->area
.x
+ c
->frame
->area
.width
/ 2;
182 my_cy
= c
->frame
->area
.y
+ c
->frame
->area
.height
/ 2;
187 for (it
= g_list_first(client_list
); it
; it
= g_list_next(it
)) {
190 /* the currently selected window isn't interesting */
193 if (!focus_valid_target(it
->data
, FALSE
, FALSE
, dock_windows
,
197 /* find the centre coords of this window, from the
198 * currently focused window's point of view */
199 his_cx
= (cur
->frame
->area
.x
- my_cx
)
200 + cur
->frame
->area
.width
/ 2;
201 his_cy
= (cur
->frame
->area
.y
- my_cy
)
202 + cur
->frame
->area
.height
/ 2;
204 if (dir
== OB_DIRECTION_NORTHEAST
|| dir
== OB_DIRECTION_SOUTHEAST
||
205 dir
== OB_DIRECTION_SOUTHWEST
|| dir
== OB_DIRECTION_NORTHWEST
)
208 /* Rotate the diagonals 45 degrees counterclockwise.
209 * To do this, multiply the matrix /+h +h\ with the
210 * vector (x y). \-h +h/
211 * h = sqrt(0.5). We can set h := 1 since absolute
212 * distance doesn't matter here. */
213 tx
= his_cx
+ his_cy
;
214 his_cy
= -his_cx
+ his_cy
;
219 case OB_DIRECTION_NORTH
:
220 case OB_DIRECTION_SOUTH
:
221 case OB_DIRECTION_NORTHEAST
:
222 case OB_DIRECTION_SOUTHWEST
:
223 offset
= (his_cx
< 0) ? -his_cx
: his_cx
;
224 distance
= ((dir
== OB_DIRECTION_NORTH
||
225 dir
== OB_DIRECTION_NORTHEAST
) ?
228 case OB_DIRECTION_EAST
:
229 case OB_DIRECTION_WEST
:
230 case OB_DIRECTION_SOUTHEAST
:
231 case OB_DIRECTION_NORTHWEST
:
232 offset
= (his_cy
< 0) ? -his_cy
: his_cy
;
233 distance
= ((dir
== OB_DIRECTION_WEST
||
234 dir
== OB_DIRECTION_NORTHWEST
) ?
239 /* the target must be in the requested direction */
243 /* Calculate score for this window. The smaller the better. */
244 score
= distance
+ offset
;
246 /* windows more than 45 degrees off the direction are
247 * heavily penalized and will only be chosen if nothing
248 * else within a million pixels */
249 if (offset
> distance
)
252 if (best_score
== -1 || score
< best_score
) {
261 ObClient
* focus_directional_cycle(ObDirection dir
, gboolean dock_windows
,
262 gboolean desktop_windows
,
263 gboolean interactive
,
265 gboolean done
, gboolean cancel
)
267 static ObClient
*first
= NULL
;
269 ObClient
*ret
= NULL
;
272 focus_cycle_target
= NULL
;
274 } else if (done
&& interactive
)
280 if (focus_cycle_target
== NULL
) {
281 focus_cycle_iconic_windows
= FALSE
;
282 focus_cycle_all_desktops
= FALSE
;
283 focus_cycle_dock_windows
= dock_windows
;
284 focus_cycle_desktop_windows
= desktop_windows
;
287 if (!first
) first
= focus_client
;
289 if (focus_cycle_target
)
290 ft
= focus_find_directional(focus_cycle_target
, dir
, dock_windows
,
293 ft
= focus_find_directional(first
, dir
, dock_windows
, desktop_windows
);
297 for (it
= focus_order
; it
; it
= g_list_next(it
))
298 if (focus_valid_target(it
->data
,
299 focus_cycle_iconic_windows
,
300 focus_cycle_all_desktops
,
301 focus_cycle_dock_windows
,
302 focus_cycle_desktop_windows
))
306 if (ft
&& ft
!= focus_cycle_target
) {/* prevents flicker */
307 focus_cycle_target
= ft
;
310 focus_cycle_draw_indicator(ft
);
312 if (focus_cycle_target
&& dialog
)
313 /* same arguments as focus_target_valid */
314 focus_cycle_popup_single_show(focus_cycle_target
,
315 focus_cycle_iconic_windows
,
316 focus_cycle_all_desktops
,
317 focus_cycle_dock_windows
,
318 focus_cycle_desktop_windows
);
322 if (done
&& !cancel
) ret
= focus_cycle_target
;
325 focus_cycle_target
= NULL
;
327 focus_cycle_draw_indicator(NULL
);
328 focus_cycle_popup_single_hide();