dwm-meillo

view view.c @ 658:b4d1ef7e407f

fixing some minor issues
author Anselm R. Garbe <arg@suckless.org>
date Fri, 05 Jan 2007 16:35:45 +0100
parents c30805f6bb08
children 498b86c72a40
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 *c1p = c1->prev;
36 Client *c1n = c1->next;
37 Client *c2p = c2->prev;
38 Client *c2n = c2->next;
40 *c1 = *c2;
41 *c2 = tmp;
42 c1->prev = c1p;
43 c1->next = c1n;
44 c2->prev = c2p;
45 c2->next = c2n;
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;
198 arrange();
199 }
201 Bool
202 isvisible(Client *c) {
203 unsigned int i;
205 for(i = 0; i < ntags; i++)
206 if(c->tags[i] && seltag[i])
207 return True;
208 return False;
209 }
211 void
212 resizemaster(Arg *arg) {
213 if(arg->i == 0)
214 master = MASTER;
215 else {
216 if(master + arg->i > 950 || master + arg->i < 50)
217 return;
218 master += arg->i;
219 }
220 arrange();
221 }
223 void
224 restack(void) {
225 Client *c;
226 XEvent ev;
228 if(!sel) {
229 drawstatus();
230 return;
231 }
232 if(sel->isfloat || arrange == dofloat) {
233 XRaiseWindow(dpy, sel->win);
234 XRaiseWindow(dpy, sel->twin);
235 }
236 if(arrange != dofloat) {
237 if(!sel->isfloat) {
238 XLowerWindow(dpy, sel->twin);
239 XLowerWindow(dpy, sel->win);
240 }
241 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
242 if(c == sel)
243 continue;
244 XLowerWindow(dpy, c->twin);
245 XLowerWindow(dpy, c->win);
246 }
247 }
248 drawall();
249 XSync(dpy, False);
250 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
251 }
253 void
254 togglefloat(Arg *arg) {
255 if (!sel || arrange == dofloat)
256 return;
257 sel->isfloat = !sel->isfloat;
258 arrange();
259 }
261 void
262 togglemode(Arg *arg) {
263 arrange = (arrange == dofloat) ? dotile : dofloat;
264 if(sel)
265 arrange();
266 else
267 drawstatus();
268 }
270 void
271 toggleview(Arg *arg) {
272 unsigned int i;
274 seltag[arg->i] = !seltag[arg->i];
275 for(i = 0; i < ntags && !seltag[i]; i++);
276 if(i == ntags)
277 seltag[arg->i] = True; /* cannot toggle last view */
278 arrange();
279 }
281 void
282 view(Arg *arg) {
283 unsigned int i;
285 for(i = 0; i < ntags; i++)
286 seltag[i] = (arg->i == -1) ? True : False;
287 if(arg->i >= 0 && arg->i < ntags)
288 seltag[arg->i] = True;
289 arrange();
290 }
292 void
293 zoom(Arg *arg) {
294 unsigned int n;
295 Client *c;
297 if(!sel)
298 return;
299 if(sel->isfloat || (arrange == dofloat)) {
300 togglemax(sel);
301 return;
302 }
303 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
304 n++;
306 c = sel;
307 if(arrange == dofloat)
308 return;
309 else if(n <= nmaster)
310 pop(c);
311 else if(ismaster(sel)) {
312 if(!(c = topofstack()))
313 return;
314 swap(c, sel);
315 c = sel;
316 }
317 else
318 pop(c);
320 focus(c);
321 arrange();
322 }