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