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 area = screen_area_xinerama(c->desktop, client_xinerama_area(c));
115 ar = al + area->width - 1;
116 ab = at + area->height - 1;
118 /* snap to screen edges */
119 if (cl >= al && l < al && l >= al - resistance)
121 else if (cr <= ar && r > ar && r <= ar + resistance)
123 if (ct >= at && t < at && t >= at - resistance)
125 else if (cb <= ab && b > ab && b < ab + resistance)
129 static void resist_size(Client *c, int *w, int *h, Corner corn)
132 Client *target; /* target */
133 int l, t, r, b; /* my left, top, right and bottom sides */
134 int dlt, drb; /* my destination left/top and right/bottom sides */
135 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
137 int al, at, ar, ab; /* screen boundaries */
138 Client *snapx = NULL, *snapy = NULL;
140 /* don't snap windows with size increments */
141 if (c->size_inc.width > 1 || c->size_inc.height > 1)
144 l = c->frame->area.x;
145 r = l + c->frame->area.width - 1;
146 t = c->frame->area.y;
147 b = t + c->frame->area.height - 1;
149 /* get the screen boundaries */
150 area = screen_area(c->desktop);
153 ar = al + area->width - 1;
154 ab = at + area->height - 1;
156 /* snap to other windows */
157 if (resist_windows) {
158 for (it = stacking_list; it != NULL; it = it->next) {
159 if (!WINDOW_IS_CLIENT(it->data))
163 /* don't snap to invisibles or ourself */
164 if (!target->frame->visible || target == c) continue;
166 tl = target->frame->area.x;
167 tr = target->frame->area.x + target->frame->area.width - 1;
168 tt = target->frame->area.y;
169 tb = target->frame->area.y + target->frame->area.height - 1;
172 /* horizontal snapping */
173 if (t < tb && b > tt) {
176 case Corner_BottomLeft:
178 drb = r + *w - c->frame->area.width;
179 if (r < tl && drb >= tl && drb < tl + resistance)
180 *w = tl - l, snapx = target;
182 case Corner_TopRight:
183 case Corner_BottomRight:
184 dlt = l - *w + c->frame->area.width;
186 if (l > tr && dlt <= tr && dlt > tr - resistance)
187 *w = r - tr, snapx = target;
194 /* vertical snapping */
195 if (l < tr && r > tl) {
198 case Corner_TopRight:
200 drb = b + *h - c->frame->area.height;
201 if (b < tt && drb >= tt && drb < tt + resistance)
202 *h = tt - t, snapy = target;
204 case Corner_BottomLeft:
205 case Corner_BottomRight:
206 dlt = t - *h + c->frame->area.height;
208 if (t > tb && dlt <= tb && dlt > tb - resistance)
209 *h = b - tb, snapy = target;
215 /* snapped both ways */
216 if (snapx && snapy) break;
220 /* snap to screen edges */
222 /* horizontal snapping */
225 case Corner_BottomLeft:
227 drb = r + *w - c->frame->area.width;
228 if (r <= ar && drb > ar && drb <= ar + resistance)
231 case Corner_TopRight:
232 case Corner_BottomRight:
233 dlt = l - *w + c->frame->area.width;
235 if (l >= al && dlt < al && dlt >= al - resistance)
240 /* vertical snapping */
243 case Corner_TopRight:
245 drb = b + *h - c->frame->area.height;
246 if (b <= ab && drb > ab && drb <= ab + resistance)
249 case Corner_BottomLeft:
250 case Corner_BottomRight:
251 dlt = t - *h + c->frame->area.height;
253 if (t >= at && dlt < at && dlt >= at - resistance)
259 static void event(ObEvent *e, void *foo)
261 if (e->type == Event_Client_Moving)
262 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
263 else if (e->type == Event_Client_Resizing)
264 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
268 void plugin_startup()
270 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
271 (EventHandler)event, NULL);
274 void plugin_shutdown()
276 dispatch_register(0, (EventHandler)event, NULL);