]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/resist.c
move the resistance plugin into the kernel. dont resist when move/resizing with the...
[mikachu/openbox.git] / openbox / resist.c
1 #include "dispatch.h"
2 #include "client.h"
3 #include "frame.h"
4 #include "stacking.h"
5 #include "screen.h"
6 #include "config.h"
7 #include "parser/parse.h"
8
9 #include <glib.h>
10
11 void resist_move(ObClient *c, gint *x, gint *y)
12 {
13     GList *it;
14     Rect *area;
15     guint i;
16     gint l, t, r, b; /* requested edges */
17     gint al, at, ar, ab; /* screen area edges */
18     gint cl, ct, cr, cb; /* current edges */
19     gint w, h; /* current size */
20     ObClient *snapx = NULL, *snapy = NULL;
21
22     w = c->frame->area.width;
23     h = c->frame->area.height;
24
25     l = *x;
26     t = *y;
27     r = l + w - 1;
28     b = t + h - 1;
29
30     cl = c->frame->area.x;
31     ct = c->frame->area.y;
32     cr = cl + c->frame->area.width - 1;
33     cb = ct + c->frame->area.height - 1;
34     
35     /* snap to other clients */
36     if (config_resist_win)
37         for (it = stacking_list; it != NULL; it = it->next) {
38             ObClient *target;
39             int tl, tt, tr, tb; /* 1 past the target's edges on each side */
40
41             if (!WINDOW_IS_CLIENT(it->data))
42                 continue;
43             target = it->data;
44             /* don't snap to self or non-visibles */
45             if (!target->frame->visible || target == c) continue; 
46
47             tl = target->frame->area.x - 1;
48             tt = target->frame->area.y - 1;
49             tr = tl + target->frame->area.width + 1;
50             tb = tt + target->frame->area.height + 1;
51
52             /* snapx and snapy ensure that the window snaps to the top-most
53                window edge available, without going all the way from
54                bottom-to-top in the stacking list
55             */
56             if (snapx == NULL) {
57                 if (ct < tb && cb > tt) {
58                     if (cl >= tr && l < tr && l >= tr - config_resist_win)
59                         *x = tr, snapx = target;
60                     else if (cr <= tl && r > tl &&
61                              r <= tl + config_resist_win)
62                         *x = tl - w + 1, snapx = target;
63                     if (snapx != NULL) {
64                         /* try to corner snap to the window */
65                         if (ct > tt && t <= tt &&
66                             t > tt - config_resist_win)
67                             *y = tt + 1, snapy = target;
68                         else if (cb < tb && b >= tb &&
69                                  b < tb + config_resist_win)
70                             *y = tb - h, snapy = target;
71                     }
72                 }
73             }
74             if (snapy == NULL) {
75                 if (cl < tr && cr > tl) {
76                     if (ct >= tb && t < tb && t >= tb - config_resist_win)
77                         *y = tb, snapy = target;
78                     else if (cb <= tt && b > tt &&
79                              b <= tt + config_resist_win)
80                         *y = tt - h + 1, snapy = target;
81                     if (snapy != NULL) {
82                         /* try to corner snap to the window */
83                         if (cl > tl && l <= tl &&
84                             l > tl - config_resist_win)
85                             *x = tl + 1, snapx = target;
86                         else if (cr < tr && r >= tr &&
87                                  r < tr + config_resist_win)
88                             *x = tr - w, snapx = target;
89                     }
90                 }
91             }
92
93             if (snapx && snapy) break;
94         }
95
96     /* get the screen boundaries */
97     if (config_resist_edge) {
98         for (i = 0; i < screen_num_monitors; ++i) {
99             area = screen_area_monitor(c->desktop, i);
100
101             if (!RECT_INTERSECTS_RECT(*area, c->frame->area))
102                 continue;
103
104             al = area->x;
105             at = area->y;
106             ar = al + area->width - 1;
107             ab = at + area->height - 1;
108
109             /* snap to screen edges */
110             if (cl >= al && l < al && l >= al - config_resist_edge)
111                 *x = al;
112             else if (cr <= ar && r > ar && r <= ar + config_resist_edge)
113                 *x = ar - w + 1;
114             if (ct >= at && t < at && t >= at - config_resist_edge)
115                 *y = at;
116             else if (cb <= ab && b > ab && b < ab + config_resist_edge)
117                 *y = ab - h + 1;
118         }
119     }
120 }
121
122 void resist_size(ObClient *c, gint *w, gint *h, ObCorner corn)
123 {
124     GList *it;
125     ObClient *target; /* target */
126     gint l, t, r, b; /* my left, top, right and bottom sides */
127     gint dlt, drb; /* my destination left/top and right/bottom sides */
128     gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
129     Rect *area;
130     gint al, at, ar, ab; /* screen boundaries */
131     ObClient *snapx = NULL, *snapy = NULL;
132
133     /* don't snap windows with size increments */
134     if (c->size_inc.width > 1 || c->size_inc.height > 1)
135         return;
136
137     l = c->frame->area.x;
138     r = l + c->frame->area.width - 1;
139     t = c->frame->area.y;
140     b = t + c->frame->area.height - 1;
141
142     /* get the screen boundaries */
143     area = screen_area(c->desktop);
144     al = area->x;
145     at = area->y;
146     ar = al + area->width - 1;
147     ab = at + area->height - 1;
148
149     /* snap to other windows */
150     if (config_resist_win) {
151         for (it = stacking_list; it != NULL; it = it->next) {
152             if (!WINDOW_IS_CLIENT(it->data))
153                 continue;
154             target = it->data;
155
156             /* don't snap to invisibles or ourself */
157             if (!target->frame->visible || target == c) continue;
158
159             tl = target->frame->area.x;
160             tr = target->frame->area.x + target->frame->area.width - 1;
161             tt = target->frame->area.y;
162             tb = target->frame->area.y + target->frame->area.height - 1;
163
164             if (snapx == NULL) {
165                 /* horizontal snapping */
166                 if (t < tb && b > tt) {
167                     switch (corn) {
168                     case OB_CORNER_TOPLEFT:
169                     case OB_CORNER_BOTTOMLEFT:
170                         dlt = l;
171                         drb = r + *w - c->frame->area.width;
172                         if (r < tl && drb >= tl &&
173                             drb < tl + config_resist_win)
174                             *w = tl - l, snapx = target;
175                         break;
176                     case OB_CORNER_TOPRIGHT:
177                     case OB_CORNER_BOTTOMRIGHT:
178                         dlt = l - *w + c->frame->area.width;
179                         drb = r;
180                         if (l > tr && dlt <= tr &&
181                             dlt > tr - config_resist_win)
182                             *w = r - tr, snapx = target;
183                         break;
184                     }
185                 }
186             }
187
188             if (snapy == NULL) {
189                 /* vertical snapping */
190                 if (l < tr && r > tl) {
191                     switch (corn) {
192                     case OB_CORNER_TOPLEFT:
193                     case OB_CORNER_TOPRIGHT:
194                         dlt = t;
195                         drb = b + *h - c->frame->area.height;
196                         if (b < tt && drb >= tt &&
197                             drb < tt + config_resist_win)
198                             *h = tt - t, snapy = target;
199                         break;
200                     case OB_CORNER_BOTTOMLEFT:
201                     case OB_CORNER_BOTTOMRIGHT:
202                         dlt = t - *h + c->frame->area.height;
203                         drb = b;
204                         if (t > tb && dlt <= tb &&
205                             dlt > tb - config_resist_win)
206                             *h = b - tb, snapy = target;
207                         break;
208                     }
209                 }
210             }
211
212             /* snapped both ways */
213             if (snapx && snapy) break;
214         }
215     }
216
217     /* snap to screen edges */
218
219     if (config_resist_edge) {
220         /* horizontal snapping */
221         switch (corn) {
222         case OB_CORNER_TOPLEFT:
223         case OB_CORNER_BOTTOMLEFT:
224             dlt = l;
225             drb = r + *w - c->frame->area.width;
226             if (r <= ar && drb > ar && drb <= ar + config_resist_edge)
227                 *w = ar - l + 1;
228             break;
229         case OB_CORNER_TOPRIGHT:
230         case OB_CORNER_BOTTOMRIGHT:
231             dlt = l - *w + c->frame->area.width;
232             drb = r;
233             if (l >= al && dlt < al && dlt >= al - config_resist_edge)
234                 *w = r - al + 1;
235             break;
236         }
237
238         /* vertical snapping */
239         switch (corn) {
240         case OB_CORNER_TOPLEFT:
241         case OB_CORNER_TOPRIGHT:
242             dlt = t;
243             drb = b + *h - c->frame->area.height;
244             if (b <= ab && drb > ab && drb <= ab + config_resist_edge)
245                 *h = ab - t + 1;
246             break;
247         case OB_CORNER_BOTTOMLEFT:
248         case OB_CORNER_BOTTOMRIGHT:
249             dlt = t - *h + c->frame->area.height;
250             drb = b;
251             if (t >= at && dlt < at && dlt >= at - config_resist_edge)
252                 *h = b - at + 1;
253             break;
254         }
255     }
256 }