xinerama support like crazy for struts and everything else too. this probably crashes...
[dana/openbox.git] / openbox / resist.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    resist.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "client.h"
21 #include "frame.h"
22 #include "stacking.h"
23 #include "screen.h"
24 #include "config.h"
25 #include "parser/parse.h"
26
27 #include <glib.h>
28
29 void resist_move_windows(ObClient *c, gint resist, gint *x, gint *y)
30 {
31     GList *it;
32     gint l, t, r, b; /* requested edges */
33     gint cl, ct, cr, cb; /* current edges */
34     gint w, h; /* current size */
35     ObClient *snapx = NULL, *snapy = NULL;
36
37     if (!resist) return;
38
39     frame_client_gravity(c->frame, x, y, c->area.width, c->area.height);
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 = RECT_LEFT(c->frame->area);
50     ct = RECT_TOP(c->frame->area);
51     cr = RECT_RIGHT(c->frame->area);
52     cb = RECT_BOTTOM(c->frame->area);
53     
54     for (it = stacking_list; it; it = g_list_next(it)) {
55         ObClient *target;
56         gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
57
58         if (!WINDOW_IS_CLIENT(it->data))
59             continue;
60         target = it->data;
61
62         /* don't snap to self or non-visibles */
63         if (!target->frame->visible || target == c) continue; 
64
65         tl = RECT_LEFT(target->frame->area) - 1;
66         tt = RECT_TOP(target->frame->area) - 1;
67         tr = RECT_RIGHT(target->frame->area) + 1;
68         tb = RECT_BOTTOM(target->frame->area) + 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 - resist)
77                     *x = tr, snapx = target;
78                 else if (cr <= tl && r > tl &&
79                          r <= tl + resist)
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 &&
84                         t > tt - resist)
85                         *y = tt + 1, snapy = target;
86                     else if (cb < tb && b >= tb &&
87                              b < tb + resist)
88                         *y = tb - h, snapy = target;
89                 }
90             }
91         }
92         if (snapy == NULL) {
93             if (cl < tr && cr > tl) {
94                 if (ct >= tb && t < tb && t >= tb - resist)
95                     *y = tb, snapy = target;
96                 else if (cb <= tt && b > tt &&
97                          b <= tt + resist)
98                     *y = tt - h + 1, snapy = target;
99                 if (snapy != NULL) {
100                     /* try to corner snap to the window */
101                     if (cl > tl && l <= tl &&
102                         l > tl - resist)
103                         *x = tl + 1, snapx = target;
104                     else if (cr < tr && r >= tr &&
105                              r < tr + resist)
106                         *x = tr - w, snapx = target;
107                 }
108             }
109         }
110
111         if (snapx && snapy) break;
112     }
113
114     frame_frame_gravity(c->frame, x, y, c->area.width, c->area.height);
115 }
116
117 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
118 {
119     Rect *area, *parea;
120     guint i;
121     gint l, t, r, b; /* requested edges */
122     gint al, at, ar, ab; /* screen area edges */
123     gint pl, pt, pr, pb; /* physical screen area edges */
124     gint cl, ct, cr, cb; /* current edges */
125     gint w, h; /* current size */
126     Rect desired_area;
127
128     if (!resist) return;
129
130     frame_client_gravity(c->frame, x, y, c->area.width, c->area.height);
131
132     w = c->frame->area.width;
133     h = c->frame->area.height;
134
135     l = *x;
136     t = *y;
137     r = l + w - 1;
138     b = t + h - 1;
139
140     cl = RECT_LEFT(c->frame->area);
141     ct = RECT_TOP(c->frame->area);
142     cr = RECT_RIGHT(c->frame->area);
143     cb = RECT_BOTTOM(c->frame->area);
144
145     RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);
146     
147     for (i = 0; i < screen_num_monitors; ++i) {
148         parea = screen_physical_area_monitor(i);
149
150         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
151             g_free(parea);
152             continue;
153         }
154
155         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
156                            &desired_area);
157
158         al = RECT_LEFT(*area);
159         at = RECT_TOP(*area);
160         ar = RECT_RIGHT(*area);
161         ab = RECT_BOTTOM(*area);
162         pl = RECT_LEFT(*parea);
163         pt = RECT_TOP(*parea);
164         pr = RECT_RIGHT(*parea);
165         pb = RECT_BOTTOM(*parea);
166
167         if (cl >= al && l < al && l >= al - resist)
168             *x = al;
169         else if (cr <= ar && r > ar && r <= ar + resist)
170             *x = ar - w + 1;
171         else if (cl >= pl && l < pl && l >= pl - resist)
172             *x = pl;
173         else if (cr <= pr && r > pr && r <= pr + resist)
174             *x = pr - w + 1;
175
176         if (ct >= at && t < at && t >= at - resist)
177             *y = at;
178         else if (cb <= ab && b > ab && b < ab + resist)
179             *y = ab - h + 1;
180         else if (ct >= pt && t < pt && t >= pt - resist)
181             *y = pt;
182         else if (cb <= pb && b > pb && b < pb + resist)
183             *y = pb - h + 1;
184
185         g_free(area);
186         g_free(parea);
187     }
188
189     frame_frame_gravity(c->frame, x, y, c->area.width, c->area.height);
190 }
191
192 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
193                          ObCorner corn)
194 {
195     GList *it;
196     ObClient *target; /* target */
197     gint l, t, r, b; /* my left, top, right and bottom sides */
198     gint dlt, drb; /* my destination left/top and right/bottom sides */
199     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
200     gint incw, inch;
201     ObClient *snapx = NULL, *snapy = NULL;
202
203     if (!resist) return;
204
205     incw = c->size_inc.width;
206     inch = c->size_inc.height;
207
208     l = RECT_LEFT(c->frame->area);
209     r = RECT_RIGHT(c->frame->area);
210     t = RECT_TOP(c->frame->area);
211     b = RECT_BOTTOM(c->frame->area);
212
213     for (it = stacking_list; it; it = g_list_next(it)) {
214         if (!WINDOW_IS_CLIENT(it->data))
215             continue;
216         target = it->data;
217
218         /* don't snap to invisibles or ourself */
219         if (!target->frame->visible || target == c) continue; 
220
221         tl = RECT_LEFT(target->frame->area);
222         tr = RECT_RIGHT(target->frame->area);
223         tt = RECT_TOP(target->frame->area);
224         tb = RECT_BOTTOM(target->frame->area);
225
226         if (snapx == NULL) {
227             /* horizontal snapping */
228             if (t < tb && b > tt) {
229                 switch (corn) {
230                 case OB_CORNER_TOPLEFT:
231                 case OB_CORNER_BOTTOMLEFT:
232                     dlt = l;
233                     drb = r + *w - c->frame->area.width;
234                     if (r < tl && drb >= tl &&
235                         drb < tl + resist)
236                         *w = tl - l, snapx = target;
237                     break;
238                 case OB_CORNER_TOPRIGHT:
239                 case OB_CORNER_BOTTOMRIGHT:
240                     dlt = l - *w + c->frame->area.width;
241                     drb = r;
242                     if (l > tr && dlt <= tr &&
243                         dlt > tr - resist)
244                         *w = r - tr, snapx = target;
245                     break;
246                 }
247             }
248         }
249
250         if (snapy == NULL) {
251             /* vertical snapping */
252             if (l < tr && r > tl) {
253                 switch (corn) {
254                 case OB_CORNER_TOPLEFT:
255                 case OB_CORNER_TOPRIGHT:
256                     dlt = t;
257                     drb = b + *h - c->frame->area.height;
258                     if (b < tt && drb >= tt &&
259                         drb < tt + resist)
260                         *h = tt - t, snapy = target;
261                     break;
262                 case OB_CORNER_BOTTOMLEFT:
263                 case OB_CORNER_BOTTOMRIGHT:
264                     dlt = t - *h + c->frame->area.height;
265                     drb = b;
266                     if (t > tb && dlt <= tb &&
267                         dlt > tb - resist)
268                         *h = b - tb, snapy = target;
269                     break;
270                 }
271             }
272         }
273
274         /* snapped both ways */
275         if (snapx && snapy) break;
276     }
277 }
278
279 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
280                           ObCorner corn)
281 {
282     gint l, t, r, b; /* my left, top, right and bottom sides */
283     gint dlt, drb; /* my destination left/top and right/bottom sides */
284     Rect *area, *parea;
285     gint al, at, ar, ab; /* screen boundaries */ 
286     gint pl, pt, pr, pb; /* physical screen boundaries */
287     gint incw, inch;
288     guint i;
289     Rect desired_area;
290
291     if (!resist) return;
292
293     l = RECT_LEFT(c->frame->area);
294     r = RECT_RIGHT(c->frame->area);
295     t = RECT_TOP(c->frame->area);
296     b = RECT_BOTTOM(c->frame->area);
297
298     incw = c->size_inc.width;
299     inch = c->size_inc.height;
300
301     RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
302
303     for (i = 0; i < screen_num_monitors; ++i) {
304         parea = screen_physical_area_monitor(i);
305
306         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
307             g_free(parea);
308             continue;
309         }
310
311         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
312                            &desired_area);
313
314         /* get the screen boundaries */
315         al = RECT_LEFT(*area);
316         at = RECT_TOP(*area);
317         ar = RECT_RIGHT(*area);
318         ab = RECT_BOTTOM(*area);
319         pl = RECT_LEFT(*parea);
320         pt = RECT_TOP(*parea);
321         pr = RECT_RIGHT(*parea);
322         pb = RECT_BOTTOM(*parea);
323
324         /* horizontal snapping */
325         switch (corn) {
326         case OB_CORNER_TOPLEFT:
327         case OB_CORNER_BOTTOMLEFT:
328             dlt = l;
329             drb = r + *w - c->frame->area.width;
330             if (r <= ar && drb > ar && drb <= ar + resist)
331                 *w = ar - l + 1;
332             else if (r <= pr && drb > pr && drb <= pr + resist)
333                 *w = pr - l + 1;
334             break;
335         case OB_CORNER_TOPRIGHT:
336         case OB_CORNER_BOTTOMRIGHT:
337             dlt = l - *w + c->frame->area.width;
338             drb = r;
339             if (l >= al && dlt < al && dlt >= al - resist)
340                 *w = r - al + 1;
341             else if (l >= pl && dlt < pl && dlt >= pl - resist)
342                 *w = r - pl + 1;
343             break;
344         }
345
346         /* vertical snapping */
347         switch (corn) {
348         case OB_CORNER_TOPLEFT:
349         case OB_CORNER_TOPRIGHT:
350             dlt = t;
351             drb = b + *h - c->frame->area.height;
352             if (b <= ab && drb > ab && drb <= ab + resist)
353                 *h = ab - t + 1;
354             else if (b <= pb && drb > pb && drb <= pb + resist)
355                 *h = pb - t + 1;
356             break;
357         case OB_CORNER_BOTTOMLEFT:
358         case OB_CORNER_BOTTOMRIGHT:
359             dlt = t - *h + c->frame->area.height;
360             drb = b;
361             if (t >= at && dlt < at && dlt >= at - resist)
362                 *h = b - at + 1;
363             else if (t >= pt && dlt < pt && dlt >= pt - resist)
364                 *h = b - pt + 1;
365             break;
366         }
367
368         g_free(area);
369         g_free(parea);
370     }
371 }