all repos — dwm @ 0045ad87dfb32f35fc17b5b8942049cfe84d623c

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