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