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_move(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 (ct < tb && cb > tt) {
82 if (cl >= tr && l < tr && l >= tr - resist.integer)
83 *x = tr, snapx = target;
84 else if (cr <= tl && r > tl && r <= tl + resist.integer)
85 *x = tl - w + 1, snapx = target;
87 /* try to corner snap to the window */
88 if (ct > tt && t <= tt && t > tt - resist.integer)
89 *y = tt + 1, snapy = target;
90 else if (cb < tb && b >= tb && b < tb + resist.integer)
91 *y = tb - h, snapy = target;
96 if (cl < tr && cr > tl) {
97 if (ct >= tb && t < tb && t >= tb - resist.integer)
98 *y = tb, snapy = target;
99 else if (cb <= tt && b > tt && b <= tt + resist.integer)
100 *y = tt - h + 1, snapy = target;
102 /* try to corner snap to the window */
103 if (cl > tl && l <= tl && l > tl - resist.integer)
104 *x = tl + 1, snapx = target;
105 else if (cr < tr && r >= tr && r < tr + resist.integer)
106 *x = tr - w, snapx = target;
111 if (snapx && snapy) break;
114 /* get the screen boundaries */
115 area = screen_area(c->desktop);
118 ar = al + area->width - 1;
119 ab = at + area->height - 1;
121 /* snap to screen edges */
122 if (cl >= al && l < al && l >= al - resist.integer)
124 else if (cr <= ar && r > ar && r <= ar + resist.integer)
126 if (ct >= at && t < at && t >= at - resist.integer)
128 else if (cb <= ab && b > ab && b < ab + resist.integer)
132 static void resist_size(Client *c, int *w, int *h, Corner corn)
135 Client *target; /* target */
136 int l, t, r, b; /* my left, top, right and bottom sides */
137 int dlt, drb; /* my destination left/top and right/bottom sides */
138 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
140 int al, at, ar, ab; /* screen boundaries */
141 Client *snapx = NULL, *snapy = NULL;
142 ConfigValue resist, window_resist;
144 if (!config_get("resistance", Config_Integer, &resist) ||
145 resist.integer < 0) {
146 resist.integer = DEFAULT_RESISTANCE;
147 config_set("resistance", Config_Integer, resist);
149 if (!config_get("resistance.windows", Config_Bool, &window_resist))
150 g_assert_not_reached();
152 l = c->frame->area.x;
153 r = l + c->frame->area.width - 1;
154 t = c->frame->area.y;
155 b = t + c->frame->area.height - 1;
157 /* get the screen boundaries */
158 area = screen_area(c->desktop);
161 ar = al + area->width - 1;
162 ab = at + area->height - 1;
164 /* snap to other windows */
165 if (window_resist.bool) {
166 for (it = stacking_list; it != NULL; it = it->next) {
169 /* don't snap to invisibles or ourself */
170 if (!target->frame->visible || target == c) continue;
172 tl = target->frame->area.x;
173 tr = target->frame->area.x + target->frame->area.width - 1;
174 tt = target->frame->area.y;
175 tb = target->frame->area.y + target->frame->area.height - 1;
178 /* horizontal snapping */
179 if (t < tb && b > tt) {
182 case Corner_BottomLeft:
184 drb = r + *w - c->frame->area.width;
185 if (r < tl && drb >= tl && drb < tl + resist.integer)
186 *w = tl - l, snapx = target;
188 case Corner_TopRight:
189 case Corner_BottomRight:
190 dlt = l - *w + c->frame->area.width;
192 if (l > tr && dlt <= tr && dlt > tr - resist.integer)
193 *w = r - tr, snapx = target;
200 /* vertical snapping */
201 if (l < tr && r > tl) {
204 case Corner_TopRight:
206 drb = b + *h - c->frame->area.height;
207 if (b < tt && drb >= tt && drb < tt + resist.integer)
208 *h = tt - t, snapy = target;
210 case Corner_BottomLeft:
211 case Corner_BottomRight:
212 dlt = t - *h + c->frame->area.height;
214 if (t > tb && dlt <= tb && dlt > tb - resist.integer)
215 *h = b - tb, snapy = target;
221 /* snapped both ways */
222 if (snapx && snapy) break;
226 /* snap to screen edges */
228 /* horizontal snapping */
231 case Corner_BottomLeft:
233 drb = r + *w - c->frame->area.width;
234 if (r <= ar && drb > ar && drb <= ar + resist.integer)
237 case Corner_TopRight:
238 case Corner_BottomRight:
239 dlt = l - *w + c->frame->area.width;
241 if (l >= al && dlt < al && dlt >= al - resist.integer)
246 /* vertical snapping */
249 case Corner_TopRight:
251 drb = b + *h - c->frame->area.height;
252 if (b <= ab && drb > ab && drb <= ab + resist.integer)
255 case Corner_BottomLeft:
256 case Corner_BottomRight:
257 dlt = t - *h + c->frame->area.height;
259 if (t >= at && dlt < at && dlt >= at - resist.integer)
264 /* round down for size increments */
265 *w -= c->frame->area.width + c->frame->size.left + c->frame->size.right;
266 *w = *w / c->size_inc.width * c->size_inc.width;
267 *w += c->frame->area.width + c->frame->size.left + c->frame->size.right;
269 *h -= c->frame->area.height + c->frame->size.top + c->frame->size.bottom;
270 *h = *h / c->size_inc.height * c->size_inc.height;
271 *h += c->frame->area.height + c->frame->size.top + c->frame->size.bottom;
274 static void event(ObEvent *e, void *foo)
276 if (e->type == Event_Client_Moving)
277 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
278 else if (e->type == Event_Client_Resizing)
279 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
283 void plugin_startup()
285 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
286 (EventHandler)event, NULL);
289 void plugin_shutdown()
291 dispatch_register(0, (EventHandler)event, NULL);