]> Dogcows Code - chaz/openbox/blob - plugins/resistance.c
tyop
[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 target = it->data;
69 /* don't snap to self or non-visibles */
70 if (!target->frame->visible || target == c) continue;
71
72 tl = target->frame->area.x - 1;
73 tt = target->frame->area.y - 1;
74 tr = tl + target->frame->area.width + 1;
75 tb = tt + target->frame->area.height + 1;
76
77 /* snapx and snapy ensure that the window snaps to the top-most
78 window edge available, without going all the way from
79 bottom-to-top in the stacking list
80 */
81 if (snapx == NULL) {
82 if (ct < tb && cb > tt) {
83 if (cl >= tr && l < tr && l >= tr - resistance)
84 *x = tr, snapx = target;
85 else if (cr <= tl && r > tl && r <= tl + resistance)
86 *x = tl - w + 1, snapx = target;
87 if (snapx != NULL) {
88 /* try to corner snap to the window */
89 if (ct > tt && t <= tt && t > tt - resistance)
90 *y = tt + 1, snapy = target;
91 else if (cb < tb && b >= tb && b < tb + resistance)
92 *y = tb - h, snapy = target;
93 }
94 }
95 }
96 if (snapy == NULL) {
97 if (cl < tr && cr > tl) {
98 if (ct >= tb && t < tb && t >= tb - resistance)
99 *y = tb, snapy = target;
100 else if (cb <= tt && b > tt && b <= tt + resistance)
101 *y = tt - h + 1, snapy = target;
102 if (snapy != NULL) {
103 /* try to corner snap to the window */
104 if (cl > tl && l <= tl && l > tl - resistance)
105 *x = tl + 1, snapx = target;
106 else if (cr < tr && r >= tr && r < tr + resistance)
107 *x = tr - w, snapx = target;
108 }
109 }
110 }
111
112 if (snapx && snapy) break;
113 }
114
115 /* get the screen boundaries */
116 area = screen_area(c->desktop);
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 static void resist_size(Client *c, int *w, int *h, Corner corn)
134 {
135 GList *it;
136 Client *target; /* target */
137 int l, t, r, b; /* my left, top, right and bottom sides */
138 int dlt, drb; /* my destination left/top and right/bottom sides */
139 int tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides */
140 Rect *area;
141 int al, at, ar, ab; /* screen boundaries */
142 Client *snapx = NULL, *snapy = NULL;
143
144 /* don't snap windows with size increments */
145 if (c->size_inc.width > 1 || c->size_inc.height > 1)
146 return;
147
148 l = c->frame->area.x;
149 r = l + c->frame->area.width - 1;
150 t = c->frame->area.y;
151 b = t + c->frame->area.height - 1;
152
153 /* get the screen boundaries */
154 area = screen_area(c->desktop);
155 al = area->x;
156 at = area->y;
157 ar = al + area->width - 1;
158 ab = at + area->height - 1;
159
160 /* snap to other windows */
161 if (resist_windows) {
162 for (it = stacking_list; it != NULL; it = it->next) {
163 target = it->data;
164
165 /* don't snap to invisibles or ourself */
166 if (!target->frame->visible || target == c) continue;
167
168 tl = target->frame->area.x;
169 tr = target->frame->area.x + target->frame->area.width - 1;
170 tt = target->frame->area.y;
171 tb = target->frame->area.y + target->frame->area.height - 1;
172
173 if (snapx == NULL) {
174 /* horizontal snapping */
175 if (t < tb && b > tt) {
176 switch (corn) {
177 case Corner_TopLeft:
178 case Corner_BottomLeft:
179 dlt = l;
180 drb = r + *w - c->frame->area.width;
181 if (r < tl && drb >= tl && drb < tl + resistance)
182 *w = tl - l, snapx = target;
183 break;
184 case Corner_TopRight:
185 case Corner_BottomRight:
186 dlt = l - *w + c->frame->area.width;
187 drb = r;
188 if (l > tr && dlt <= tr && dlt > tr - resistance)
189 *w = r - tr, snapx = target;
190 break;
191 }
192 }
193 }
194
195 if (snapy == NULL) {
196 /* vertical snapping */
197 if (l < tr && r > tl) {
198 switch (corn) {
199 case Corner_TopLeft:
200 case Corner_TopRight:
201 dlt = t;
202 drb = b + *h - c->frame->area.height;
203 if (b < tt && drb >= tt && drb < tt + resistance)
204 *h = tt - t, snapy = target;
205 break;
206 case Corner_BottomLeft:
207 case Corner_BottomRight:
208 dlt = t - *h + c->frame->area.height;
209 drb = b;
210 if (t > tb && dlt <= tb && dlt > tb - resistance)
211 *h = b - tb, snapy = target;
212 break;
213 }
214 }
215 }
216
217 /* snapped both ways */
218 if (snapx && snapy) break;
219 }
220 }
221
222 /* snap to screen edges */
223
224 /* horizontal snapping */
225 switch (corn) {
226 case Corner_TopLeft:
227 case Corner_BottomLeft:
228 dlt = l;
229 drb = r + *w - c->frame->area.width;
230 if (r <= ar && drb > ar && drb <= ar + resistance)
231 *w = ar - l + 1;
232 break;
233 case Corner_TopRight:
234 case Corner_BottomRight:
235 dlt = l - *w + c->frame->area.width;
236 drb = r;
237 if (l >= al && dlt < al && dlt >= al - resistance)
238 *w = r - al + 1;
239 break;
240 }
241
242 /* vertical snapping */
243 switch (corn) {
244 case Corner_TopLeft:
245 case Corner_TopRight:
246 dlt = t;
247 drb = b + *h - c->frame->area.height;
248 if (b <= ab && drb > ab && drb <= ab + resistance)
249 *h = ab - t + 1;
250 break;
251 case Corner_BottomLeft:
252 case Corner_BottomRight:
253 dlt = t - *h + c->frame->area.height;
254 drb = b;
255 if (t >= at && dlt < at && dlt >= at - resistance)
256 *h = b - at + 1;
257 break;
258 }
259 }
260
261 static void event(ObEvent *e, void *foo)
262 {
263 if (e->type == Event_Client_Moving)
264 resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
265 else if (e->type == Event_Client_Resizing)
266 resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
267 e->data.c.num[2]);
268 }
269
270 void plugin_startup()
271 {
272 dispatch_register(Event_Client_Moving | Event_Client_Resizing,
273 (EventHandler)event, NULL);
274 }
275
276 void plugin_shutdown()
277 {
278 dispatch_register(0, (EventHandler)event, NULL);
279 }
This page took 0.045059 seconds and 4 git commands to generate.