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