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"
8 void plugin_setup_config()
12 static int resistance = 10;
13 static gboolean window_resistance = TRUE; /* window-to-window */
15 static void resist(Client *c, int *x, int *y)
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;
25 w = c->frame->area.width;
26 h = c->frame->area.height;
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;
38 /* snap to other clients */
39 if (window_resistance)
40 for (it = stacking_list; it != NULL; it = it->next) {
42 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
45 /* don't snap to self or non-visibles */
46 if (!target->frame->visible || target == c) continue;
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;
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
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;
66 if (snapx && snapy) break;
69 /* get the screen boundaries */
70 area = screen_area(c->desktop);
73 ar = al + area->width - 1;
74 ab = at + area->height - 1;
76 /* snap to screen edges */
77 if (cl >= al && l < al && l >= al - resistance)
79 else if (cr <= ar && r > ar && r <= ar + resistance)
81 if (ct >= at && t < at && t >= at - resistance)
83 else if (cb <= ab && b > ab && b < ab + resistance)
87 static void event(ObEvent *e, void *foo)
89 g_assert(e->type == Event_Client_Moving);
91 resist(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
96 dispatch_register(Event_Client_Moving, (EventHandler)event, NULL);
99 void plugin_shutdown()
101 dispatch_register(0, (EventHandler)event, NULL);