]> Dogcows Code - chaz/openbox/blob - plugins/resistance.c
find the right offset in the source data for parent relative
[chaz/openbox] / plugins / 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 "../kernel/config.h"
7 #include <glib.h>
8
9 #define DEFAULT_RESISTANCE 10
10
11 void plugin_setup_config()
12 {
13 ConfigValue val;
14
15 config_def_set(config_def_new("resistance", Config_Integer,
16 "Edge Resistance",
17 "The amount of resistance to provide when "
18 "moving windows past edges."
19 "positioned."));
20 config_def_set(config_def_new("resistance.windows", Config_Bool,
21 "Edge Resistance On Windows",
22 "Whether to provide edge resistance when "
23 "moving windows past the edge of another "
24 "window."));
25 val.bool = TRUE;
26 config_set("resistance.windows", Config_Bool, val);
27 }
28
29 static void resist_move(Client *c, int *x, int *y)
30 {
31 GList *it;
32 Rect *area;
33 int l, t, r, b; /* requested edges */
34 int al, at, ar, ab; /* screen area edges */
35 int cl, ct, cr, cb; /* current edges */
36 int w, h; /* current size */
37 Client *snapx = NULL, *snapy = NULL;
38 ConfigValue resist, window_resist;
39
40 if (!config_get("resistance", Config_Integer, &resist) ||
41 resist.integer < 0) {
42 resist.integer = DEFAULT_RESISTANCE;
43 config_set("resistance", Config_Integer, resist);
44 }
45 if (!config_get("resistance.windows", Config_Bool, &window_resist))
46 g_assert_not_reached();
47
48 w = c->frame->area.width;
49 h = c->frame->area.height;
50
51 l = *x;
52 t = *y;
53 r = l + w - 1;
54 b = t + h - 1;
55
56 cl = c->frame->area.x;
57 ct = c->frame->area.y;
58 cr = cl + c->frame->area.width - 1;
59 cb = ct + c->frame->area.height - 1;
60
61 /* snap to other clients */
62 if (window_resist.bool)
63 for (it = stacking_list; it != NULL; it = it->next) {
64 Client *target;
65 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
66
67 target = it->data;
68 /* don't snap to self or non-visibles */
69 if (!target->frame->visible || target == c) continue;
70
71 tl = target->frame->area.x - 1;
72 tt = target->frame->area.y - 1;
73 tr = tl + target->frame->area.width + 1;
74 tb = tt + target->frame->area.height + 1;
75
76 /* snapx and snapy ensure that the window snaps to the top-most
77 window edge available, without going all the way from
78 bottom-to-top in the stacking list
79 */
80 if (snapx == NULL) {
81 if (ct < tb && cb > tt) {
82 if (cl >= tr && l < tr && l >= tr - resist.integer)
83 *x = tr, snapx = target;
84 else if (cr <= tl && r > tl && r <= tl + resist.integer)
85 *x = tl - w + 1, snapx = target;
86 if (snapx != NULL) {
87 /* try to corner snap to the window */
88 if (ct > tt && t <= tt && t > tt - resist.integer)
89 *y = tt + 1, snapy = target;
90 else if (cb < tb && b >= tb && b < tb + resist.integer)
91 *y = tb - h, snapy = target;
92 }
93 }
94 }
95 if (snapy == NULL) {
96 if (cl < tr && cr > tl) {
97 if (ct >= tb && t < tb && t >= tb - resist.integer)
98 *y = tb, snapy = target;
99 else if (cb <= tt && b > tt && b <= tt + resist.integer)
100 *y = tt - h + 1, snapy = target;
101 if (snapy != NULL) {
102 /* try to corner snap to the window */
103 if (cl > tl && l <= tl && l > tl - resist.integer)
104 *x = tl + 1, snapx = target;
105 else if (cr < tr && r >= tr && r < tr + resist.integer)
106 *x = tr - w, snapx = target;
107 }
108 }
109 }
110
111 if (snapx && snapy) break;
112 }
113
114 /* get the screen boundaries */
115 area = screen_area(c->desktop);
116 al = area->x;
117 at = area->y;
118 ar = al + area->width - 1;
119 ab = at + area->height - 1;
120
121 /* snap to screen edges */
122 if (cl >= al && l < al && l >= al - resist.integer)
123 *x = al;
124 else if (cr <= ar && r > ar && r <= ar + resist.integer)
125 *x = ar - w + 1;
126 if (ct >= at && t < at && t >= at - resist.integer)
127 *y = at;
128 else if (cb <= ab && b > ab && b < ab + resist.integer)
129 *y = ab - h + 1;
130 }
131
132 static void resist_size(Client *c, int *w, int *h, Corner corn)
133 {
134 GList *it;
135 Client *target; /* target */
136 int l, t, r, b; /* my left, top, right and bottom sides */
137 int dlt, drb; /* my destination left/top and right/bottom sides */
138 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
139 Rect *area;
140 int al, at, ar, ab; /* screen boundaries */
141 Client *snapx = NULL, *snapy = NULL;
142 ConfigValue resist, window_resist;
143
144 if (!config_get("resistance", Config_Integer, &resist) ||
145 resist.integer < 0) {
146 resist.integer = DEFAULT_RESISTANCE;
147 config_set("resistance", Config_Integer, resist);
148 }
149 if (!config_get("resistance.windows", Config_Bool, &window_resist))
150 g_assert_not_reached();
151
152 l = c->frame->area.x;
153 r = l + c->frame->area.width - 1;
154 t = c->frame->area.y;
155 b = t + c->frame->area.height - 1;
156
157 /* get the screen boundaries */
158 area = screen_area(c->desktop);
159 al = area->x;
160 at = area->y;
161 ar = al + area->width - 1;
162 ab = at + area->height - 1;
163
164 /* snap to other windows */
165 if (window_resist.bool) {
166 for (it = stacking_list; it != NULL; it = it->next) {
167 target = it->data;
168
169 /* don't snap to invisibles or ourself */
170 if (!target->frame->visible || target == c) continue;
171
172 tl = target->frame->area.x;
173 tr = target->frame->area.x + target->frame->area.width - 1;
174 tt = target->frame->area.y;
175 tb = target->frame->area.y + target->frame->area.height - 1;
176
177 if (snapx == NULL) {
178 /* horizontal snapping */
179 if (t < tb && b > tt) {
180 switch (corn) {
181 case Corner_TopLeft:
182 case Corner_BottomLeft:
183 dlt = l;
184 drb = r + *w - c->frame->area.width;
185 if (r < tl && drb >= tl && drb < tl + resist.integer)
186 *w = tl - l, snapx = target;
187 break;
188 case Corner_TopRight:
189 case Corner_BottomRight:
190 dlt = l - *w + c->frame->area.width;
191 drb = r;
192 if (l > tr && dlt <= tr && dlt > tr - resist.integer)
193 *w = r - tr, snapx = target;
194 break;
195 }
196 }
197 }
198
199 if (snapy == NULL) {
200 /* vertical snapping */
201 if (l < tr && r > tl) {
202 switch (corn) {
203 case Corner_TopLeft:
204 case Corner_TopRight:
205 dlt = t;
206 drb = b + *h - c->frame->area.height;
207 if (b < tt && drb >= tt && drb < tt + resist.integer)
208 *h = tt - t, snapy = target;
209 break;
210 case Corner_BottomLeft:
211 case Corner_BottomRight:
212 dlt = t - *h + c->frame->area.height;
213 drb = b;
214 if (t > tb && dlt <= tb && dlt > tb - resist.integer)
215 *h = b - tb, snapy = target;
216 break;
217 }
218 }
219 }
220
221 /* snapped both ways */
222 if (snapx && snapy) break;
223 }
224 }
225
226 /* snap to screen edges */
227
228 /* horizontal snapping */
229 switch (corn) {
230 case Corner_TopLeft:
231 case Corner_BottomLeft:
232 dlt = l;
233 drb = r + *w - c->frame->area.width;
234 if (r <= ar && drb > ar && drb <= ar + resist.integer)
235 *w = ar - l + 1;
236 break;
237 case Corner_TopRight:
238 case Corner_BottomRight:
239 dlt = l - *w + c->frame->area.width;
240 drb = r;
241 if (l >= al && dlt < al && dlt >= al - resist.integer)
242 *w = r - al + 1;
243 break;
244 }
245
246 /* vertical snapping */
247 switch (corn) {
248 case Corner_TopLeft:
249 case Corner_TopRight:
250 dlt = t;
251 drb = b + *h - c->frame->area.height;
252 if (b <= ab && drb > ab && drb <= ab + resist.integer)
253 *h = ab - t + 1;
254 break;
255 case Corner_BottomLeft:
256 case Corner_BottomRight:
257 dlt = t - *h + c->frame->area.height;
258 drb = b;
259 if (t >= at && dlt < at && dlt >= at - resist.integer)
260 *h = b - at + 1;
261 break;
262 }
263
264 /* round down for size increments */
265 *w -= c->frame->area.width + c->frame->size.left + c->frame->size.right;
266 *w = *w / c->size_inc.width * c->size_inc.width;
267 *w += c->frame->area.width + c->frame->size.left + c->frame->size.right;
268
269 *h -= c->frame->area.height + c->frame->size.top + c->frame->size.bottom;
270 *h = *h / c->size_inc.height * c->size_inc.height;
271 *h += c->frame->area.height + c->frame->size.top + c->frame->size.bottom;
272 }
273
274 static void event(ObEvent *e, void *foo)
275 {
276 if (e->type == Event_Client_Moving)
277 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
278 else if (e->type == Event_Client_Resizing)
279 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
280 e->data.c.num[2]);
281 }
282
283 void plugin_startup()
284 {
285 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
286 (EventHandler)event, NULL);
287 }
288
289 void plugin_shutdown()
290 {
291 dispatch_register(0, (EventHandler)event, NULL);
292 }
This page took 0.048216 seconds and 4 git commands to generate.