aewl
view view.c @ 529:e2bd83191fc6
small boundary check fix
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Thu, 05 Oct 2006 18:23:28 +0200 |
parents | c0fffe6c207e |
children | 451f19d48845 |
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
5 #include "dwm.h"
7 /* static */
9 static Client *
10 minclient(void) {
11 Client *c, *min;
13 if((clients && clients->isfloat) || arrange == dofloat)
14 return clients; /* don't touch floating order */
15 for(min = c = clients; c; c = c->next)
16 if(c->weight < min->weight)
17 min = c;
18 return min;
19 }
21 static Client *
22 nexttiled(Client *c) {
23 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
24 return c;
25 }
27 static void
28 reorder(void) {
29 Client *c, *newclients, *tail;
31 newclients = tail = NULL;
32 while((c = minclient())) {
33 detach(c);
34 if(tail) {
35 c->prev = tail;
36 tail->next = c;
37 tail = c;
38 }
39 else
40 tail = newclients = c;
41 }
42 clients = newclients;
43 }
45 static void
46 togglemax(Client *c)
47 {
48 XEvent ev;
49 if((c->ismax = !c->ismax)) {
50 c->rx = c->x; c->x = sx;
51 c->ry = c->y; c->y = bh;
52 c->rw = c->w; c->w = sw - 2 * BORDERPX;
53 c->rh = c->h; c->h = sh - bh - 2 * BORDERPX;
54 }
55 else {
56 c->x = c->rx;
57 c->y = c->ry;
58 c->w = c->rw;
59 c->h = c->rh;
60 }
61 resize(c, True, TopLeft);
62 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
63 }
65 /* extern */
67 void (*arrange)(Arg *) = DEFMODE;
68 StackPos stackpos = STACKPOS;
70 void
71 detach(Client *c) {
72 if(c->prev)
73 c->prev->next = c->next;
74 if(c->next)
75 c->next->prev = c->prev;
76 if(c == clients)
77 clients = c->next;
78 c->next = c->prev = NULL;
79 }
81 void
82 dofloat(Arg *arg) {
83 Client *c;
85 for(c = clients; c; c = c->next) {
86 if(isvisible(c)) {
87 resize(c, True, TopLeft);
88 }
89 else
90 ban(c);
91 }
92 if(!sel || !isvisible(sel)) {
93 for(c = stack; c && !isvisible(c); c = c->snext);
94 focus(c);
95 }
96 restack();
97 }
99 /* This algorithm is based on a (M)aster area and a (S)tacking area.
100 * It supports following arrangements:
101 * SSMMM MMMMM MMMSS
102 * SSMMM SSSSS MMMSS
103 */
104 void
105 dotile(Arg *arg) {
106 unsigned int i, n, md, stackw, stackh, tw, th;
107 Client *c;
109 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
110 n++;
112 if(stackpos == StackBottom) {
113 md = ((sh - bh) * master) / 1000;
114 stackw = sw;
115 stackh = sh - bh - md;
116 }
117 else {
118 md = (sw * master) / 1000;
119 stackw = sw - md;
120 stackh = sh - bh;
121 }
123 tw = stackw;
124 if(n > 1)
125 th = stackh / (n - 1);
126 else
127 th = stackh;
129 for(i = 0, c = clients; c; c = c->next) {
130 if(isvisible(c)) {
131 if(c->isfloat) {
132 resize(c, True, TopLeft);
133 continue;
134 }
135 c->ismax = False;
136 c->x = sx;
137 c->y = sy + bh;
138 if(n == 1) { /* only 1 window */
139 c->w = sw - 2 * BORDERPX;
140 c->h = sh - 2 * BORDERPX - bh;
141 }
142 else if(i == 0) { /* master window */
143 if(stackpos == StackLeft)
144 c->x += stackw;
145 switch(stackpos) {
146 case StackLeft:
147 case StackRight:
148 c->w = md - 2 * BORDERPX;
149 c->h = sh - bh - 2 * BORDERPX;
150 break;
151 case StackBottom:
152 c->w = sw - 2 * BORDERPX;
153 c->h = md - 2 * BORDERPX;
154 break;
155 }
156 }
157 else { /* tile window */
158 if(stackpos == StackRight)
159 c->x += md;
160 if(th > bh) {
161 switch(stackpos) {
162 case StackLeft:
163 case StackRight:
164 c->y = sy + (i - 1) * th + bh;
165 if(i + 1 == n)
166 c->h = sh - c->y - 2 * BORDERPX;
167 break;
168 case StackBottom:
169 c->y = sy + md + (i - 1) * th + bh;
170 if(i + 1 == n)
171 c->h = sh - c->y - 2 * BORDERPX;
172 break;
173 }
174 c->w = tw - 2 * BORDERPX;
175 c->h = th - 2 * BORDERPX;
176 }
177 else { /* fallback if th < bh */
178 if(stackpos == StackBottom)
179 c->y += md;
180 c->w = stackw - 2 * BORDERPX;
181 c->h = stackh - 2 * BORDERPX;
182 }
183 }
184 resize(c, False, TopLeft);
185 i++;
186 }
187 else
188 ban(c);
189 }
190 if(!sel || !isvisible(sel)) {
191 for(c = stack; c && !isvisible(c); c = c->snext);
192 focus(c);
193 }
194 restack();
195 }
197 void
198 focusnext(Arg *arg) {
199 Client *c;
201 if(!sel)
202 return;
204 if(!(c = getnext(sel->next)))
205 c = getnext(clients);
206 if(c) {
207 focus(c);
208 restack();
209 }
210 }
212 void
213 focusprev(Arg *arg) {
214 Client *c;
216 if(!sel)
217 return;
219 if(!(c = getprev(sel->prev))) {
220 for(c = clients; c && c->next; c = c->next);
221 c = getprev(c);
222 }
223 if(c) {
224 focus(c);
225 restack();
226 }
227 }
229 Bool
230 isvisible(Client *c) {
231 unsigned int i;
233 for(i = 0; i < ntags; i++)
234 if(c->tags[i] && seltag[i])
235 return True;
236 return False;
237 }
239 void
240 resizecol(Arg *arg) {
241 unsigned int n;
242 Client *c;
244 for(n = 0, c = clients; c; c = c->next)
245 if(isvisible(c) && !c->isfloat)
246 n++;
247 if(!sel || sel->isfloat || n < 2 || (arrange == dofloat))
248 return;
250 if(sel == getnext(clients)) {
251 if(master + arg->i > 950 || master + arg->i < 50)
252 return;
253 master += arg->i;
254 }
255 else {
256 if(master - arg->i > 950 || master - arg->i < 50)
257 return;
258 master -= arg->i;
259 }
260 arrange(NULL);
261 }
263 void
264 restack(void) {
265 Client *c;
266 XEvent ev;
268 if(!sel) {
269 drawstatus();
270 return;
271 }
272 if(sel->isfloat || arrange == dofloat) {
273 XRaiseWindow(dpy, sel->win);
274 XRaiseWindow(dpy, sel->twin);
275 }
276 if(arrange != dofloat) {
277 if(!sel->isfloat) {
278 XLowerWindow(dpy, sel->twin);
279 XLowerWindow(dpy, sel->win);
280 }
281 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
282 if(c == sel)
283 continue;
284 XLowerWindow(dpy, c->twin);
285 XLowerWindow(dpy, c->win);
286 }
287 }
288 drawall();
289 XSync(dpy, False);
290 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
291 }
293 void
294 togglemode(Arg *arg) {
295 arrange = (arrange == dofloat) ? dotile : dofloat;
296 if(sel)
297 arrange(NULL);
298 else
299 drawstatus();
300 }
302 void
303 toggleview(Arg *arg) {
304 unsigned int i;
306 seltag[arg->i] = !seltag[arg->i];
307 for(i = 0; i < ntags && !seltag[i]; i++);
308 if(i == ntags)
309 seltag[arg->i] = True; /* cannot toggle last view */
310 reorder();
311 arrange(NULL);
312 }
314 void
315 togglestackpos(Arg *arg) {
316 if(arrange == dofloat)
317 return;
318 if(stackpos == StackBottom)
319 stackpos = STACKPOS;
320 else
321 stackpos = StackBottom;
322 arrange(NULL);
323 }
325 void
326 view(Arg *arg) {
327 unsigned int i;
329 for(i = 0; i < ntags; i++)
330 seltag[i] = False;
331 seltag[arg->i] = True;
332 reorder();
333 arrange(NULL);
334 }
336 void
337 viewall(Arg *arg) {
338 unsigned int i;
340 for(i = 0; i < ntags; i++)
341 seltag[i] = True;
342 reorder();
343 arrange(NULL);
344 }
348 void
349 zoom(Arg *arg) {
350 unsigned int n;
351 Client *c;
353 if(!sel)
354 return;
356 if(sel->isfloat || (arrange == dofloat)) {
357 togglemax(sel);
358 return;
359 }
361 for(n = 0, c = clients; c; c = c->next)
362 if(isvisible(c) && !c->isfloat)
363 n++;
364 if(n < 2 || (arrange == dofloat))
365 return;
367 if((c = sel) == nexttiled(clients))
368 if(!(c = nexttiled(c->next)))
369 return;
370 detach(c);
371 if(clients)
372 clients->prev = c;
373 c->next = clients;
374 clients = c;
375 focus(c);
376 arrange(NULL);
377 }