]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/resist.c
Merge branch 'work' into wip/mikabox
[mikachu/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 "resist.h"
21 #include "client.h"
22 #include "frame.h"
23 #include "stacking.h"
24 #include "screen.h"
25 #include "dock.h"
26 #include "config.h"
27
28 #include <glib.h>
29
30 static gboolean resist_move_window(Rect window,
31                                    Rect target, gint resist,
32                                    gint *x, gint *y)
33 {
34     gint l, t, r, b; /* requested edges */
35     gint cl, ct, cr, cb; /* current edges */
36     gint w, h; /* current size */
37     gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
38     gboolean snapx = 0, snapy = 0;
39
40     w = window.width;
41     h = window.height;
42
43     l = *x;
44     t = *y;
45     r = l + w - 1;
46     b = t + h - 1;
47
48     cl = RECT_LEFT(window);
49     ct = RECT_TOP(window);
50     cr = RECT_RIGHT(window);
51     cb = RECT_BOTTOM(window);
52
53     tl = RECT_LEFT(target) - 1;
54     tt = RECT_TOP(target) - 1;
55     tr = RECT_RIGHT(target) + 1;
56     tb = RECT_BOTTOM(target) + 1;
57
58     /* snapx and snapy ensure that the window snaps to the top-most
59        window edge available, without going all the way from
60        bottom-to-top in the stacking list
61     */
62     if (!snapx) {
63         if (ct < tb && cb > tt) {
64             if (cl >= tr && l < tr && l >= tr - resist)
65                 *x = tr, snapx = TRUE;
66             else if (cr <= tl && r > tl &&
67                      r <= tl + resist)
68                 *x = tl - w + 1, snapx = TRUE;
69             if (snapx) {
70                 /* try to corner snap to the window */
71                 if (ct > tt && t <= tt &&
72                     t > tt - resist)
73                     *y = tt + 1, snapy = TRUE;
74                 else if (cb < tb && b >= tb &&
75                          b < tb + resist)
76                     *y = tb - h, snapy = TRUE;
77             }
78         }
79     }
80     if (!snapy) {
81         if (cl < tr && cr > tl) {
82             if (ct >= tb && t < tb && t >= tb - resist)
83                 *y = tb, snapy = TRUE;
84             else if (cb <= tt && b > tt &&
85                      b <= tt + resist)
86                 *y = tt - h + 1, snapy = TRUE;
87             if (snapy) {
88                 /* try to corner snap to the window */
89                 if (cl > tl && l <= tl &&
90                     l > tl - resist)
91                     *x = tl + 1, snapx = TRUE;
92                 else if (cr < tr && r >= tr &&
93                          r < tr + resist)
94                     *x = tr - w, snapx = TRUE;
95             }
96         }
97     }
98
99     return snapx && snapy;
100 }
101
102 void resist_move_windows(ObClient *c, gint resist, gint *x, gint *y)
103 {
104     GList *it;
105     Rect dock_area;
106     Rect expand;
107
108     if (!resist) return;
109
110     frame_client_gravity(c->frame, x, y);
111
112     for (it = stacking_list; it; it = g_list_next(it)) {
113         ObClient *target;
114
115         if (!WINDOW_IS_CLIENT(it->data))
116             continue;
117         target = it->data;
118
119         /* don't snap to self or non-visibles */
120         if (!target->frame->visible || target == c)
121             continue;
122
123         /* check window boundary as it is */
124         if (resist_move_window(c->frame->area, target->frame->area,
125                                resist, x, y))
126             break;
127
128         /* now check window with the expanded margin area */
129         if (config_window_margin == 0)
130             continue;
131
132         RECT_SET(expand, target->frame->area.x - config_window_margin, 
133                          target->frame->area.y - config_window_margin, 
134                          target->frame->area.width + 2 * config_window_margin, 
135                          target->frame->area.height + 2 * config_window_margin);
136
137         if (resist_move_window(c->frame->area, expand,
138                                resist, x, y))
139             break;
140     }
141     dock_get_area(&dock_area);
142     resist_move_window(c->frame->area, dock_area, resist, x, y);
143
144     frame_frame_gravity(c->frame, x, y);
145 }
146
147 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
148 {
149     Rect *area, *parea;
150     guint i;
151     gint l, t, r, b; /* requested edges */
152     gint al, at, ar, ab; /* screen area edges */
153     gint pl, pt, pr, pb; /* physical screen area edges */
154     gint cl, ct, cr, cb; /* current edges */
155     gint w, h; /* current size */
156     Rect desired_area;
157
158     if (!resist) return;
159
160     frame_client_gravity(c->frame, x, y);
161
162     w = c->frame->area.width;
163     h = c->frame->area.height;
164
165     l = *x;
166     t = *y;
167     r = l + w - 1;
168     b = t + h - 1;
169
170     cl = RECT_LEFT(c->frame->area);
171     ct = RECT_TOP(c->frame->area);
172     cr = RECT_RIGHT(c->frame->area);
173     cb = RECT_BOTTOM(c->frame->area);
174
175     RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);
176
177     for (i = 0; i < screen_num_monitors; ++i) {
178         parea = screen_physical_area_monitor(i);
179
180         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
181             g_free(parea);
182             continue;
183         }
184
185         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
186                            &desired_area);
187
188         al = RECT_LEFT(*area);
189         at = RECT_TOP(*area);
190         ar = RECT_RIGHT(*area);
191         ab = RECT_BOTTOM(*area);
192         pl = RECT_LEFT(*parea);
193         pt = RECT_TOP(*parea);
194         pr = RECT_RIGHT(*parea);
195         pb = RECT_BOTTOM(*parea);
196
197         if (cl >= al && l < al && l >= al - resist)
198             *x = al;
199         else if (cr <= ar && r > ar && r <= ar + resist)
200             *x = ar - w + 1;
201         else if (cl >= pl && l < pl && l >= pl - resist)
202             *x = pl;
203         else if (cr <= pr && r > pr && r <= pr + resist)
204             *x = pr - w + 1;
205
206         if (ct >= at && t < at && t >= at - resist)
207             *y = at;
208         else if (cb <= ab && b > ab && b < ab + resist)
209             *y = ab - h + 1;
210         else if (ct >= pt && t < pt && t >= pt - resist)
211             *y = pt;
212         else if (cb <= pb && b > pb && b < pb + resist)
213             *y = pb - h + 1;
214
215         g_free(area);
216         g_free(parea);
217     }
218
219     frame_frame_gravity(c->frame, x, y);
220 }
221
222 static gboolean resist_size_window(Rect window, Rect target, gint resist,
223                                    gint *w, gint *h, ObDirection dir)
224 {
225     gint l, t, r, b; /* my left, top, right and bottom sides */
226     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
227     gint dlt, drb; /* my destination left/top and right/bottom sides */
228     gboolean snapx = 0, snapy = 0;
229     gint orgw, orgh;
230
231     l = RECT_LEFT(window);
232     t = RECT_TOP(window);
233     r = RECT_RIGHT(window);
234     b = RECT_BOTTOM(window);
235
236     orgw = window.width;
237     orgh = window.height;
238
239     tl = RECT_LEFT(target);
240     tt = RECT_TOP(target);
241     tr = RECT_RIGHT(target);
242     tb = RECT_BOTTOM(target);
243
244     if (!snapx) {
245         /* horizontal snapping */
246         if (t < tb && b > tt) {
247             switch (dir) {
248             case OB_DIRECTION_EAST:
249             case OB_DIRECTION_NORTHEAST:
250             case OB_DIRECTION_SOUTHEAST:
251             case OB_DIRECTION_NORTH:
252             case OB_DIRECTION_SOUTH:
253                 dlt = l;
254                 drb = r + *w - orgw;
255                 if (r < tl && drb >= tl &&
256                     drb < tl + resist)
257                     *w = tl - l, snapx = TRUE;
258                 break;
259             case OB_DIRECTION_WEST:
260             case OB_DIRECTION_NORTHWEST:
261             case OB_DIRECTION_SOUTHWEST:
262                 dlt = l - *w + orgw;
263                 drb = r;
264                 if (l > tr && dlt <= tr &&
265                     dlt > tr - resist)
266                     *w = r - tr, snapx = TRUE;
267                 break;
268             }
269         }
270     }
271
272     if (!snapy) {
273         /* vertical snapping */
274         if (l < tr && r > tl) {
275             switch (dir) {
276             case OB_DIRECTION_SOUTH:
277             case OB_DIRECTION_SOUTHWEST:
278             case OB_DIRECTION_SOUTHEAST:
279             case OB_DIRECTION_EAST:
280             case OB_DIRECTION_WEST:
281                 dlt = t;
282                 drb = b + *h - orgh;
283                 if (b < tt && drb >= tt &&
284                     drb < tt + resist)
285                     *h = tt - t, snapy = TRUE;
286                 break;
287             case OB_DIRECTION_NORTH:
288             case OB_DIRECTION_NORTHWEST:
289             case OB_DIRECTION_NORTHEAST:
290                 dlt = t - *h + orgh;
291                 drb = b;
292                 if (t > tb && dlt <= tb &&
293                     dlt > tb - resist)
294                     *h = b - tb, snapy = TRUE;
295                 break;
296             }
297         }
298     }
299
300     /* snapped both ways */
301     return snapx && snapy;
302 }
303
304 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
305                          ObDirection dir)
306 {
307     GList *it;
308     ObClient *target; /* target */
309     Rect dock_area;
310     Rect expand;
311
312     if (!resist) return;
313
314     for (it = stacking_list; it; it = g_list_next(it)) {
315         if (!WINDOW_IS_CLIENT(it->data))
316             continue;
317         target = it->data;
318
319         /* don't snap to invisibles or ourself */
320         if (!target->frame->visible || target == c)
321             continue;
322
323         /* check window boundary as it is */
324         if (resist_size_window(c->frame->area, target->frame->area,
325                                resist, w, h, dir))
326             break;
327
328         /* now check window with the expanded margin area */
329         if (config_window_margin == 0)
330             continue;
331
332         RECT_SET(expand, target->frame->area.x - config_window_margin, 
333                          target->frame->area.y - config_window_margin, 
334                          target->frame->area.width + 2 * config_window_margin, 
335                          target->frame->area.height + 2 * config_window_margin);
336
337         if (resist_size_window(c->frame->area, expand,
338                                resist, w, h, dir))
339             break;
340     }
341     dock_get_area(&dock_area);
342     resist_size_window(c->frame->area, dock_area,
343                        resist, w, h, dir);
344 }
345
346 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
347                           ObDirection dir)
348 {
349     gint l, t, r, b; /* my left, top, right and bottom sides */
350     gint dlt, drb; /* my destination left/top and right/bottom sides */
351     Rect *area, *parea;
352     gint al, at, ar, ab; /* screen boundaries */
353     gint pl, pt, pr, pb; /* physical screen boundaries */
354     gint incw, inch;
355     guint i;
356     Rect desired_area;
357
358     if (!resist) return;
359
360     l = RECT_LEFT(c->frame->area);
361     r = RECT_RIGHT(c->frame->area);
362     t = RECT_TOP(c->frame->area);
363     b = RECT_BOTTOM(c->frame->area);
364
365     incw = c->size_inc.width;
366     inch = c->size_inc.height;
367
368     RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
369
370     for (i = 0; i < screen_num_monitors; ++i) {
371         parea = screen_physical_area_monitor(i);
372
373         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
374             g_free(parea);
375             continue;
376         }
377
378         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
379                            &desired_area);
380
381         /* get the screen boundaries */
382         al = RECT_LEFT(*area);
383         at = RECT_TOP(*area);
384         ar = RECT_RIGHT(*area);
385         ab = RECT_BOTTOM(*area);
386         pl = RECT_LEFT(*parea);
387         pt = RECT_TOP(*parea);
388         pr = RECT_RIGHT(*parea);
389         pb = RECT_BOTTOM(*parea);
390
391         /* horizontal snapping */
392         switch (dir) {
393         case OB_DIRECTION_EAST:
394         case OB_DIRECTION_NORTHEAST:
395         case OB_DIRECTION_SOUTHEAST:
396         case OB_DIRECTION_NORTH:
397         case OB_DIRECTION_SOUTH:
398             dlt = l;
399             drb = r + *w - c->frame->area.width;
400             if (r <= ar && drb > ar && drb <= ar + resist)
401                 *w = ar - l + 1;
402             else if (r <= pr && drb > pr && drb <= pr + resist)
403                 *w = pr - l + 1;
404             break;
405         case OB_DIRECTION_WEST:
406         case OB_DIRECTION_NORTHWEST:
407         case OB_DIRECTION_SOUTHWEST:
408             dlt = l - *w + c->frame->area.width;
409             drb = r;
410             if (l >= al && dlt < al && dlt >= al - resist)
411                 *w = r - al + 1;
412             else if (l >= pl && dlt < pl && dlt >= pl - resist)
413                 *w = r - pl + 1;
414             break;
415         }
416
417         /* vertical snapping */
418         switch (dir) {
419         case OB_DIRECTION_SOUTH:
420         case OB_DIRECTION_SOUTHWEST:
421         case OB_DIRECTION_SOUTHEAST:
422         case OB_DIRECTION_WEST:
423         case OB_DIRECTION_EAST:
424             dlt = t;
425             drb = b + *h - c->frame->area.height;
426             if (b <= ab && drb > ab && drb <= ab + resist)
427                 *h = ab - t + 1;
428             else if (b <= pb && drb > pb && drb <= pb + resist)
429                 *h = pb - t + 1;
430             break;
431         case OB_DIRECTION_NORTH:
432         case OB_DIRECTION_NORTHWEST:
433         case OB_DIRECTION_NORTHEAST:
434             dlt = t - *h + c->frame->area.height;
435             drb = b;
436             if (t >= at && dlt < at && dlt >= at - resist)
437                 *h = b - at + 1;
438             else if (t >= pt && dlt < pt && dlt >= pt - resist)
439                 *h = b - pt + 1;
440             break;
441         }
442
443         g_free(area);
444         g_free(parea);
445     }
446 }