resist for one key press at edges when keyboard move/resizing
[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 "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     w = c->frame->area.width;
40     h = c->frame->area.height;
41
42     l = *x;
43     t = *y;
44     r = l + w - 1;
45     b = t + h - 1;
46
47     cl = RECT_LEFT(c->frame->area);
48     ct = RECT_TOP(c->frame->area);
49     cr = RECT_RIGHT(c->frame->area);
50     cb = RECT_BOTTOM(c->frame->area);
51     
52     for (it = stacking_list; it; it = g_list_next(it)) {
53         ObClient *target;
54         gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
55
56         if (!WINDOW_IS_CLIENT(it->data))
57             continue;
58         target = it->data;
59
60         /* don't snap to self or non-visibles */
61         if (!target->frame->visible || target == c) continue; 
62
63         /* don't snap to windows in layers beneath */
64         if(target->layer < c->layer && !config_resist_layers_below)
65             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
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
127     if (!resist) return;
128
129     w = c->frame->area.width;
130     h = c->frame->area.height;
131
132     l = *x;
133     t = *y;
134     r = l + w - 1;
135     b = t + h - 1;
136
137     cl = RECT_LEFT(c->frame->area);
138     ct = RECT_TOP(c->frame->area);
139     cr = RECT_RIGHT(c->frame->area);
140     cb = RECT_BOTTOM(c->frame->area);
141     
142     for (i = 0; i < screen_num_monitors; ++i) {
143         area = screen_area_monitor(c->desktop, i);
144         parea = screen_physical_area_monitor(i);
145
146         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
147             continue;
148
149         al = RECT_LEFT(*area);
150         at = RECT_TOP(*area);
151         ar = RECT_RIGHT(*area);
152         ab = RECT_BOTTOM(*area);
153         pl = RECT_LEFT(*parea);
154         pt = RECT_TOP(*parea);
155         pr = RECT_RIGHT(*parea);
156         pb = RECT_BOTTOM(*parea);
157
158         if (cl >= al && l < al && l >= al - resist)
159             *x = al;
160         else if (cr <= ar && r > ar && r <= ar + resist)
161             *x = ar - w + 1;
162         else if (cl >= pl && l < pl && l >= pl - resist)
163             *x = pl;
164         else if (cr <= pr && r > pr && r <= pr + resist)
165             *x = pr - w + 1;
166
167         if (ct >= at && t < at && t >= at - resist)
168             *y = at;
169         else if (cb <= ab && b > ab && b < ab + resist)
170             *y = ab - h + 1;
171         else if (ct >= pt && t < pt && t >= pt - resist)
172             *y = pt;
173         else if (cb <= pb && b > pb && b < pb + resist)
174             *y = pb - h + 1;
175     }
176 }
177
178 void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
179                          ObCorner corn)
180 {
181     GList *it;
182     ObClient *target; /* target */
183     gint l, t, r, b; /* my left, top, right and bottom sides */
184     gint dlt, drb; /* my destination left/top and right/bottom sides */
185     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
186     gint incw, inch;
187     ObClient *snapx = NULL, *snapy = NULL;
188
189     if (!resist) return;
190
191     incw = c->size_inc.width;
192     inch = c->size_inc.height;
193
194     l = RECT_LEFT(c->frame->area);
195     r = RECT_RIGHT(c->frame->area);
196     t = RECT_TOP(c->frame->area);
197     b = RECT_BOTTOM(c->frame->area);
198
199     for (it = stacking_list; it; it = g_list_next(it)) {
200         if (!WINDOW_IS_CLIENT(it->data))
201             continue;
202         target = it->data;
203
204         /* don't snap to invisibles or ourself */
205         if (!target->frame->visible || target == c) continue; 
206
207         /* don't snap to windows in layers beneath */
208         if(target->layer < c->layer && !config_resist_layers_below)
209             continue;
210
211         tl = RECT_LEFT(target->frame->area);
212         tr = RECT_RIGHT(target->frame->area);
213         tt = RECT_TOP(target->frame->area);
214         tb = RECT_BOTTOM(target->frame->area);
215
216         if (snapx == NULL) {
217             /* horizontal snapping */
218             if (t < tb && b > tt) {
219                 switch (corn) {
220                 case OB_CORNER_TOPLEFT:
221                 case OB_CORNER_BOTTOMLEFT:
222                     dlt = l;
223                     drb = r + *w - c->frame->area.width;
224                     if (r < tl && drb >= tl &&
225                         drb < tl + resist)
226                         *w = tl - l, snapx = target;
227                     break;
228                 case OB_CORNER_TOPRIGHT:
229                 case OB_CORNER_BOTTOMRIGHT:
230                     dlt = l - *w + c->frame->area.width;
231                     drb = r;
232                     if (l > tr && dlt <= tr &&
233                         dlt > tr - resist)
234                         *w = r - tr, snapx = target;
235                     break;
236                 }
237             }
238         }
239
240         if (snapy == NULL) {
241             /* vertical snapping */
242             if (l < tr && r > tl) {
243                 switch (corn) {
244                 case OB_CORNER_TOPLEFT:
245                 case OB_CORNER_TOPRIGHT:
246                     dlt = t;
247                     drb = b + *h - c->frame->area.height;
248                     if (b < tt && drb >= tt &&
249                         drb < tt + resist)
250                         *h = tt - t, snapy = target;
251                     break;
252                 case OB_CORNER_BOTTOMLEFT:
253                 case OB_CORNER_BOTTOMRIGHT:
254                     dlt = t - *h + c->frame->area.height;
255                     drb = b;
256                     if (t > tb && dlt <= tb &&
257                         dlt > tb - resist)
258                         *h = b - tb, snapy = target;
259                     break;
260                 }
261             }
262         }
263
264         /* snapped both ways */
265         if (snapx && snapy) break;
266     }
267 }
268
269 void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
270                           ObCorner corn)
271 {
272     gint l, t, r, b; /* my left, top, right and bottom sides */
273     gint dlt, drb; /* my destination left/top and right/bottom sides */
274     Rect *area, *parea;
275     gint al, at, ar, ab; /* screen boundaries */ 
276     gint pl, pt, pr, pb; /* physical screen boundaries */
277     gint incw, inch;
278     guint i;
279
280     if (!resist) return;
281
282     l = RECT_LEFT(c->frame->area);
283     r = RECT_RIGHT(c->frame->area);
284     t = RECT_TOP(c->frame->area);
285     b = RECT_BOTTOM(c->frame->area);
286
287     incw = c->size_inc.width;
288     inch = c->size_inc.height;
289
290     for (i = 0; i < screen_num_monitors; ++i) {
291         area = screen_area_monitor(c->desktop, i);
292         parea = screen_physical_area_monitor(i);
293
294         if (!RECT_INTERSECTS_RECT(*parea, c->frame->area))
295             continue;
296
297         /* get the screen boundaries */
298         al = RECT_LEFT(*area);
299         at = RECT_TOP(*area);
300         ar = RECT_RIGHT(*area);
301         ab = RECT_BOTTOM(*area);
302         pl = RECT_LEFT(*parea);
303         pt = RECT_TOP(*parea);
304         pr = RECT_RIGHT(*parea);
305         pb = RECT_BOTTOM(*parea);
306
307         /* horizontal snapping */
308         switch (corn) {
309         case OB_CORNER_TOPLEFT:
310         case OB_CORNER_BOTTOMLEFT:
311             dlt = l;
312             drb = r + *w - c->frame->area.width;
313             if (r <= ar && drb > ar && drb <= ar + resist)
314                 *w = ar - l + 1;
315             else if (r <= pr && drb > pr && drb <= pr + resist)
316                 *w = pr - l + 1;
317             break;
318         case OB_CORNER_TOPRIGHT:
319         case OB_CORNER_BOTTOMRIGHT:
320             dlt = l - *w + c->frame->area.width;
321             drb = r;
322             if (l >= al && dlt < al && dlt >= al - resist)
323                 *w = r - al + 1;
324             else if (l >= pl && dlt < pl && dlt >= pl - resist)
325                 *w = r - pl + 1;
326             break;
327         }
328
329         /* vertical snapping */
330         switch (corn) {
331         case OB_CORNER_TOPLEFT:
332         case OB_CORNER_TOPRIGHT:
333             dlt = t;
334             drb = b + *h - c->frame->area.height;
335             if (b <= ab && drb > ab && drb <= ab + resist)
336                 *h = ab - t + 1;
337             else if (b <= pb && drb > pb && drb <= pb + resist)
338                 *h = pb - t + 1;
339             break;
340         case OB_CORNER_BOTTOMLEFT:
341         case OB_CORNER_BOTTOMRIGHT:
342             dlt = t - *h + c->frame->area.height;
343             drb = b;
344             if (t >= at && dlt < at && dlt >= at - resist)
345                 *h = b - at + 1;
346             else if (t >= pt && dlt < pt && dlt >= pt - resist)
347                 *h = b - pt + 1;
348             break;
349         }
350     }
351 }