view.c (view raw)
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
9static Client *
10nexttiled(Client *c) {
11 for(; c && (c->isfloat || !isvisible(c)); c = c->next);
12 return c;
13}
14
15static void
16togglemax(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
36void (*arrange)(void) = DEFMODE;
37
38void
39detach(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
49void
50dofloat(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
72void
73dotile(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
124void
125focusnext(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
139void
140focusprev(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
156void
157incnmaster(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
168Bool
169isvisible(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
178void
179resizemaster(Arg *arg) {
180 if(arg->i == 0)
181 master = MASTER;
182 else {
183 if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
184 || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
185 return;
186 master += arg->i;
187 }
188 arrange();
189}
190
191void
192restack(void) {
193 Client *c;
194 XEvent ev;
195
196 drawstatus();
197 if(!sel)
198 return;
199 if(sel->isfloat || arrange == dofloat)
200 XRaiseWindow(dpy, sel->win);
201 if(arrange != dofloat) {
202 if(!sel->isfloat)
203 XLowerWindow(dpy, sel->win);
204 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
205 if(c == sel)
206 continue;
207 XLowerWindow(dpy, c->win);
208 }
209 }
210 XSync(dpy, False);
211 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
212}
213
214void
215togglefloat(Arg *arg) {
216 if(!sel || arrange == dofloat)
217 return;
218 sel->isfloat = !sel->isfloat;
219 arrange();
220}
221
222void
223togglemode(Arg *arg) {
224 arrange = (arrange == dofloat) ? dotile : dofloat;
225 if(sel)
226 arrange();
227 else
228 drawstatus();
229}
230
231void
232toggleview(Arg *arg) {
233 unsigned int i;
234
235 seltag[arg->i] = !seltag[arg->i];
236 for(i = 0; i < ntags && !seltag[i]; i++);
237 if(i == ntags)
238 seltag[arg->i] = True; /* cannot toggle last view */
239 arrange();
240}
241
242void
243view(Arg *arg) {
244 unsigned int i;
245
246 for(i = 0; i < ntags; i++)
247 seltag[i] = (arg->i == -1) ? True : False;
248 if(arg->i >= 0 && arg->i < ntags)
249 seltag[arg->i] = True;
250 arrange();
251}
252
253void
254zoom(Arg *arg) {
255 unsigned int n;
256 Client *c;
257
258 if(!sel)
259 return;
260 if(sel->isfloat || (arrange == dofloat)) {
261 togglemax(sel);
262 return;
263 }
264 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
265 n++;
266
267 if((c = sel) == nexttiled(clients))
268 if(!(c = nexttiled(c->next)))
269 return;
270 detach(c);
271 if(clients)
272 clients->prev = c;
273 c->next = clients;
274 clients = c;
275 focus(c);
276 arrange();
277}