Merge branch 'backport' into work
[dana/openbox.git] / 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 && ifclient &&
58         (ifclient == focus_cycle_target ||
59          focus_cycle_popup_is_showing(ifclient)))
60     {
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);
63     }
64 }
65
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)
71 {
72     static GList *order = NULL;
73     GList *it, *start, *list;
74     ObClient *ft = NULL;
75     ObClient *ret = NULL;
76
77     if (interactive) {
78         if (cancel) {
79             focus_cycle_target = NULL;
80             goto done_cycle;
81         } else if (done)
82             goto done_cycle;
83
84         if (!focus_order)
85             goto done_cycle;
86
87         if (linear) list = client_list;
88         else        list = focus_order;
89     } else {
90         if (!focus_order)
91             goto done_cycle;
92         list = client_list;
93     }
94
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);
101     } else
102         start = it = g_list_find(list, focus_cycle_target);
103
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;
107
108     do {
109         if (forward) {
110             it = it->next;
111             if (it == NULL) it = g_list_first(list);
112         } else {
113             it = it->prev;
114             if (it == NULL) it = g_list_last(list);
115         }
116         ft = it->data;
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,
122                                FALSE))
123         {
124             if (interactive) {
125                 if (ft != focus_cycle_target) { /* prevents flicker */
126                     focus_cycle_target = ft;
127                     focus_cycle_draw_indicator(showbar ? ft : NULL);
128                 }
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,
135                                        mode);
136                 return focus_cycle_target;
137             } else if (ft != focus_cycle_target) {
138                 focus_cycle_target = ft;
139                 done = TRUE;
140                 break;
141             }
142         }
143     } while (it != start);
144
145 done_cycle:
146     if (done && !cancel) ret = focus_cycle_target;
147
148     focus_cycle_target = NULL;
149     g_list_free(order);
150     order = NULL;
151
152     if (interactive) {
153         focus_cycle_draw_indicator(NULL);
154         focus_cycle_popup_hide();
155     }
156
157     return ret;
158 }
159
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)
164 {
165     gint my_cx, my_cy, his_cx, his_cy;
166     gint offset = 0;
167     gint distance = 0;
168     gint score, best_score;
169     ObClient *best_client, *cur;
170     GList *it;
171
172     if (!client_list)
173         return NULL;
174
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;
178
179     best_score = -1;
180     best_client = c;
181
182     for (it = g_list_first(client_list); it; it = g_list_next(it)) {
183         cur = it->data;
184
185         /* the currently selected window isn't interesting */
186         if (cur == c)
187             continue;
188         if (!focus_valid_target(it->data, TRUE, FALSE, FALSE, dock_windows,
189                                 desktop_windows, FALSE))
190             continue;
191
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;
198
199         if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
200             dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
201         {
202             gint tx;
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;
210             his_cx = tx;
211         }
212
213         switch (dir) {
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) ?
221                         -his_cy : his_cy);
222             break;
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) ?
230                         -his_cx : his_cx);
231             break;
232         }
233
234         /* the target must be in the requested direction */
235         if (distance <= 0)
236             continue;
237
238         /* Calculate score for this window.  The smaller the better. */
239         score = distance + offset;
240
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)
245             score += 1000000;
246
247         if (best_score == -1 || score < best_score) {
248             best_client = cur;
249             best_score = score;
250         }
251     }
252
253     return best_client;
254 }
255
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)
261 {
262     static ObClient *first = NULL;
263     ObClient *ft = NULL;
264     ObClient *ret = NULL;
265
266     if (cancel) {
267         focus_cycle_target = NULL;
268         goto done_cycle;
269     } else if (done && interactive)
270         goto done_cycle;
271
272     if (!focus_order)
273         goto done_cycle;
274
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;
280     }
281
282     if (!first) first = focus_client;
283
284     if (focus_cycle_target)
285         ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
286                                     desktop_windows);
287     else if (first)
288         ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
289     else {
290         GList *it;
291
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))
298                 ft = it->data;
299     }
300
301     if (ft && ft != focus_cycle_target) {/* prevents flicker */
302         focus_cycle_target = ft;
303         if (!interactive)
304             goto done_cycle;
305         focus_cycle_draw_indicator(showbar ? ft : NULL);
306     }
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;
315
316 done_cycle:
317     if (done && !cancel) ret = focus_cycle_target;
318
319     first = NULL;
320     focus_cycle_target = NULL;
321
322     focus_cycle_draw_indicator(NULL);
323     focus_cycle_popup_single_hide();
324
325     return ret;
326 }