view layout.c @ 16:cb5813c117e2

merge heads
author paulo
date Mon, 09 Apr 2018 20:13:53 -0600
parents 162accc5d36d
children
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"
6 unsigned int master = MASTER;
7 unsigned int nmaster = NMASTER;
8 unsigned int blw = 0;
9 Layout *lt = NULL;
11 /* static */
13 static unsigned int nlayouts = 0;
15 static void
16 spiral(void) {
17 unsigned int i, n, nx, ny, nw, nh;
18 Client *c;
20 nx = wax;
21 ny = way + wah;
22 nw = waw;
23 nh = wah;
24 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
25 n++;
26 for(i = 0, c = clients; c; c = c->next)
27 if(isvisible(c)) {
28 if(c->isbanned)
29 XMoveWindow(dpy, c->win, c->x, c->y);
30 c->isbanned = False;
31 if(c->isversatile)
32 continue;
33 c->ismax = False;
34 if((i % 2 && nh / 2 > 2 * c->border)
35 || (!(i % 2) && nw / 2 > 2 * c->border))
36 {
37 if(i < n - 1) {
38 if(i % 2)
39 nh /= 2;
40 else
41 nw /= 2;
42 if((i % 4) == 2)
43 nx += nw;
44 else if((i % 4) == 3)
45 ny += nh;
46 }
47 if((i % 4) == 0)
48 ny -= nh;
49 else if((i % 4) == 1)
50 nx += nw;
51 else if((i % 4) == 2)
52 ny += nh;
53 else
54 nx -= nw;
55 i++;
56 }
57 resize(c, nx, ny, nw - 2 * c->border, nh - 2 * c->border, False);
58 }
59 else {
60 c->isbanned = True;
61 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
62 }
63 if(!sel || !isvisible(sel)) {
64 for(c = stack; c && !isvisible(c); c = c->snext);
65 focus(c);
66 }
67 restack();
68 }
70 static void
71 dwindle_m(void) {
72 unsigned int i, j, n, nx, ny, nw, nh, mn, mr, mx, mw;
73 unsigned int m = 2;
74 //unsigned int m = nmaster;
75 Client *c;
77 nx = wax;
78 //ny = way + wah;
79 ny = 0;
80 nw = waw;
81 nh = wah;
82 mx = nx;
83 mw = nw / m;
84 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
85 n++;
86 mn = n / m;
87 mr = n % m;
88 for(i = 0, j = 0, c = clients; c; c = c->next)
89 if(isvisible(c)) {
90 if(c->isbanned)
91 XMoveWindow(dpy, c->win, c->x, c->y);
92 c->isbanned = False;
93 if(c->isversatile)
94 continue;
95 c->ismax = False;
96 if((i % 2 && nh / 2 > 2 * c->border)
97 || (!(i % 2) && nw / 2 > 2 * c->border))
98 {
99 if((j % m) == 0) {
100 if(i < mn - (mr > 0 ? 0 : 1)) {
101 if(i % 2)
102 nh /= 2;
103 else {
104 nw /= 2;
105 mw = nw / m;
106 }
107 }
108 else if(mr)
109 mw = nw / mr;
110 if((i % 4) == 0)
111 //ny -= nh;
112 ny += (i == 0 ? way : nh);
113 else if((i % 4) == 1)
114 nx += nw;
115 else if((i % 4) == 2)
116 ny += nh;
117 else
118 //nx -= nw;
119 nx += nw;
120 i++;
121 }
122 mx = nx + (j % m) * mw;
123 j++;
124 }
125 resize(c, mx, ny, mw - 2 * c->border, nh - 2 * c->border, False);
126 }
127 else {
128 c->isbanned = True;
129 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
130 }
131 if(!sel || !isvisible(sel)) {
132 for(c = stack; c && !isvisible(c); c = c->snext);
133 focus(c);
134 }
135 restack();
136 }
138 static void
139 spiral_h(void) {
140 unsigned int i, n, nx, ny, nw, nh;
141 Client *c;
143 nx = wax + waw;
144 ny = way;
145 nw = waw;
146 nh = wah;
147 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
148 n++;
149 for(i = 0, c = clients; c; c = c->next)
150 if(isvisible(c)) {
151 if(c->isbanned)
152 XMoveWindow(dpy, c->win, c->x, c->y);
153 c->isbanned = False;
154 if(c->isversatile)
155 continue;
156 c->ismax = False;
157 if((i % 2 && nw / 2 > 2 * c->border)
158 || (!(i % 2) && nh / 2 > 2 * c->border))
159 {
160 if(i < n - 1) {
161 if(i % 2)
162 nw /= 2;
163 else
164 nh /= 2;
165 if((i % 4) == 2)
166 ny += nh;
167 else if((i % 4) == 3)
168 nx += nw;
169 }
170 if((i % 4) == 0)
171 nx -= nw;
172 else if((i % 4) == 1)
173 ny += nh;
174 else if((i % 4) == 2)
175 nx += nw;
176 else
177 ny -= nh;
178 i++;
179 }
180 resize(c, nx, ny, nw - 2 * c->border, nh - 2 * c->border, False);
181 }
182 else {
183 c->isbanned = True;
184 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
185 }
186 if(!sel || !isvisible(sel)) {
187 for(c = stack; c && !isvisible(c); c = c->snext);
188 focus(c);
189 }
190 restack();
191 }
193 static void
194 monocle(void) {
195 Client *c;
197 for(c = clients; c; c = c->next)
198 if(isvisible(c) && c->isbanned) {
199 XMoveWindow(dpy, c->win, c->x, c->y);
200 c->isbanned = False;
201 }
202 else if(!isvisible(c)) {
203 c->isbanned = True;
204 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
205 }
207 for(c = nexttiled(clients); c; c = nexttiled(c->next))
208 resize(c, wax, way, waw - 2*c->border, wah - 2*c->border, False);
210 restack();
211 }
213 static void
214 bstack(void) {
215 unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
216 Client *c;
218 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
219 n++;
220 /* window geoms */
221 mh = (n > nmaster) ? (wah * master) / 1000 : wah;
222 mw = (n > nmaster) ? waw / nmaster : waw / (n > 0 ? n : 1);
223 th = wah - mh;
224 tw = (n > nmaster) ? waw / (n - nmaster) : 0;
226 for(i = 0, c = clients; c; c = c->next)
227 if(isvisible(c)) {
228 if(c->isbanned)
229 XMoveWindow(dpy, c->win, c->x, c->y);
230 c->isbanned = False;
231 if(c->isversatile)
232 continue;
233 c->ismax = False;
234 nx = wax;
235 ny = way;
236 if(i < nmaster) {
237 nx += i * mw;
238 nw = mw - 2 * BORDERPX;
239 nh = mh - 2 * BORDERPX;
240 }
241 else { /* tile window */
242 ny += mh;
243 nh = th - 2 * BORDERPX;
244 if(tw > 2 * BORDERPX) {
245 nx += (i - nmaster) * tw;
246 nw = tw - 2 * BORDERPX;
247 }
248 else /* fallback if th <= 2 * BORDERPX */
249 nw = waw - 2 * BORDERPX;
250 }
251 resize(c, nx, ny, nw, nh, False);
252 i++;
253 }
254 else {
255 c->isbanned = True;
256 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
257 }
258 if(!sel || !isvisible(sel)) {
259 for(c = stack; c && !isvisible(c); c = c->snext);
260 focus(c);
261 }
262 restack();
263 }
265 static void
266 grid(void) {
267 unsigned int n, cols, rows, cn, rn, i, cx, cy, cw, ch;
268 Client *c;
270 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
271 n++;
272 if(n == 0)
273 return;
275 /* grid dimensions */
276 for(cols = 0; cols <= n/2; cols++)
277 if(cols*cols >= n)
278 break;
279 if(n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */
280 cols = 2;
281 rows = n/cols;
283 /* window geometries */
284 cw = cols ? waw / cols : waw;
285 cn = 0; /* current column number */
286 rn = 0; /* current row number */
287 for(i = 0, c = clients; c; c = c->next)
288 if(isvisible(c)) {
289 if(i/rows + 1 > cols - n%cols)
290 rows = n/cols + 1;
291 ch = rows ? wah / rows : wah;
292 cx = cn*cw;
293 cy = (TOPBAR ? dc.h : 0) + rn*ch;
294 resize(c, cx, cy, cw - 2 * BORDERPX, ch - 2 * BORDERPX, False);
295 rn++;
296 if(rn >= rows) {
297 rn = 0;
298 cn++;
299 }
300 i++;
301 }
302 else {
303 c->isbanned = True;
304 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
305 }
307 if(!sel || !isvisible(sel)) {
308 for(c = stack; c && !isvisible(c); c = c->snext);
309 focus(c);
310 }
311 restack();
312 }
314 static void
315 tile(void) {
316 unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
317 Client *c;
319 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
320 n++;
321 /* window geoms */
322 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
323 mw = (n > nmaster) ? (waw * master) / 1000 : waw;
324 th = (n > nmaster) ? wah / (n - nmaster) : 0;
325 tw = waw - mw;
327 for(i = 0, c = clients; c; c = c->next)
328 if(isvisible(c)) {
329 if(c->isbanned)
330 XMoveWindow(dpy, c->win, c->x, c->y);
331 c->isbanned = False;
332 if(c->isversatile)
333 continue;
334 c->ismax = False;
335 nx = wax;
336 ny = way;
337 if(i < nmaster) {
338 ny += i * mh;
339 nw = mw - 2 * BORDERPX;
340 nh = mh - 2 * BORDERPX;
341 }
342 else { /* tile window */
343 nx += mw;
344 nw = tw - 2 * BORDERPX;
345 if(th > 2 * BORDERPX) {
346 ny += (i - nmaster) * th;
347 nh = th - 2 * BORDERPX;
348 }
349 else /* fallback if th <= 2 * BORDERPX */
350 nh = wah - 2 * BORDERPX;
351 }
352 resize(c, nx, ny, nw, nh, False);
353 i++;
354 }
355 else {
356 c->isbanned = True;
357 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
358 }
359 if(!sel || !isvisible(sel)) {
360 for(c = stack; c && !isvisible(c); c = c->snext);
361 focus(c);
362 }
363 restack();
364 }
366 LAYOUTS
368 /* extern */
370 void
371 focusnext(Arg *arg) {
372 Client *c;
374 if(!sel)
375 return;
376 for(c = sel->next; c && !isvisible(c); c = c->next);
377 if(!c)
378 for(c = clients; c && !isvisible(c); c = c->next);
379 if(c) {
380 focus(c);
381 restack();
382 }
383 }
385 void
386 focusprev(Arg *arg) {
387 Client *c;
389 if(!sel)
390 return;
391 for(c = sel->prev; c && !isvisible(c); c = c->prev);
392 if(!c) {
393 for(c = clients; c && c->next; c = c->next);
394 for(; c && !isvisible(c); c = c->prev);
395 }
396 if(c) {
397 focus(c);
398 restack();
399 }
400 }
402 void
403 incnmaster(Arg *arg) {
404 //if((lt->arrange != tile) || (nmaster + arg->i < 1)
405 //|| (wah / (nmaster + arg->i) <= 2 * BORDERPX))
406 // return;
407 if(nmaster + arg->i < 1 && nmaster + arg->i <= MAX_TASKS)
408 return;
409 nmaster += arg->i;
410 if(sel)
411 lt->arrange();
412 else
413 drawstatus();
414 }
416 void
417 initlayouts(void) {
418 unsigned int i, w;
420 lt = &layout[0];
421 nlayouts = sizeof layout / sizeof layout[0];
422 for(blw = i = 0; i < nlayouts; i++) {
423 w = textw(layout[i].symbol);
424 if(w > blw)
425 blw = w;
426 }
427 }
429 Client *
430 nexttiled(Client *c) {
431 for(; c && (c->isversatile || !isvisible(c)); c = c->next);
432 return c;
433 }
435 void
436 resizemaster(Arg *arg) {
437 //if(lt->arrange != tile)
438 // return;
439 if(arg->i == 0)
440 master = MASTER;
441 else {
442 //if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
443 //|| waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
444 // return;
445 master += arg->i;
446 }
447 lt->arrange();
448 }
450 void
451 restack(void) {
452 Client *c;
453 XEvent ev;
455 drawstatus();
456 if(!sel)
457 return;
458 if(sel->isversatile || lt->arrange == versatile)
459 XRaiseWindow(dpy, sel->win);
460 if(lt->arrange != versatile) {
461 if(!sel->isversatile)
462 XLowerWindow(dpy, sel->win);
463 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
464 if(c == sel)
465 continue;
466 XLowerWindow(dpy, c->win);
467 }
468 }
469 XSync(dpy, False);
470 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
471 }
473 void
474 setlayout(Arg *arg) {
475 int i;
477 if(arg->i < 0) {
478 for(i = 0; i < nlayouts && lt != &layout[i]; i++);
479 if(arg->i == -1)
480 lt = &layout[(i + 1) % nlayouts];
481 else if(arg->i == -2)
482 lt = &layout[(i - 1) >= 0 ? i - 1 : nlayouts - 1];
483 }
484 else {
485 if(arg->i < 0 || arg->i >= nlayouts)
486 return;
487 lt = &layout[arg->i];
488 }
489 if(sel)
490 lt->arrange();
491 else
492 drawstatus();
493 }
495 void
496 versatile(void) {
497 Client *c;
499 for(c = clients; c; c = c->next) {
500 if(isvisible(c)) {
501 if(c->isbanned)
502 XMoveWindow(dpy, c->win, c->x, c->y);
503 c->isbanned = False;
504 resize(c, c->x, c->y, c->w, c->h, True);
505 }
506 else {
507 c->isbanned = True;
508 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
509 }
510 }
511 if(!sel || !isvisible(sel)) {
512 for(c = stack; c && !isvisible(c); c = c->snext);
513 focus(c);
514 }
515 restack();
516 }