all repos — dwm @ 6e22ccf7b1602b33624692eefd51b6398ffa5454

fork of suckless dynamic window manager

view.c (view raw)

  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"
  5#include <stdio.h>
  6
  7/* static */
  8
  9static Client *
 10nexttiled(Client *c) {
 11	for(; c && (c->isfloat || !isvisible(c)); c = c->next);
 12	return c;
 13}
 14
 15static void
 16togglemax(Client *c) {
 17	XEvent ev;
 18		
 19	if(c->isfixed)
 20		return;
 21
 22	if((c->ismax = !c->ismax)) {
 23		c->rx = c->x;
 24		c->ry = c->y;
 25		c->rw = c->w;
 26		c->rh = c->h;
 27		resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
 28	}
 29	else
 30		resize(c, c->rx, c->ry, c->rw, c->rh, True);
 31	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
 32}
 33
 34/* extern */
 35
 36void (*arrange)(void) = DEFMODE;
 37
 38void
 39detach(Client *c) {
 40	if(c->prev)
 41		c->prev->next = c->next;
 42	if(c->next)
 43		c->next->prev = c->prev;
 44	if(c == clients)
 45		clients = c->next;
 46	c->next = c->prev = NULL;
 47}
 48
 49void
 50dofloat(void) {
 51	Client *c;
 52
 53	for(c = clients; c; c = c->next) {
 54		if(isvisible(c)) {
 55			if(c->isbanned)
 56				XMoveWindow(dpy, c->win, c->x, c->y);
 57			c->isbanned = False;
 58		}
 59		else {
 60			c->isbanned = True;
 61			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
 62		}
 63	}
 64	if(!sel || !isvisible(sel)) {
 65		for(c = stack; c && !isvisible(c); c = c->snext);
 66		focus(c);
 67	}
 68	restack();
 69}
 70
 71void
 72dotile(void) {
 73	unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
 74	Client *c;
 75
 76	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
 77		n++;
 78	/* window geoms */
 79	mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
 80	mw = (n > nmaster) ? (waw * master) / 1000 : waw;
 81	th = (n > nmaster) ? wah / (n - nmaster) : 0;
 82	tw = waw - mw;
 83
 84	for(i = 0, c = clients; c; c = c->next)
 85		if(isvisible(c)) {
 86			if(c->isbanned)
 87				XMoveWindow(dpy, c->win, c->x, c->y);
 88			c->isbanned = False;
 89			if(c->isfloat)
 90				continue;
 91			c->ismax = False;
 92			nx = wax;
 93			ny = way;
 94			if(i < nmaster) {
 95				ny += i * mh;
 96				nw = mw - 2 * BORDERPX;
 97				nh = mh - 2 * BORDERPX;
 98			}
 99			else {  /* tile window */
100				nx += mw;
101				nw = tw - 2 * BORDERPX;
102				if(th > 2 * BORDERPX) {
103					ny += (i - nmaster) * th;
104					nh = th - 2 * BORDERPX;
105				}
106				else /* fallback if th <= 2 * BORDERPX */
107					nh = wah - 2 * BORDERPX;
108			}
109			resize(c, nx, ny, nw, nh, False);
110			i++;
111		}
112		else {
113			c->isbanned = True;
114			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
115		}
116	if(!sel || !isvisible(sel)) {
117		for(c = stack; c && !isvisible(c); c = c->snext);
118		focus(c);
119	}
120	restack();
121}
122
123void
124focusnext(Arg *arg) {
125	Client *c;
126   
127	if(!sel)
128		return;
129	for(c = sel->next; c && !isvisible(c); c = c->next);
130	if(!c)
131		for(c = clients; c && !isvisible(c); c = c->next);
132	if(c) {
133		focus(c);
134		restack();
135	}
136}
137
138void
139focusprev(Arg *arg) {
140	Client *c;
141
142	if(!sel)
143		return;
144	for(c = sel->prev; c && !isvisible(c); c = c->prev);
145	if(!c) {
146		for(c = clients; c && c->next; c = c->next);
147		for(; c && !isvisible(c); c = c->prev);
148	}
149	if(c) {
150		focus(c);
151		restack();
152	}
153}
154
155void
156incnmaster(Arg *arg) {
157	if((arrange == dofloat) || (nmaster + arg->i < 1)
158	|| (wah / (nmaster + arg->i) <= 2 * BORDERPX))
159		return;
160	nmaster += arg->i;
161	if(sel)
162		arrange();
163	else
164		drawstatus();
165}
166
167Bool
168isvisible(Client *c) {
169	unsigned int i;
170
171	for(i = 0; i < ntags; i++)
172		if(c->tags[i] && seltag[i])
173			return True;
174	return False;
175}
176
177void
178resizemaster(Arg *arg) {
179	if(arg->i == 0)
180		master = MASTER;
181	else {
182		if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
183		|| waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
184			return;
185		master += arg->i;
186	}
187	arrange();
188}
189
190void
191restack(void) {
192	Client *c;
193	XEvent ev;
194
195	drawstatus();
196	if(!sel)
197		return;
198	if(sel->isfloat || arrange == dofloat)
199		XRaiseWindow(dpy, sel->win);
200	if(arrange != dofloat) {
201		if(!sel->isfloat)
202			XLowerWindow(dpy, sel->win);
203		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
204			if(c == sel)
205				continue;
206			XLowerWindow(dpy, c->win);
207		}
208	}
209	XSync(dpy, False);
210	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
211}
212
213void
214togglefloat(Arg *arg) {
215	if(!sel || arrange == dofloat)
216		return;
217	sel->isfloat = !sel->isfloat;
218	arrange();
219}
220
221void
222togglemode(Arg *arg) {
223	arrange = (arrange == dofloat) ? dotile : dofloat;
224	if(sel)
225		arrange();
226	else
227		drawstatus();
228}
229
230void
231toggleview(Arg *arg) {
232	unsigned int i;
233
234	seltag[arg->i] = !seltag[arg->i];
235	for(i = 0; i < ntags && !seltag[i]; i++);
236	if(i == ntags)
237		seltag[arg->i] = True; /* cannot toggle last view */
238	arrange();
239}
240
241void
242view(Arg *arg) {
243	unsigned int i;
244
245	for(i = 0; i < ntags; i++)
246		seltag[i] = (arg->i == -1) ? True : False;
247	if(arg->i >= 0 && arg->i < ntags)
248		seltag[arg->i] = True;
249	arrange();
250}
251
252void
253zoom(Arg *arg) {
254	unsigned int n;
255	Client *c;
256
257	if(!sel)
258		return;
259	if(sel->isfloat || (arrange == dofloat)) {
260		togglemax(sel);
261		return;
262	}
263	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
264		n++;
265
266	if((c = sel) == nexttiled(clients))
267		if(!(c = nexttiled(c->next)))
268			return;
269	detach(c);
270	if(clients)
271		clients->prev = c;
272	c->next = clients;
273	clients = c;
274	focus(c);
275	arrange();
276}