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