aewl

view view.c @ 400:052657ff2e7b

applied Sanders max_and_focus.patch
author Anselm R. Garbe <arg@10kloc.org>
date Mon, 04 Sep 2006 08:55:49 +0200
parents cb8a231610c7
children 1eb2eb405653
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 }
22 static void
23 reorder()
24 {
25 Client *c, *newclients, *tail;
27 newclients = tail = NULL;
28 while((c = minclient())) {
29 detach(c);
30 if(tail) {
31 c->prev = tail;
32 tail->next = c;
33 tail = c;
34 }
35 else
36 tail = newclients = c;
37 }
38 clients = newclients;
39 }
41 /* extern */
43 void (*arrange)(Arg *) = DEFMODE;
45 void
46 detach(Client *c)
47 {
48 if(c->prev)
49 c->prev->next = c->next;
50 if(c->next)
51 c->next->prev = c->prev;
52 if(c == clients)
53 clients = c->next;
54 c->next = c->prev = NULL;
55 }
57 void
58 dofloat(Arg *arg)
59 {
60 Client *c, *fc;
62 maximized = False;
64 for(c = clients; c; c = c->next) {
65 if(isvisible(c)) {
66 resize(c, True, TopLeft);
67 }
68 else
69 ban(c);
70 }
71 if(!(fc = sel) || !isvisible(fc))
72 fc = getnext(clients);
73 focus(fc);
74 restack();
75 }
77 void
78 dotile(Arg *arg)
79 {
80 int h, i, n, w;
81 Client *c, *fc;
83 maximized = False;
85 w = sw - mw;
86 for(n = 0, c = clients; c; c = c->next)
87 if(isvisible(c) && !c->isfloat)
88 n++;
90 if(n > 1)
91 h = (sh - bh) / (n - 1);
92 else
93 h = sh - bh;
95 for(i = 0, c = clients; c; c = c->next) {
96 if(isvisible(c)) {
97 if(c->isfloat) {
98 resize(c, True, TopLeft);
99 continue;
100 }
101 if(n == 1) {
102 c->x = sx;
103 c->y = sy + bh;
104 c->w = sw - 2;
105 c->h = sh - 2 - bh;
106 }
107 else if(i == 0) {
108 c->x = sx;
109 c->y = sy + bh;
110 c->w = mw - 2;
111 c->h = sh - 2 - bh;
112 }
113 else if(h > bh) {
114 c->x = sx + mw;
115 c->y = sy + (i - 1) * h + bh;
116 c->w = w - 2;
117 if(i + 1 == n)
118 c->h = sh - c->y - 2;
119 else
120 c->h = h - 2;
121 }
122 else { /* fallback if h < bh */
123 c->x = sx + mw;
124 c->y = sy + bh;
125 c->w = w - 2;
126 c->h = sh - 2 - bh;
127 }
128 resize(c, False, TopLeft);
129 i++;
130 }
131 else
132 ban(c);
133 }
134 if(!(fc = sel) || !isvisible(fc))
135 fc = getnext(clients);
136 focus(fc);
137 restack();
138 }
140 void
141 focusnext(Arg *arg)
142 {
143 Client *c;
145 if(!sel)
146 return;
148 if(!(c = getnext(sel->next)))
149 c = getnext(clients);
150 if(c) {
151 focus(c);
152 restack();
153 }
154 }
156 void
157 focusprev(Arg *arg)
158 {
159 Client *c;
161 if(!sel)
162 return;
164 if(!(c = getprev(sel->prev))) {
165 for(c = clients; c && c->next; c = c->next);
166 c = getprev(c);
167 }
168 if(c) {
169 focus(c);
170 restack();
171 }
172 }
174 Bool
175 isvisible(Client *c)
176 {
177 unsigned int i;
179 for(i = 0; i < ntags; i++)
180 if(c->tags[i] && seltag[i])
181 return True;
182 return False;
183 }
185 void
186 restack()
187 {
188 static unsigned int nwins = 0;
189 static Window *wins = NULL;
190 unsigned int f, fi, m, mi, n;
191 Client *c;
192 XEvent ev;
194 for(f = 0, m = 0, c = clients; c; c = c->next)
195 if(isvisible(c)) {
196 if(c->isfloat || arrange == dofloat)
197 f++;
198 else
199 m++;
200 }
201 if(!(n = 2 * (f + m))) {
202 drawstatus();
203 return;
204 }
205 if(nwins < n) {
206 nwins = n;
207 wins = erealloc(wins, nwins * sizeof(Window));
208 }
210 fi = 0;
211 mi = 2 * f;
212 if(sel->isfloat || arrange == dofloat) {
213 wins[fi++] = sel->twin;
214 wins[fi++] = sel->win;
215 }
216 else {
217 wins[mi++] = sel->twin;
218 wins[mi++] = sel->win;
219 }
220 for(c = clients; c; c = c->next)
221 if(isvisible(c) && c != sel) {
222 if(c->isfloat || arrange == dofloat) {
223 wins[fi++] = c->twin;
224 wins[fi++] = c->win;
225 }
226 else {
227 wins[mi++] = c->twin;
228 wins[mi++] = c->win;
229 }
230 }
231 XRestackWindows(dpy, wins, n);
232 drawall();
233 XSync(dpy, False);
234 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
235 }
237 void
238 togglemode(Arg *arg)
239 {
240 arrange = (arrange == dofloat) ? dotile : dofloat;
241 if(sel)
242 arrange(NULL);
243 else
244 drawstatus();
245 }
247 void
248 toggleview(Arg *arg)
249 {
250 unsigned int i;
252 seltag[arg->i] = !seltag[arg->i];
253 for(i = 0; i < ntags && !seltag[i]; i++);
254 if(i == ntags)
255 seltag[arg->i] = True; /* cannot toggle last view */
256 reorder();
257 arrange(NULL);
258 }
260 void
261 view(Arg *arg)
262 {
263 unsigned int i;
265 for(i = 0; i < ntags; i++)
266 seltag[i] = False;
267 seltag[arg->i] = True;
268 reorder();
269 arrange(NULL);
270 }
272 void
273 viewall(Arg *arg)
274 {
275 unsigned int i;
277 for(i = 0; i < ntags; i++)
278 seltag[i] = True;
279 reorder();
280 arrange(NULL);
281 }
283 void
284 zoom(Arg *arg)
285 {
286 Client *c = sel;
288 if(!c || (arrange != dotile) || c->isfloat || maximized)
289 return;
291 if(c == getnext(clients))
292 if(!(c = getnext(c->next)))
293 return;
294 detach(c);
295 c->next = clients;
296 clients->prev = c;
297 clients = c;
298 focus(c);
299 arrange(NULL);
300 }