]> Dogcows Code - chaz/openbox/blob - plugins/resistance/resistance.c
rename the Client struct to ObClient
[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 resistance;
11 static gboolean resist_windows;
12
13 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
14 {
15 xmlNodePtr n;
16
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);
21 }
22
23 void plugin_setup_config()
24 {
25 resistance = DEFAULT_RESISTANCE;
26 resist_windows = DEFAULT_RESIST_WINDOWS;
27
28 parse_register("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 (resist_windows)
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 - resistance)
79 *x = tr, snapx = target;
80 else if (cr <= tl && r > tl && r <= tl + 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 - resistance)
85 *y = tt + 1, snapy = target;
86 else if (cb < tb && b >= tb && b < tb + 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 - resistance)
94 *y = tb, snapy = target;
95 else if (cb <= tt && b > tt && b <= tt + 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 - resistance)
100 *x = tl + 1, snapx = target;
101 else if (cr < tr && r >= tr && r < tr + resistance)
102 *x = tr - w, snapx = target;
103 }
104 }
105 }
106
107 if (snapx && snapy) break;
108 }
109
110 /* get the screen boundaries */
111 for (i = 0; i < screen_num_monitors; ++i) {
112 area = screen_area_monitor(c->desktop, i);
113
114 if (!RECT_INTERSECTS_RECT(*area, c->frame->area))
115 continue;
116
117 al = area->x;
118 at = area->y;
119 ar = al + area->width - 1;
120 ab = at + area->height - 1;
121
122 /* snap to screen edges */
123 if (cl >= al && l < al && l >= al - resistance)
124 *x = al;
125 else if (cr <= ar && r > ar && r <= ar + resistance)
126 *x = ar - w + 1;
127 if (ct >= at && t < at && t >= at - resistance)
128 *y = at;
129 else if (cb <= ab && b > ab && b < ab + resistance)
130 *y = ab - h + 1;
131 }
132 }
133
134 static void resist_size(ObClient *c, int *w, int *h, ObCorner corn)
135 {
136 GList *it;
137 ObClient *target; /* target */
138 int l, t, r, b; /* my left, top, right and bottom sides */
139 int dlt, drb; /* my destination left/top and right/bottom sides */
140 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
141 Rect *area;
142 int al, at, ar, ab; /* screen boundaries */
143 ObClient *snapx = NULL, *snapy = NULL;
144
145 /* don't snap windows with size increments */
146 if (c->size_inc.width > 1 || c->size_inc.height > 1)
147 return;
148
149 l = c->frame->area.x;
150 r = l + c->frame->area.width - 1;
151 t = c->frame->area.y;
152 b = t + c->frame->area.height - 1;
153
154 /* get the screen boundaries */
155 area = screen_area(c->desktop);
156 al = area->x;
157 at = area->y;
158 ar = al + area->width - 1;
159 ab = at + area->height - 1;
160
161 /* snap to other windows */
162 if (resist_windows) {
163 for (it = stacking_list; it != NULL; it = it->next) {
164 if (!WINDOW_IS_CLIENT(it->data))
165 continue;
166 target = it->data;
167
168 /* don't snap to invisibles or ourself */
169 if (!target->frame->visible || target == c) continue;
170
171 tl = target->frame->area.x;
172 tr = target->frame->area.x + target->frame->area.width - 1;
173 tt = target->frame->area.y;
174 tb = target->frame->area.y + target->frame->area.height - 1;
175
176 if (snapx == NULL) {
177 /* horizontal snapping */
178 if (t < tb && b > tt) {
179 switch (corn) {
180 case OB_CORNER_TOPLEFT:
181 case OB_CORNER_BOTTOMLEFT:
182 dlt = l;
183 drb = r + *w - c->frame->area.width;
184 if (r < tl && drb >= tl && drb < tl + resistance)
185 *w = tl - l, snapx = target;
186 break;
187 case OB_CORNER_TOPRIGHT:
188 case OB_CORNER_BOTTOMRIGHT:
189 dlt = l - *w + c->frame->area.width;
190 drb = r;
191 if (l > tr && dlt <= tr && dlt > tr - resistance)
192 *w = r - tr, snapx = target;
193 break;
194 }
195 }
196 }
197
198 if (snapy == NULL) {
199 /* vertical snapping */
200 if (l < tr && r > tl) {
201 switch (corn) {
202 case OB_CORNER_TOPLEFT:
203 case OB_CORNER_TOPRIGHT:
204 dlt = t;
205 drb = b + *h - c->frame->area.height;
206 if (b < tt && drb >= tt && drb < tt + resistance)
207 *h = tt - t, snapy = target;
208 break;
209 case OB_CORNER_BOTTOMLEFT:
210 case OB_CORNER_BOTTOMRIGHT:
211 dlt = t - *h + c->frame->area.height;
212 drb = b;
213 if (t > tb && dlt <= tb && dlt > tb - resistance)
214 *h = b - tb, snapy = target;
215 break;
216 }
217 }
218 }
219
220 /* snapped both ways */
221 if (snapx && snapy) break;
222 }
223 }
224
225 /* snap to screen edges */
226
227 /* horizontal snapping */
228 switch (corn) {
229 case OB_CORNER_TOPLEFT:
230 case OB_CORNER_BOTTOMLEFT:
231 dlt = l;
232 drb = r + *w - c->frame->area.width;
233 if (r <= ar && drb > ar && drb <= ar + resistance)
234 *w = ar - l + 1;
235 break;
236 case OB_CORNER_TOPRIGHT:
237 case OB_CORNER_BOTTOMRIGHT:
238 dlt = l - *w + c->frame->area.width;
239 drb = r;
240 if (l >= al && dlt < al && dlt >= al - resistance)
241 *w = r - al + 1;
242 break;
243 }
244
245 /* vertical snapping */
246 switch (corn) {
247 case OB_CORNER_TOPLEFT:
248 case OB_CORNER_TOPRIGHT:
249 dlt = t;
250 drb = b + *h - c->frame->area.height;
251 if (b <= ab && drb > ab && drb <= ab + resistance)
252 *h = ab - t + 1;
253 break;
254 case OB_CORNER_BOTTOMLEFT:
255 case OB_CORNER_BOTTOMRIGHT:
256 dlt = t - *h + c->frame->area.height;
257 drb = b;
258 if (t >= at && dlt < at && dlt >= at - resistance)
259 *h = b - at + 1;
260 break;
261 }
262 }
263
264 static void event(ObEvent *e, void *foo)
265 {
266 if (e->type == Event_Client_Moving)
267 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
268 else if (e->type == Event_Client_Resizing)
269 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
270 e->data.c.num[2]);
271 }
272
273 void plugin_startup()
274 {
275 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
276 (EventHandler)event, NULL);
277 }
278
279 void plugin_shutdown()
280 {
281 dispatch_register(0, (EventHandler)event, NULL);
282 }
This page took 0.044613 seconds and 4 git commands to generate.