all repos — dwm @ fe3dfbbe90f813294bb69e41f996a000f63c1560

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