all repos — openbox @ 3ae58f457bcdfa90b26dad4c9d192f045874ddae

openbox fork - make it a bit more like ryudo

openbox/resist.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-

   resist.c for the Openbox window manager
   Copyright (c) 2006        Mikael Magnusson
   Copyright (c) 2003-2007   Dana Jansens

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   See the COPYING file for a copy of the GNU General Public License.
*/

#include "client.h"
#include "frame.h"
#include "stacking.h"
#include "screen.h"
#include "dock.h"
#include "config.h"
#include "resist.h"
#include "parser/parse.h"

#include <glib.h>

static gboolean resist_move_window(Rect window,
                                   Rect target, gint resist,
                                   gint *x, gint *y)
{
    gint l, t, r, b; /* requested edges */
    gint cl, ct, cr, cb; /* current edges */
    gint w, h; /* current size */
    gint tl, tt, tr, tb; /* 1 past the target's edges on each side */
    gboolean snapx = 0, snapy = 0;

    w = window.width;
    h = window.height;

    l = *x;
    t = *y;
    r = l + w - 1;
    b = t + h - 1;

    cl = RECT_LEFT(window);
    ct = RECT_TOP(window);
    cr = RECT_RIGHT(window);
    cb = RECT_BOTTOM(window);

    tl = RECT_LEFT(target) - 1;
    tt = RECT_TOP(target) - 1;
    tr = RECT_RIGHT(target) + 1;
    tb = RECT_BOTTOM(target) + 1;

    /* snapx and snapy ensure that the window snaps to the top-most
       window edge available, without going all the way from
       bottom-to-top in the stacking list
    */
    if (!snapx) {
        if (ct < tb && cb > tt) {
            if (cl >= tr && l < tr && l >= tr - resist)
                *x = tr, snapx = TRUE;
            else if (cr <= tl && r > tl &&
                     r <= tl + resist)
                *x = tl - w + 1, snapx = TRUE;
            if (snapx) {
                /* try to corner snap to the window */
                if (ct > tt && t <= tt &&
                    t > tt - resist)
                    *y = tt + 1, snapy = TRUE;
                else if (cb < tb && b >= tb &&
                         b < tb + resist)
                    *y = tb - h, snapy = TRUE;
            }
        }
    }
    if (!snapy) {
        if (cl < tr && cr > tl) {
            if (ct >= tb && t < tb && t >= tb - resist)
                *y = tb, snapy = TRUE;
            else if (cb <= tt && b > tt &&
                     b <= tt + resist)
                *y = tt - h + 1, snapy = TRUE;
            if (snapy) {
                /* try to corner snap to the window */
                if (cl > tl && l <= tl &&
                    l > tl - resist)
                    *x = tl + 1, snapx = TRUE;
                else if (cr < tr && r >= tr &&
                         r < tr + resist)
                    *x = tr - w, snapx = TRUE;
            }
        }
    }

    return snapx && snapy;
}

void resist_move_windows(ObClient *c, gint resist, gint *x, gint *y)
{
    GList *it;
    Rect dock_area;

    if (!resist) return;

    frame_client_gravity(c->frame, x, y);


    for (it = stacking_list; it; it = g_list_next(it)) {
        ObClient *target;

        if (!WINDOW_IS_CLIENT(it->data))
            continue;
        target = it->data;

        /* don't snap to self or non-visibles */
        if (!target->frame->visible || target == c)
            continue;
        /* don't snap to windows set to below and skip_taskbar (desklets) */
        if (target->below && !c->below && target->skip_taskbar)
            continue;

        if (resist_move_window(c->frame->area, target->frame->area,
                               resist, x, y))
            break;
    }
    dock_get_area(&dock_area);
    resist_move_window(c->frame->area, dock_area, resist, x, y);

    frame_frame_gravity(c->frame, x, y);
}

