simplifying regexps in config samples
[dwm.git] / view.c
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"
5 #include <stdio.h>
6
7 /* static */
8
9 static Client *
10 nexttiled(Client *c) {
11         for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
12         return c;
13 }
14
15 static void
16 togglemax(Client *c) {
17         XEvent ev;
18                 
19         if(c->isfixed)
20                 return;
21
22         if((c->ismax = !c->ismax)) {
23                 c->rx = c->x; c->x = wax;
24                 c->ry = c->y; c->y = way;
25                 c->rw = c->w; c->w = waw - 2 * BORDERPX;
26                 c->rh = c->h; c->h = wah - 2 * BORDERPX;
27         }
28         else {
29                 c->x = c->rx;
30                 c->y = c->ry;
31                 c->w = c->rw;
32                 c->h = c->rh;
33         }
34         resize(c, True);
35         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
36 }
37
38 /* extern */
39
40 void (*arrange)(void) = DEFMODE;
41
42 void
43 detach(Client *c) {
44         if(c->prev)
45                 c->prev->next = c->next;
46         if(c->next)
47                 c->next->prev = c->prev;
48         if(c == clients)
49                 clients = c->next;
50         c->next = c->prev = NULL;
51 }
52
53 void
54 dofloat(void) {
55         Client *c;
56
57         for(c = clients; c; c = c->next) {
58                 if(isvisible(c))
59                         resize(c, True);
60                 else
61                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
62         }
63         if(!sel || !isvisible(sel)) {
64                 for(c = stack; c && !isvisible(c); c = c->snext);
65                 focus(c);
66         }
67         restack();
68 }
69
70 void
71 dotile(void) {
72         unsigned int i, n, mw, mh, tw, th;
73         Client *c;
74
75         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
76                 n++;
77         /* window geoms */
78         mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
79         mw = (n > nmaster) ? (waw * master) / 1000 : waw;
80         th = (n > nmaster) ? wah / (n - nmaster) : 0;
81         tw = waw - mw;
82
83         for(i = 0, c = clients; c; c = c->next)
84                 if(isvisible(c)) {
85                         if(c->isfloat) {
86                                 resize(c, True);
87                                 continue;
88                         }
89                         c->ismax = False;
90                         c->x = wax;
91                         c->y = way;
92                         if(i < nmaster) {
93                                 c->y += i * mh;
94                                 c->w = mw - 2 * BORDERPX;
95                                 c->h = mh - 2 * BORDERPX;
96                         }
97                         else {  /* tile window */
98                                 c->x += mw;
99                                 c->w = tw - 2 * BORDERPX;
100                                 if(th > 2 * BORDERPX) {
101                                         c->y += (i - nmaster) * th;
102                                         c->h = th - 2 * BORDERPX;
103                                 }
104                                 else /* fallback if th <= 2 * BORDERPX */
105                                         c->h = wah - 2 * BORDERPX;
106                         }
107                         resize(c, False);
108                         i++;
109                 }
110                 else
111                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
112         if(!sel || !isvisible(sel)) {
113                 for(c = stack; c && !isvisible(c); c = c->snext);
114                 focus(c);
115         }
116         restack();
117 }
118
119 void
120 focusnext(Arg *arg) {
121         Client *c;
122    
123         if(!sel)
124                 return;
125         if(!(c = getnext(sel->next)))
126                 c = getnext(clients);
127         if(c) {
128                 focus(c);
129                 restack();
130         }
131 }
132
133 void
134 focusprev(Arg *arg) {
135         Client *c;
136
137         if(!sel)
138                 return;
139         if(!(c = getprev(sel->prev))) {
140                 for(c = clients; c && c->next; c = c->next);
141                 c = getprev(c);
142         }
143         if(c) {
144                 focus(c);
145                 restack();
146         }
147 }
148
149 void
150 incnmaster(Arg *arg) {
151         if((arrange == dofloat) || (nmaster + arg->i < 1)
152         || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
153                 return;
154         nmaster += arg->i;
155         if(sel)
156                 arrange();
157         else
158                 drawstatus();
159 }
160
161 Bool
162 isvisible(Client *c) {
163         unsigned int i;
164
165         for(i = 0; i < ntags; i++)
166                 if(c->tags[i] && seltag[i])
167                         return True;
168         return False;
169 }
170
171 void
172 resizemaster(Arg *arg) {
173         if(arg->i == 0)
174                 master = MASTER;
175         else {
176                 if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
177                 || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
178                         return;
179                 master += arg->i;
180         }
181         arrange();
182 }
183
184 void
185 restack(void) {
186         Client *c;
187         XEvent ev;
188
189         drawstatus();
190         if(!sel)
191                 return;
192         if(sel->isfloat || arrange == dofloat)
193                 XRaiseWindow(dpy, sel->win);
194         if(arrange != dofloat) {
195                 if(!sel->isfloat)
196                         XLowerWindow(dpy, sel->win);
197                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
198                         if(c == sel)
199                                 continue;
200                         XLowerWindow(dpy, c->win);
201                 }
202         }
203         XSync(dpy, False);
204         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
205 }
206
207 void
208 togglefloat(Arg *arg) {
209         if(!sel || arrange == dofloat)
210                 return;
211         sel->isfloat = !sel->isfloat;
212         arrange();
213 }
214
215 void
216 togglemode(Arg *arg) {
217         arrange = (arrange == dofloat) ? dotile : dofloat;
218         if(sel)
219                 arrange();
220         else
221                 drawstatus();
222 }
223
224 void
225 toggleview(Arg *arg) {
226         unsigned int i;
227
228         seltag[arg->i] = !seltag[arg->i];
229         for(i = 0; i < ntags && !seltag[i]; i++);
230         if(i == ntags)
231                 seltag[arg->i] = True; /* cannot toggle last view */
232         arrange();
233 }
234
235 void
236 view(Arg *arg) {
237         unsigned int i;
238
239         for(i = 0; i < ntags; i++)
240                 seltag[i] = (arg->i == -1) ? True : False;
241         if(arg->i >= 0 && arg->i < ntags)
242                 seltag[arg->i] = True;
243         arrange();
244 }
245
246 void
247 zoom(Arg *arg) {
248         unsigned int n;
249         Client *c;
250
251         if(!sel)
252                 return;
253         if(sel->isfloat || (arrange == dofloat)) {
254                 togglemax(sel);
255                 return;
256         }
257         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
258                 n++;
259
260         if((c = sel) == nexttiled(clients))
261                 if(!(c = nexttiled(c->next)))
262                         return;
263         detach(c);
264         if(clients)
265                 clients->prev = c;
266         c->next = clients;
267         clients = c;
268         focus(c);
269         arrange();
270 }