move focus cycling into its own file
[mikachu/openbox.git] / openbox / focus_cycle.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focus.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 "client.h"
22 #include "focus.h"
23 #include "screen.h"
24 #include "openbox.h"
25 #include "frame.h"
26 #include "popup.h"
27 #include "debug.h"
28 #include "group.h"
29 #include "event.h"
30 #include "render/render.h"
31
32 #include <X11/Xlib.h>
33 #include <glib.h>
34
35 #define FOCUS_INDICATOR_WIDTH 6
36
37 struct {
38     InternalWindow top;
39     InternalWindow left;
40     InternalWindow right;
41     InternalWindow bottom;
42 } focus_indicator;
43
44 ObClient     *focus_cycle_target = NULL;
45
46 static RrAppearance *a_focus_indicator;
47 static RrColor      *color_white;
48 static ObIconPopup  *focus_cycle_popup;
49
50 static void      focus_cycle_destroy_notify (ObClient *client, gpointer data);
51 static Window    create_window              (Window parent, gulong mask,
52                                              XSetWindowAttributes *attrib);
53 static gboolean  focus_target_valid         (ObClient *ft,
54                                              gboolean all_desktops,
55                                              gboolean dock_windows,
56                                              gboolean desktop_windows);
57 static gboolean  focus_target_has_siblings  (ObClient *ft,
58                                              gboolean all_desktops);
59 static gchar    *popup_get_name             (ObClient *c,
60                                              ObClient **nametarget);
61 static void      popup_cycle                (ObClient *c,
62                                              gboolean show,
63                                              gboolean all_desktops,
64                                              gboolean dock_windows,
65                                              gboolean desktop_windows);
66 static ObClient *focus_find_directional    (ObClient *c,
67                                             ObDirection dir,
68                                             gboolean dock_windows,
69                                             gboolean desktop_windows);
70 static ObClient *focus_find_directional    (ObClient *c,
71                                             ObDirection dir,
72                                             gboolean dock_windows,
73                                             gboolean desktop_windows);
74
75 void focus_cycle_startup(gboolean reconfig)
76 {
77     focus_cycle_popup = icon_popup_new(TRUE);
78
79     if (!reconfig) {
80         XSetWindowAttributes attr;
81
82         client_add_destroy_notify(focus_cycle_destroy_notify, NULL);
83
84         focus_indicator.top.obwin.type = Window_Internal;
85         focus_indicator.left.obwin.type = Window_Internal;
86         focus_indicator.right.obwin.type = Window_Internal;
87         focus_indicator.bottom.obwin.type = Window_Internal;
88
89         attr.override_redirect = True;
90         attr.background_pixel = BlackPixel(ob_display, ob_screen);
91         focus_indicator.top.win =
92             create_window(RootWindow(ob_display, ob_screen),
93                           CWOverrideRedirect | CWBackPixel, &attr);
94         focus_indicator.left.win =
95             create_window(RootWindow(ob_display, ob_screen),
96                           CWOverrideRedirect | CWBackPixel, &attr);
97         focus_indicator.right.win =
98             create_window(RootWindow(ob_display, ob_screen),
99                           CWOverrideRedirect | CWBackPixel, &attr);
100         focus_indicator.bottom.win =
101             create_window(RootWindow(ob_display, ob_screen),
102                           CWOverrideRedirect | CWBackPixel, &attr);
103
104         stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.top));
105         stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.left));
106         stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.right));
107         stacking_add(INTERNAL_AS_WINDOW(&focus_indicator.bottom));
108
109         color_white = RrColorNew(ob_rr_inst, 0xff, 0xff, 0xff);
110
111         a_focus_indicator = RrAppearanceNew(ob_rr_inst, 4);
112         a_focus_indicator->surface.grad = RR_SURFACE_SOLID;
113         a_focus_indicator->surface.relief = RR_RELIEF_FLAT;
114         a_focus_indicator->surface.primary = RrColorNew(ob_rr_inst,
115                                                         0, 0, 0);
116         a_focus_indicator->texture[0].type = RR_TEXTURE_LINE_ART;
117         a_focus_indicator->texture[0].data.lineart.color = color_white;
118         a_focus_indicator->texture[1].type = RR_TEXTURE_LINE_ART;
119         a_focus_indicator->texture[1].data.lineart.color = color_white;
120         a_focus_indicator->texture[2].type = RR_TEXTURE_LINE_ART;
121         a_focus_indicator->texture[2].data.lineart.color = color_white;
122         a_focus_indicator->texture[3].type = RR_TEXTURE_LINE_ART;
123         a_focus_indicator->texture[3].data.lineart.color = color_white;
124     }
125 }
126
127 void focus_cycle_shutdown(gboolean reconfig)
128 {
129     icon_popup_free(focus_cycle_popup);
130
131     if (!reconfig) {
132         client_remove_destroy_notify(focus_cycle_destroy_notify);
133
134         RrColorFree(color_white);
135
136         RrAppearanceFree(a_focus_indicator);
137
138         XDestroyWindow(ob_display, focus_indicator.top.win);
139         XDestroyWindow(ob_display, focus_indicator.left.win);
140         XDestroyWindow(ob_display, focus_indicator.right.win);
141         XDestroyWindow(ob_display, focus_indicator.bottom.win);
142     }
143 }
144
145 void focus_cycle_stop()
146 {
147     if (focus_cycle_target)
148         focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
149 }
150
151
152 static Window create_window(Window parent, gulong mask,
153                             XSetWindowAttributes *attrib)
154 {
155     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
156                          RrDepth(ob_rr_inst), InputOutput,
157                          RrVisual(ob_rr_inst), mask, attrib);
158                        
159 }
160
161 static gchar *popup_get_name(ObClient *c, ObClient **nametarget)
162 {
163     ObClient *p;
164     gchar *title = NULL;
165     const gchar *desk = NULL;
166     gchar *ret;
167
168     /* find our highest direct parent, including non-normal windows */
169     for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
170          p = p->transient_for);
171
172     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
173         desk = screen_desktop_names[c->desktop];
174
175     /* use the transient's parent's title/icon if we don't have one */
176     if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
177         title = g_strdup(p->iconic ? p->icon_title : p->title);
178
179     if (title == NULL)
180         title = g_strdup(c->iconic ? c->icon_title : c->title);
181
182     if (desk)
183         ret = g_strdup_printf("%s [%s]", title, desk);
184     else {
185         ret = title;
186         title = NULL;
187     }
188     g_free(title);
189
190     /* set this only if we're returning true and they asked for it */
191     if (ret && nametarget) *nametarget = p;
192     return ret;
193 }
194
195 static void popup_cycle(ObClient *c, gboolean show,
196                         gboolean all_desktops, gboolean dock_windows,
197                         gboolean desktop_windows)
198 {
199     gchar *showtext = NULL;
200     ObClient *showtarget;
201
202     if (!show) {
203         icon_popup_hide(focus_cycle_popup);
204         return;
205     }
206
207     /* do this stuff only when the dialog is first showing */
208     if (!focus_cycle_popup->popup->mapped &&
209         !focus_cycle_popup->popup->delay_mapped)
210     {
211         Rect *a;
212         gchar **names;
213         GList *targets = NULL, *it;
214         gint n = 0, i;
215
216         /* position the popup */
217         a = screen_physical_area_monitor(0);
218         icon_popup_position(focus_cycle_popup, CenterGravity,
219                             a->x + a->width / 2, a->y + a->height / 2);
220         icon_popup_height(focus_cycle_popup, POPUP_HEIGHT);
221         icon_popup_min_width(focus_cycle_popup, POPUP_WIDTH);
222         icon_popup_max_width(focus_cycle_popup,
223                              MAX(a->width/3, POPUP_WIDTH));
224
225
226         /* make its width to be the width of all the possible titles */
227
228         /* build a list of all the valid focus targets */
229         for (it = focus_order; it; it = g_list_next(it)) {
230             ObClient *ft = it->data;
231             if (focus_target_valid(ft, all_desktops, dock_windows
232                                    , desktop_windows))
233             {
234                 targets = g_list_prepend(targets, ft);
235                 ++n;
236             }
237         }
238         /* make it null terminated so we can use g_strfreev */
239         names = g_new(char*, n+1);
240         for (it = targets, i = 0; it; it = g_list_next(it), ++i) {
241             ObClient *ft = it->data, *t;
242             names[i] = popup_get_name(ft, &t);
243
244             /* little optimization.. save this text and client, so we dont
245                have to get it again */
246             if (ft == c) {
247                 showtext = g_strdup(names[i]);
248                 showtarget = t;
249             }
250         }
251         names[n] = NULL;
252
253         icon_popup_text_width_to_strings(focus_cycle_popup, names, n);
254         g_strfreev(names);
255     }
256
257
258     if (!showtext) showtext = popup_get_name(c, &showtarget);
259     icon_popup_show(focus_cycle_popup, showtext,
260                     client_icon(showtarget, 48, 48));
261     g_free(showtext);
262 }
263
264 static void focus_cycle_destroy_notify(ObClient *client, gpointer data)
265 {
266     /* end cycling if the target disappears. CurrentTime is fine, time won't
267        be used
268     */
269     if (focus_cycle_target == client)
270         focus_cycle(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
271 }
272
273 void focus_cycle_draw_indicator()
274 {
275     if (!focus_cycle_target) {
276         XUnmapWindow(ob_display, focus_indicator.top.win);
277         XUnmapWindow(ob_display, focus_indicator.left.win);
278         XUnmapWindow(ob_display, focus_indicator.right.win);
279         XUnmapWindow(ob_display, focus_indicator.bottom.win);
280
281         /* kill enter events cause by this unmapping */
282         event_ignore_all_queued_enters();
283     } else {
284         /*
285           if (focus_cycle_target)
286               frame_adjust_focus(focus_cycle_target->frame, FALSE);
287           frame_adjust_focus(focus_cycle_target->frame, TRUE);
288         */
289         gint x, y, w, h;
290         gint wt, wl, wr, wb;
291
292         wt = wl = wr = wb = FOCUS_INDICATOR_WIDTH;
293
294         x = focus_cycle_target->frame->area.x;
295         y = focus_cycle_target->frame->area.y;
296         w = focus_cycle_target->frame->area.width;
297         h = wt;
298
299         XMoveResizeWindow(ob_display, focus_indicator.top.win,
300                           x, y, w, h);
301         a_focus_indicator->texture[0].data.lineart.x1 = 0;
302         a_focus_indicator->texture[0].data.lineart.y1 = h-1;
303         a_focus_indicator->texture[0].data.lineart.x2 = 0;
304         a_focus_indicator->texture[0].data.lineart.y2 = 0;
305         a_focus_indicator->texture[1].data.lineart.x1 = 0;
306         a_focus_indicator->texture[1].data.lineart.y1 = 0;
307         a_focus_indicator->texture[1].data.lineart.x2 = w-1;
308         a_focus_indicator->texture[1].data.lineart.y2 = 0;
309         a_focus_indicator->texture[2].data.lineart.x1 = w-1;
310         a_focus_indicator->texture[2].data.lineart.y1 = 0;
311         a_focus_indicator->texture[2].data.lineart.x2 = w-1;
312         a_focus_indicator->texture[2].data.lineart.y2 = h-1;
313         a_focus_indicator->texture[3].data.lineart.x1 = (wl-1);
314         a_focus_indicator->texture[3].data.lineart.y1 = h-1;
315         a_focus_indicator->texture[3].data.lineart.x2 = w - wr;
316         a_focus_indicator->texture[3].data.lineart.y2 = h-1;
317         RrPaint(a_focus_indicator, focus_indicator.top.win,
318                 w, h);
319
320         x = focus_cycle_target->frame->area.x;
321         y = focus_cycle_target->frame->area.y;
322         w = wl;
323         h = focus_cycle_target->frame->area.height;
324
325         XMoveResizeWindow(ob_display, focus_indicator.left.win,
326                           x, y, w, h);
327         a_focus_indicator->texture[0].data.lineart.x1 = w-1;
328         a_focus_indicator->texture[0].data.lineart.y1 = 0;
329         a_focus_indicator->texture[0].data.lineart.x2 = 0;
330         a_focus_indicator->texture[0].data.lineart.y2 = 0;
331         a_focus_indicator->texture[1].data.lineart.x1 = 0;
332         a_focus_indicator->texture[1].data.lineart.y1 = 0;
333         a_focus_indicator->texture[1].data.lineart.x2 = 0;
334         a_focus_indicator->texture[1].data.lineart.y2 = h-1;
335         a_focus_indicator->texture[2].data.lineart.x1 = 0;
336         a_focus_indicator->texture[2].data.lineart.y1 = h-1;
337         a_focus_indicator->texture[2].data.lineart.x2 = w-1;
338         a_focus_indicator->texture[2].data.lineart.y2 = h-1;
339         a_focus_indicator->texture[3].data.lineart.x1 = w-1;
340         a_focus_indicator->texture[3].data.lineart.y1 = wt-1;
341         a_focus_indicator->texture[3].data.lineart.x2 = w-1;
342         a_focus_indicator->texture[3].data.lineart.y2 = h - wb;
343         RrPaint(a_focus_indicator, focus_indicator.left.win,
344                 w, h);
345
346         x = focus_cycle_target->frame->area.x +
347             focus_cycle_target->frame->area.width - wr;
348         y = focus_cycle_target->frame->area.y;
349         w = wr;
350         h = focus_cycle_target->frame->area.height ;
351
352         XMoveResizeWindow(ob_display, focus_indicator.right.win,
353                           x, y, w, h);
354         a_focus_indicator->texture[0].data.lineart.x1 = 0;
355         a_focus_indicator->texture[0].data.lineart.y1 = 0;
356         a_focus_indicator->texture[0].data.lineart.x2 = w-1;
357         a_focus_indicator->texture[0].data.lineart.y2 = 0;
358         a_focus_indicator->texture[1].data.lineart.x1 = w-1;
359         a_focus_indicator->texture[1].data.lineart.y1 = 0;
360         a_focus_indicator->texture[1].data.lineart.x2 = w-1;
361         a_focus_indicator->texture[1].data.lineart.y2 = h-1;
362         a_focus_indicator->texture[2].data.lineart.x1 = w-1;
363         a_focus_indicator->texture[2].data.lineart.y1 = h-1;
364         a_focus_indicator->texture[2].data.lineart.x2 = 0;
365         a_focus_indicator->texture[2].data.lineart.y2 = h-1;
366         a_focus_indicator->texture[3].data.lineart.x1 = 0;
367         a_focus_indicator->texture[3].data.lineart.y1 = wt-1;
368         a_focus_indicator->texture[3].data.lineart.x2 = 0;
369         a_focus_indicator->texture[3].data.lineart.y2 = h - wb;
370         RrPaint(a_focus_indicator, focus_indicator.right.win,
371                 w, h);
372
373         x = focus_cycle_target->frame->area.x;
374         y = focus_cycle_target->frame->area.y +
375             focus_cycle_target->frame->area.height - wb;
376         w = focus_cycle_target->frame->area.width;
377         h = wb;
378
379         XMoveResizeWindow(ob_display, focus_indicator.bottom.win,
380                           x, y, w, h);
381         a_focus_indicator->texture[0].data.lineart.x1 = 0;
382         a_focus_indicator->texture[0].data.lineart.y1 = 0;
383         a_focus_indicator->texture[0].data.lineart.x2 = 0;
384         a_focus_indicator->texture[0].data.lineart.y2 = h-1;
385         a_focus_indicator->texture[1].data.lineart.x1 = 0;
386         a_focus_indicator->texture[1].data.lineart.y1 = h-1;
387         a_focus_indicator->texture[1].data.lineart.x2 = w-1;
388         a_focus_indicator->texture[1].data.lineart.y2 = h-1;
389         a_focus_indicator->texture[2].data.lineart.x1 = w-1;
390         a_focus_indicator->texture[2].data.lineart.y1 = h-1;
391         a_focus_indicator->texture[2].data.lineart.x2 = w-1;
392         a_focus_indicator->texture[2].data.lineart.y2 = 0;
393         a_focus_indicator->texture[3].data.lineart.x1 = wl-1;
394         a_focus_indicator->texture[3].data.lineart.y1 = 0;
395         a_focus_indicator->texture[3].data.lineart.x2 = w - wr;
396         a_focus_indicator->texture[3].data.lineart.y2 = 0;
397         RrPaint(a_focus_indicator, focus_indicator.bottom.win,
398                 w, h);
399
400         XMapWindow(ob_display, focus_indicator.top.win);
401         XMapWindow(ob_display, focus_indicator.left.win);
402         XMapWindow(ob_display, focus_indicator.right.win);
403         XMapWindow(ob_display, focus_indicator.bottom.win);
404     }
405 }
406
407 /*! Returns if a focus target has valid group siblings that can be cycled
408   to in its place */
409 static gboolean focus_target_has_siblings(ObClient *ft, gboolean all_desktops)
410                                                          
411 {
412     GSList *it;
413
414     if (!ft->group) return FALSE;
415
416     for (it = ft->group->members; it; it = g_slist_next(it)) {
417         ObClient *c = it->data;
418         /* check that it's not a helper window to avoid infinite recursion */
419         if (c != ft && !client_helper(c) &&
420             focus_target_valid(c, all_desktops, FALSE, FALSE))
421         {
422             return TRUE;
423         }
424     }
425     return FALSE;
426 }
427
428 /*! @param allow_helpers This is used for calling itself recursively while
429                          checking helper windows. */
430 static gboolean focus_target_valid(ObClient *ft,
431                                    gboolean all_desktops,
432                                    gboolean dock_windows,
433                                    gboolean desktop_windows)
434 {
435     gboolean ok = FALSE;
436
437     /* it's on this desktop unless you want all desktops.
438
439        do this check first because it will usually filter out the most
440        windows */
441     ok = (all_desktops || ft->desktop == screen_desktop ||
442           ft->desktop == DESKTOP_ALL);
443
444     /* the window can receive focus somehow */
445     ok = ok && (ft->can_focus || ft->focus_notify);
446
447     /* it's the right type of window */
448     if (dock_windows || desktop_windows)
449         ok = ok && ((dock_windows && ft->type == OB_CLIENT_TYPE_DOCK) ||
450                     (desktop_windows && ft->type == OB_CLIENT_TYPE_DESKTOP));
451     else
452         /* normal non-helper windows are valid targets */
453         ok = ok &&
454             ((client_normal(ft) && !client_helper(ft))
455              ||
456              /* helper windows are valid targets it... */
457              (client_helper(ft) &&
458               /* ...a window in its group already has focus ... */
459               ((focus_client && ft->group == focus_client->group) ||
460                /* ... or if there are no other windows in its group 
461                   that can be cycled to instead */
462                !focus_target_has_siblings(ft, all_desktops))));
463
464     /* it's not set to skip the taskbar (unless it is a type that would be
465        expected to set this hint */
466     ok = ok && ((ft->type == OB_CLIENT_TYPE_DOCK ||
467                  ft->type == OB_CLIENT_TYPE_DESKTOP ||
468                  ft->type == OB_CLIENT_TYPE_TOOLBAR ||
469                  ft->type == OB_CLIENT_TYPE_MENU ||
470                  ft->type == OB_CLIENT_TYPE_UTILITY) ||
471                 !ft->skip_taskbar);
472
473     /* it's not going to just send fous off somewhere else (modal window) */
474     ok = ok && ft == client_focus_target(ft);
475
476     return ok;
477 }
478
479 void focus_cycle(gboolean forward, gboolean all_desktops,
480                  gboolean dock_windows, gboolean desktop_windows,
481                  gboolean linear, gboolean interactive,
482                  gboolean dialog, gboolean done, gboolean cancel)
483 {
484     static ObClient *first = NULL;
485     static ObClient *t = NULL;
486     static GList *order = NULL;
487     GList *it, *start, *list;
488     ObClient *ft = NULL;
489
490     if (interactive) {
491         if (cancel) {
492             focus_cycle_target = NULL;
493             goto done_cycle;
494         } else if (done)
495             goto done_cycle;
496
497         if (!focus_order)
498             goto done_cycle;
499
500         if (!first) first = focus_client;
501
502         if (linear) list = client_list;
503         else        list = focus_order;
504     } else {
505         if (!focus_order)
506             goto done_cycle;
507         list = client_list;
508     }
509     if (!focus_cycle_target) focus_cycle_target = focus_client;
510
511     start = it = g_list_find(list, focus_cycle_target);
512     if (!start) /* switched desktops or something? */
513         start = it = forward ? g_list_last(list) : g_list_first(list);
514     if (!start) goto done_cycle;
515
516     do {
517         if (forward) {
518             it = it->next;
519             if (it == NULL) it = g_list_first(list);
520         } else {
521             it = it->prev;
522             if (it == NULL) it = g_list_last(list);
523         }
524         ft = it->data;
525         if (focus_target_valid(ft, all_desktops, dock_windows,
526                                desktop_windows))
527         {
528             if (interactive) {
529                 if (ft != focus_cycle_target) { /* prevents flicker */
530                     focus_cycle_target = ft;
531                     focus_cycle_draw_indicator();
532                 }
533                 /* same arguments as focus_target_valid */
534                 popup_cycle(ft, dialog, all_desktops, dock_windows,
535                             desktop_windows);
536                 return;
537             } else if (ft != focus_cycle_target) {
538                 focus_cycle_target = ft;
539                 done = TRUE;
540                 break;
541             }
542         }
543     } while (it != start);
544
545 done_cycle:
546     if (done && focus_cycle_target)
547         client_activate(focus_cycle_target, FALSE, TRUE);
548
549     t = NULL;
550     first = NULL;
551     focus_cycle_target = NULL;
552     g_list_free(order);
553     order = NULL;
554
555     if (interactive) {
556         focus_cycle_draw_indicator();
557         popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
558     }
559
560     return;
561 }
562
563 /* this be mostly ripped from fvwm */
564 static ObClient *focus_find_directional(ObClient *c, ObDirection dir,
565                                         gboolean dock_windows,
566                                         gboolean desktop_windows) 
567 {
568     gint my_cx, my_cy, his_cx, his_cy;
569     gint offset = 0;
570     gint distance = 0;
571     gint score, best_score;
572     ObClient *best_client, *cur;
573     GList *it;
574
575     if(!client_list)
576         return NULL;
577
578     /* first, find the centre coords of the currently focused window */
579     my_cx = c->frame->area.x + c->frame->area.width / 2;
580     my_cy = c->frame->area.y + c->frame->area.height / 2;
581
582     best_score = -1;
583     best_client = NULL;
584
585     for(it = g_list_first(client_list); it; it = g_list_next(it)) {
586         cur = it->data;
587
588         /* the currently selected window isn't interesting */
589         if(cur == c)
590             continue;
591         if (cur->type == OB_CLIENT_TYPE_DOCK && !dock_windows)
592             continue;
593         if (cur->type == OB_CLIENT_TYPE_DESKTOP && !desktop_windows)
594             continue;
595         if (!client_normal(cur) &&
596             cur->type != OB_CLIENT_TYPE_DOCK &&
597             cur->type != OB_CLIENT_TYPE_DESKTOP)
598             continue;
599         /* using c->desktop instead of screen_desktop doesn't work if the
600          * current window was omnipresent, hope this doesn't have any other
601          * side effects */
602         if(screen_desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
603             continue;
604         if(cur->iconic)
605             continue;
606         if(!(client_focus_target(cur) == cur &&
607              client_can_focus(cur)))
608             continue;
609
610         /* find the centre coords of this window, from the
611          * currently focused window's point of view */
612         his_cx = (cur->frame->area.x - my_cx)
613             + cur->frame->area.width / 2;
614         his_cy = (cur->frame->area.y - my_cy)
615             + cur->frame->area.height / 2;
616
617         if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
618            dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
619             gint tx;
620             /* Rotate the diagonals 45 degrees counterclockwise.
621              * To do this, multiply the matrix /+h +h\ with the
622              * vector (x y).                   \-h +h/
623              * h = sqrt(0.5). We can set h := 1 since absolute
624              * distance doesn't matter here. */
625             tx = his_cx + his_cy;
626             his_cy = -his_cx + his_cy;
627             his_cx = tx;
628         }
629
630         switch(dir) {
631         case OB_DIRECTION_NORTH:
632         case OB_DIRECTION_SOUTH:
633         case OB_DIRECTION_NORTHEAST:
634         case OB_DIRECTION_SOUTHWEST:
635             offset = (his_cx < 0) ? -his_cx : his_cx;
636             distance = ((dir == OB_DIRECTION_NORTH ||
637                          dir == OB_DIRECTION_NORTHEAST) ?
638                         -his_cy : his_cy);
639             break;
640         case OB_DIRECTION_EAST:
641         case OB_DIRECTION_WEST:
642         case OB_DIRECTION_SOUTHEAST:
643         case OB_DIRECTION_NORTHWEST:
644             offset = (his_cy < 0) ? -his_cy : his_cy;
645             distance = ((dir == OB_DIRECTION_WEST ||
646                          dir == OB_DIRECTION_NORTHWEST) ?
647                         -his_cx : his_cx);
648             break;
649         }
650
651         /* the target must be in the requested direction */
652         if(distance <= 0)
653             continue;
654
655         /* Calculate score for this window.  The smaller the better. */
656         score = distance + offset;
657
658         /* windows more than 45 degrees off the direction are
659          * heavily penalized and will only be chosen if nothing
660          * else within a million pixels */
661         if(offset > distance)
662             score += 1000000;
663
664         if(best_score == -1 || score < best_score)
665             best_client = cur,
666                 best_score = score;
667     }
668
669     return best_client;
670 }
671
672 void focus_directional_cycle(ObDirection dir, gboolean dock_windows,
673                              gboolean desktop_windows, gboolean interactive,
674                              gboolean dialog, gboolean done, gboolean cancel)
675 {
676     static ObClient *first = NULL;
677     ObClient *ft = NULL;
678
679     if (!interactive)
680         return;
681
682     if (cancel) {
683         focus_cycle_target = NULL;
684         goto done_cycle;
685     } else if (done)
686         goto done_cycle;
687
688     if (!focus_order)
689         goto done_cycle;
690
691     if (!first) first = focus_client;
692     if (!focus_cycle_target) focus_cycle_target = focus_client;
693
694     if (focus_cycle_target)
695         ft = focus_find_directional(focus_cycle_target, dir, dock_windows,
696                                     desktop_windows);
697     else {
698         GList *it;
699
700         for (it = focus_order; it; it = g_list_next(it))
701             if (focus_target_valid(it->data, FALSE, dock_windows,
702                                    desktop_windows))
703                 ft = it->data;
704     }
705         
706     if (ft) {
707         if (ft != focus_cycle_target) {/* prevents flicker */
708             focus_cycle_target = ft;
709             focus_cycle_draw_indicator();
710         }
711     }
712     if (focus_cycle_target) {
713         /* same arguments as focus_target_valid */
714         popup_cycle(focus_cycle_target, dialog, FALSE, dock_windows,
715                     desktop_windows);
716         if (dialog)
717             return;
718     }
719
720
721 done_cycle:
722     if (done && focus_cycle_target)
723         client_activate(focus_cycle_target, FALSE, TRUE);
724
725     first = NULL;
726     focus_cycle_target = NULL;
727
728     focus_cycle_draw_indicator();
729     popup_cycle(ft, FALSE, FALSE, FALSE, FALSE);
730
731     return;
732 }
733
734 void focus_order_add_new(ObClient *c)
735 {
736     if (c->iconic)
737         focus_order_to_top(c);
738     else {
739         g_assert(!g_list_find(focus_order, c));
740         /* if there are any iconic windows, put this above them in the order,
741            but if there are not, then put it under the currently focused one */
742         if (focus_order && ((ObClient*)focus_order->data)->iconic)
743             focus_order = g_list_insert(focus_order, c, 0);
744         else
745             focus_order = g_list_insert(focus_order, c, 1);
746     }
747 }
748