void resist_move_monitors(ObClient *c, gint resist, gint *x, gint *y)
{
    Rect *area, *parea;
    guint i;
    gint l, t, r, b; /* requested edges */
    gint al, at, ar, ab; /* screen area edges */
    gint pl, pt, pr, pb; /* physical screen area edges */
    gint cl, ct, cr, cb; /* current edges */
    gint w, h; /* current size */
    Rect desired_area;

    if (!resist) return;

    frame_client_gravity(c->frame, x, y);

    w = c->frame->area.width;
    h = c->frame->area.height;

    l = *x;
    t = *y;
    r = l + w - 1;
    b = t + h - 1;

    cl = RECT_LEFT(c->frame->area);
    ct = RECT_TOP(c->frame->area);
    cr = RECT_RIGHT(c->frame->area);
    cb = RECT_BOTTOM(c->frame->area);

    RECT_SET(desired_area, *x, *y, c->area.width, c->area.height);

    for (i = 0; i < screen_num_monitors; ++i) {
        parea = screen_physical_area_monitor(i);

        if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
            g_free(parea);
            continue;
        }

        area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
                           &desired_area);

        al = RECT_LEFT(*area);
        at = RECT_TOP(*area);
        ar = RECT_RIGHT(*area);
        ab = RECT_BOTTOM(*area);
        pl = RECT_LEFT(*parea);
        pt = RECT_TOP(*parea);
        pr = RECT_RIGHT(*parea);
        pb = RECT_BOTTOM(*parea);

        if (cl >= al && l < al && l >= al - resist)
            *x = al;
        else if (cr <= ar && r > ar && r <= ar + resist)
            *x = ar - w + 1;
        else if (cl >= pl && l < pl && l >= pl - resist)
            *x = pl;
        else if (cr <= pr && r > pr && r <= pr + resist)
            *x = pr - w + 1;

        if (ct >= at && t < at && t >= at - resist)
            *y = at;
        else if (cb <= ab && b > ab && b < ab + resist)
            *y = ab - h + 1;
        else if (ct >= pt && t < pt && t >= pt - resist)
            *y = pt;
        else if (cb <= pb && b > pb && b < pb + resist)
            *y = pb - h + 1;

        g_free(area);
        g_free(parea);
    }

    frame_frame_gravity(c->frame, x, y);
}

static gboolean resist_size_window(Rect window, Rect target, gint resist,
                                   gint *w, gint *h, ObDirection dir)
{
    gint l, t, r, b; /* my left, top, right and bottom sides */
    gint tl, tt, tr, tb; /* target's left, top, right and bottom bottom sides*/
    gint dlt, drb; /* my destination left/top and right/bottom sides */
    gboolean snapx = 0, snapy = 0;
    gint orgw, orgh;

    l = RECT_LEFT(window);
    t = RECT_TOP(window);
    r = RECT_RIGHT(window);
    b = RECT_BOTTOM(window);

    orgw = window.width;
    orgh = window.height;

    tl = RECT_LEFT(target);
    tt = RECT_TOP(target);
    tr = RECT_RIGHT(target);
    tb = RECT_BOTTOM(target);

    if (!snapx) {
        /* horizontal snapping */
        if (t < tb && b > tt) {
            switch (dir) {
            case OB_DIRECTION_EAST:
            case OB_DIRECTION_NORTHEAST:
            case OB_DIRECTION_SOUTHEAST:
            case OB_DIRECTION_NORTH:
            case OB_DIRECTION_SOUTH:
                dlt = l;
                drb = r + *w - orgw;
                if (r < tl && drb >= tl &&
                    drb < tl + resist)
                    *w = tl - l, snapx = TRUE;
                break;
            case OB_DIRECTION_WEST:
            case OB_DIRECTION_NORTHWEST:
            case OB_DIRECTION_SOUTHWEST:
                dlt = l - *w + orgw;
                drb = r;
                if (l > tr && dlt <= tr &&
                    dlt > tr - resist)
                    *w = r - tr, snapx = TRUE;
                break;
            }
        }
    }

    if (!snapy) {
        /* vertical snapping */
        if (l < tr && r > tl) {
            switch (dir) {
            case OB_DIRECTION_SOUTH:
            case OB_DIRECTION_SOUTHWEST:
            case OB_DIRECTION_SOUTHEAST:
            case OB_DIRECTION_EAST:
            case OB_DIRECTION_WEST:
                dlt = t;
                drb = b + *h - orgh;
                if (b < tt && drb >= tt &&
                    drb < tt + resist)
                    *h = tt - t, snapy = TRUE;
                break;
            case OB_DIRECTION_NORTH:
            case OB_DIRECTION_NORTHWEST:
            case OB_DIRECTION_NORTHEAST:
                dlt = t - *h + orgh;
                drb = b;
                if (t > tb && dlt <= tb &&
                    dlt > tb - resist)
                    *h = b - tb, snapy = TRUE;
                break;
            }
        }
    }

    /* snapped both ways */
    return snapx && snapy;
}

