]> Dogcows Code - chaz/openbox/blob - plugins/resistance/resistance.c
f5e68052051e795a5cee139729ebfab6c57ece98
[chaz/openbox] / plugins / resistance / resistance.c
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"
8 #include <glib.h>
9
10 static int win_resistance;
11 static int edge_resistance;
12
13 static void parse_xml(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, void *d)
14 {
15 xmlNodePtr n;
16
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);
22 }
23
24 void plugin_setup_config(ObParseInst *i)
25 {
26 win_resistance = edge_resistance = DEFAULT_RESISTANCE;
27
28 parse_register(i, "resistance", parse_xml, NULL);
29 }
30
31 static void resist_move(ObClient *c, int *x, int *y)
32 {
33 GList *it;
34 Rect *area;
35 guint i;
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;
41
42 w = c->frame->area.width;
43 h = c->frame->area.height;
44
45 l = *x;
46 t = *y;
47 r = l + w - 1;
48 b = t + h - 1;
49
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;
54
55 /* snap to other clients */
56 if (win_resistance)
57 for (it = stacking_list; it != NULL; it = it->next) {
58 ObClient *target;
59 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
60
61 if (!WINDOW_IS_CLIENT(it->data))
62 continue;
63 target = it->data;
64 /* don't snap to self or non-visibles */
65 if (!target->frame->visible || target == c) continue;
66
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;
71
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
75 */
76 if (snapx == NULL) {
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;
82 if (snapx != NULL) {
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;
88 }
89 }
90 }
91 if (snapy == NULL) {
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;
97 if (snapy != NULL) {
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;
103 }
104 }
105 }
106
107 if (snapx && snapy) break;
108 }
109
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);
114
115 if (!RECT_INTERSECTS_RECT(*area, c->frame->area))
116 continue;
117
118 al = area->x;
119 at = area->y;
120 ar = al + area->width - 1;
121 ab = at + area->height - 1;
122
123 /* snap to screen edges */
124 if (cl >= al && l < al && l >= al - edge_resistance)
125 *x = al;
126 else if (cr <= ar && r > ar && r <= ar + edge_resistance)
127 *x = ar - w + 1;
128 if (ct >= at && t < at && t >= at - edge_resistance)
129 *y = at;
130 else if (cb <= ab && b > ab && b < ab + edge_resistance)
131 *y = ab - h + 1;
132 }
133 }
134 }
135
136 static void resist_size(ObClient *c, int *w, int *h, ObCorner corn)
137 {
138 GList *it;
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 */
143 Rect *area;
144 int al, at, ar, ab; /* screen boundaries */
145 ObClient *snapx = NULL, *snapy = NULL;
146
147 /* don't snap windows with size increments */
148 if (c->size_inc.width > 1 || c->size_inc.height > 1)
149 return;
150
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;
155
156 /* get the screen boundaries */
157 area = screen_area(c->desktop);
158 al = area->x;
159 at = area->y;
160 ar = al + area->width - 1;
161 ab = at + area->height - 1;
162
163 /* snap to other windows */
164 if (win_resistance) {
165 for (it = stacking_list; it != NULL; it = it->next) {
166 if (!WINDOW_IS_CLIENT(it->data))
167 continue;
168 target = it->data;
169
170 /* don't snap to invisibles or ourself */
171 if (!target->frame->visible || target == c) continue;
172
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;
177
178 if (snapx == NULL) {
179 /* horizontal snapping */
180 if (t < tb && b > tt) {
181 switch (corn) {
182 case OB_CORNER_TOPLEFT:
183 case OB_CORNER_BOTTOMLEFT:
184 dlt = l;
185 drb = r + *w - c->frame->area.width;
186 if (r < tl && drb >= tl && drb < tl + win_resistance)
187 *w = tl - l, snapx = target;
188 break;
189 case OB_CORNER_TOPRIGHT:
190 case OB_CORNER_BOTTOMRIGHT:
191 dlt = l - *w + c->frame->area.width;
192 drb = r;
193 if (l > tr && dlt <= tr && dlt > tr - win_resistance)
194 *w = r - tr, snapx = target;
195 break;
196 }
197 }
198 }
199
200 if (snapy == NULL) {
201 /* vertical snapping */
202 if (l < tr && r > tl) {
203 switch (corn) {
204 case OB_CORNER_TOPLEFT:
205 case OB_CORNER_TOPRIGHT:
206 dlt = t;
207 drb = b + *h - c->frame->area.height;
208 if (b < tt && drb >= tt && drb < tt + win_resistance)
209 *h = tt - t, snapy = target;
210 break;
211 case OB_CORNER_BOTTOMLEFT:
212 case OB_CORNER_BOTTOMRIGHT:
213 dlt = t - *h + c->frame->area.height;
214 drb = b;
215 if (t > tb && dlt <= tb && dlt > tb - win_resistance)
216 *h = b - tb, snapy = target;
217 break;
218 }
219 }
220 }
221
222 /* snapped both ways */
223 if (snapx && snapy) break;
224 }
225 }
226
227 /* snap to screen edges */
228
229 if (edge_resistance) {
230 /* horizontal snapping */
231 switch (corn) {
232 case OB_CORNER_TOPLEFT:
233 case OB_CORNER_BOTTOMLEFT:
234 dlt = l;
235 drb = r + *w - c->frame->area.width;
236 if (r <= ar && drb > ar && drb <= ar + edge_resistance)
237 *w = ar - l + 1;
238 break;
239 case OB_CORNER_TOPRIGHT:
240 case OB_CORNER_BOTTOMRIGHT:
241 dlt = l - *w + c->frame->area.width;
242 drb = r;
243 if (l >= al && dlt < al && dlt >= al - edge_resistance)
244 *w = r - al + 1;
245 break;
246 }
247
248 /* vertical snapping */
249 switch (corn) {
250 case OB_CORNER_TOPLEFT:
251 case OB_CORNER_TOPRIGHT:
252 dlt = t;
253 drb = b + *h - c->frame->area.height;
254 if (b <= ab && drb > ab && drb <= ab + edge_resistance)
255 *h = ab - t + 1;
256 break;
257 case OB_CORNER_BOTTOMLEFT:
258 case OB_CORNER_BOTTOMRIGHT:
259 dlt = t - *h + c->frame->area.height;
260 drb = b;
261 if (t >= at && dlt < at && dlt >= at - edge_resistance)
262 *h = b - at + 1;
263 break;
264 }
265 }
266 }
267
268 static void event(ObEvent *e, void *foo)
269 {
270 if (e->type == Event_Client_Moving)
271 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
272 else if (e->type == Event_Client_Resizing)
273 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
274 e->data.c.num[2]);
275 }
276
277 void plugin_startup()
278 {
279 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
280 (EventHandler)event, NULL);
281 }
282
283 void plugin_shutdown()
284 {
285 dispatch_register(0, (EventHandler)event, NULL);
286 }
This page took 0.044137 seconds and 3 git commands to generate.