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