dwm-meillo

view view.c @ 708:a2d568a5cdb8

applied Sanders all5.patch (thanks for your weekend session, Sander!)
author Anselm R. Garbe <arg@suckless.org>
date Mon, 22 Jan 2007 10:22:58 +0100
parents 5d7e363f889d
children d3876aa79292
line source
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>
7 /* static */
9 static Client *
10 nexttiled(Client *c) {
11 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
12 return c;
13 }
15 static void
16 togglemax(Client *c) {
17 XEvent ev;
19 if(c->isfixed)
20 return;
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 }
38 /* extern */
40 void (*arrange)(void) = DEFMODE;
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 }
53 void
54 dofloat(void) {
55 Client *c;
57 for(c = clients; c; c = c->next) {
58 if(isvisible(c)) {
59 resize(c, True);
60 }
61 else
62 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
63 }
64 if(!sel || !isvisible(sel)) {
65 for(c = stack; c && !isvisible(c); c = c->snext);
66 focus(c);
67 }
68 restack();
69 }
71 void
72 dotile(void) {
73 unsigned int i, n, mw, mh, tw, th;
74 Client *c;
76 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
77 n++;
78 /* window geoms */
79 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
80 mw = (n > nmaster) ? (waw * master) / 1000 : waw;
81 th = (n > nmaster) ? wah / (n - nmaster) : 0;
82 tw = waw - mw;
84 for(i = 0, c = clients; c; c = c->next)
85 if(isvisible(c)) {
86 if(c->isfloat) {
87 resize(c, True);
88 continue;
89 }
90 c->ismax = False;
91 c->x = wax;
92 c->y = way;
93 if(i < nmaster) {
94 c->y += i * mh;
95 c->w = mw - 2 * BORDERPX;
96 c->h = mh - 2 * BORDERPX;
97 }
98 else { /* tile window */
99 c->x += mw;
100 c->w = tw - 2 * BORDERPX;
101 if(th > bh) {
102 c->y += (i - nmaster) * th;
103 c->h = th - 2 * BORDERPX;
104 }
105 else /* fallback if th < bh */
106 c->h = wah - 2 * BORDERPX;
107 }
108 resize(c, False);
109 i++;
110 }
111 else
112 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
113 if(!sel || !isvisible(sel)) {
114 for(c = stack; c && !isvisible(c); c = c->snext);
115 focus(c);
116 }
117 restack();
118 }
120 void
121 focusnext(Arg *arg) {
122 Client *c;
124 if(!sel)
125 return;
126 if(!(c = getnext(sel->next)))
127 c = getnext(clients);
128 if(c) {
129 focus(c);
130 restack();
131 }
132 }
134 void
135 focusprev(Arg *arg) {
136 Client *c;
138 if(!sel)
139 return;
140 if(!(c = getprev(sel->prev))) {
141 for(c = clients; c && c->next; c = c->next);
142 c = getprev(c);
143 }
144 if(c) {
145 focus(c);
146 restack();
147 }
148 }
150 void
151 incnmaster(Arg *arg) {
152 if((arrange == dofloat) || (nmaster + arg->i < 1) || (wah / (nmaster + arg->i) < bh))
153 return;
154 nmaster += arg->i;
155 if(sel)
156 arrange();
157 else
158 drawstatus();
159 }
161 Bool
162 isvisible(Client *c) {
163 unsigned int i;
165 for(i = 0; i < ntags; i++)
166 if(c->tags[i] && seltag[i])
167 return True;
168 return False;
169 }
171 void
172 resizemaster(Arg *arg) {
173 if(arg->i == 0)
174 master = MASTER;
175 else {
176 if(master + arg->i > 950 || master + arg->i < 50)
177 return;
178 master += arg->i;
179 }
180 arrange();
181 }
183 void
184 restack(void) {
185 Client *c;
186 XEvent ev;
188 drawstatus();
189 if(!sel)
190 return;
191 if(sel->isfloat || arrange == dofloat)
192 XRaiseWindow(dpy, sel->win);
193 if(arrange != dofloat) {
194 if(!sel->isfloat)
195 XLowerWindow(dpy, sel->win);
196 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
197 if(c == sel)
198 continue;
199 XLowerWindow(dpy, c->win);
200 }
201 }
202 XSync(dpy, False);
203 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
204 }
206 void
207 togglefloat(Arg *arg) {
208 if (!sel || arrange == dofloat)
209 return;
210 sel->isfloat = !sel->isfloat;
211 arrange();
212 }
214 void
215 togglemode(Arg *arg) {
216 arrange = (arrange == dofloat) ? dotile : dofloat;
217 if(sel)
218 arrange();
219 else
220 drawstatus();
221 }
223 void
224 toggleview(Arg *arg) {
225 unsigned int i;
227 seltag[arg->i] = !seltag[arg->i];
228 for(i = 0; i < ntags && !seltag[i]; i++);
229 if(i == ntags)
230 seltag[arg->i] = True; /* cannot toggle last view */
231 arrange();
232 }
234 void
235 view(Arg *arg) {
236 unsigned int i;
238 for(i = 0; i < ntags; i++)
239 seltag[i] = (arg->i == -1) ? True : False;
240 if(arg->i >= 0 && arg->i < ntags)
241 seltag[arg->i] = True;
242 arrange();
243 }
245 void
246 zoom(Arg *arg) {
247 unsigned int n;
248 Client *c;
250 if(!sel)
251 return;
252 if(sel->isfloat || (arrange == dofloat)) {
253 togglemax(sel);
254 return;
255 }
256 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
257 n++;
259 if((c = sel) == nexttiled(clients))
260 if(!(c = nexttiled(c->next)))
261 return;
262 detach(c);
263 if(clients)
264 clients->prev = c;
265 c->next = clients;
266 clients = c;
267 focus(c);
268 arrange();
269 }