all repos — dwm @ 4135e34dfa61d32625ef20e8e38064b465402f4a

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