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 "../kernel/config.h"
9 #define DEFAULT_RESISTANCE 10
11 void plugin_setup_config()
15 config_def_set(config_def_new("resistance", Config_Integer,
17 "The amount of resistance to provide when "
18 "moving windows past edges."
20 config_def_set(config_def_new("resistance.windows", Config_Bool,
21 "Edge Resistance On Windows",
22 "Whether to provide edge resistance when "
23 "moving windows past the edge of another "
26 config_set("resistance.windows", Config_Bool, val);
29 static void resist(Client *c, int *x, int *y)
33 int l, t, r, b; /* requested edges */
34 int al, at, ar, ab; /* screen area edges */
35 int cl, ct, cr, cb; /* current edges */
36 int w, h; /* current size */
37 Client *snapx = NULL, *snapy = NULL;
38 ConfigValue resist, window_resist;
40 if (!config_get("resistance", Config_Integer, &resist) ||
42 resist.integer = DEFAULT_RESISTANCE;
43 config_set("resistance", Config_Integer, resist);
45 if (!config_get("resistance.windows", Config_Bool, &window_resist))
46 g_assert_not_reached();
48 w = c->frame->area.width;
49 h = c->frame->area.height;
56 cl = c->frame->area.x;
57 ct = c->frame->area.y;
58 cr = cl + c->frame->area.width - 1;
59 cb = ct + c->frame->area.height - 1;
61 /* snap to other clients */
62 if (window_resist.bool)
63 for (it = stacking_list; it != NULL; it = it->next) {
65 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
68 /* don't snap to self or non-visibles */
69 if (!target->frame->visible || target == c) continue;
71 tl = target->frame->area.x - 1;
72 tt = target->frame->area.y - 1;
73 tr = tl + target->frame->area.width + 1;
74 tb = tt + target->frame->area.height + 1;
76 /* snapx and snapy ensure that the window snaps to the top-most
77 window edge available, without going all the way from
78 bottom-to-top in the stacking list
81 if (cl >= tr && l < tr && l >= tr - resist.integer)
82 *x = tr, snapx = target;
83 else if (cr <= tl && r > tl && r <= tl + resist.integer)
84 *x = tl - w + 1, snapx = target;
86 /* try to corner snap to the window */
87 if (ct > tt && t <= tt && t > tt - resist.integer)
88 *y = tt + 1, snapy = target;
89 else if (cb < tb && b >= tb && b < tb + resist.integer)
90 *y = tb - h, snapy = target;
94 if (ct >= tb && t < tb && t >= tb - resist.integer)
95 *y = tb, snapy = target;
96 else if (!cb <= tt && b > tt && b <= tt + resist.integer)
97 *y = tt - h + 1, snapy = target;
99 /* try to corner snap to the window */
100 if (cl > tl && l <= tl && l > tl - resist.integer)
101 *x = tl + 1, snapx = target;
102 else if (cr < tr && r >= tr && r < tr + resist.integer)
103 *x = tr - w, snapx = target;
107 if (snapx && snapy) break;
110 /* get the screen boundaries */
111 area = screen_area(c->desktop);
114 ar = al + area->width - 1;
115 ab = at + area->height - 1;
117 /* snap to screen edges */
118 if (cl >= al && l < al && l >= al - resist.integer)
120 else if (cr <= ar && r > ar && r <= ar + resist.integer)
122 if (ct >= at && t < at && t >= at - resist.integer)
124 else if (cb <= ab && b > ab && b < ab + resist.integer)
128 static void event(ObEvent *e, void *foo)
130 g_assert(e->type == Event_Client_Moving);
132 resist(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
135 void plugin_startup()
137 dispatch_register(Event_Client_Moving, (EventHandler)event, NULL);
140 void plugin_shutdown()
142 dispatch_register(0, (EventHandler)event, NULL);