File: float-placement.c

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (198 lines) | stat: -rw-r--r-- 4,417 bytes parent folder | download | duplicates (3)
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
/*
 * ion/ioncore/float-placement.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2007.
 *
 * See the included file LICENSE for details.
 */

#include <string.h>

#include "common.h"
#include "rootwin.h"
#include "xwindow.h"
#include "group.h"
#include "float-placement.h"


WFloatPlacement ioncore_placement_method=PLACEMENT_LRUD;


static void random_placement(WRectangle box, WRectangle *g)
{
    box.w-=g->w;
    box.h-=g->h;
    g->x=box.x+(box.w<=0 ? 0 : rand()%box.w);
    g->y=box.y+(box.h<=0 ? 0 : rand()%box.h);
}


static void ggeom(WRegion *reg, WRectangle *geom)
{
    *geom=REGION_GEOM(reg);
}


static bool st_filt(WStacking *st, void *lvl)
{
    uint level=*(uint*)lvl;

    return (st->reg!=NULL &&
            REGION_IS_MAPPED(st->reg) &&
            st->level==level);
}


#define FOR_ALL_STACKING_NODES(VAR, WS, LVL, TMP)          \
    for(stacking_iter_init(&(TMP), group_get_stacking(ws), \
                          st_filt, &LVL),                  \
        VAR=stacking_iter_nodes(&(TMP));                   \
        VAR!=NULL;                                         \
        VAR=stacking_iter_nodes(&(TMP)))


#define IGNORE_ST(ST, WS) ((ST)->reg==NULL || (ST)==(WS)->bottom)


static WRegion* is_occupied(WGroup *ws, uint level, const WRectangle *r)
{
    WStackingIterTmp tmp;
    WStacking *st;
    WRectangle p;

    FOR_ALL_STACKING_NODES(st, ws, level, tmp){
        ggeom(st->reg, &p);

        if(r->x>=p.x+p.w)
            continue;
        if(r->y>=p.y+p.h)
            continue;
        if(r->x+r->w<=p.x)
            continue;
        if(r->y+r->h<=p.y)
            continue;
        return st->reg;
    }

    return NULL;
}


static int next_least_x(WGroup *ws, uint level, int x)
{
    WRectangle p;
    int retx=REGION_GEOM(ws).x+REGION_GEOM(ws).w;
    WStackingIterTmp tmp;
    WStacking *st;

    FOR_ALL_STACKING_NODES(st, ws, level, tmp){
        ggeom(st->reg, &p);

        if(p.x+p.w>x && p.x+p.w<retx)
            retx=p.x+p.w;
    }

    return retx+1;
}



static int next_lowest_y(WGroup *ws, uint level, int y)
{
    WRectangle p;
    int rety=REGION_GEOM(ws).y+REGION_GEOM(ws).h;
    WStackingIterTmp tmp;
    WStacking *st;

    FOR_ALL_STACKING_NODES(st, ws, level, tmp){
        ggeom(st->reg, &p);

        if(p.y+p.h>y && p.y+p.h<rety)
            rety=p.y+p.h;
    }

    return rety+1;
}


static bool tiling_placement(WGroup *ws, uint level, WRectangle *g)
{
    WRegion *p;
    WRectangle r, r2;
    int maxx, maxy;

    r=REGION_GEOM(ws);
    r.w=g->w;
    r.h=g->h;

    maxx=REGION_GEOM(ws).x+REGION_GEOM(ws).w;
    maxy=REGION_GEOM(ws).y+REGION_GEOM(ws).h;

    if(ioncore_placement_method==PLACEMENT_UDLR){
        while(r.x<maxx){
            p=is_occupied(ws, level, &r);
            while(p!=NULL && r.y+r.h<maxy){
                ggeom(p, &r2);
                r.y=r2.y+r2.h+1;
                p=is_occupied(ws, level, &r);
            }
            if(r.y+r.h<maxy && r.x+r.w<maxx){
                g->x=r.x;
                g->y=r.y;
                return TRUE;
            }else{
                r.x=next_least_x(ws, level, r.x);
                r.y=0;
            }
        }
    }else{
        while(r.y<maxy){
            p=is_occupied(ws, level, &r);
            while(p!=NULL && r.x+r.w<maxx){
                ggeom(p, &r2);
                r.x=r2.x+r2.w+1;
                p=is_occupied(ws, level, &r);
            }
            if(r.y+r.h<maxy && r.x+r.w<maxx){
                g->x=r.x;
                g->y=r.y;
                return TRUE;
            }else{
                r.y=next_lowest_y(ws, level, r.y);
                r.x=0;
            }
        }
    }

    return FALSE;

}

static bool pointer_placement(WGroup *ws, WRectangle *g)
{
    WRootWin *rootwin=region_rootwin_of((WRegion*)ws);
    int px=0, py=0;

    if(xwindow_pointer_pos(WROOTWIN_ROOT(rootwin), &px, &py)){
        const WRectangle r=REGION_GEOM(ws);
        g->x=px-(g->w/2);
        g->y=py-(g->h/2);
        rectangle_clamp_or_center(g, &r);
        return TRUE;
    }
    return FALSE;
}


void group_calc_placement(WGroup *ws, uint level, WRectangle *geom)
{
    if(ioncore_placement_method==PLACEMENT_POINTER){
        if(pointer_placement(ws, geom))
            return;
    }else if(ioncore_placement_method!=PLACEMENT_RANDOM){
        if(tiling_placement(ws, level, geom))
            return;
    }
    random_placement(REGION_GEOM(ws), geom);
}