all repos — dwm @ e87bed6df228844f130eff6f1ab63b386b88a34b

fork of suckless dynamic window manager

view.c (view raw)

  1/*
  2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3 * See LICENSE file for license details.
  4 */
  5#include "dwm.h"
  6
  7/* static */
  8
  9static Client *
 10minclient(void) {
 11	Client *c, *min;
 12
 13	if((clients && clients->isfloat) || arrange == dofloat)
 14		return clients; /* don't touch floating order */
 15	for(min = c = clients; c; c = c->next)
 16		if(c->weight < min->weight)
 17			min = c;
 18	return min;
 19}
 20
 21static Client *
 22nexttiled(Client *c) {
 23	for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
 24	return c;
 25}
 26
 27static void
 28reorder(void) {
 29	Client *c, *newclients, *tail;
 30
 31	newclients = tail = NULL;
 32	while((c = minclient())) {
 33		detach(c);
 34		if(tail) {
 35			c->prev = tail;
 36			tail->next = c;
 37			tail = c;
 38		}
 39		else
 40			tail = newclients = c;
 41	}
 42	clients = newclients;
 43}
 44
 45static void
 46togglemax(Client *c)
 47{
 48	XEvent ev;
 49	if((c->ismax = !c->ismax)) {
 50		c->rx = c->x; c->x = sx;
 51		c->ry = c->y; c->y = bh;
 52		c->rw = c->w; c->w = sw;
 53		c->rh = c->h; c->h = sh - bh - 2;
 54	}
 55	else {
 56		c->x = c->rx;
 57		c->y = c->ry;
 58		c->w = c->rw;
 59		c->h = c->rh;
 60	}
 61	resize(c, True, TopLeft);
 62	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
 63}
 64
 65/* extern */
 66
 67void (*arrange)(Arg *) = DEFMODE;
 68
 69void
 70detach(Client *c) {
 71	if(c->prev)
 72		c->prev->next = c->next;
 73	if(c->next)
 74		c->next->prev = c->prev;
 75	if(c == clients)
 76		clients = c->next;
 77	c->next = c->prev = NULL;
 78}
 79
 80void
 81dofloat(Arg *arg) {
 82	Client *c;
 83
 84	for(c = clients; c; c = c->next) {
 85		if(isvisible(c)) {
 86			resize(c, True, TopLeft);
 87		}
 88		else
 89			ban(c);
 90	}
 91	if(!sel || !isvisible(sel)) {
 92		for(c = stack; c && !isvisible(c); c = c->snext);
 93		focus(c);
 94	}
 95	restack();
 96}
 97
 98void
 99dotile(Arg *arg) {
100	int h, i, n, w;
101	Client *c;
102
103	w = sw - mw;
104	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
105		n++;
106
107	if(n > 1)
108		h = (sh - bh) / (n - 1);
109	else
110		h = sh - bh;
111
112	for(i = 0, c = clients; c; c = c->next) {
113		if(isvisible(c)) {
114			if(c->isfloat) {
115				resize(c, True, TopLeft);
116				continue;
117			}
118			c->ismax = False;
119			if(n == 1) {
120				c->x = sx;
121				c->y = sy + bh;
122				c->w = sw - 2;
123				c->h = sh - 2 - bh;
124			}
125			else if(i == 0) {
126				c->x = sx;
127				c->y = sy + bh;
128				c->w = mw - 2;
129				c->h = sh - 2 - bh;
130			}
131			else if(h > bh) {
132				c->x = sx + mw;
133				c->y = sy + (i - 1) * h + bh;
134				c->w = w - 2;
135				if(i + 1 == n)
136					c->h = sh - c->y - 2;
137				else
138					c->h = h - 2;
139			}
140			else { /* fallback if h < bh */
141				c->x = sx + mw;
142				c->y = sy + bh;
143				c->w = w - 2;
144				c->h = sh - 2 - bh;
145			}
146			resize(c, False, TopLeft);
147			i++;
148		}
149		else
150			ban(c);
151	}
152	if(!sel || !isvisible(sel)) {
153		for(c = stack; c && !isvisible(c); c = c->snext);
154		focus(c);
155	}
156	restack();
157}
158
159void
160focusnext(Arg *arg) {
161	Client *c;
162   
163	if(!sel)
164		return;
165
166	if(!(c = getnext(sel->next)))
167		c = getnext(clients);
168	if(c) {
169		focus(c);
170		restack();
171	}
172}
173
174void
175focusprev(Arg *arg) {
176	Client *c;
177
178	if(!sel)
179		return;
180
181	if(!(c = getprev(sel->prev))) {
182		for(c = clients; c && c->next; c = c->next);
183		c = getprev(c);
184	}
185	if(c) {
186		focus(c);
187		restack();
188	}
189}
190
191Bool
192isvisible(Client *c) {
193	unsigned int i;
194
195	for(i = 0; i < ntags; i++)
196		if(c->tags[i] && seltag[i])
197			return True;
198	return False;
199}
200
201void
202resizecol(Arg *arg) {
203	unsigned int n;
204	Client *c;
205
206	for(n = 0, c = clients; c; c = c->next)
207		if(isvisible(c) && !c->isfloat)
208			n++;
209	if(!sel || sel->isfloat || n < 2 || (arrange == dofloat))
210		return;
211
212	if(sel == getnext(clients)) {
213		if(mw + arg->i > sw - 100 || mw + arg->i < 100)
214			return;
215		mw += arg->i;
216	}
217	else {
218		if(mw - arg->i > sw - 100 || mw - arg->i < 100)
219			return;
220		mw -= arg->i;
221	}
222	arrange(NULL);
223}
224
225void
226restack(void) {
227	Client *c;
228	XEvent ev;
229
230	if(!sel) {
231		drawstatus();
232		return;
233	}
234	if(sel->isfloat || arrange == dofloat) {
235		XRaiseWindow(dpy, sel->win);
236		XRaiseWindow(dpy, sel->twin);
237	}
238	if(arrange != dofloat)
239		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
240			XLowerWindow(dpy, c->twin);
241			XLowerWindow(dpy, c->win);
242		}
243	drawall();
244	XSync(dpy, False);
245	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
246}
247
248void
249togglemode(Arg *arg) {
250	arrange = (arrange == dofloat) ? dotile : dofloat;
251	if(sel)
252		arrange(NULL);
253	else
254		drawstatus();
255}
256
257void
258toggleview(Arg *arg) {
259	unsigned int i;
260
261	seltag[arg->i] = !seltag[arg->i];
262	for(i = 0; i < ntags && !seltag[i]; i++);
263	if(i == ntags)
264		seltag[arg->i] = True; /* cannot toggle last view */
265	reorder();
266	arrange(NULL);
267}
268
269void
270view(Arg *arg) {
271	unsigned int i;
272
273	for(i = 0; i < ntags; i++)
274		seltag[i] = False;
275	seltag[arg->i] = True;
276	reorder();
277	arrange(NULL);
278}
279
280void
281viewall(Arg *arg) {
282	unsigned int i;
283
284	for(i = 0; i < ntags; i++)
285		seltag[i] = True;
286	reorder();
287	arrange(NULL);
288}
289
290void
291zoom(Arg *arg) {
292	unsigned int n;
293	Client *c;
294
295	if(!sel)
296		return;
297
298	if(sel->isfloat || (arrange == dofloat)) {
299		togglemax(sel);
300		return;
301	}
302
303	for(n = 0, c = clients; c; c = c->next)
304		if(isvisible(c) && !c->isfloat)
305			n++;
306	if(n < 2 || (arrange == dofloat))
307		return;
308
309	if((c = sel) == nexttiled(clients))
310		if(!(c = nexttiled(c->next)))
311			return;
312	detach(c);
313	if(clients)
314		clients->prev = c;
315	c->next = clients;
316	clients = c;
317	focus(c);
318	arrange(NULL);
319}