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 win_resistance;
11 static int edge_resistance;
13 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
17 node = node->xmlChildrenNode;
18 if ((n = parse_find_node("strength", node)))
19 win_resistance = parse_int(doc, n);
20 if ((n = parse_find_node("screen_edge_strength", node)))
21 edge_resistance = parse_int(doc, n);
24 void plugin_setup_config()
26 win_resistance = edge_resistance = DEFAULT_RESISTANCE;
28 parse_register("resistance", parse_xml, NULL);
31 static void resist_move(ObClient *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 ObClient *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 - win_resistance)
79 *x = tr, snapx = target;
80 else if (cr <= tl && r > tl && r <= tl + win_resistance)
81 *x = tl - w + 1, snapx = target;
83 /* try to corner snap to the window */
84 if (ct > tt && t <= tt && t > tt - win_resistance)
85 *y = tt + 1, snapy = target;
86 else if (cb < tb && b >= tb && b < tb + win_resistance)
87 *y = tb - h, snapy = target;
92 if (cl < tr && cr > tl) {
93 if (ct >= tb && t < tb && t >= tb - win_resistance)
94 *y = tb, snapy = target;
95 else if (cb <= tt && b > tt && b <= tt + win_resistance)
96 *y = tt - h + 1, snapy = target;
98 /* try to corner snap to the window */
99 if (cl > tl && l <= tl && l > tl - win_resistance)
100 *x = tl + 1, snapx = target;
101 else if (cr < tr && r >= tr && r < tr + win_resistance)
102 *x = tr - w, snapx = target;
107 if (snapx && snapy) break;
110 /* get the screen boundaries */
111 if (edge_resistance) {
112 for (i = 0; i < screen_num_monitors; ++i) {
113 area = screen_area_monitor(c->desktop, i);
115 if (!RECT_INTERSECTS_RECT(*area, c->frame->area))
120 ar = al + area->width - 1;
121 ab = at + area->height - 1;
123 /* snap to screen edges */
124 if (cl >= al && l < al && l >= al - edge_resistance)
126 else if (cr <= ar && r > ar && r <= ar + edge_resistance)
128 if (ct >= at && t < at && t >= at - edge_resistance)
130 else if (cb <= ab && b > ab && b < ab + edge_resistance)
136 static void resist_size(ObClient *c, int *w, int *h, ObCorner corn)
139 ObClient *target; /* target */
140 int l, t, r, b; /* my left, top, right and bottom sides */
141 int dlt, drb; /* my destination left/top and right/bottom sides */
142 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
144 int al, at, ar, ab; /* screen boundaries */
145 ObClient *snapx = NULL, *snapy = NULL;
147 /* don't snap windows with size increments */
148 if (c->size_inc.width > 1 || c->size_inc.height > 1)
151 l = c->frame->area.x;
152 r = l + c->frame->area.width - 1;
153 t = c->frame->area.y;
154 b = t + c->frame->area.height - 1;
156 /* get the screen boundaries */
157 area = screen_area(c->desktop);
160 ar = al + area->width - 1;
161 ab = at + area->height - 1;
163 /* snap to other windows */
164 if (resist_windows) {
165 for (it = stacking_list; it != NULL; it = it->next) {
166 if (!WINDOW_IS_CLIENT(it->data))
170 /* don't snap to invisibles or ourself */
171 if (!target->frame->visible || target == c) continue;
173 tl = target->frame->area.x;
174 tr = target->frame->area.x + target->frame->area.width - 1;
175 tt = target->frame->area.y;
176 tb = target->frame->area.y + target->frame->area.height - 1;
179 /* horizontal snapping */
180 if (t < tb && b > tt) {
182 case OB_CORNER_TOPLEFT:
183 case OB_CORNER_BOTTOMLEFT:
185 drb = r + *w - c->frame->area.width;
186 if (r < tl && drb >= tl && drb < tl + win_resistance)
187 *w = tl - l, snapx = target;
189 case OB_CORNER_TOPRIGHT:
190 case OB_CORNER_BOTTOMRIGHT:
191 dlt = l - *w + c->frame->area.width;
193 if (l > tr && dlt <= tr && dlt > tr - win_resistance)
194 *w = r - tr, snapx = target;
201 /* vertical snapping */
202 if (l < tr && r > tl) {
204 case OB_CORNER_TOPLEFT:
205 case OB_CORNER_TOPRIGHT:
207 drb = b + *h - c->frame->area.height;
208 if (b < tt && drb >= tt && drb < tt + win_resistance)
209 *h = tt - t, snapy = target;
211 case OB_CORNER_BOTTOMLEFT:
212 case OB_CORNER_BOTTOMRIGHT:
213 dlt = t - *h + c->frame->area.height;
215 if (t > tb && dlt <= tb && dlt > tb - win_resistance)
216 *h = b - tb, snapy = target;
222 /* snapped both ways */
223 if (snapx && snapy) break;
227 /* snap to screen edges */
229 /* horizontal snapping */
231 case OB_CORNER_TOPLEFT:
232 case OB_CORNER_BOTTOMLEFT:
234 drb = r + *w - c->frame->area.width;
235 if (r <= ar && drb > ar && drb <= ar + edge_resistance)
238 case OB_CORNER_TOPRIGHT:
239 case OB_CORNER_BOTTOMRIGHT:
240 dlt = l - *w + c->frame->area.width;
242 if (l >= al && dlt < al && dlt >= al - edge_resistance)
247 /* vertical snapping */
249 case OB_CORNER_TOPLEFT:
250 case OB_CORNER_TOPRIGHT:
252 drb = b + *h - c->frame->area.height;
253 if (b <= ab && drb > ab && drb <= ab + edge_resistance)
256 case OB_CORNER_BOTTOMLEFT:
257 case OB_CORNER_BOTTOMRIGHT:
258 dlt = t - *h + c->frame->area.height;
260 if (t >= at && dlt < at && dlt >= at - edge_resistance)
266 static void event(ObEvent *e, void *foo)
268 if (e->type == Event_Client_Moving)
269 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
270 else if (e->type == Event_Client_Resizing)
271 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
275 void plugin_startup()
277 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
278 (EventHandler)event, NULL);
281 void plugin_shutdown()
283 dispatch_register(0, (EventHandler)event, NULL);