remove trailing whitespace
[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);
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         /* don't snap to windows set to below and skip_taskbar (desklets) */
65         if (target->below && !c->below && target->skip_taskbar) continue;
66
67         tl = RECT_LEFT(target->frame->area) - 1;
68         tt = RECT_TOP(target->frame->area) - 1;
69         tr = RECT_RIGHT(target->frame->area) + 1;
70         tb = RECT_BOTTOM(target->frame->area) + 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 - resist)
79                     *x = tr, snapx = target;
80                 else if (cr <= tl && r > tl &&
81                          r <= tl + resist)
82                     *x = tl - w + 1, snapx = target;
83                 if (snapx != NULL) {
84                     /* try to corner snap to the window */
85                     if (ct > tt && t <= tt &&
86                         t > tt - resist)
87                         *y = tt + 1, snapy = target;
88                     else if (cb < tb && b >= tb &&
89                              b < tb + resist)
90                         *y = tb - h, snapy = target;
91                 }
92             }
93         }
94         if (snapy == NULL) {
95             if (cl < tr && cr > tl) {
96                 if (ct >= tb && t < tb && t >= tb - resist)
97                     *y = tb, snapy = target;
98                 else if (cb <= tt && b > tt &&
99                          b <= tt + resist)
100                     *y = tt - h + 1, snapy = target;
101                 if (snapy != NULL) {
102                     /* try to corner snap to the window */
103                     if (cl > tl && l <= tl &&
104                         l > tl - resist)
105                         *x = tl + 1, snapx = target;
106                     else if (cr < tr && r >= tr &&
107                              r < tr + resist)
108                         *x = tr - w, snapx = target;
109                 }
110             }
111         }
112
113         if (snapx && snapy) break;
114     }
115
116     frame_frame_gravity(c->frame, x, y);
117 }
118
119 void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
120 {
121     Rect *area, *parea;
122     guint i;
123     gint l, t, r, b; /* requested edges */
124     gint al, at, ar, ab; /* screen area edges */
125     gint pl, pt, pr, pb; /* physical screen area edges */
126     gint cl, ct, cr, cb; /* current edges */
127     gint w, h; /* current size */
128     Rect desired_area;
129
130     if (!resist) return;
131
132     frame_client_gravity(c->frame, x, y);
133
134     w = c->frame->area.width;
135     h = c->frame->area.height;
136
137     l = *x;
138     t = *y;
139     r = l + w - 1;
140     b = t + h - 1;
141
142     cl = RECT_LEFT(c->frame->area);
143     ct = RECT_TOP(c->frame->area);
144     cr = RECT_RIGHT(c->frame->area);
145     cb = RECT_BOTTOM(c->frame->area);
146
147     RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);
148
149     for (i = 0; i < screen_num_monitors; ++i) {
150         parea = screen_physical_area_monitor(i);
151
152         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
153             g_free(parea);
154             continue;
155         }
156
157         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
158                            &desired_area);
159
160         al = RECT_LEFT(*area);
161         at = RECT_TOP(*area);
162         ar = RECT_RIGHT(*area);
163         ab = RECT_BOTTOM(*area);
164         pl = RECT_LEFT(*parea);
165         pt = RECT_TOP(*parea);
166         pr = RECT_RIGHT(*parea);
167         pb = RECT_BOTTOM(*parea);
168
169         if (cl >= al && l < al && l >= al - resist)
170             *x = al;
171         else if (cr <= ar && r > ar && r <= ar + resist)
172             *x = ar - w + 1;
173         else if (cl >= pl && l < pl && l >= pl - resist)
174             *x = pl;
175         else if (cr <= pr && r > pr && r <= pr + resist)
176             *x = pr - w + 1;
177
178         if (ct >= at && t < at && t >= at - resist)
179             *y = at;
180         else if (cb <= ab && b > ab && b < ab + resist)
181             *y = ab - h + 1;
182         else if (ct >= pt && t < pt && t >= pt - resist)
183             *y = pt;
184         else if (cb <= pb && b > pb && b < pb + resist)
185             *y = pb - h + 1;
186
187         g_free(area);
188         g_free(parea);
189     }
190
191     frame_frame_gravity(c->frame, x, y);
192 }
193
194 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
195                          ObCorner corn)
196 {
197     GList *it;
198     ObClient *target; /* target */
199     gint l, t, r, b; /* my left, top, right and bottom sides */
200     gint dlt, drb; /* my destination left/top and right/bottom sides */
201     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
202     gint incw, inch;
203     ObClient *snapx = NULL, *snapy = NULL;
204
205     if (!resist) return;
206
207     incw = c->size_inc.width;
208     inch = c->size_inc.height;
209
210     l = RECT_LEFT(c->frame->area);
211     r = RECT_RIGHT(c->frame->area);
212     t = RECT_TOP(c->frame->area);
213     b = RECT_BOTTOM(c->frame->area);
214
215     for (it = stacking_list; it; it = g_list_next(it)) {
216         if (!WINDOW_IS_CLIENT(it->data))
217             continue;
218         target = it->data;
219
220         /* don't snap to invisibles or ourself */
221         if (!target->frame->visible || target == c) continue;
222         /* don't snap to windows set to below and skip_taskbar (desklets) */
223         if (target->below && !c->below && target->skip_taskbar) continue;
224
225         tl = RECT_LEFT(target->frame->area);
226         tr = RECT_RIGHT(target->frame->area);
227         tt = RECT_TOP(target->frame->area);
228         tb = RECT_BOTTOM(target->frame->area);
229
230         if (snapx == NULL) {
231             /* horizontal snapping */
232             if (t < tb && b > tt) {
233                 switch (corn) {
234                 case OB_CORNER_TOPLEFT:
235                 case OB_CORNER_BOTTOMLEFT:
236                     dlt = l;
237                     drb = r + *w - c->frame->area.width;
238                     if (r < tl && drb >= tl &&
239                         drb < tl + resist)
240                         *w = tl - l, snapx = target;
241                     break;
242                 case OB_CORNER_TOPRIGHT:
243                 case OB_CORNER_BOTTOMRIGHT:
244                     dlt = l - *w + c->frame->area.width;
245                     drb = r;
246                     if (l > tr && dlt <= tr &&
247                         dlt > tr - resist)
248                         *w = r - tr, snapx = target;
249                     break;
250                 }
251             }
252         }
253
254         if (snapy == NULL) {
255             /* vertical snapping */
256             if (l < tr && r > tl) {
257                 switch (corn) {
258                 case OB_CORNER_TOPLEFT:
259                 case OB_CORNER_TOPRIGHT:
260                     dlt = t;
261                     drb = b + *h - c->frame->area.height;
262                     if (b < tt && drb >= tt &&
263                         drb < tt + resist)
264                         *h = tt - t, snapy = target;
265                     break;
266                 case OB_CORNER_BOTTOMLEFT:
267                 case OB_CORNER_BOTTOMRIGHT:
268                     dlt = t - *h + c->frame->area.height;
269                     drb = b;
270                     if (t > tb && dlt <= tb &&
271                         dlt > tb - resist)
272                         *h = b - tb, snapy = target;
273                     break;
274                 }
275             }
276         }
277
278         /* snapped both ways */
279         if (snapx && snapy) break;
280     }
281 }
282
283 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
284                           ObCorner corn)
285 {
286     gint l, t, r, b; /* my left, top, right and bottom sides */
287     gint dlt, drb; /* my destination left/top and right/bottom sides */
288     Rect *area, *parea;
289     gint al, at, ar, ab; /* screen boundaries */
290     gint pl, pt, pr, pb; /* physical screen boundaries */
291     gint incw, inch;
292     guint i;
293     Rect desired_area;
294
295     if (!resist) return;
296
297     l = RECT_LEFT(c->frame->area);
298     r = RECT_RIGHT(c->frame->area);
299     t = RECT_TOP(c->frame->area);
300     b = RECT_BOTTOM(c->frame->area);
301
302     incw = c->size_inc.width;
303     inch = c->size_inc.height;
304
305     RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);
306
307     for (i = 0; i < screen_num_monitors; ++i) {
308         parea = screen_physical_area_monitor(i);
309
310         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
311             g_free(parea);
312             continue;
313         }
314
315         area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
316                            &desired_area);
317
318         /* get the screen boundaries */
319         al = RECT_LEFT(*area);
320         at = RECT_TOP(*area);
321         ar = RECT_RIGHT(*area);
322         ab = RECT_BOTTOM(*area);
323         pl = RECT_LEFT(*parea);
324         pt = RECT_TOP(*parea);
325         pr = RECT_RIGHT(*parea);
326         pb = RECT_BOTTOM(*parea);
327
328         /* horizontal snapping */
329         switch (corn) {
330         case OB_CORNER_TOPLEFT:
331         case OB_CORNER_BOTTOMLEFT:
332             dlt = l;
333             drb = r + *w - c->frame->area.width;
334             if (r <= ar && drb > ar && drb <= ar + resist)
335                 *w = ar - l + 1;
336             else if (r <= pr && drb > pr && drb <= pr + resist)
337                 *w = pr - l + 1;
338             break;
339         case OB_CORNER_TOPRIGHT:
340         case OB_CORNER_BOTTOMRIGHT:
341             dlt = l - *w + c->frame->area.width;
342             drb = r;
343             if (l >= al && dlt < al && dlt >= al - resist)
344                 *w = r - al + 1;
345             else if (l >= pl && dlt < pl && dlt >= pl - resist)
346                 *w = r - pl + 1;
347             break;
348         }
349
350         /* vertical snapping */
351         switch (corn) {
352         case OB_CORNER_TOPLEFT:
353         case OB_CORNER_TOPRIGHT:
354             dlt = t;
355             drb = b + *h - c->frame->area.height;
356             if (b <= ab && drb > ab && drb <= ab + resist)
357                 *h = ab - t + 1;
358             else if (b <= pb && drb > pb && drb <= pb + resist)
359                 *h = pb - t + 1;
360             break;
361         case OB_CORNER_BOTTOMLEFT:
362         case OB_CORNER_BOTTOMRIGHT:
363             dlt = t - *h + c->frame->area.height;
364             drb = b;
365             if (t >= at && dlt < at && dlt >= at - resist)
366                 *h = b - at + 1;
367             else if (t >= pt && dlt < pt && dlt >= pt - resist)
368                 *h = b - pt + 1;
369             break;
370         }
371
372         g_free(area);
373         g_free(parea);
374     }
375 }