Merge branch 'backport' into work
[mikachu/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         focus_valid_target(ifclient, TRUE,
59                            focus_cycle_iconic_windows,
60                            focus_cycle_all_desktops,
61                            focus_cycle_dock_windows,
62                            focus_cycle_desktop_windows))
63     {
64         focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,TRUE);
65         focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
66     }
67 }
68
69 ObClient* focus_cycle(gboolean forward, gboolean all_desktops,
70                       gboolean dock_windows, gboolean desktop_windows,
71                       gboolean linear, gboolean interactive,
72                       gboolean showbar, ObFocusCyclePopupMode mode,
73                       gboolean done, gboolean cancel)
74 {
75     static GList *order = NULL;
76     GList *it, *start, *list;
77     ObClient *ft = NULL;
78     ObClient *ret = NULL;
79
80     if (interactive) {
81         if (cancel) {
82             focus_cycle_target = NULL;
83             goto done_cycle;
84         } else if (done)
85             goto done_cycle;
86
87         if (!focus_order)
88             goto done_cycle;
89
90         if (linear) list = client_list;
91         else        list = focus_order;
92     } else {
93         if (!focus_order)
94             goto done_cycle;
95         list = client_list;
96     }
97
98     if (focus_cycle_target == NULL) {
99         focus_cycle_iconic_windows = TRUE;
100         focus_cycle_all_desktops = all_desktops;
101         focus_cycle_dock_windows = dock_windows;
102         focus_cycle_desktop_windows = desktop_windows;
103         start = it = g_list_find(list, focus_client);
104     } else
105         start = it = g_list_find(list, focus_cycle_target);
106
107     if (!start) /* switched desktops or something? */
108         start = it = forward ? g_list_last(list) : g_list_first(list);
109     if (!start) goto done_cycle;
110
111     do {
112         if (forward) {
113             it = it->next;
114             if (it == NULL) it = g_list_first(list);
115         } else {
116             it = it->prev;
117             if (it == NULL) it = g_list_last(list);
118         }
119         ft = it->data;
120         if (focus_valid_target(ft, TRUE,
121                                focus_cycle_iconic_windows,
122                                focus_cycle_all_desktops,
123                                focus_cycle_dock_windows,
124                                focus_cycle_desktop_windows))
125         {
126             if (interactive) {
127                 if (ft != focus_cycle_target) { /* prevents flicker */
128                     focus_cycle_target = ft;
129                     focus_cycle_draw_indicator(showbar ? ft : NULL);
130                 }
131                 /* same arguments as focus_target_valid */
132                 focus_cycle_popup_show(ft,
133                                        focus_cycle_iconic_windows,
134                                        focus_cycle_all_desktops,
135                                        focus_cycle_dock_windows,
136                                        focus_cycle_desktop_windows,
137                                        mode);
138                 return focus_cycle_target;
139             } else if (ft != focus_cycle_target) {
140                 focus_cycle_target = ft;
141                 done = TRUE;
142                 break;
143             }
144         }
145     } while (it != start);
146
147 done_cycle:
148     if (done && !cancel) ret = focus_cycle_target;
149
150     focus_cycle_target = NULL;
151     g_list_free(order);
152     order = NULL;
153
154     if (interactive) {
155         focus_cycle_draw_indicator(NULL);
156         focus_cycle_popup_hide();
157     }
158
159     return ret;
160 }
161
162 /* this be mostly ripped from fvwm */
163 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
164                                         gboolean dock_windows,
165                                         gboolean desktop_windows)
166 {
167     gint my_cx, my_cy, his_cx, his_cy;
168     gint offset = 0;
169     gint distance = 0;
170     gint score, best_score;
171     ObClient *best_client, *cur;
172     GList *it;
173
174     if (!client_list)
175         return NULL;
176
177     /* first, find the centre coords of the currently focused window */
178     my_cx = c->frame->area.x + c->frame->area.width / 2;
179     my_cy = c->frame->area.y + c->frame->area.height / 2;
180
181     best_score = -1;
182     best_client = c;
183
184     for (it = g_list_first(client_list); it; it = g_list_next(it)) {
185         cur = it->data;
186
187         /* the currently selected window isn't interesting */
188         if (cur == c)
189             continue;
190         if (!focus_valid_target(it->data, TRUE, FALSE, FALSE, dock_windows,
191                                 desktop_windows))
192             continue;
193
194         /* find the centre coords of this window, from the
195          * currently focused window's point of view */
196         his_cx = (cur->frame->area.x - my_cx)
197             + cur->frame->area.width / 2;
198         his_cy = (cur->frame->area.y - my_cy)
199             + cur->frame->area.height / 2;
200
201         if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
202             dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
203         {
204             gint tx;
205             /* Rotate the diagonals 45 degrees counterclockwise.
206              * To do this, multiply the matrix /+h +h\ with the
207              * vector (x y).                   \-h +h/
208              * h = sqrt(0.5). We can set h := 1 since absolute
209              * distance doesn't matter here. */
210             tx = his_cx + his_cy;
211             his_cy = -his_cx + his_cy;
212             his_cx = tx;
213         }
214
215         switch (dir) {
216         case OB_DIRECTION_NORTH:
217         case OB_DIRECTION_SOUTH:
218         case OB_DIRECTION_NORTHEAST:
219         case OB_DIRECTION_SOUTHWEST:
220             offset = (his_cx < 0) ? -his_cx : his_cx;
221             distance = ((dir == OB_DIRECTION_NORTH ||
222                          dir == OB_DIRECTION_NORTHEAST) ?
223                         -his_cy : his_cy);
224             break;
225         case OB_DIRECTION_EAST:
226         case OB_DIRECTION_WEST:
227         case OB_DIRECTION_SOUTHEAST:
228         case OB_DIRECTION_NORTHWEST:
229             offset = (his_cy < 0) ? -his_cy : his_cy;
230             distance = ((dir == OB_DIRECTION_WEST ||
231                          dir == OB_DIRECTION_NORTHWEST) ?
232                         -his_cx : his_cx);
233             break;
234         }
235
236         /* the target must be in the requested direction */
237         if (distance <= 0)
238             continue;
239
240         /* Calculate score for this window.  The smaller the better. */
241         score = distance + offset;
242
243         /* windows more than 45 degrees off the direction are
244          * heavily penalized and will only be chosen if nothing
245          * else within a million pixels */
246         if (offset > distance)
247             score += 1000000;
248
249         if (best_score == -1 || score < best_score) {
250             best_client = cur;
251             best_score = score;
252         }
253     }
254
255     return best_client;
256 }
257
258 ObClient* focus_directional_cycle(ObDirection dir, gboolean dock_windows,
259                                   gboolean desktop_windows,
260                                   gboolean interactive,
261                                   gboolean showbar, gboolean dialog,
262                                   gboolean done, gboolean cancel)
263 {
264     static ObClient *first = NULL;
265     ObClient *ft = NULL;
266     ObClient *ret = NULL;
267
268     if (cancel) {
269         focus_cycle_target = NULL;
270         goto done_cycle;
271     } else if (done && interactive)
272         goto done_cycle;
273
274     if (!focus_order)
275         goto done_cycle;
276
277     if (focus_cycle_target == NULL) {
278         focus_cycle_iconic_windows = FALSE;
279         focus_cycle_all_desktops = FALSE;
280         focus_cycle_dock_windows = dock_windows;
281         focus_cycle_desktop_windows = desktop_windows;
282     }
283
284     if (!first) first = focus_client;
285
286     if (focus_cycle_target)
287         ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
288                                     desktop_windows);
289     else if (first)
290         ft = focus_find_directional(first, dir, dock_windows, desktop_windows);
291     else {
292         GList *it;
293
294         for (it = focus_order; it; it = g_list_next(it))
295             if (focus_valid_target(it->data, TRUE,
296                                    focus_cycle_iconic_windows,
297                                    focus_cycle_all_desktops,
298                                    focus_cycle_dock_windows,
299                                    focus_cycle_desktop_windows))
300                 ft = it->data;
301     }
302
303     if (ft && ft != focus_cycle_target) {/* prevents flicker */
304         focus_cycle_target = ft;
305         if (!interactive)
306             goto done_cycle;
307         focus_cycle_draw_indicator(showbar ? ft : NULL);
308     }
309     if (focus_cycle_target && dialog)
310         /* same arguments as focus_target_valid */
311         focus_cycle_popup_single_show(focus_cycle_target,
312                                       focus_cycle_iconic_windows,
313                                       focus_cycle_all_desktops,
314                                       focus_cycle_dock_windows,
315                                       focus_cycle_desktop_windows);
316     return focus_cycle_target;
317
318 done_cycle:
319     if (done && !cancel) ret = focus_cycle_target;
320
321     first = NULL;
322     focus_cycle_target = NULL;
323
324     focus_cycle_draw_indicator(NULL);
325     focus_cycle_popup_single_hide();
326
327     return ret;
328 }