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