tsst
[skippy-xd] / focus.c
1 /* Skippy - Seduces Kids Into Perversion
2  *
3  * Copyright (C) 2004 Hyriand <hyriand@thegraveyard.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "skippy.h"
21
22 typedef float (*dist_func)(SkippyWindow *, SkippyWindow *);
23 typedef int (*match_func)(dlist *, SkippyWindow *);
24
25 static void
26 dir_focus(ClientWin *cw, match_func match, dist_func func)
27 {
28         float diff = 0.0;
29         ClientWin *candidate = NULL;
30         dlist *iter, *candidates;
31         
32         candidates = dlist_first(dlist_find_all(cw->mainwin->cod, (dlist_match_func)match, &cw->mini));
33         if(! candidates)
34                 return;
35         
36         for(iter = dlist_first(candidates); iter; iter = iter->next)
37         {
38                 ClientWin *win = (ClientWin *)iter->data;
39                 float distance = func(&cw->mini, &win->mini);
40                 if(! candidate || distance < diff)
41                 {
42                         candidate = win;
43                         diff = distance;
44                 }
45         }
46         
47         XWarpPointer(candidate->mainwin->dpy, None, candidate->mini.window, 0, 0, 0, 0, candidate->mini.width / 2, candidate->mini.height / 2);
48         XSetInputFocus(candidate->mainwin->dpy, candidate->mini.window, RevertToParent, CurrentTime);
49         dlist_free(candidates);
50 }
51
52 #define HALF_H(w) (w->x + (int)w->width / 2)
53 #define HALF_V(w) (w->y + (int)w->height / 2)
54 #define SQR(x) pow(x, 2)
55
56 #define DISTFUNC(name, d_x, d_y) \
57 static float name (SkippyWindow *a, SkippyWindow *b) \
58 { return sqrt(SQR(d_x) + SQR(d_y)); }
59
60 #define QUALFUNC(name, expr) \
61 static int name(dlist *l, SkippyWindow *b) \
62 { SkippyWindow *a = &((ClientWin*)l->data)->mini; return expr; }
63
64 #define FOCUSFUNC(name, qual, dist) \
65 void name(ClientWin *cw) { dir_focus(cw, qual, dist); }
66
67 DISTFUNC(dist_top_bottom, HALF_H(a) - HALF_H(b), a->y - b->y - (int)b->height)
68 DISTFUNC(dist_bottom_top, HALF_H(a) - HALF_H(b), b->y - a->y - (int)a->height)
69 DISTFUNC(dist_left_right, HALF_V(a) - HALF_V(b), a->x - b->x - (int)b->width)
70 DISTFUNC(dist_right_left, HALF_V(a) - HALF_V(b), b->x - a->x - (int)a->width)
71
72 QUALFUNC(win_above, a->y + a->height < b->y)
73 QUALFUNC(win_below, b->y + b->height < a->y)
74 QUALFUNC(win_left, a->x + a->width < b->x)
75 QUALFUNC(win_right, b->x + b->width < a->x)
76
77 FOCUSFUNC(focus_up, win_above, dist_top_bottom)
78 FOCUSFUNC(focus_down, win_below, dist_bottom_top)
79 FOCUSFUNC(focus_left, win_left, dist_left_right)
80 FOCUSFUNC(focus_right, win_right, dist_right_left)