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