]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/resist.c
Merge branch 'mikabox/personal' 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
107     if (!resist) return;
108
109     frame_client_gravity(c->frame, x, y);
110
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         if (resist_move_window(c->frame->area, target->frame->area,
124                                resist, x, y))
125             break;
126     }
127     dock_get_area(&dock_area);
128     resist_move_window(c->frame->area, dock_area, resist, x, y);
129
130     frame_frame_gravity(c->frame, x, y);
131 }
132
133 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
134 {
135     Rect *area;
136     const Rect *parea;
137     guint i;
138     gint l, t, r, b; /* requested edges */
139     gint al, at, ar, ab; /* screen area edges */
140     gint pl, pt, pr, pb; /* physical screen area edges */
141     gint cl, ct, cr, cb; /* current edges */
142     gint w, h; /* current size */
143     Rect desired_area;
144
145     if (!resist) return;
146
147     frame_client_gravity(c->frame, x, y);
148
149     w = c->frame->area.width;
150     h = c->frame->area.height;
151
152     l = *x;
153     t = *y;
154     r = l + w - 1;
155     b = t + h - 1;
156
157     cl = RECT_LEFT(c->frame->area);
158     ct = RECT_TOP(c->frame->area);
159     cr = RECT_RIGHT(c->frame->area);
160     cb = RECT_BOTTOM(c->frame->area);
161
162     RECT_SET(desired_area, c->frame->area.x, c->frame->area.y,
163              c->frame->area.width, c->frame->area.height);
164
165     for (i = 0; i < screen_num_monitors; ++i) {
166         parea = screen_physical_area_monitor(i);
167
168         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
169             continue;
170
171         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
172                            &desired_area);
173
174         al = RECT_LEFT(*area);
175         at = RECT_TOP(*area);
176         ar = RECT_RIGHT(*area);
177         ab = RECT_BOTTOM(*area);
178         pl = RECT_LEFT(*parea);
179         pt = RECT_TOP(*parea);
180         pr = RECT_RIGHT(*parea);
181         pb = RECT_BOTTOM(*parea);
182
183         if (cl >= al && l < al && l >= al - resist)
184             *x = al;
185         else if (cr <= ar && r > ar && r <= ar + resist)
186             *x = ar - w + 1;
187         else if (cl >= pl && l < pl && l >= pl - resist)
188             *x = pl;
189         else if (cr <= pr && r > pr && r <= pr + resist)
190             *x = pr - w + 1;
191
192         if (ct >= at && t < at && t >= at - resist)
193             *y = at;
194         else if (cb <= ab && b > ab && b < ab + resist)
195             *y = ab - h + 1;
196         else if (ct >= pt && t < pt && t >= pt - resist)
197             *y = pt;
198         else if (cb <= pb && b > pb && b < pb + resist)
199             *y = pb - h + 1;
200
201         g_slice_free(Rect, area);
202     }
203
204     frame_frame_gravity(c->frame, x, y);
205 }
206
207 static gboolean resist_size_window(Rect window, Rect target, gint resist,
208                                    gint *w, gint *h, ObDirection dir)
209 {
210     gint l, t, r, b; /* my left, top, right and bottom sides */
211     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
212     gint dlt, drb; /* my destination left/top and right/bottom sides */
213     gboolean snapx = 0, snapy = 0;
214     gint orgw, orgh;
215
216     l = RECT_LEFT(window);
217     t = RECT_TOP(window);
218     r = RECT_RIGHT(window);
219     b = RECT_BOTTOM(window);
220
221     orgw = window.width;
222     orgh = window.height;
223
224     tl = RECT_LEFT(target);
225     tt = RECT_TOP(target);
226     tr = RECT_RIGHT(target);
227     tb = RECT_BOTTOM(target);
228
229     if (!snapx) {
230         /* horizontal snapping */
231         if (t < tb && b > tt) {
232             switch (dir) {
233             case OB_DIRECTION_EAST:
234             case OB_DIRECTION_NORTHEAST:
235             case OB_DIRECTION_SOUTHEAST:
236             case OB_DIRECTION_NORTH:
237             case OB_DIRECTION_SOUTH:
238                 dlt = l;
239                 drb = r + *w - orgw;
240                 if (r < tl && drb >= tl &&
241                     drb < tl + resist)
242                     *w = tl - l, snapx = TRUE;
243                 break;
244             case OB_DIRECTION_WEST:
245             case OB_DIRECTION_NORTHWEST:
246             case OB_DIRECTION_SOUTHWEST:
247                 dlt = l - *w + orgw;
248                 drb = r;
249                 if (l > tr && dlt <= tr &&
250                     dlt > tr - resist)
251                     *w = r - tr, snapx = TRUE;
252                 break;
253             }
254         }
255     }
256
257     if (!snapy) {
258         /* vertical snapping */
259         if (l < tr && r > tl) {
260             switch (dir) {
261             case OB_DIRECTION_SOUTH:
262             case OB_DIRECTION_SOUTHWEST:
263             case OB_DIRECTION_SOUTHEAST:
264             case OB_DIRECTION_EAST:
265             case OB_DIRECTION_WEST:
266                 dlt = t;
267                 drb = b + *h - orgh;
268                 if (b < tt && drb >= tt &&
269                     drb < tt + resist)
270                     *h = tt - t, snapy = TRUE;
271                 break;
272             case OB_DIRECTION_NORTH:
273             case OB_DIRECTION_NORTHWEST:
274             case OB_DIRECTION_NORTHEAST:
275                 dlt = t - *h + orgh;
276                 drb = b;
277                 if (t > tb && dlt <= tb &&
278                     dlt > tb - resist)
279                     *h = b - tb, snapy = TRUE;
280                 break;
281             }
282         }
283     }
284
285     /* snapped both ways */
286     return snapx && snapy;
287 }
288
289 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
290                          ObDirection dir)
291 {
292     GList *it;
293     ObClient *target; /* target */
294     Rect dock_area;
295
296     if (!resist) return;
297
298     for (it = stacking_list; it; it = g_list_next(it)) {
299         if (!WINDOW_IS_CLIENT(it->data))
300             continue;
301         target = it->data;
302
303         /* don't snap to invisibles or ourself */
304         if (!target->frame->visible || target == c)
305             continue;
306
307         if (resist_size_window(c->frame->area, target->frame->area,
308                                resist, w, h, dir))
309             break;
310     }
311     dock_get_area(&dock_area);
312     resist_size_window(c->frame->area, dock_area,
313                        resist, w, h, dir);
314 }
315
316 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
317                           ObDirection dir)
318 {
319     gint l, t, r, b; /* my left, top, right and bottom sides */
320     gint dlt, drb; /* my destination left/top and right/bottom sides */
321     Rect *area;
322     const Rect *parea;
323     gint al, at, ar, ab; /* screen boundaries */
324     gint pl, pt, pr, pb; /* physical screen boundaries */
325     guint i;
326     Rect desired_area;
327
328     if (!resist) return;
329
330     l = RECT_LEFT(c->frame->area);
331     r = RECT_RIGHT(c->frame->area);
332     t = RECT_TOP(c->frame->area);
333     b = RECT_BOTTOM(c->frame->area);
334
335     RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
336
337     for (i = 0; i < screen_num_monitors; ++i) {
338         parea = screen_physical_area_monitor(i);
339
340         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
341             continue;
342
343         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
344                            &desired_area);
345
346         /* get the screen boundaries */
347         al = RECT_LEFT(*area);
348         at = RECT_TOP(*area);
349         ar = RECT_RIGHT(*area);
350         ab = RECT_BOTTOM(*area);
351         pl = RECT_LEFT(*parea);
352         pt = RECT_TOP(*parea);
353         pr = RECT_RIGHT(*parea);
354         pb = RECT_BOTTOM(*parea);
355
356         /* horizontal snapping */
357         switch (dir) {
358         case OB_DIRECTION_EAST:
359         case OB_DIRECTION_NORTHEAST:
360         case OB_DIRECTION_SOUTHEAST:
361         case OB_DIRECTION_NORTH:
362         case OB_DIRECTION_SOUTH:
363             dlt = l;
364             drb = r + *w - c->frame->area.width;
365             if (r <= ar && drb > ar && drb <= ar + resist)
366                 *w = ar - l + 1;
367             else if (r <= pr && drb > pr && drb <= pr + resist)
368                 *w = pr - l + 1;
369             break;
370         case OB_DIRECTION_WEST:
371         case OB_DIRECTION_NORTHWEST:
372         case OB_DIRECTION_SOUTHWEST:
373             dlt = l - *w + c->frame->area.width;
374             drb = r;
375             if (l >= al && dlt < al && dlt >= al - resist)
376                 *w = r - al + 1;
377             else if (l >= pl && dlt < pl && dlt >= pl - resist)
378                 *w = r - pl + 1;
379             break;
380         }
381
382         /* vertical snapping */
383         switch (dir) {
384         case OB_DIRECTION_SOUTH:
385         case OB_DIRECTION_SOUTHWEST:
386         case OB_DIRECTION_SOUTHEAST:
387         case OB_DIRECTION_WEST:
388         case OB_DIRECTION_EAST:
389             dlt = t;
390             drb = b + *h - c->frame->area.height;
391             if (b <= ab && drb > ab && drb <= ab + resist)
392                 *h = ab - t + 1;
393             else if (b <= pb && drb > pb && drb <= pb + resist)
394                 *h = pb - t + 1;
395             break;
396         case OB_DIRECTION_NORTH:
397         case OB_DIRECTION_NORTHWEST:
398         case OB_DIRECTION_NORTHEAST:
399             dlt = t - *h + c->frame->area.height;
400             drb = b;
401             if (t >= at && dlt < at && dlt >= at - resist)
402                 *h = b - at + 1;
403             else if (t >= pt && dlt < pt && dlt >= pt - resist)
404                 *h = b - pt + 1;
405             break;
406         }
407
408         g_slice_free(Rect, area);
409     }
410 }