all repos — dwm @ 796925089cff58fbea4a1954cf0df57a6a648562

fork of suckless dynamic window manager

layout.c (view raw)

  1/* See LICENSE file for copyright and license details. */
  2#include "dwm.h"
  3#include <stdio.h>
  4#include <stdlib.h>
  5
  6unsigned int blw = 0;
  7Layout *lt = NULL;
  8
  9/* static */
 10
 11static double hratio = HRATIO;
 12static double vratio = VRATIO;
 13static unsigned int nlayouts = 0;
 14static unsigned int nmaster = NMASTER;
 15
 16static double /* simple pow() */
 17spow(double x, double y)
 18{
 19	if(y == 0)
 20		return 1;
 21	while(--y)
 22		x *= x;
 23	return x;
 24}
 25
 26static void
 27tile(void) {
 28	Bool mmaxtile = False, smaxtile = False; /* fallback tiling */
 29	double mscale = 0, sscale = 0, sum = 0;
 30	unsigned int i, n, nx, ny, nw, nh, mw, tw;
 31	Client *c;
 32
 33	/* preparation */
 34	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
 35		n++;
 36	nx = wax;
 37	ny = way;
 38	mw = (n <= nmaster) ? waw :  waw / (1 + hratio);
 39	tw = waw - mw;
 40	if(n > 0) {
 41		if(n <= nmaster) {
 42			for(i = 0; i < n; i++)
 43				sum += spow(vratio, i);
 44			mscale = wah / sum;
 45			if(vratio >= 1)
 46				mmaxtile = bh > (mscale * spow(vratio, 0));
 47			else
 48				mmaxtile = bh > (mscale * spow(vratio, n - 1));
 49		}
 50		else {
 51			for(i = 0; i < nmaster; i++)
 52				sum += spow(vratio, i);
 53			mscale = wah / sum;
 54			for(sum = 0, i = 0; i < (n - nmaster); i++)
 55				sum += spow(vratio, i);
 56			sscale = wah / sum;
 57			if(vratio >= 1) {
 58				mmaxtile = bh > (mscale * spow(vratio, 0));
 59				smaxtile = bh > (sscale * spow(vratio, 0));
 60			}
 61			else {
 62				mmaxtile = bh > (mscale * spow(vratio, nmaster - 1));
 63				smaxtile = bh > (sscale * spow(vratio, n - nmaster - 1));
 64			}
 65		}
 66	}
 67	/* tiling */
 68	for(i = 0, c = clients; c; c = c->next)
 69		if(isvisible(c)) {
 70			unban(c);
 71			if(c->isfloating)
 72				continue;
 73			c->ismax = False;
 74			if(i < nmaster) { /* master window */
 75				nw = mw - 2 * c->border;
 76				if(mmaxtile) {
 77					ny = way;
 78					nh = wah - 2 * c->border;
 79				}
 80				else if(i + 1 == (n < nmaster ? n : nmaster))
 81					nh = (way + wah) - ny - 2 * c->border;
 82				else
 83					nh = (mscale * spow(vratio, i)) - 2 * c->border;
 84			}
 85			else { /* tile window */
 86				nw = tw - 2 * c->border;
 87				if(i == nmaster) {
 88					ny = way;
 89					nx = wax + mw;
 90				}
 91				if(smaxtile) {
 92					ny = way;
 93					nh = wah - 2 * c->border;
 94				}
 95				else if(i + 1 == n)
 96					nh = (way + wah) - ny - 2 * c->border;
 97				else
 98					nh = (sscale * spow(vratio, i - nmaster)) - 2 * c->border;
 99			}
100			resize(c, nx, ny, nw, nh, False);
101			ny += nh;
102			i++;
103		}
104		else
105			ban(c);
106	focus(NULL);
107	restack();
108}
109
110LAYOUTS
111
112static void
113incratio(const char *arg, double *ratio, double def) {
114	double delta;
115
116	if(lt->arrange != tile)
117		return;
118	if(!arg)
119		*ratio = def;
120	else {
121		if(1 == sscanf(arg, "%lf", &delta)) {
122			if(delta + (*ratio) < .1 || delta + (*ratio) > 1.9)
123				return;
124			*ratio += delta;
125		}
126	}
127	lt->arrange();
128}
129
130/* extern */
131
132void
133floating(void) {
134	Client *c;
135
136	for(c = clients; c; c = c->next)
137		if(isvisible(c)) {
138			unban(c);
139			resize(c, c->x, c->y, c->w, c->h, True);
140		}
141		else
142			ban(c);
143	focus(NULL);
144	restack();
145}
146
147void
148focusclient(const char *arg) {
149	Client *c;
150   
151	if(!sel || !arg)
152		return;
153	if(atoi(arg) < 0) {
154		for(c = sel->prev; c && !isvisible(c); c = c->prev);
155		if(!c) {
156			for(c = clients; c && c->next; c = c->next);
157			for(; c && !isvisible(c); c = c->prev);
158		}
159	}
160	else {
161		for(c = sel->next; c && !isvisible(c); c = c->next);
162		if(!c)
163			for(c = clients; c && !isvisible(c); c = c->next);
164	}
165	if(c) {
166		focus(c);
167		restack();
168	}
169}
170
171void
172inchratio(const char *arg) {
173	incratio(arg, &hratio, HRATIO);
174}
175
176void
177incvratio(const char *arg) {
178	incratio(arg, &vratio, VRATIO);
179}
180
181void
182incnmaster(const char *arg) {
183	int i;
184
185	if(!arg)
186		nmaster = NMASTER;
187	else {
188		i = atoi(arg);
189		if((lt->arrange != tile) || (nmaster + i < 1)
190		|| (wah / (nmaster + i) <= 2 * BORDERPX))
191			return;
192		nmaster += i;
193	}
194	if(sel)
195		lt->arrange();
196	else
197		drawstatus();
198}
199
200void
201initlayouts(void) {
202	unsigned int i, w;
203
204	lt = &layout[0];
205	nlayouts = sizeof layout / sizeof layout[0];
206	for(blw = i = 0; i < nlayouts; i++) {
207		w = textw(layout[i].symbol);
208		if(w > blw)
209			blw = w;
210	}
211}
212
213Client *
214nexttiled(Client *c) {
215	for(; c && (c->isfloating || !isvisible(c)); c = c->next);
216	return c;
217}
218
219void
220restack(void) {
221	Client *c;
222	XEvent ev;
223	XWindowChanges wc;
224
225	drawstatus();
226	if(!sel)
227		return;
228	if(sel->isfloating || lt->arrange == floating)
229		XRaiseWindow(dpy, sel->win);
230	if(lt->arrange != floating) {
231		wc.stack_mode = Below;
232		wc.sibling = barwin;
233		if(!sel->isfloating) {
234			XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
235			wc.sibling = sel->win;
236		}
237		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
238			if(c == sel)
239				continue;
240			XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
241			wc.sibling = c->win;
242		}
243	}
244	XSync(dpy, False);
245	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
246}
247
248void
249setlayout(const char *arg) {
250	int i;
251
252	if(!arg) {
253		lt++;
254		if(lt == layout + nlayouts)
255			lt = layout;
256	}
257	else {
258		i = atoi(arg);
259		if(i < 0 || i >= nlayouts)
260			return;
261		lt = &layout[i];
262	}
263	if(sel)
264		lt->arrange();
265	else
266		drawstatus();
267}
268
269void
270togglebar(const char *arg) {
271	if(bpos == BarOff)
272		bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
273	else
274		bpos = BarOff;
275	updatebarpos();
276	lt->arrange();
277}
278
279void
280togglemax(const char *arg) {
281	XEvent ev;
282
283	if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed)
284		return;
285	if((sel->ismax = !sel->ismax)) {
286		sel->rx = sel->x;
287		sel->ry = sel->y;
288		sel->rw = sel->w;
289		sel->rh = sel->h;
290		resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
291	}
292	else
293		resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
294	drawstatus();
295	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
296}
297
298void
299zoom(const char *arg) {
300	Client *c;
301
302	if(!sel || lt->arrange == floating || sel->isfloating)
303		return;
304	if((c = sel) == nexttiled(clients))
305		if(!(c = nexttiled(c->next)))
306			return;
307	detach(c);
308	attach(c);
309	focus(c);
310	lt->arrange();
311}