]> icculus.org git repositories - dana/openbox.git/blob - plugins/resistance.c
make history placement optional
[dana/openbox.git] / plugins / resistance.c
1 #include "../kernel/dispatch.h"
2 #include "../kernel/client.h"
3 #include "../kernel/frame.h"
4 #include "../kernel/stacking.h"
5 #include "../kernel/screen.h"
6 #include <glib.h>
7
8 void plugin_setup_config()
9 {
10 }
11
12 static int resistance = 10;
13 static gboolean window_resistance = TRUE; /* window-to-window */
14
15 static void resist(Client *c, int *x, int *y)
16 {
17     GList *it;
18     Rect *area;
19     int l, t, r, b; /* requested edges */
20     int al, at, ar, ab; /* screen area edges */
21     int cl, ct, cr, cb; /* current edges */
22     int w, h; /* current size */
23     gboolean snapx = FALSE, snapy = FALSE;
24
25     w = c->frame->area.width;
26     h = c->frame->area.height;
27
28     l = *x;
29     t = *y;
30     r = l + w - 1;
31     b = t + h - 1;
32
33     cl = c->frame->area.x;
34     ct = c->frame->area.y;
35     cr = cl + c->frame->area.width - 1;
36     cb = ct + c->frame->area.height - 1;
37     
38     /* snap to other clients */
39     if (window_resistance)
40         for (it = stacking_list; it != NULL; it = it->next) {
41             Client *target;
42             int tl, tt, tr, tb; /* 1 past the target's edges on each side */
43
44             target = it->data;
45             /* don't snap to self or non-visibles */
46             if (!target->frame->visible || target == c) continue; 
47
48             tl = target->frame->area.x - 1;
49             tt = target->frame->area.y - 1;
50             tr = tl + target->frame->area.width + 1;
51             tb = tt + target->frame->area.height + 1;
52
53             /* snapx and snapy ensure that the window snaps to the top-most
54                window edge available, without going all the way from
55                bottom-to-top in the stacking list
56             */
57             if (!snapx && cl >= tr && l < tr && l >= tr - resistance)
58                 *x = tr, snapx = TRUE;
59             else if (!snapx && cr <= tl && r > tl && r <= tl + resistance)
60                 *x = tl - w + 1, snapx = TRUE;
61             else if (!snapy && ct >= tb && t < tb && t >= tb - resistance)
62                 *y = tb, snapy = TRUE;
63             else if (!snapy && cb <= tt && b > tt && b <= tt + resistance)
64                 *y = tt - h + 1, snapy = TRUE;
65
66             if (snapx && snapy) break;
67         }
68
69     /* get the screen boundaries */
70     area = screen_area(c->desktop);
71     al = area->x;
72     at = area->y;
73     ar = al + area->width - 1;
74     ab = at + area->height - 1;
75
76     /* snap to screen edges */
77     if (cl >= al && l < al && l >= al - resistance)
78         *x = al;
79     else if (cr <= ar && r > ar && r <= ar + resistance)
80             *x = ar - w + 1;
81     if (ct >= at && t < at && t >= at - resistance)
82         *y = at;
83     else if (cb <= ab && b > ab && b < ab + resistance)
84         *y = ab - h + 1;
85 }
86
87 static void event(ObEvent *e, void *foo)
88 {
89     g_assert(e->type == Event_Client_Moving);
90
91     resist(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
92 }
93
94 void plugin_startup()
95 {
96     dispatch_register(Event_Client_Moving, (EventHandler)event, NULL);
97 }
98
99 void plugin_shutdown()
100 {
101     dispatch_register(0, (EventHandler)event, NULL);
102 }