dwm-meillo

view view.c @ 656:c30805f6bb08

adding some prevention that master clients get smaller than bh
author Anselm R. Garbe <arg@suckless.org>
date Fri, 05 Jan 2007 15:16:39 +0100
parents 04734db9a219
children b4d1ef7e407f
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
6 /* static */
8 static Client *
9 nexttiled(Client *c) {
10 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
11 return c;
12 }
14 static Bool
15 ismaster(Client *c) {
16 Client *cl;
17 unsigned int i;
19 for(cl = nexttiled(clients), i = 0; cl && cl != c; cl = nexttiled(cl->next), i++);
20 return i < nmaster;
21 }
23 static void
24 pop(Client *c) {
25 detach(c);
26 if(clients)
27 clients->prev = c;
28 c->next = clients;
29 clients = c;
30 }
32 static void
33 swap(Client *c1, Client *c2) {
34 Client tmp = *c1;
35 Client *cp = c1->prev;
36 Client *cn = c1->next;
38 *c1 = *c2;
39 c1->prev = cp;
40 c1->next = cn;
41 cp = c2->prev;
42 cn = c2->next;
43 *c2 = tmp;
44 c2->prev = cp;
45 c2->next = cn;
46 }
48 static void
49 togglemax(Client *c) {
50 XEvent ev;
52 if(c->isfixed)
53 return;
55 if((c->ismax = !c->ismax)) {
56 c->rx = c->x; c->x = wax;
57 c->ry = c->y; c->y = way;
58 c->rw = c->w; c->w = waw - 2 * BORDERPX;
59 c->rh = c->h; c->h = wah - 2 * BORDERPX;
60 }
61 else {
62 c->x = c->rx;
63 c->y = c->ry;
64 c->w = c->rw;
65 c->h = c->rh;
66 }
67 resize(c, True, TopLeft);
68 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
69 }
71 static Client *
72 topofstack() {
73 Client *c;
74 unsigned int i;
76 for(c = nexttiled(clients), i = 0; c && i < nmaster; c = nexttiled(c->next), i++);
77 return (i < nmaster) ? NULL : c;
78 }
80 /* extern */
82 void (*arrange)(void) = DEFMODE;
84 void
85 detach(Client *c) {
86 if(c->prev)
87 c->prev->next = c->next;
88 if(c->next)
89 c->next->prev = c->prev;
90 if(c == clients)
91 clients = c->next;
92 c->next = c->prev = NULL;
93 }
95 void
96 dofloat(void) {
97 Client *c;
99 for(c = clients; c; c = c->next) {
100 if(isvisible(c)) {
101 resize(c, True, TopLeft);
102 }
103 else
104 ban(c);
105 }
106 if(!sel || !isvisible(sel)) {
107 for(c = stack; c && !isvisible(c); c = c->snext);
108 focus(c);
109 }
110 restack();
111 }
113 void
114 dotile(void) {
115 unsigned int i, n, mw, mh, tw, th;
116 Client *c;
118 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
119 n++;
120 /* window geoms */
121 mw = (n > nmaster) ? (waw * master) / 1000 : waw;
122 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
123 tw = waw - mw;
124 th = (n > nmaster) ? wah / (n - nmaster) : 0;
126 for(i = 0, c = clients; c; c = c->next)
127 if(isvisible(c)) {
128 if(c->isfloat) {
129 resize(c, True, TopLeft);
130 continue;
131 }
132 c->ismax = False;
133 c->x = wax;
134 c->y = way;
135 if(i < nmaster) {
136 c->y += i * mh;
137 c->w = mw - 2 * BORDERPX;
138 c->h = mh - 2 * BORDERPX;
139 }
140 else { /* tile window */
141 c->x += mw;
142 c->w = tw - 2 * BORDERPX;
143 if(th > bh) {
144 c->y += (i - nmaster) * th;
145 c->h = th - 2 * BORDERPX;
146 }
147 else /* fallback if th < bh */
148 c->h = wah - 2 * BORDERPX;
149 }
150 resize(c, False, TopLeft);
151 i++;
152 }
153 else
154 ban(c);
156 if(!sel || !isvisible(sel)) {
157 for(c = stack; c && !isvisible(c); c = c->snext);
158 focus(c);
159 }
160 restack();
161 }
163 void
164 focusnext(Arg *arg) {
165 Client *c;
167 if(!sel)
168 return;
169 if(!(c = getnext(sel->next)))
170 c = getnext(clients);
171 if(c) {
172 focus(c);
173 restack();
174 }
175 }
177 void
178 focusprev(Arg *arg) {
179 Client *c;
181 if(!sel)
182 return;
183 if(!(c = getprev(sel->prev))) {
184 for(c = clients; c && c->next; c = c->next);
185 c = getprev(c);
186 }
187 if(c) {
188 focus(c);
189 restack();
190 }
191 }
193 void
194 incnmaster(Arg *arg) {
195 if(nmaster + arg->i < 1 || (wah / (nmaster + arg->i) < bh))
196 return;
197 nmaster += arg->i;
199 arrange();
200 }
202 Bool
203 isvisible(Client *c) {
204 unsigned int i;
206 for(i = 0; i < ntags; i++)
207 if(c->tags[i] && seltag[i])
208 return True;
209 return False;
210 }
212 void
213 resizemaster(Arg *arg) {
214 if(arg->i == 0)
215 master = MASTER;
216 else {
217 if(master + arg->i > 950 || master + arg->i < 50)
218 return;
219 master += arg->i;
220 }
221 arrange();
222 }
224 void
225 restack(void) {
226 Client *c;
227 XEvent ev;
229 if(!sel) {
230 drawstatus();
231 return;
232 }
233 if(sel->isfloat || arrange == dofloat) {
234 XRaiseWindow(dpy, sel->win);
235 XRaiseWindow(dpy, sel->twin);
236 }
237 if(arrange != dofloat) {
238 if(!sel->isfloat) {
239 XLowerWindow(dpy, sel->twin);
240 XLowerWindow(dpy, sel->win);
241 }
242 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
243 if(c == sel)
244 continue;
245 XLowerWindow(dpy, c->twin);
246 XLowerWindow(dpy, c->win);
247 }
248 }
249 drawall();
250 XSync(dpy, False);
251 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
252 }
254 void
255 togglefloat(Arg *arg) {
256 if (!sel || arrange == dofloat)
257 return;
258 sel->isfloat = !sel->isfloat;
259 arrange();
260 }
262 void
263 togglemode(Arg *arg) {
264 arrange = (arrange == dofloat) ? dotile : dofloat;
265 if(sel)
266 arrange();
267 else
268 drawstatus();
269 }
271 void
272 toggleview(Arg *arg) {
273 unsigned int i;
275 seltag[arg->i] = !seltag[arg->i];
276 for(i = 0; i < ntags && !seltag[i]; i++);
277 if(i == ntags)
278 seltag[arg->i] = True; /* cannot toggle last view */
279 arrange();
280 }
282 void
283 view(Arg *arg) {
284 unsigned int i;
286 for(i = 0; i < ntags; i++)
287 seltag[i] = (arg->i == -1) ? True : False;
288 if(arg->i >= 0 && arg->i < ntags)
289 seltag[arg->i] = True;
290 arrange();
291 }
293 void
294 zoom(Arg *arg) {
295 unsigned int n;
296 Client *c;
298 if(!sel)
299 return;
300 if(sel->isfloat || (arrange == dofloat)) {
301 togglemax(sel);
302 return;
303 }
304 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
305 n++;
307 c = sel;
308 if(n <= nmaster || (arrange == dofloat))
309 pop(c);
310 else if(ismaster(sel)) {
311 if(!(c = topofstack()))
312 return;
313 swap(c, sel);
314 c = sel;
315 }
316 else
317 pop(c);
319 focus(c);
320 arrange();
321 }