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)
35 int l, t, r, b; /* requested edges */
36 int al, at, ar, ab; /* screen area edges */
37 int cl, ct, cr, cb; /* current edges */
38 int w, h; /* current size */
39 Client *snapx = NULL, *snapy = NULL;
41 w = c->frame->area.width;
42 h = c->frame->area.height;
49 cl = c->frame->area.x;
50 ct = c->frame->area.y;
51 cr = cl + c->frame->area.width - 1;
52 cb = ct + c->frame->area.height - 1;
54 /* snap to other clients */
56 for (it = stacking_list; it != NULL; it = it->next) {
58 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
60 if (!WINDOW_IS_CLIENT(it->data))
63 /* don't snap to self or non-visibles */
64 if (!target->frame->visible || target == c) continue;
66 tl = target->frame->area.x - 1;
67 tt = target->frame->area.y - 1;
68 tr = tl + target->frame->area.width + 1;
69 tb = tt + target->frame->area.height + 1;
71 /* snapx and snapy ensure that the window snaps to the top-most
72 window edge available, without going all the way from
73 bottom-to-top in the stacking list
76 if (ct < tb && cb > tt) {
77 if (cl >= tr && l < tr && l >= tr - resistance)
78 *x = tr, snapx = target;
79 else if (cr <= tl && r > tl && r <= tl + resistance)
80 *x = tl - w + 1, snapx = target;
82 /* try to corner snap to the window */
83 if (ct > tt && t <= tt && t > tt - resistance)
84 *y = tt + 1, snapy = target;
85 else if (cb < tb && b >= tb && b < tb + resistance)
86 *y = tb - h, snapy = target;
91 if (cl < tr && cr > tl) {
92 if (ct >= tb && t < tb && t >= tb - resistance)
93 *y = tb, snapy = target;
94 else if (cb <= tt && b > tt && b <= tt + resistance)
95 *y = tt - h + 1, snapy = target;
97 /* try to corner snap to the window */
98 if (cl > tl && l <= tl && l > tl - resistance)
99 *x = tl + 1, snapx = target;
100 else if (cr < tr && r >= tr && r < tr + resistance)
101 *x = tr - w, snapx = target;
106 if (snapx && snapy) break;
109 /* get the screen boundaries */
110 area = screen_area(c->desktop);
113 ar = al + area->width - 1;
114 ab = at + area->height - 1;
116 /* snap to screen edges */
117 if (cl >= al && l < al && l >= al - resistance)
119 else if (cr <= ar && r > ar && r <= ar + resistance)
121 if (ct >= at && t < at && t >= at - resistance)
123 else if (cb <= ab && b > ab && b < ab + resistance)
127 static void resist_size(Client *c, int *w, int *h, Corner corn)
130 Client *target; /* target */
131 int l, t, r, b; /* my left, top, right and bottom sides */
132 int dlt, drb; /* my destination left/top and right/bottom sides */
133 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
135 int al, at, ar, ab; /* screen boundaries */
136 Client *snapx = NULL, *snapy = NULL;
138 /* don't snap windows with size increments */
139 if (c->size_inc.width > 1 || c->size_inc.height > 1)
142 l = c->frame->area.x;
143 r = l + c->frame->area.width - 1;
144 t = c->frame->area.y;
145 b = t + c->frame->area.height - 1;
147 /* get the screen boundaries */
148 area = screen_area(c->desktop);
151 ar = al + area->width - 1;
152 ab = at + area->height - 1;
154 /* snap to other windows */
155 if (resist_windows) {
156 for (it = stacking_list; it != NULL; it = it->next) {
157 if (!WINDOW_IS_CLIENT(it->data))
161 /* don't snap to invisibles or ourself */
162 if (!target->frame->visible || target == c) continue;
164 tl = target->frame->area.x;
165 tr = target->frame->area.x + target->frame->area.width - 1;
166 tt = target->frame->area.y;
167 tb = target->frame->area.y + target->frame->area.height - 1;
170 /* horizontal snapping */
171 if (t < tb && b > tt) {
174 case Corner_BottomLeft:
176 drb = r + *w - c->frame->area.width;
177 if (r < tl && drb >= tl && drb < tl + resistance)
178 *w = tl - l, snapx = target;
180 case Corner_TopRight:
181 case Corner_BottomRight:
182 dlt = l - *w + c->frame->area.width;
184 if (l > tr && dlt <= tr && dlt > tr - resistance)
185 *w = r - tr, snapx = target;
192 /* vertical snapping */
193 if (l < tr && r > tl) {
196 case Corner_TopRight:
198 drb = b + *h - c->frame->area.height;
199 if (b < tt && drb >= tt && drb < tt + resistance)
200 *h = tt - t, snapy = target;
202 case Corner_BottomLeft:
203 case Corner_BottomRight:
204 dlt = t - *h + c->frame->area.height;
206 if (t > tb && dlt <= tb && dlt > tb - resistance)
207 *h = b - tb, snapy = target;
213 /* snapped both ways */
214 if (snapx && snapy) break;
218 /* snap to screen edges */
220 /* horizontal snapping */
223 case Corner_BottomLeft:
225 drb = r + *w - c->frame->area.width;
226 if (r <= ar && drb > ar && drb <= ar + resistance)
229 case Corner_TopRight:
230 case Corner_BottomRight:
231 dlt = l - *w + c->frame->area.width;
233 if (l >= al && dlt < al && dlt >= al - resistance)
238 /* vertical snapping */
241 case Corner_TopRight:
243 drb = b + *h - c->frame->area.height;
244 if (b <= ab && drb > ab && drb <= ab + resistance)
247 case Corner_BottomLeft:
248 case Corner_BottomRight:
249 dlt = t - *h + c->frame->area.height;
251 if (t >= at && dlt < at && dlt >= at - resistance)
257 static void event(ObEvent *e, void *foo)
259 if (e->type == Event_Client_Moving)
260 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
261 else if (e->type == Event_Client_Resizing)
262 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
266 void plugin_startup()
268 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
269 (EventHandler)event, NULL);
272 void plugin_shutdown()
274 dispatch_register(0, (EventHandler)event, NULL);