]> Dogcows Code - chaz/openbox/blob - plugins/resistance.c
bafd362c0ec8c62a5cf77a204898d0ff6874b1de
[chaz/openbox] / plugins / resistance.c
1 #include "kernel/dispatch.h"
2 #include "kernel/client.h"
3 #include "kernel/frame.h"
4 #include "kernel/parse.h"
5 #include "kernel/stacking.h"
6 #include "kernel/screen.h"
7 #include <glib.h>
8
9 static int resistance;
10 static gboolean resist_windows;
11
12 static void parse_assign(char *name, ParseToken *value)
13 {
14 if (!g_ascii_strcasecmp(name, "strength")) {
15 if (value->type != TOKEN_INTEGER)
16 yyerror("invalid value");
17 else {
18 if (value->data.integer >= 0)
19 resistance = value->data.integer;
20 }
21 } else if (!g_ascii_strcasecmp(name, "windows")) {
22 if (value->type != TOKEN_BOOL)
23 yyerror("invalid value");
24 else
25 resist_windows = value->data.bool;
26 } else
27 yyerror("invalid option");
28 parse_free_token(value);
29 }
30
31 void plugin_setup_config()
32 {
33 resistance = 10;
34 resist_windows = TRUE;
35
36 parse_reg_section("resistance", NULL, parse_assign);
37 }
38
39 static void resist_move(Client *c, int *x, int *y)
40 {
41 GList *it;
42 Rect *area;
43 int l, t, r, b; /* requested edges */
44 int al, at, ar, ab; /* screen area edges */
45 int cl, ct, cr, cb; /* current edges */
46 int w, h; /* current size */
47 Client *snapx = NULL, *snapy = NULL;
48
49 w = c->frame->area.width;
50 h = c->frame->area.height;
51
52 l = *x;
53 t = *y;
54 r = l + w - 1;
55 b = t + h - 1;
56
57 cl = c->frame->area.x;
58 ct = c->frame->area.y;
59 cr = cl + c->frame->area.width - 1;
60 cb = ct + c->frame->area.height - 1;
61
62 /* snap to other clients */
63 if (resist_windows)
64 for (it = stacking_list; it != NULL; it = it->next) {
65 Client *target;
66 int tl, tt, tr, tb; /* 1 past the target's edges on each side */
67
68 if (!WINDOW_IS_CLIENT(it->data))
69 continue;
70 target = it->data;
71 /* don't snap to self or non-visibles */
72 if (!target->frame->visible || target == c) continue;
73
74 tl = target->frame->area.x - 1;
75 tt = target->frame->area.y - 1;
76 tr = tl + target->frame->area.width + 1;
77 tb = tt + target->frame->area.height + 1;
78
79 /* snapx and snapy ensure that the window snaps to the top-most
80 window edge available, without going all the way from
81 bottom-to-top in the stacking list
82 */
83 if (snapx == NULL) {
84 if (ct < tb && cb > tt) {
85 if (cl >= tr && l < tr && l >= tr - resistance)
86 *x = tr, snapx = target;
87 else if (cr <= tl && r > tl && r <= tl + resistance)
88 *x = tl - w + 1, snapx = target;
89 if (snapx != NULL) {
90 /* try to corner snap to the window */
91 if (ct > tt && t <= tt && t > tt - resistance)
92 *y = tt + 1, snapy = target;
93 else if (cb < tb && b >= tb && b < tb + resistance)
94 *y = tb - h, snapy = target;
95 }
96 }
97 }
98 if (snapy == NULL) {
99 if (cl < tr && cr > tl) {
100 if (ct >= tb && t < tb && t >= tb - resistance)
101 *y = tb, snapy = target;
102 else if (cb <= tt && b > tt && b <= tt + resistance)
103 *y = tt - h + 1, snapy = target;
104 if (snapy != NULL) {
105 /* try to corner snap to the window */
106 if (cl > tl && l <= tl && l > tl - resistance)
107 *x = tl + 1, snapx = target;
108 else if (cr < tr && r >= tr && r < tr + resistance)
109 *x = tr - w, snapx = target;
110 }
111 }
112 }
113
114 if (snapx && snapy) break;
115 }
116
117 /* get the screen boundaries */
118 area = screen_area(c->desktop);
119 al = area->x;
120 at = area->y;
121 ar = al + area->width - 1;
122 ab = at + area->height - 1;
123
124 /* snap to screen edges */
125 if (cl >= al && l < al && l >= al - resistance)
126 *x = al;
127 else if (cr <= ar && r > ar && r <= ar + resistance)
128 *x = ar - w + 1;
129 if (ct >= at && t < at && t >= at - resistance)
130 *y = at;
131 else if (cb <= ab && b > ab && b < ab + resistance)
132 *y = ab - h + 1;
133 }
134
135 static void resist_size(Client *c, int *w, int *h, Corner corn)
136 {
137 GList *it;
138 Client *target; /* target */
139 int l, t, r, b; /* my left, top, right and bottom sides */
140 int dlt, drb; /* my destination left/top and right/bottom sides */
141 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
142 Rect *area;
143 int al, at, ar, ab; /* screen boundaries */
144 Client *snapx = NULL, *snapy = NULL;
145
146 /* don't snap windows with size increments */
147 if (c->size_inc.width > 1 || c->size_inc.height > 1)
148 return;
149
150 l = c->frame->area.x;
151 r = l + c->frame->area.width - 1;
152 t = c->frame->area.y;
153 b = t + c->frame->area.height - 1;
154
155 /* get the screen boundaries */
156 area = screen_area(c->desktop);
157 al = area->x;
158 at = area->y;
159 ar = al + area->width - 1;
160 ab = at + area->height - 1;
161
162 /* snap to other windows */
163 if (resist_windows) {
164 for (it = stacking_list; it != NULL; it = it->next) {
165 if (!WINDOW_IS_CLIENT(it->data))
166 continue;
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 + resistance)
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 - resistance)
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 + resistance)
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 - resistance)
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 + resistance)
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 - resistance)
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 + resistance)
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 - resistance)
260 *h = b - at + 1;
261 break;
262 }
263 }
264
265 static void event(ObEvent *e, void *foo)
266 {
267 if (e->type == Event_Client_Moving)
268 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
269 else if (e->type == Event_Client_Resizing)
270 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
271 e->data.c.num[2]);
272 }
273
274 void plugin_startup()
275 {
276 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
277 (EventHandler)event, NULL);
278 }
279
280 void plugin_shutdown()
281 {
282 dispatch_register(0, (EventHandler)event, NULL);
283 }
This page took 0.048891 seconds and 3 git commands to generate.