dwm-meillo
view view.c @ 442:056a5072c70a
no this is better
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Wed, 06 Sep 2006 15:36:42 +0200 |
parents | 433a5c662f73 |
children | 548084f8d92e |
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
5 #include "dwm.h"
6 #include <stdio.h>
8 /* static */
10 static Client *
11 minclient()
12 {
13 Client *c, *min;
15 for(min = c = clients; c; c = c->next)
16 if(c->weight < min->weight)
17 min = c;
18 return min;
19 }
21 static void
22 pop(Client *c)
23 {
24 detach(c);
25 if(clients)
26 clients->prev = c;
27 c->next = clients;
28 clients = c;
29 }
31 static void
32 reorder()
33 {
34 Client *c, *newclients, *tail;
36 newclients = tail = NULL;
37 while((c = minclient())) {
38 detach(c);
39 if(tail) {
40 c->prev = tail;
41 tail->next = c;
42 tail = c;
43 }
44 else
45 tail = newclients = c;
46 }
47 clients = newclients;
48 }
50 static Client *
51 nexttiled(Client *c)
52 {
53 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
54 return c;
55 }
57 /* extern */
59 void (*arrange)(Arg *) = DEFMODE;
61 void
62 detach(Client *c)
63 {
64 if(c->prev)
65 c->prev->next = c->next;
66 if(c->next)
67 c->next->prev = c->prev;
68 if(c == clients)
69 clients = c->next;
70 c->next = c->prev = NULL;
71 }
73 void
74 dofloat(Arg *arg)
75 {
76 Client *c;
78 maximized = False;
80 for(c = clients; c; c = c->next) {
81 if(isvisible(c)) {
82 resize(c, True, TopLeft);
83 }
84 else
85 ban(c);
86 }
87 if(!sel || !isvisible(sel))
88 focus(getnext(clients));
89 restack();
90 }
92 void
93 dotile(Arg *arg)
94 {
95 int h, i, n, w;
96 Client *c;
98 maximized = False;
100 w = sw - mw;
101 for(n = 0, c = clients; c; c = c->next)
102 if(isvisible(c) && !c->isfloat)
103 n++;
105 if(n > 1)
106 h = (sh - bh) / (n - 1);
107 else
108 h = sh - bh;
110 for(i = 0, c = clients; c; c = c->next) {
111 if(isvisible(c)) {
112 if(c->isfloat) {
113 resize(c, True, TopLeft);
114 continue;
115 }
116 if(n == 1) {
117 c->x = sx;
118 c->y = sy + bh;
119 c->w = sw - 2;
120 c->h = sh - 2 - bh;
121 }
122 else if(i == 0) {
123 c->x = sx;
124 c->y = sy + bh;
125 c->w = mw - 2;
126 c->h = sh - 2 - bh;
127 }
128 else if(h > bh) {
129 c->x = sx + mw;
130 c->y = sy + (i - 1) * h + bh;
131 c->w = w - 2;
132 if(i + 1 == n)
133 c->h = sh - c->y - 2;
134 else
135 c->h = h - 2;
136 }
137 else { /* fallback if h < bh */
138 c->x = sx + mw;
139 c->y = sy + bh;
140 c->w = w - 2;
141 c->h = sh - 2 - bh;
142 }
143 resize(c, False, TopLeft);
144 i++;
145 }
146 else
147 ban(c);
148 }
149 if(!sel || !isvisible(sel))
150 focus(getnext(clients));
151 restack();
152 }
154 void
155 focusnext(Arg *arg)
156 {
157 Client *c;
159 if(!sel)
160 return;
162 if(!(c = getnext(sel->next)))
163 c = getnext(clients);
164 if(c) {
165 focus(c);
166 restack();
167 }
168 }
170 void
171 focusprev(Arg *arg)
172 {
173 Client *c;
175 if(!sel)
176 return;
178 if(!(c = getprev(sel->prev))) {
179 for(c = clients; c && c->next; c = c->next);
180 c = getprev(c);
181 }
182 if(c) {
183 focus(c);
184 restack();
185 }
186 }
188 Bool
189 isvisible(Client *c)
190 {
191 unsigned int i;
193 for(i = 0; i < ntags; i++)
194 if(c->tags[i] && seltag[i])
195 return True;
196 return False;
197 }
199 void
200 resizecol(Arg *arg)
201 {
202 unsigned int n;
203 Client *c;
205 for(n = 0, c = clients; c; c = c->next)
206 if(isvisible(c) && !c->isfloat)
207 n++;
208 if(!sel || sel->isfloat || n < 2 || (arrange != dotile) || maximized)
209 return;
211 if(sel == getnext(clients)) {
212 if(mw + arg->i > sw - 100 || mw + arg->i < 100)
213 return;
214 mw += arg->i;
215 }
216 else {
217 if(mw - arg->i > sw - 100 || mw - arg->i < 100)
218 return;
219 mw -= arg->i;
220 }
221 arrange(NULL);
222 }
224 void
225 restack()
226 {
227 Client *c;
228 XEvent ev;
230 if(!sel) {
231 drawstatus();
232 return;
233 }
234 if(sel->isfloat || arrange == dofloat) {
235 pop(sel);
236 XRaiseWindow(dpy, sel->win);
237 XRaiseWindow(dpy, sel->twin);
238 }
239 if(arrange != dofloat)
240 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
241 XLowerWindow(dpy, c->twin);
242 XLowerWindow(dpy, c->win);
243 }
244 drawall();
245 XSync(dpy, False);
246 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
247 }
249 void
250 togglemode(Arg *arg)
251 {
252 arrange = (arrange == dofloat) ? dotile : dofloat;
253 if(sel)
254 arrange(NULL);
255 else
256 drawstatus();
257 }
259 void
260 toggleview(Arg *arg)
261 {
262 unsigned int i;
264 seltag[arg->i] = !seltag[arg->i];
265 for(i = 0; i < ntags && !seltag[i]; i++);
266 if(i == ntags)
267 seltag[arg->i] = True; /* cannot toggle last view */
268 reorder();
269 arrange(NULL);
270 }
272 void
273 view(Arg *arg)
274 {
275 unsigned int i;
277 for(i = 0; i < ntags; i++)
278 seltag[i] = False;
279 seltag[arg->i] = True;
280 reorder();
281 arrange(NULL);
282 }
284 void
285 viewall(Arg *arg)
286 {
287 unsigned int i;
289 for(i = 0; i < ntags; i++)
290 seltag[i] = True;
291 reorder();
292 arrange(NULL);
293 }
295 void
296 zoom(Arg *arg)
297 {
298 unsigned int n;
299 Client *c;
301 for(n = 0, c = clients; c; c = c->next)
302 if(isvisible(c) && !c->isfloat)
303 n++;
304 if(!sel || sel->isfloat || n < 2 || (arrange != dotile) || maximized)
305 return;
307 if((c = sel) == nexttiled(clients))
308 if(!(c = nexttiled(c->next)))
309 return;
310 pop(c);
311 focus(c);
312 arrange(NULL);
313 }