]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle.c
when canceling focus cycling, cancel both normal and directional
[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 #include "group.h"
30
31 #include <X11/Xlib.h>
32 #include <glib.h>
33
34 ObClient     *focus_cycle_target = NULL;
35
36 static void      focus_cycle_destroy_notify (ObClient *client, gpointer data);
37 static gboolean  focus_target_has_siblings  (ObClient *ft,
38                                              gboolean iconic_windows,
39                                              gboolean all_desktops);
40 static ObClient *focus_find_directional    (ObClient *c,
41                                             ObDirection dir,
42                                             gboolean dock_windows,
43                                             gboolean desktop_windows);
44 static ObClient *focus_find_directional    (ObClient *c,
45                                             ObDirection dir,
46                                             gboolean dock_windows,
47                                             gboolean desktop_windows);
48
49 void focus_cycle_startup(gboolean reconfig)
50 {
51     if (reconfig) return;
52
53     client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
54 }
55
56 void focus_cycle_shutdown(gboolean reconfig)
57 {
58     if (reconfig) return;
59
60     client_remove_destroy_notify(focus_cycle_destroy_notify);
61 }
62
63 void focus_cycle_stop()
64 {
65     if (focus_cycle_target) {
66         focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
67         focus_directional_cycle(0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
68     }
69 }
70
71 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
72 {
73     /* end cycling if the target disappears. CurrentTime is fine, time won't
74        be used
75     */
76     if (focus_cycle_target == client)
77         focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
78 }
79
80 /*! Returns if a focus target has valid group siblings that can be cycled
81   to in its place */
82 static gboolean focus_target_has_siblings(ObClient *ft,
83                                           gboolean iconic_windows,
84                                           gboolean all_desktops)
85                                                          
86 {
87     GSList *it;
88
89     if (!ft->group) return FALSE;
90
91     for (it = ft->group->members; it; it = g_slist_next(it)) {
92         ObClient *c = it->data;
93         /* check that it's not a helper window to avoid infinite recursion */
94         if (c != ft && !client_helper(c) &&
95             focus_cycle_target_valid(c, iconic_windows, all_desktops, FALSE,
96                                      FALSE))
97         {
98             return TRUE;
99         }
100     }
101     return FALSE;
102 }
103
104 gboolean focus_cycle_target_valid(ObClient *ft,
105                                   gboolean iconic_windows,
106                                   gboolean all_desktops,
107                                   gboolean dock_windows,
108                                   gboolean desktop_windows)
109 {
110     gboolean ok = FALSE;
111
112     /* it's on this desktop unless you want all desktops.
113
114        do this check first because it will usually filter out the most
115        windows */
116     ok = (all_desktops || ft->desktop == screen_desktop ||
117           ft->desktop == DESKTOP_ALL);
118
119     /* the window can receive focus somehow */
120     ok = ok && (ft->can_focus || ft->focus_notify);
121
122     /* the window is not iconic, or we're allowed to go to iconic ones */
123     ok = ok && (iconic_windows || !ft->iconic);
124
125     /* it's the right type of window */
126     if (dock_windows || desktop_windows)
127         ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
128                     (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
129     /* modal windows are important and can always get focus if they are
130        visible and stuff, so don't change 'ok' based on their type */ 
131     else if (!ft->modal)
132         /* normal non-helper windows are valid targets */
133         ok = ok &&
134             ((client_normal(ft) && !client_helper(ft))
135              ||
136              /* helper windows are valid targets it... */
137              (client_helper(ft) &&
138               /* ...a window in its group already has focus ... */
139               ((focus_client && ft->group == focus_client->group) ||
140                /* ... or if there are no other windows in its group 
141                   that can be cycled to instead */
142                !focus_target_has_siblings(ft, iconic_windows, all_desktops))));
143
144     /* it's not set to skip the taskbar (unless it is a type that would be
145        expected to set this hint, or modal) */
146     ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
147                  ft->type == OB_CLIENT_TYPE_DESKTOP ||
148                  ft->type == OB_CLIENT_TYPE_TOOLBAR ||
149                  ft->type == OB_CLIENT_TYPE_MENU ||
150                  ft->type == OB_CLIENT_TYPE_UTILITY) ||
151                 ft->modal ||
152                 !ft->skip_taskbar);
153
154     /* it's not going to just send fous off somewhere else (modal window) */
155     ok = ok && ft == client_focus_target(ft);
156
157     return ok;
158 }
159
160 void focus_cycle(gboolean forward, gboolean all_desktops,
161                  gboolean dock_windows, gboolean desktop_windows,
162                  gboolean linear, gboolean interactive,
163                  gboolean dialog, gboolean done, gboolean cancel)
164 {
165     static ObClient *first = NULL;
166     static ObClient *t = NULL;
167     static GList *order = NULL;
168     GList *it, *start, *list;
169     ObClient *ft = NULL;
170
171     if (interactive) {
172         if (cancel) {
173             focus_cycle_target = NULL;
174             goto done_cycle;
175         } else if (done)
176             goto done_cycle;
177
178         if (!focus_order)
179             goto done_cycle;
180
181         if (!first) first = focus_client;
182
183         if (linear) list = client_list;
184         else        list = focus_order;
185     } else {
186         if (!focus_order)
187             goto done_cycle;
188         list = client_list;
189     }
190     if (!focus_cycle_target) focus_cycle_target = focus_client;
191
192     start = it = g_list_find(list, focus_cycle_target);
193     if (!start) /* switched desktops or something? */
194         start = it = forward ? g_list_last(list) : g_list_first(list);
195     if (!start) goto done_cycle;
196
197     do {
198         if (forward) {
199             it = it->next;
200             if (it == NULL) it = g_list_first(list);
201         } else {
202             it = it->prev;
203             if (it == NULL) it = g_list_last(list);
204         }
205         ft = it->data;
206         if (focus_cycle_target_valid(ft, TRUE,
207                                      all_desktops, dock_windows,
208                                      desktop_windows))
209         {
210             if (interactive) {
211                 if (ft != focus_cycle_target) { /* prevents flicker */
212                     focus_cycle_target = ft;
213                     focus_cycle_draw_indicator(ft);
214                 }
215                 if (dialog)
216                     /* same arguments as focus_target_valid */
217                     focus_cycle_popup_show(ft, TRUE,
218                                            all_desktops, dock_windows,
219                                            desktop_windows);
220                 return;
221             } else if (ft != focus_cycle_target) {
222                 focus_cycle_target = ft;
223                 done = TRUE;
224                 break;
225             }
226         }
227     } while (it != start);
228
229 done_cycle:
230     if (done && focus_cycle_target)
231         client_activate(focus_cycle_target, FALSE, TRUE);
232
233     t = NULL;
234     first = NULL;
235     focus_cycle_target = NULL;
236     g_list_free(order);
237     order = NULL;
238
239     if (interactive) {
240         focus_cycle_draw_indicator(NULL);
241         focus_cycle_popup_hide();
242     }
243
244     return;
245 }
246
247 /* this be mostly ripped from fvwm */
248 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
249                                         gboolean dock_windows,
250                                         gboolean desktop_windows) 
251 {
252     gint my_cx, my_cy, his_cx, his_cy;
253     gint offset = 0;
254     gint distance = 0;
255     gint score, best_score;
256     ObClient *best_client, *cur;
257     GList *it;
258
259     if(!client_list)
260         return NULL;
261
262     /* first, find the centre coords of the currently focused window */
263     my_cx = c->frame->area.x + c->frame->area.width / 2;
264     my_cy = c->frame->area.y + c->frame->area.height / 2;
265
266     best_score = -1;
267     best_client = NULL;
268
269     for(it = g_list_first(client_list); it; it = g_list_next(it)) {
270         cur = it->data;
271
272         /* the currently selected window isn't interesting */
273         if(cur == c)
274             continue;
275         if (!focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
276                                       desktop_windows))
277             continue;
278
279         /* find the centre coords of this window, from the
280          * currently focused window's point of view */
281         his_cx = (cur->frame->area.x - my_cx)
282             + cur->frame->area.width / 2;
283         his_cy = (cur->frame->area.y - my_cy)
284             + cur->frame->area.height / 2;
285
286         if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
287            dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
288             gint tx;
289             /* Rotate the diagonals 45 degrees counterclockwise.
290              * To do this, multiply the matrix /+h +h\ with the
291              * vector (x y).                   \-h +h/
292              * h = sqrt(0.5). We can set h := 1 since absolute
293              * distance doesn't matter here. */
294             tx = his_cx + his_cy;
295             his_cy = -his_cx + his_cy;
296             his_cx = tx;
297         }
298
299         switch(dir) {
300         case OB_DIRECTION_NORTH:
301         case OB_DIRECTION_SOUTH:
302         case OB_DIRECTION_NORTHEAST:
303         case OB_DIRECTION_SOUTHWEST:
304             offset = (his_cx < 0) ? -his_cx : his_cx;
305             distance = ((dir == OB_DIRECTION_NORTH ||
306                          dir == OB_DIRECTION_NORTHEAST) ?
307                         -his_cy : his_cy);
308             break;
309         case OB_DIRECTION_EAST:
310         case OB_DIRECTION_WEST:
311         case OB_DIRECTION_SOUTHEAST:
312         case OB_DIRECTION_NORTHWEST:
313             offset = (his_cy < 0) ? -his_cy : his_cy;
314             distance = ((dir == OB_DIRECTION_WEST ||
315                          dir == OB_DIRECTION_NORTHWEST) ?
316                         -his_cx : his_cx);
317             break;
318         }
319
320         /* the target must be in the requested direction */
321         if(distance <= 0)
322             continue;
323
324         /* Calculate score for this window.  The smaller the better. */
325         score = distance + offset;
326
327         /* windows more than 45 degrees off the direction are
328          * heavily penalized and will only be chosen if nothing
329          * else within a million pixels */
330         if(offset > distance)
331             score += 1000000;
332
333         if(best_score == -1 || score < best_score)
334             best_client = cur,
335                 best_score = score;
336     }
337
338     return best_client;
339 }
340
341 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
342                              gboolean desktop_windows, gboolean interactive,
343                              gboolean dialog, gboolean done, gboolean cancel)
344 {
345     static ObClient *first = NULL;
346     ObClient *ft = NULL;
347
348     if (!interactive)
349         return;
350
351     if (cancel) {
352         focus_cycle_target = NULL;
353         goto done_cycle;
354     } else if (done)
355         goto done_cycle;
356
357     if (!focus_order)
358         goto done_cycle;
359
360     if (!first) first = focus_client;
361     if (!focus_cycle_target) focus_cycle_target = focus_client;
362
363     if (focus_cycle_target)
364         ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
365                                     desktop_windows);
366     else {
367         GList *it;
368
369         for (it = focus_order; it; it = g_list_next(it))
370             if (focus_cycle_target_valid(it->data, FALSE, FALSE, dock_windows,
371                                          desktop_windows))
372                 ft = it->data;
373     }
374         
375     if (ft) {
376         if (ft != focus_cycle_target) {/* prevents flicker */
377             focus_cycle_target = ft;
378             focus_cycle_draw_indicator(ft);
379         }
380     }
381     if (focus_cycle_target && dialog) {
382         /* same arguments as focus_target_valid */
383         focus_cycle_popup_single_show(focus_cycle_target,
384                                       FALSE, FALSE, dock_windows,
385                                       desktop_windows);
386         return;
387     }
388
389 done_cycle:
390     if (done && focus_cycle_target)
391         client_activate(focus_cycle_target, FALSE, TRUE);
392
393     first = NULL;
394     focus_cycle_target = NULL;
395
396     focus_cycle_draw_indicator(NULL);
397     focus_cycle_popup_single_hide();
398
399     return;
400 }