Xinqi Bao's Git

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