void resist_size_windows(ObClient *c, gint resist, gint *w, gint *h,
                         ObDirection dir)
{
    GList *it;
    ObClient *target; /* target */
    Rect dock_area;

    if (!resist) return;

    for (it = stacking_list; it; it = g_list_next(it)) {
        if (!WINDOW_IS_CLIENT(it->data))
            continue;
        target = it->data;

        /* don't snap to invisibles or ourself */
        if (!target->frame->visible || target == c)
            continue;
        /* don't snap to windows set to below and skip_taskbar (desklets) */
        if (target->below && !c->below && target->skip_taskbar)
            continue;

        if (resist_size_window(c->frame->area, target->frame->area,
                               resist, w, h, dir))
            break;
    }
    dock_get_area(&dock_area);
    resist_size_window(c->frame->area, dock_area,
                       resist, w, h, dir);
}

void resist_size_monitors(ObClient *c, gint resist, gint *w, gint *h,
                          ObDirection dir)
{
    gint l, t, r, b; /* my left, top, right and bottom sides */
    gint dlt, drb; /* my destination left/top and right/bottom sides */
    Rect *area, *parea;
    gint al, at, ar, ab; /* screen boundaries */
    gint pl, pt, pr, pb; /* physical screen boundaries */
    gint incw, inch;
    guint i;
    Rect desired_area;

    if (!resist) return;

    l = RECT_LEFT(c->frame->area);
    r = RECT_RIGHT(c->frame->area);
    t = RECT_TOP(c->frame->area);
    b = RECT_BOTTOM(c->frame->area);

    incw = c->size_inc.width;
    inch = c->size_inc.height;

    RECT_SET(desired_area, c->area.x, c->area.y, *w, *h);

    for (i = 0; i < screen_num_monitors; ++i) {
        parea = screen_physical_area_monitor(i);

        if (!RECT_INTERSECTS_RECT(*parea, c->frame->area)) {
            g_free(parea);
            continue;
        }

        area = screen_area(c->desktop, SCREEN_AREA_ALL_MONITORS,
                           &desired_area);

        /* get the screen boundaries */
        al = RECT_LEFT(*area);
        at = RECT_TOP(*area);
        ar = RECT_RIGHT(*area);
        ab = RECT_BOTTOM(*area);
        pl = RECT_LEFT(*parea);
        pt = RECT_TOP(*parea);
        pr = RECT_RIGHT(*parea);
        pb = RECT_BOTTOM(*parea);

        /* horizontal snapping */
        switch (dir) {
        case OB_DIRECTION_EAST:
        case OB_DIRECTION_NORTHEAST:
        case OB_DIRECTION_SOUTHEAST:
        case OB_DIRECTION_NORTH:
        case OB_DIRECTION_SOUTH:
            dlt = l;
            drb = r + *w - c->frame->area.width;
            if (r <= ar && drb > ar && drb <= ar + resist)
                *w = ar - l + 1;
            else if (r <= pr && drb > pr && drb <= pr + resist)
                *w = pr - l + 1;
            break;
        case OB_DIRECTION_WEST:
        case OB_DIRECTION_NORTHWEST:
        case OB_DIRECTION_SOUTHWEST:
            dlt = l - *w + c->frame->area.width;
            drb = r;
            if (l >= al && dlt < al && dlt >= al - resist)
                *w = r - al + 1;
            else if (l >= pl && dlt < pl && dlt >= pl - resist)
                *w = r - pl + 1;
            break;
        }

        /* vertical snapping */
        switch (dir) {
        case OB_DIRECTION_SOUTH:
        case OB_DIRECTION_SOUTHWEST:
        case OB_DIRECTION_SOUTHEAST:
        case OB_DIRECTION_WEST:
        case OB_DIRECTION_EAST:
            dlt = t;
            drb = b + *h - c->frame->area.height;
            if (b <= ab && drb > ab && drb <= ab + resist)
                *h = ab - t + 1;
            else if (b <= pb && drb > pb && drb <= pb + resist)
                *h = pb - t + 1;
            break;
        case OB_DIRECTION_NORTH:
        case OB_DIRECTION_NORTHWEST:
        case OB_DIRECTION_NORTHEAST:
            dlt = t - *h + c->frame->area.height;
            drb = b;
            if (t >= at && dlt < at && dlt >= at - resist)
                *h = b - at + 1;
            else if (t >= pt && dlt < pt && dlt >= pt - resist)
                *h = b - pt + 1;
            break;
        }

        g_free(area);
        g_free(parea);
    }
}