all repos — dwm @ 08d85d6d66bc4493414d76e470e473fa689c5990

fork of suckless dynamic window manager

tag.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#include <regex.h>
  7#include <stdio.h>
  8#include <stdlib.h>
  9#include <string.h>
 10#include <sys/types.h>
 11#include <X11/Xutil.h>
 12
 13
 14typedef struct {
 15	const char *clpattern;
 16	const char *tpattern;
 17	Bool isfloat;
 18} Rule;
 19
 20typedef struct {
 21	regex_t *clregex;
 22	regex_t *tregex;
 23} RReg;
 24
 25/* static */
 26
 27TAGS
 28RULES
 29
 30static RReg *rreg = NULL;
 31static unsigned int len = 0;
 32
 33void (*arrange)(Arg *) = DEFMODE;
 34
 35/* extern */
 36
 37void
 38dofloat(Arg *arg)
 39{
 40	Client *c;
 41
 42	for(c = clients; c; c = c->next) {
 43		c->ismax = False;
 44		if(isvisible(c)) {
 45			resize(c, True, TopLeft);
 46		}
 47		else
 48			ban(c);
 49	}
 50	if(!sel || !isvisible(sel))
 51		sel = getnext(clients);
 52	if(sel)
 53		focus(sel);
 54	else
 55		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
 56	restack();
 57}
 58
 59void
 60dotile(Arg *arg)
 61{
 62	int h, i, n, w;
 63	Client *c;
 64
 65	w = sw - mw;
 66	for(n = 0, c = clients; c; c = c->next)
 67		if(isvisible(c) && !c->isfloat)
 68			n++;
 69
 70	if(n > 1)
 71		h = (sh - bh) / (n - 1);
 72	else
 73		h = sh - bh;
 74
 75	for(i = 0, c = clients; c; c = c->next) {
 76		c->ismax = False;
 77		if(isvisible(c)) {
 78			if(c->isfloat) {
 79				resize(c, True, TopLeft);
 80				continue;
 81			}
 82			if(n == 1) {
 83				c->x = sx;
 84				c->y = sy + bh;
 85				c->w = sw - 2;
 86				c->h = sh - 2 - bh;
 87			}
 88			else if(i == 0) {
 89				c->x = sx;
 90				c->y = sy + bh;
 91				c->w = mw - 2;
 92				c->h = sh - 2 - bh;
 93			}
 94			else if(h > bh) {
 95				c->x = sx + mw;
 96				c->y = sy + (i - 1) * h + bh;
 97				c->w = w - 2;
 98				if(i + 1 == n)
 99					c->h = sh - c->y - 2;
100				else
101					c->h = h - 2;
102			}
103			else { /* fallback if h < bh */
104				c->x = sx + mw;
105				c->y = sy + bh;
106				c->w = w - 2;
107				c->h = sh - 2 - bh;
108			}
109			resize(c, False, TopLeft);
110			i++;
111		}
112		else
113			ban(c);
114	}
115	if(!sel || !isvisible(sel))
116		sel = getnext(clients);
117	if(sel)
118		focus(sel);
119	else
120		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
121	restack();
122}
123
124Client *
125getnext(Client *c)
126{
127	for(; c && !isvisible(c); c = c->next);
128	return c;
129}
130
131Client *
132getprev(Client *c)
133{
134	for(; c && !isvisible(c); c = c->prev);
135	return c;
136}
137
138void
139initrregs()
140{
141	unsigned int i;
142	regex_t *reg;
143
144	if(rreg)
145		return;
146	len = sizeof(rule) / sizeof(rule[0]);
147	rreg = emallocz(len * sizeof(RReg));
148
149	for(i = 0; i < len; i++) {
150		if(rule[i].clpattern) {
151			reg = emallocz(sizeof(regex_t));
152			if(regcomp(reg, rule[i].clpattern, 0))
153				free(reg);
154			else
155				rreg[i].clregex = reg;
156		}
157		if(rule[i].tpattern) {
158			reg = emallocz(sizeof(regex_t));
159			if(regcomp(reg, rule[i].tpattern, 0))
160				free(reg);
161			else
162				rreg[i].tregex = reg;
163		}
164	}
165}
166
167Bool
168isvisible(Client *c)
169{
170	unsigned int i;
171
172	for(i = 0; i < ntags; i++)
173		if(c->tags[i] && seltag[i])
174			return True;
175	return False;
176}
177
178void
179restack()
180{
181	static unsigned int nwins = 0;
182	static Window *wins = NULL;
183	unsigned int f, fi, m, mi, n;
184	Client *c;
185	XEvent ev;
186
187	for(f = 0, m = 0, c = clients; c; c = c->next)
188		if(isvisible(c)) {
189			if(c->isfloat || arrange == dofloat)
190				f++;
191			else
192				m++;
193		}
194	if(!(n = 2 * (f + m))) {
195		drawstatus();
196		return;
197	}
198	if(nwins < n) {
199		nwins = n;
200		wins = erealloc(wins, nwins * sizeof(Window));
201	}
202
203	fi = 0;
204	mi = 2 * f;
205	if(sel->isfloat || arrange == dofloat) {
206		wins[fi++] = sel->title;
207		wins[fi++] = sel->win;
208	}
209	else {
210		wins[mi++] = sel->title;
211		wins[mi++] = sel->win;
212	}
213	for(c = clients; c; c = c->next)
214		if(isvisible(c) && c != sel) {
215			if(c->isfloat || arrange == dofloat) {
216				wins[fi++] = c->title;
217				wins[fi++] = c->win;
218			}
219			else {
220				wins[mi++] = c->title;
221				wins[mi++] = c->win;
222			}
223		}
224	XRestackWindows(dpy, wins, n);
225	drawall();
226	XSync(dpy, False);
227	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
228}
229
230void
231settags(Client *c)
232{
233	char classinst[256];
234	unsigned int i, j;
235	regmatch_t tmp;
236	Bool matched = False;
237	XClassHint ch;
238
239	if(XGetClassHint(dpy, c->win, &ch)) {
240		snprintf(classinst, sizeof(classinst), "%s:%s",
241				ch.res_class ? ch.res_class : "",
242				ch.res_name ? ch.res_name : "");
243		for(i = 0; !matched && i < len; i++)
244			if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
245				c->isfloat = rule[i].isfloat;
246				for(j = 0; rreg[i].tregex && j < ntags; j++) {
247					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
248						matched = True;
249						c->tags[j] = True;
250					}
251				}
252			}
253		if(ch.res_class)
254			XFree(ch.res_class);
255		if(ch.res_name)
256			XFree(ch.res_name);
257	}
258	if(!matched)
259		for(i = 0; i < ntags; i++)
260			c->tags[i] = seltag[i];
261}
262
263void
264tag(Arg *arg)
265{
266	unsigned int i;
267
268	if(!sel)
269		return;
270
271	for(i = 0; i < ntags; i++)
272		sel->tags[i] = False;
273	sel->tags[arg->i] = True;
274	settitle(sel);
275	if(!isvisible(sel))
276		arrange(NULL);
277}
278
279void
280togglemode(Arg *arg)
281{
282	arrange = arrange == dofloat ? dotile : dofloat;
283	if(sel)
284		arrange(NULL);
285	else
286		drawstatus();
287}
288
289void
290toggletag(Arg *arg)
291{
292	unsigned int i;
293
294	if(!sel)
295		return;
296
297	sel->tags[arg->i] = !sel->tags[arg->i];
298	for(i = 0; i < ntags && !sel->tags[i]; i++);
299	if(i == ntags)
300		sel->tags[arg->i] = True;
301	settitle(sel);
302	if(!isvisible(sel))
303		arrange(NULL);
304}
305
306
307void
308toggleview(Arg *arg)
309{
310	unsigned int i;
311
312	seltag[arg->i] = !seltag[arg->i];
313	for(i = 0; i < ntags && !seltag[i]; i++);
314	if(i == ntags)
315		seltag[arg->i] = True; /* cannot toggle last view */
316	arrange(NULL);
317}
318
319void
320view(Arg *arg)
321{
322	unsigned int i;
323
324	for(i = 0; i < ntags; i++)
325		seltag[i] = False;
326	seltag[arg->i] = True;
327	arrange(NULL);
328}