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 "parser/parse.h"
7 #include "resistance.h"
10 static int resistance;
11 static gboolean resist_windows;
13 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
17 if ((n = parse_find_node("strength", node)))
18 resistance = parse_int(doc, n);
19 if ((n = parse_find_node("windows", node)))
20 resist_windows = parse_bool(doc, n);
23 void plugin_setup_config()
25 resistance = DEFAULT_RESISTANCE;
26 resist_windows = DEFAULT_RESIST_WINDOWS;
28 parse_register("resistance", parse_xml, NULL);
31 static void resist_move(Client *c, int *x, int *y)
36 int l, t, r, b; /* requested edges */
37 int al, at, ar, ab; /* screen area edges */
38 int cl, ct, cr, cb; /* current edges */
39 int w, h; /* current size */
40 Client *snapx = NULL, *snapy = NULL;
42 w = c->frame->area.width;
43 h = c->frame->area.height;
50 cl = c->frame->area.x;
51 ct = c->frame->area.y;
52 cr = cl + c->frame->area.width - 1;
53 cb = ct + c->frame->area.height - 1;
55 /* snap to other clients */
57 for (it = stacking_list; it != NULL; it = it->next) {
59 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
61 if (!WINDOW_IS_CLIENT(it->data))
64 /* don't snap to self or non-visibles */
65 if (!target->frame->visible || target == c) continue;
67 tl = target->frame->area.x - 1;
68 tt = target->frame->area.y - 1;
69 tr = tl + target->frame->area.width + 1;
70 tb = tt + target->frame->area.height + 1;
72 /* snapx and snapy ensure that the window snaps to the top-most
73 window edge available, without going all the way from
74 bottom-to-top in the stacking list
77 if (ct < tb && cb > tt) {
78 if (cl >= tr && l < tr && l >= tr - resistance)
79 *x = tr, snapx = target;
80 else if (cr <= tl && r > tl && r <= tl + resistance)
81 *x = tl - w + 1, snapx = target;
83 /* try to corner snap to the window */
84 if (ct > tt && t <= tt && t > tt - resistance)
85 *y = tt + 1, snapy = target;
86 else if (cb < tb && b >= tb && b < tb + resistance)
87 *y = tb - h, snapy = target;
92 if (cl < tr && cr > tl) {
93 if (ct >= tb && t < tb && t >= tb - resistance)
94 *y = tb, snapy = target;
95 else if (cb <= tt && b > tt && b <= tt + resistance)
96 *y = tt - h + 1, snapy = target;
98 /* try to corner snap to the window */
99 if (cl > tl && l <= tl && l > tl - resistance)
100 *x = tl + 1, snapx = target;
101 else if (cr < tr && r >= tr && r < tr + resistance)
102 *x = tr - w, snapx = target;
107 if (snapx && snapy) break;
110 /* get the screen boundaries */
111 for (i = 0; i < screen_num_xin_areas; ++i) {
112 area = screen_area_xinerama(c->desktop, i);
114 if (!RECT_INTERSECTS_RECT(*area, c->frame->area))
119 ar = al + area->width - 1;
120 ab = at + area->height - 1;
122 /* snap to screen edges */
123 if (cl >= al && l < al && l >= al - resistance)
125 else if (cr <= ar && r > ar && r <= ar + resistance)
127 if (ct >= at && t < at && t >= at - resistance)
129 else if (cb <= ab && b > ab && b < ab + resistance)
134 static void resist_size(Client *c, int *w, int *h, Corner corn)
137 Client *target; /* target */
138 int l, t, r, b; /* my left, top, right and bottom sides */
139 int dlt, drb; /* my destination left/top and right/bottom sides */
140 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
142 int al, at, ar, ab; /* screen boundaries */
143 Client *snapx = NULL, *snapy = NULL;
145 /* don't snap windows with size increments */
146 if (c->size_inc.width > 1 || c->size_inc.height > 1)
149 l = c->frame->area.x;
150 r = l + c->frame->area.width - 1;
151 t = c->frame->area.y;
152 b = t + c->frame->area.height - 1;
154 /* get the screen boundaries */
155 area = screen_area(c->desktop);
158 ar = al + area->width - 1;
159 ab = at + area->height - 1;
161 /* snap to other windows */
162 if (resist_windows) {
163 for (it = stacking_list; it != NULL; it = it->next) {
164 if (!WINDOW_IS_CLIENT(it->data))
168 /* don't snap to invisibles or ourself */
169 if (!target->frame->visible || target == c) continue;
171 tl = target->frame->area.x;
172 tr = target->frame->area.x + target->frame->area.width - 1;
173 tt = target->frame->area.y;
174 tb = target->frame->area.y + target->frame->area.height - 1;
177 /* horizontal snapping */
178 if (t < tb && b > tt) {
181 case Corner_BottomLeft:
183 drb = r + *w - c->frame->area.width;
184 if (r < tl && drb >= tl && drb < tl + resistance)
185 *w = tl - l, snapx = target;
187 case Corner_TopRight:
188 case Corner_BottomRight:
189 dlt = l - *w + c->frame->area.width;
191 if (l > tr && dlt <= tr && dlt > tr - resistance)
192 *w = r - tr, snapx = target;
199 /* vertical snapping */
200 if (l < tr && r > tl) {
203 case Corner_TopRight:
205 drb = b + *h - c->frame->area.height;
206 if (b < tt && drb >= tt && drb < tt + resistance)
207 *h = tt - t, snapy = target;
209 case Corner_BottomLeft:
210 case Corner_BottomRight:
211 dlt = t - *h + c->frame->area.height;
213 if (t > tb && dlt <= tb && dlt > tb - resistance)
214 *h = b - tb, snapy = target;
220 /* snapped both ways */
221 if (snapx && snapy) break;
225 /* snap to screen edges */
227 /* horizontal snapping */
230 case Corner_BottomLeft:
232 drb = r + *w - c->frame->area.width;
233 if (r <= ar && drb > ar && drb <= ar + resistance)
236 case Corner_TopRight:
237 case Corner_BottomRight:
238 dlt = l - *w + c->frame->area.width;
240 if (l >= al && dlt < al && dlt >= al - resistance)
245 /* vertical snapping */
248 case Corner_TopRight:
250 drb = b + *h - c->frame->area.height;
251 if (b <= ab && drb > ab && drb <= ab + resistance)
254 case Corner_BottomLeft:
255 case Corner_BottomRight:
256 dlt = t - *h + c->frame->area.height;
258 if (t >= at && dlt < at && dlt >= at - resistance)
264 static void event(ObEvent *e, void *foo)
266 if (e->type == Event_Client_Moving)
267 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
268 else if (e->type == Event_Client_Resizing)
269 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
273 void plugin_startup()
275 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
276 (EventHandler)event, NULL);
279 void plugin_shutdown()
281 dispatch_register(0, (EventHandler)event, NULL);