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