1 #include "kernel/dispatch.h"
2 #include "kernel/client.h"
3 #include "kernel/frame.h"
4 #include "kernel/parse.h"
5 #include "kernel/stacking.h"
6 #include "kernel/screen.h"
10 static gboolean resist_windows;
12 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
16 if ((n = parse_find_node("strength", node)))
17 resistance = parse_int(doc, n);
18 if ((n = parse_find_node("windows", node)))
19 resist_windows = parse_bool(doc, n);
22 void plugin_setup_config()
25 resist_windows = TRUE;
27 parse_register("resistance", parse_xml, NULL);
30 static void resist_move(Client *c, int *x, int *y)
34 int l, t, r, b; /* requested edges */
35 int al, at, ar, ab; /* screen area edges */
36 int cl, ct, cr, cb; /* current edges */
37 int w, h; /* current size */
38 Client *snapx = NULL, *snapy = NULL;
40 w = c->frame->area.width;
41 h = c->frame->area.height;
48 cl = c->frame->area.x;
49 ct = c->frame->area.y;
50 cr = cl + c->frame->area.width - 1;
51 cb = ct + c->frame->area.height - 1;
53 /* snap to other clients */
55 for (it = stacking_list; it != NULL; it = it->next) {
57 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
59 if (!WINDOW_IS_CLIENT(it->data))
62 /* don't snap to self or non-visibles */
63 if (!target->frame->visible || target == c) continue;
65 tl = target->frame->area.x - 1;
66 tt = target->frame->area.y - 1;
67 tr = tl + target->frame->area.width + 1;
68 tb = tt + target->frame->area.height + 1;
70 /* snapx and snapy ensure that the window snaps to the top-most
71 window edge available, without going all the way from
72 bottom-to-top in the stacking list
75 if (ct < tb && cb > tt) {
76 if (cl >= tr && l < tr && l >= tr - resistance)
77 *x = tr, snapx = target;
78 else if (cr <= tl && r > tl && r <= tl + resistance)
79 *x = tl - w + 1, snapx = target;
81 /* try to corner snap to the window */
82 if (ct > tt && t <= tt && t > tt - resistance)
83 *y = tt + 1, snapy = target;
84 else if (cb < tb && b >= tb && b < tb + resistance)
85 *y = tb - h, snapy = target;
90 if (cl < tr && cr > tl) {
91 if (ct >= tb && t < tb && t >= tb - resistance)
92 *y = tb, snapy = target;
93 else if (cb <= tt && b > tt && b <= tt + resistance)
94 *y = tt - h + 1, snapy = target;
96 /* try to corner snap to the window */
97 if (cl > tl && l <= tl && l > tl - resistance)
98 *x = tl + 1, snapx = target;
99 else if (cr < tr && r >= tr && r < tr + resistance)
100 *x = tr - w, snapx = target;
105 if (snapx && snapy) break;
108 /* get the screen boundaries */
109 area = screen_area(c->desktop);
112 ar = al + area->width - 1;
113 ab = at + area->height - 1;
115 /* snap to screen edges */
116 if (cl >= al && l < al && l >= al - resistance)
118 else if (cr <= ar && r > ar && r <= ar + resistance)
120 if (ct >= at && t < at && t >= at - resistance)
122 else if (cb <= ab && b > ab && b < ab + resistance)
126 static void resist_size(Client *c, int *w, int *h, Corner corn)
129 Client *target; /* target */
130 int l, t, r, b; /* my left, top, right and bottom sides */
131 int dlt, drb; /* my destination left/top and right/bottom sides */
132 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
134 int al, at, ar, ab; /* screen boundaries */
135 Client *snapx = NULL, *snapy = NULL;
137 /* don't snap windows with size increments */
138 if (c->size_inc.width > 1 || c->size_inc.height > 1)
141 l = c->frame->area.x;
142 r = l + c->frame->area.width - 1;
143 t = c->frame->area.y;
144 b = t + c->frame->area.height - 1;
146 /* get the screen boundaries */
147 area = screen_area(c->desktop);
150 ar = al + area->width - 1;
151 ab = at + area->height - 1;
153 /* snap to other windows */
154 if (resist_windows) {
155 for (it = stacking_list; it != NULL; it = it->next) {
156 if (!WINDOW_IS_CLIENT(it->data))
160 /* don't snap to invisibles or ourself */
161 if (!target->frame->visible || target == c) continue;
163 tl = target->frame->area.x;
164 tr = target->frame->area.x + target->frame->area.width - 1;
165 tt = target->frame->area.y;
166 tb = target->frame->area.y + target->frame->area.height - 1;
169 /* horizontal snapping */
170 if (t < tb && b > tt) {
173 case Corner_BottomLeft:
175 drb = r + *w - c->frame->area.width;
176 if (r < tl && drb >= tl && drb < tl + resistance)
177 *w = tl - l, snapx = target;
179 case Corner_TopRight:
180 case Corner_BottomRight:
181 dlt = l - *w + c->frame->area.width;
183 if (l > tr && dlt <= tr && dlt > tr - resistance)
184 *w = r - tr, snapx = target;
191 /* vertical snapping */
192 if (l < tr && r > tl) {
195 case Corner_TopRight:
197 drb = b + *h - c->frame->area.height;
198 if (b < tt && drb >= tt && drb < tt + resistance)
199 *h = tt - t, snapy = target;
201 case Corner_BottomLeft:
202 case Corner_BottomRight:
203 dlt = t - *h + c->frame->area.height;
205 if (t > tb && dlt <= tb && dlt > tb - resistance)
206 *h = b - tb, snapy = target;
212 /* snapped both ways */
213 if (snapx && snapy) break;
217 /* snap to screen edges */
219 /* horizontal snapping */
222 case Corner_BottomLeft:
224 drb = r + *w - c->frame->area.width;
225 if (r <= ar && drb > ar && drb <= ar + resistance)
228 case Corner_TopRight:
229 case Corner_BottomRight:
230 dlt = l - *w + c->frame->area.width;
232 if (l >= al && dlt < al && dlt >= al - resistance)
237 /* vertical snapping */
240 case Corner_TopRight:
242 drb = b + *h - c->frame->area.height;
243 if (b <= ab && drb > ab && drb <= ab + resistance)
246 case Corner_BottomLeft:
247 case Corner_BottomRight:
248 dlt = t - *h + c->frame->area.height;
250 if (t >= at && dlt < at && dlt >= at - resistance)
256 static void event(ObEvent *e, void *foo)
258 if (e->type == Event_Client_Moving)
259 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
260 else if (e->type == Event_Client_Resizing)
261 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
265 void plugin_startup()
267 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
268 (EventHandler)event, NULL);
271 void plugin_shutdown()
273 dispatch_register(0, (EventHandler)event, NULL);