]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle.c
use the same decision code to focus new windows as for focus cycling or focus fallbac...
[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 "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         focus_valid_target(ifclient,
60                            focus_cycle_iconic_windows,
61                            focus_cycle_all_desktops,
62                            focus_cycle_dock_windows,
63                            focus_cycle_desktop_windows))
64     {
65         focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
66         focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
67     }
68 }
69
70 void 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)
74 {
75     static ObClient *t = NULL;
76     static GList *order = NULL;
77     GList *it, *start, *list;
78     ObClient *ft = 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
99     if (focus_cycle_target == NULL) {
100         focus_cycle_iconic_windows = TRUE;
101         focus_cycle_all_desktops = all_desktops;
102         focus_cycle_dock_windows = dock_windows;
103         focus_cycle_desktop_windows = desktop_windows;
104         start = it = g_list_find(list, focus_client);
105     } else
106         start = it = g_list_find(list, focus_cycle_target);
107
108     if (!start) /* switched desktops or something? */
109         start = it = forward ? g_list_last(list) : g_list_first(list);
110     if (!start) goto done_cycle;
111
112     do {
113         if (forward) {
114             it = it->next;
115             if (it == NULL) it = g_list_first(list);
116         } else {
117             it = it->prev;
118             if (it == NULL) it = g_list_last(list);
119         }
120         ft = it->data;
121         if (focus_valid_target(ft,
122                                focus_cycle_iconic_windows,
123                                focus_cycle_all_desktops,
124                                focus_cycle_dock_windows,
125                                focus_cycle_desktop_windows))
126         {
127             if (interactive) {
128                 if (ft != focus_cycle_target) { /* prevents flicker */
129                     focus_cycle_target = ft;
130                     focus_cycle_draw_indicator(ft);
131                 }
132                 if (dialog)
133                     /* same arguments as focus_target_valid */
134                     focus_cycle_popup_show(ft,
135                                            focus_cycle_iconic_windows,
136                                            focus_cycle_all_desktops,
137                                            focus_cycle_dock_windows,
138                                            focus_cycle_desktop_windows);
139                 return;
140             } else if (ft != focus_cycle_target) {
141                 focus_cycle_target = ft;
142                 done = TRUE;
143                 break;
144             }
145         }
146     } while (it != start);
147
148 done_cycle:
149     if (done && focus_cycle_target)
150         client_activate(focus_cycle_target, FALSE, TRUE);
151
152     t = NULL;
153     focus_cycle_target = NULL;
154     g_list_free(order);
155     order = NULL;
156
157     if (interactive) {
158         focus_cycle_draw_indicator(NULL);
159         focus_cycle_popup_hide();
160     }
161
162     return;
163 }
164
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) 
169 {
170     gint my_cx, my_cy, his_cx, his_cy;
171     gint offset = 0;
172     gint distance = 0;
173     gint score, best_score;
174     ObClient *best_client, *cur;
175     GList *it;
176
177     if (!client_list)
178         return NULL;
179
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;
183
184     best_score = -1;
185     best_client = c;
186
187     for (it = g_list_first(client_list); it; it = g_list_next(it)) {
188         cur = it->data;
189
190         /* the currently selected window isn't interesting */
191         if (cur == c)
192             continue;
193         if (!focus_valid_target(it->data, FALSE, FALSE, dock_windows,
194                                 desktop_windows))
195             continue;
196
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;
203
204         if (dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
205             dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST)
206         {
207             gint tx;
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;
215             his_cx = tx;
216         }
217
218         switch (dir) {
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) ?
226                         -his_cy : his_cy);
227             break;
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) ?
235                         -his_cx : his_cx);
236             break;
237         }
238
239         /* the target must be in the requested direction */
240         if (distance <= 0)
241             continue;
242
243         /* Calculate score for this window.  The smaller the better. */
244         score = distance + offset;
245
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)
250             score += 1000000;
251
252         if (best_score == -1 || score < best_score) {
253             best_client = cur;
254             best_score = score;
255         }
256     }
257
258     return best_client;
259 }
260
261 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
262                              gboolean desktop_windows, gboolean interactive,
263                              gboolean dialog, gboolean done, gboolean cancel)
264 {
265     static ObClient *first = NULL;
266     ObClient *ft = 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,
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(ft);
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;
317
318 done_cycle:
319     if (done && focus_cycle_target)
320         client_activate(focus_cycle_target, FALSE, TRUE);
321
322     first = NULL;
323     focus_cycle_target = NULL;
324
325     focus_cycle_draw_indicator(NULL);
326     focus_cycle_popup_single_hide();
327
328     return;
329 }