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