changed -v output (now also anydot and nsz are contained in this output)
[dwm.git] / layout.c
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2  * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3  * © 2007 Premysl Hruby <dfenze at gmail dot com>
4  * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
5  * See LICENSE file for license details. */
6 #include "dwm.h"
7 #include <stdlib.h>
8
9 unsigned int blw = 0;
10 Layout *lt = NULL;
11
12 /* static */
13
14 static unsigned int nlayouts = 0;
15 static unsigned int masterw = MASTERWIDTH;
16 static unsigned int nmaster = NMASTER;
17
18 static void
19 ban(Client *c) {
20         if (c->isbanned)
21                 return;
22         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
23         c->isbanned = True;
24 }
25
26 static void
27 unban(Client *c) {
28         if (!c->isbanned)
29                 return;
30         XMoveWindow(dpy, c->win, c->x, c->y);
31         c->isbanned = False;
32 }
33
34 static void
35 tile(void) {
36         unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
37         Client *c;
38
39         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
40                 n++;
41         /* window geoms */
42         mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
43         mw = (n > nmaster) ? (waw * masterw) / 1000 : waw;
44         th = (n > nmaster) ? wah / (n - nmaster) : 0;
45         tw = waw - mw;
46
47         for(i = 0, c = clients; c; c = c->next)
48                 if(isvisible(c)) {
49                         unban(c);
50                         if(c->isfloating)
51                                 continue;
52                         c->ismax = False;
53                         nx = wax;
54                         ny = way;
55                         if(i < nmaster) {
56                                 ny += i * mh;
57                                 nw = mw - 2 * c->border;
58                                 nh = mh;
59                                 if(i + 1 == (n < nmaster ? n : nmaster)) /* remainder */
60                                         nh = wah - mh * i;
61                                 nh -= 2 * c->border;
62                         }
63                         else {  /* tile window */
64                                 nx += mw;
65                                 nw = tw - 2 * c->border;
66                                 if(th > 2 * c->border) {
67                                         ny += (i - nmaster) * th;
68                                         nh = th;
69                                         if(i + 1 == n) /* remainder */
70                                                 nh = wah - th * (i - nmaster);
71                                         nh -= 2 * c->border;
72                                 }
73                                 else /* fallback if th <= 2 * c->border */
74                                         nh = wah - 2 * c->border;
75                         }
76                         resize(c, nx, ny, nw, nh, False);
77                         i++;
78                 }
79                 else
80                         ban(c);
81         focus(NULL);
82         restack();
83 }
84
85 LAYOUTS
86
87 /* extern */
88
89 void
90 floating(void) {
91         Client *c;
92
93         for(c = clients; c; c = c->next)
94                 if(isvisible(c)) {
95                         if(c->isbanned)
96                                 XMoveWindow(dpy, c->win, c->x, c->y);
97                         c->isbanned = False;
98                         resize(c, c->x, c->y, c->w, c->h, True);
99                 }
100                 else
101                         ban(c);
102         focus(NULL);
103         restack();
104 }
105
106 void
107 focusclient(const char *arg) {
108         Client *c;
109    
110         if(!sel || !arg)
111                 return;
112         if(atoi(arg) < 0) {
113                 for(c = sel->prev; c && !isvisible(c); c = c->prev);
114                 if(!c) {
115                         for(c = clients; c && c->next; c = c->next);
116                         for(; c && !isvisible(c); c = c->prev);
117                 }
118         }
119         else {
120                 for(c = sel->next; c && !isvisible(c); c = c->next);
121                 if(!c)
122                         for(c = clients; c && !isvisible(c); c = c->next);
123         }
124         if(c) {
125                 focus(c);
126                 restack();
127         }
128 }
129
130 void
131 incmasterw(const char *arg) {
132         int i;
133         if(lt->arrange != tile)
134                 return;
135         if(!arg)
136                 masterw = MASTERWIDTH;
137         else {
138                 i = atoi(arg);
139                 if(waw * (masterw + i) / 1000 >= waw - 2 * BORDERPX 
140                 || waw * (masterw + i) / 1000 <= 2 * BORDERPX)
141                         return;
142                 masterw += i;
143         }
144         lt->arrange();
145 }
146
147 void
148 incnmaster(const char *arg) {
149         int i;
150
151         if(!arg)
152                 nmaster = NMASTER;
153         else {
154                 i = atoi(arg);
155                 if((lt->arrange != tile) || (nmaster + i < 1)
156                 || (wah / (nmaster + i) <= 2 * BORDERPX))
157                         return;
158                 nmaster += i;
159         }
160         if(sel)
161                 lt->arrange();
162         else
163                 drawstatus();
164 }
165
166 void
167 initlayouts(void) {
168         unsigned int i, w;
169
170         lt = &layout[0];
171         nlayouts = sizeof layout / sizeof layout[0];
172         for(blw = i = 0; i < nlayouts; i++) {
173                 w = textw(layout[i].symbol);
174                 if(w > blw)
175                         blw = w;
176         }
177 }
178
179 Client *
180 nexttiled(Client *c) {
181         for(; c && (c->isfloating || !isvisible(c)); c = c->next);
182         return c;
183 }
184
185 void
186 restack(void) {
187         Client *c;
188         XEvent ev;
189
190         drawstatus();
191         if(!sel)
192                 return;
193         if(sel->isfloating || lt->arrange == floating)
194                 XRaiseWindow(dpy, sel->win);
195         if(lt->arrange != floating) {
196                 if(!sel->isfloating)
197                         XLowerWindow(dpy, sel->win);
198                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
199                         if(c == sel)
200                                 continue;
201                         XLowerWindow(dpy, c->win);
202                 }
203         }
204         XSync(dpy, False);
205         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
206 }
207
208 void
209 setlayout(const char *arg) {
210         int i;
211
212         if(!arg) {
213                 lt++;
214                 if(lt == layout + nlayouts)
215                         lt = layout;
216         }
217         else {
218                 i = atoi(arg);
219                 if(i < 0 || i >= nlayouts)
220                         return;
221                 lt = &layout[i];
222         }
223         if(sel)
224                 lt->arrange();
225         else
226                 drawstatus();
227 }
228
229 void
230 togglebar(const char *arg) {
231         if(bpos == BarOff)
232                 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
233         else
234                 bpos = BarOff;
235         updatebarpos();
236         lt->arrange();
237 }
238
239 void
240 togglemax(const char *arg) {
241         XEvent ev;
242
243         if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed)
244                 return;
245         if((sel->ismax = !sel->ismax)) {
246                 sel->rx = sel->x;
247                 sel->ry = sel->y;
248                 sel->rw = sel->w;
249                 sel->rh = sel->h;
250                 resize(sel, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
251         }
252         else
253                 resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
254         drawstatus();
255         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
256 }
257
258 void
259 zoom(const char *arg) {
260         Client *c;
261
262         if(!sel || lt->arrange == floating || sel->isfloating)
263                 return;
264         if((c = sel) == nexttiled(clients))
265                 if(!(c = nexttiled(c->next)))
266                         return;
267         detach(c);
268         attach(c);
269         focus(c);
270         lt->arrange();
271 }