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