]> icculus.org git repositories - dana/openbox.git/blob - plugins/resistance.c
put it back
[dana/openbox.git] / 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     l = c->frame->area.x;
145     r = l + c->frame->area.width - 1;
146     t = c->frame->area.y;
147     b = t + c->frame->area.height - 1;
148
149     /* get the screen boundaries */
150     area = screen_area(c->desktop);
151     al = area->x;
152     at = area->y;
153     ar = al + area->width - 1;
154     ab = at + area->height - 1;
155
156     /* snap to other windows */
157     if (resist_windows) {
158         for (it = stacking_list; it != NULL; it = it->next) {
159             target = it->data;
160
161             /* don't snap to invisibles or ourself */
162             if (!target->frame->visible || target == c) continue;
163
164             tl = target->frame->area.x;
165             tr = target->frame->area.x + target->frame->area.width - 1;
166             tt = target->frame->area.y;
167             tb = target->frame->area.y + target->frame->area.height - 1;
168
169             if (snapx == NULL) {
170                 /* horizontal snapping */
171                 if (t < tb && b > tt) {
172                     switch (corn) {
173                     case Corner_TopLeft:
174                     case Corner_BottomLeft:
175                         dlt = l;
176                         drb = r + *w - c->frame->area.width;
177                         if (r < tl && drb >= tl && drb < tl + resistance)
178                             *w = tl - l, snapx = target;
179                         break;
180                     case Corner_TopRight:
181                     case Corner_BottomRight:
182                         dlt = l - *w + c->frame->area.width;
183                         drb = r;
184                         if (l > tr && dlt <= tr && dlt > tr - resistance)
185                             *w = r - tr, snapx = target;
186                         break;
187                     }
188                 }
189             }
190
191             if (snapy == NULL) {
192                 /* vertical snapping */
193                 if (l < tr && r > tl) {
194                     switch (corn) {
195                     case Corner_TopLeft:
196                     case Corner_TopRight:
197                         dlt = t;
198                         drb = b + *h - c->frame->area.height;
199                         if (b < tt && drb >= tt && drb < tt + resistance)
200                             *h = tt - t, snapy = target;
201                         break;
202                     case Corner_BottomLeft:
203                     case Corner_BottomRight:
204                         dlt = t - *h + c->frame->area.height;
205                         drb = b;
206                         if (t > tb && dlt <= tb && dlt > tb - resistance)
207                             *h = b - tb, snapy = target;
208                         break;
209                     }
210                 }
211             }
212
213             /* snapped both ways */
214             if (snapx && snapy) break;
215         }
216     }
217
218     /* snap to screen edges */
219     
220     /* horizontal snapping */
221     switch (corn) {
222     case Corner_TopLeft:
223     case Corner_BottomLeft:
224         dlt = l;
225         drb = r + *w - c->frame->area.width;
226         if (r <= ar && drb > ar && drb <= ar + resistance)
227             *w = ar - l + 1;
228         break;
229     case Corner_TopRight:
230     case Corner_BottomRight:
231         dlt = l - *w + c->frame->area.width;
232         drb = r;
233         if (l >= al && dlt < al && dlt >= al - resistance)
234             *w = r - al + 1;
235         break;
236     }
237
238     /* vertical snapping */
239     switch (corn) {
240     case Corner_TopLeft:
241     case Corner_TopRight:
242         dlt = t;
243         drb = b + *h - c->frame->area.height;
244         if (b <= ab && drb > ab && drb <= ab + resistance)
245             *h = ab - t + 1;
246         break;
247     case Corner_BottomLeft:
248     case Corner_BottomRight:
249         dlt = t - *h + c->frame->area.height;
250         drb = b;
251         if (t >= at && dlt < at && dlt >= at - resistance)
252             *h = b - at + 1;
253         break;
254     }
255
256     /* round down for size increments */
257     *w -= c->frame->area.width + c->frame->size.left + c->frame->size.right;
258     *w = *w / c->size_inc.width * c->size_inc.width;
259     *w += c->frame->area.width + c->frame->size.left + c->frame->size.right;
260
261     *h -= c->frame->area.height + c->frame->size.top + c->frame->size.bottom;
262     *h = *h / c->size_inc.height * c->size_inc.height;
263     *h += c->frame->area.height + c->frame->size.top + c->frame->size.bottom;
264 }
265
266 static void event(ObEvent *e, void *foo)
267 {
268     if (e->type == Event_Client_Moving)
269         resist_move(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
270     else if (e->type == Event_Client_Resizing)
271         resist_size(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1],
272                     e->data.c.num[2]);
273 }
274
275 void plugin_startup()
276 {
277     dispatch_register(Event_Client_Moving | Event_Client_Resizing,
278                       (EventHandler)event, NULL);
279 }
280
281 void plugin_shutdown()
282 {
283     dispatch_register(0, (EventHandler)event, NULL);
284 }