diff layout.c @ 0:7024076fa948

initial add
author paulo@localhost
date Sun, 22 Mar 2009 23:26:35 -0700
parents
children ba504f41828f
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout.c	Sun Mar 22 23:26:35 2009 -0700
     1.3 @@ -0,0 +1,471 @@
     1.4 +/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
     1.5 + * See LICENSE file for license details.
     1.6 + */
     1.7 +#include "dwm.h"
     1.8 +
     1.9 +unsigned int master = MASTER;
    1.10 +unsigned int nmaster = NMASTER;
    1.11 +unsigned int blw = 0;
    1.12 +Layout *lt = NULL;
    1.13 +
    1.14 +/* static */
    1.15 +
    1.16 +static unsigned int nlayouts = 0;
    1.17 +
    1.18 +static void
    1.19 +spiral(void) {
    1.20 +	unsigned int i, n, nx, ny, nw, nh;
    1.21 +	Client *c;
    1.22 +
    1.23 +	nx = wax;
    1.24 +	ny = way + wah;
    1.25 +	nw = waw;
    1.26 +	nh = wah;
    1.27 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
    1.28 +		n++;
    1.29 +	for(i = 0, c = clients; c; c = c->next)
    1.30 +		if(isvisible(c)) {
    1.31 +			if(c->isbanned)
    1.32 +				XMoveWindow(dpy, c->win, c->x, c->y);
    1.33 +			c->isbanned = False;
    1.34 +			if(c->isversatile)
    1.35 +				continue;
    1.36 +			c->ismax = False;
    1.37 +			if((i % 2 && nh / 2 > 2 * c->border)
    1.38 +				|| (!(i % 2) && nw / 2 > 2 * c->border))
    1.39 +			{
    1.40 +				if(i < n - 1) {
    1.41 +					if(i % 2)
    1.42 +						nh /= 2;
    1.43 +				   else
    1.44 +						nw /= 2;
    1.45 +				   if((i % 4) == 2)
    1.46 +						nx += nw;
    1.47 +				   else if((i % 4) == 3)
    1.48 +						ny += nh;
    1.49 +				}
    1.50 +				if((i % 4) == 0)
    1.51 +					ny -= nh;
    1.52 +				else if((i % 4) == 1)
    1.53 +					nx += nw;
    1.54 +				else if((i % 4) == 2)
    1.55 +					ny += nh;
    1.56 +				else
    1.57 +					nx -= nw;
    1.58 +				i++;
    1.59 +			}
    1.60 +			resize(c, nx, ny, nw - 2 * c->border, nh - 2 * c->border, False);
    1.61 +		}
    1.62 +		else {
    1.63 +			c->isbanned = True;
    1.64 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
    1.65 +		}
    1.66 +	if(!sel || !isvisible(sel)) {
    1.67 +		for(c = stack; c && !isvisible(c); c = c->snext);
    1.68 +		focus(c);
    1.69 +	}
    1.70 +	restack();
    1.71 +}
    1.72 +
    1.73 +static void
    1.74 +spiral_h(void) {
    1.75 +	unsigned int i, n, nx, ny, nw, nh;
    1.76 +	Client *c;
    1.77 +
    1.78 +	nx = wax + waw;
    1.79 +	ny = way;
    1.80 +	nw = waw;
    1.81 +	nh = wah;
    1.82 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
    1.83 +		n++;
    1.84 +	for(i = 0, c = clients; c; c = c->next)
    1.85 +		if(isvisible(c)) {
    1.86 +			if(c->isbanned)
    1.87 +				XMoveWindow(dpy, c->win, c->x, c->y);
    1.88 +			c->isbanned = False;
    1.89 +			if(c->isversatile)
    1.90 +				continue;
    1.91 +			c->ismax = False;
    1.92 +			if((i % 2 && nw / 2 > 2 * c->border)
    1.93 +				|| (!(i % 2) && nh / 2 > 2 * c->border))
    1.94 +			{
    1.95 +				if(i < n - 1) {
    1.96 +					if(i % 2)
    1.97 +						nw /= 2;
    1.98 +					else
    1.99 +						nh /= 2;
   1.100 +				   if((i % 4) == 2)
   1.101 +						ny += nh;
   1.102 +				   else if((i % 4) == 3)
   1.103 +						nx += nw;
   1.104 +				}
   1.105 +				if((i % 4) == 0)
   1.106 +					nx -= nw;
   1.107 +				else if((i % 4) == 1)
   1.108 +					ny += nh;
   1.109 +				else if((i % 4) == 2)
   1.110 +					nx += nw;
   1.111 +				else 
   1.112 +					ny -= nh;
   1.113 +				i++;
   1.114 +			}
   1.115 +			resize(c, nx, ny, nw - 2 * c->border, nh - 2 * c->border, False);
   1.116 +		}
   1.117 +		else {
   1.118 +			c->isbanned = True;
   1.119 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.120 +		}
   1.121 +	if(!sel || !isvisible(sel)) {
   1.122 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.123 +		focus(c);
   1.124 +	}
   1.125 +	restack();
   1.126 +}
   1.127 +
   1.128 +static void
   1.129 +monocle(void) {
   1.130 +	Client *c;
   1.131 +
   1.132 +	for(c = clients; c; c = c->next)
   1.133 +		if(isvisible(c) && c->isbanned) {
   1.134 +			XMoveWindow(dpy, c->win, c->x, c->y);
   1.135 +			c->isbanned = False;
   1.136 +		}
   1.137 +		else if(!isvisible(c)) {
   1.138 +			c->isbanned = True;
   1.139 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.140 +		}
   1.141 +
   1.142 +	for(c = nexttiled(clients); c; c = nexttiled(c->next))
   1.143 +		resize(c, wax, way, waw - 2*c->border, wah - 2*c->border, False); 
   1.144 +
   1.145 +	restack();
   1.146 +}
   1.147 +
   1.148 +static void
   1.149 +cascade(void) {
   1.150 +	unsigned int i, n, px = 20;	
   1.151 +	Client *c;
   1.152 +
   1.153 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
   1.154 +
   1.155 +	for(i = 0, c = clients; c; c = c->next)
   1.156 +		if(isvisible(c)) {
   1.157 +			if(c->isbanned) {
   1.158 +				XMoveWindow(dpy, c->win, c->x, c->y);
   1.159 +				c->isbanned = False;
   1.160 +			}
   1.161 +			if(!c->isversatile) {
   1.162 +				resize(c, wax+(px*i), way+(px*i), waw-(px*(n-1))-2*c->border, wah-(px*(n-1))-2*c->border, True); 
   1.163 +				i++;
   1.164 +			}
   1.165 +		}
   1.166 +		else if(!isvisible(c)) {
   1.167 +			c->isbanned = True;
   1.168 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.169 +		}
   1.170 +
   1.171 +	restack();
   1.172 +}
   1.173 +
   1.174 +static void
   1.175 +bstack(void) {
   1.176 +	unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
   1.177 +	Client *c;
   1.178 +
   1.179 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
   1.180 +		n++;
   1.181 +	/* window geoms */
   1.182 +	mh = (n > nmaster) ? (wah * master) / 1000 : wah;
   1.183 +	mw = (n > nmaster) ? waw / nmaster : waw / (n > 0 ? n : 1);
   1.184 +	th = wah - mh;
   1.185 +	tw = (n > nmaster) ? waw / (n - nmaster) : 0;
   1.186 +
   1.187 +	for(i = 0, c = clients; c; c = c->next)
   1.188 +		if(isvisible(c)) {
   1.189 +			if(c->isbanned)
   1.190 +				XMoveWindow(dpy, c->win, c->x, c->y);
   1.191 +			c->isbanned = False;
   1.192 +			if(c->isversatile)
   1.193 +				continue;
   1.194 +			c->ismax = False;
   1.195 +			nx = wax;
   1.196 +			ny = way;
   1.197 +			if(i < nmaster) {
   1.198 +				nx += i * mw;
   1.199 +				nw = mw - 2 * BORDERPX;
   1.200 +				nh = mh - 2 * BORDERPX;
   1.201 +			}
   1.202 +			else {  /* tile window */
   1.203 +				ny += mh;
   1.204 +				nh = th - 2 * BORDERPX;
   1.205 +				if(tw > 2 * BORDERPX) {
   1.206 +					nx += (i - nmaster) * tw;
   1.207 +					nw = tw - 2 * BORDERPX;
   1.208 +				}
   1.209 +				else /* fallback if th <= 2 * BORDERPX */
   1.210 +					nw = waw - 2 * BORDERPX;
   1.211 +			}
   1.212 +			resize(c, nx, ny, nw, nh, False);
   1.213 +			i++;
   1.214 +		}
   1.215 +		else {
   1.216 +			c->isbanned = True;
   1.217 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.218 +		}
   1.219 +	if(!sel || !isvisible(sel)) {
   1.220 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.221 +		focus(c);
   1.222 +	}
   1.223 +	restack();
   1.224 +}
   1.225 +
   1.226 +static void
   1.227 +grid(void) {
   1.228 +	unsigned int i, n, nx, ny, nw, nh, aw, ah, tw, th, cols, rows;
   1.229 +	Client *c;
   1.230 +	
   1.231 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
   1.232 +		n++;
   1.233 +	
   1.234 +	for(rows = 0; rows <= n/2; rows++)
   1.235 +		if(rows*rows >= n)
   1.236 +			break;
   1.237 +	cols = (rows && (rows - 1) * rows >= n) ? rows - 1 : rows;
   1.238 +	
   1.239 +	th = (sh - dc.h) / (rows ? rows : 1);
   1.240 +	tw = sw / (cols ? cols : 1);
   1.241 +	nw = tw - 2 * BORDERPX;
   1.242 +	nh = th - 2 * BORDERPX;
   1.243 +	
   1.244 +	for(i = 0, c = clients; c; c = c->next)
   1.245 +		if(isvisible(c)) {
   1.246 +			if(c->isbanned)
   1.247 +				XMoveWindow(dpy, c->win, c->x, c->y);
   1.248 +			c->isbanned = False;
   1.249 +			if(c->isversatile)
   1.250 +				continue;
   1.251 +			c->ismax = False;
   1.252 +			nx = (i / rows) * tw;
   1.253 +			ny = (i % rows) * th + (TOPBAR ? dc.h : 0);
   1.254 +			/* adjust height and width of last row's and last column's windows */ 
   1.255 +			ah = ((i + 1) % rows == 0) ? sh - th * rows - dc.h : 0;
   1.256 +			aw = (i >= rows * (cols - 1)) ? sw - tw * cols : 0;
   1.257 +			resize(c, nx, ny, nw + aw, nh + ah, False);
   1.258 +			i++;
   1.259 +		}
   1.260 +		else {
   1.261 +			c->isbanned = True;
   1.262 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.263 +		}
   1.264 +	
   1.265 +	if(!sel || !isvisible(sel)) {
   1.266 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.267 +		focus(c);
   1.268 +	}
   1.269 +	restack();
   1.270 +}
   1.271 +
   1.272 +static void
   1.273 +tile(void) {
   1.274 +	unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
   1.275 +	Client *c;
   1.276 +
   1.277 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
   1.278 +		n++;
   1.279 +	/* window geoms */
   1.280 +	mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
   1.281 +	mw = (n > nmaster) ? (waw * master) / 1000 : waw;
   1.282 +	th = (n > nmaster) ? wah / (n - nmaster) : 0;
   1.283 +	tw = waw - mw;
   1.284 +
   1.285 +	for(i = 0, c = clients; c; c = c->next)
   1.286 +		if(isvisible(c)) {
   1.287 +			if(c->isbanned)
   1.288 +				XMoveWindow(dpy, c->win, c->x, c->y);
   1.289 +			c->isbanned = False;
   1.290 +			if(c->isversatile)
   1.291 +				continue;
   1.292 +			c->ismax = False;
   1.293 +			nx = wax;
   1.294 +			ny = way;
   1.295 +			if(i < nmaster) {
   1.296 +				ny += i * mh;
   1.297 +				nw = mw - 2 * BORDERPX;
   1.298 +				nh = mh - 2 * BORDERPX;
   1.299 +			}
   1.300 +			else {  /* tile window */
   1.301 +				nx += mw;
   1.302 +				nw = tw - 2 * BORDERPX;
   1.303 +				if(th > 2 * BORDERPX) {
   1.304 +					ny += (i - nmaster) * th;
   1.305 +					nh = th - 2 * BORDERPX;
   1.306 +				}
   1.307 +				else /* fallback if th <= 2 * BORDERPX */
   1.308 +					nh = wah - 2 * BORDERPX;
   1.309 +			}
   1.310 +			resize(c, nx, ny, nw, nh, False);
   1.311 +			i++;
   1.312 +		}
   1.313 +		else {
   1.314 +			c->isbanned = True;
   1.315 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.316 +		}
   1.317 +	if(!sel || !isvisible(sel)) {
   1.318 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.319 +		focus(c);
   1.320 +	}
   1.321 +	restack();
   1.322 +}
   1.323 +
   1.324 +LAYOUTS
   1.325 +
   1.326 +/* extern */
   1.327 +
   1.328 +void
   1.329 +focusnext(Arg *arg) {
   1.330 +	Client *c;
   1.331 +   
   1.332 +	if(!sel)
   1.333 +		return;
   1.334 +	for(c = sel->next; c && !isvisible(c); c = c->next);
   1.335 +	if(!c)
   1.336 +		for(c = clients; c && !isvisible(c); c = c->next);
   1.337 +	if(c) {
   1.338 +		focus(c);
   1.339 +		restack();
   1.340 +	}
   1.341 +}
   1.342 +
   1.343 +void
   1.344 +focusprev(Arg *arg) {
   1.345 +	Client *c;
   1.346 +
   1.347 +	if(!sel)
   1.348 +		return;
   1.349 +	for(c = sel->prev; c && !isvisible(c); c = c->prev);
   1.350 +	if(!c) {
   1.351 +		for(c = clients; c && c->next; c = c->next);
   1.352 +		for(; c && !isvisible(c); c = c->prev);
   1.353 +	}
   1.354 +	if(c) {
   1.355 +		focus(c);
   1.356 +		restack();
   1.357 +	}
   1.358 +}
   1.359 +
   1.360 +void
   1.361 +incnmaster(Arg *arg) {
   1.362 +	//if((lt->arrange != tile) || (nmaster + arg->i < 1)
   1.363 +	//|| (wah / (nmaster + arg->i) <= 2 * BORDERPX))
   1.364 +	//	return;
   1.365 +	if(nmaster + arg->i < 1 && nmaster + arg->i <= MAX_TASKS)
   1.366 +		return;
   1.367 +	nmaster += arg->i;
   1.368 +	if(sel)
   1.369 +		lt->arrange();
   1.370 +	else
   1.371 +		drawstatus();
   1.372 +}
   1.373 +
   1.374 +void
   1.375 +initlayouts(void) {
   1.376 +	unsigned int i, w;
   1.377 +
   1.378 +	lt = &layout[0];
   1.379 +	nlayouts = sizeof layout / sizeof layout[0];
   1.380 +	for(blw = i = 0; i < nlayouts; i++) {
   1.381 +		w = textw(layout[i].symbol);
   1.382 +		if(w > blw)
   1.383 +			blw = w;
   1.384 +	}
   1.385 +}
   1.386 +
   1.387 +Client *
   1.388 +nexttiled(Client *c) {
   1.389 +	for(; c && (c->isversatile || !isvisible(c)); c = c->next);
   1.390 +	return c;
   1.391 +}
   1.392 +
   1.393 +void
   1.394 +resizemaster(Arg *arg) {
   1.395 +	//if(lt->arrange != tile)
   1.396 +	//	return;
   1.397 +	if(arg->i == 0)
   1.398 +		master = MASTER;
   1.399 +	else {
   1.400 +		//if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
   1.401 +		//|| waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
   1.402 +		//	return;
   1.403 +		master += arg->i;
   1.404 +	}
   1.405 +	lt->arrange();
   1.406 +}
   1.407 +
   1.408 +void
   1.409 +restack(void) {
   1.410 +	Client *c;
   1.411 +	XEvent ev;
   1.412 +
   1.413 +	drawstatus();
   1.414 +	if(!sel)
   1.415 +		return;
   1.416 +	if(sel->isversatile || lt->arrange == versatile)
   1.417 +		XRaiseWindow(dpy, sel->win);
   1.418 +	if(lt->arrange != versatile) {
   1.419 +		if(!sel->isversatile)
   1.420 +			XLowerWindow(dpy, sel->win);
   1.421 +		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
   1.422 +			if(c == sel)
   1.423 +				continue;
   1.424 +			XLowerWindow(dpy, c->win);
   1.425 +		}
   1.426 +	}
   1.427 +	XSync(dpy, False);
   1.428 +	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   1.429 +}
   1.430 +
   1.431 +void
   1.432 +setlayout(Arg *arg) {
   1.433 +	unsigned int i;
   1.434 +
   1.435 +	if(arg->i == -1) {
   1.436 +		for(i = 0; i < nlayouts && lt != &layout[i]; i++);
   1.437 +		if(i == nlayouts - 1)
   1.438 +			lt = &layout[0];
   1.439 +		else
   1.440 +			lt = &layout[++i];
   1.441 +	}
   1.442 +	else {
   1.443 +		if(arg->i < 0 || arg->i >= nlayouts)
   1.444 +			return;
   1.445 +		lt = &layout[arg->i];
   1.446 +	}
   1.447 +	if(sel)
   1.448 +		lt->arrange();
   1.449 +	else
   1.450 +		drawstatus();
   1.451 +}
   1.452 +
   1.453 +void
   1.454 +versatile(void) {
   1.455 +	Client *c;
   1.456 +
   1.457 +	for(c = clients; c; c = c->next) {
   1.458 +		if(isvisible(c)) {
   1.459 +			if(c->isbanned)
   1.460 +				XMoveWindow(dpy, c->win, c->x, c->y);
   1.461 +			c->isbanned = False;
   1.462 +			resize(c, c->x, c->y, c->w, c->h, True);
   1.463 +		}
   1.464 +		else {
   1.465 +			c->isbanned = True;
   1.466 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.467 +		}
   1.468 +	}
   1.469 +	if(!sel || !isvisible(sel)) {
   1.470 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.471 +		focus(c);
   1.472 +	}
   1.473 +	restack();
   1.474 +}