all repos — dwm @ 12b1d439e46e7881942a6d378b3b874c4c9f7555

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	if(!sel)
 41		return;
 42
 43	sel->tags[arg->i] = True;
 44	arrange(NULL);
 45}
 46
 47void
 48dofloat(Arg *arg)
 49{
 50	Client *c;
 51
 52	for(c = clients; c; c = c->next) {
 53		c->ismax = False;
 54		if(c->tags[tsel]) {
 55			resize(c, True, TopLeft);
 56		}
 57		else
 58			ban(c);
 59	}
 60	if(sel && !sel->tags[tsel]) {
 61		if((sel = getnext(clients))) {
 62			higher(sel);
 63			focus(sel);
 64		}
 65		else
 66			XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
 67	}
 68	drawall();
 69}
 70
 71void
 72dotile(Arg *arg)
 73{
 74	int n, i, w, h;
 75	Client *c;
 76
 77	w = sw - mw;
 78	for(n = 0, c = clients; c; c = c->next)
 79		if(c->tags[tsel] && !c->isfloat)
 80			n++;
 81
 82	if(n > 1)
 83		h = (sh - bh) / (n - 1);
 84	else
 85		h = sh - bh;
 86
 87	for(i = 0, c = clients; c; c = c->next) {
 88		c->ismax = False;
 89		if(c->tags[tsel]) {
 90			if(c->isfloat) {
 91				higher(c);
 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				c->h = h - 2;
112			}
113			else { /* fallback if h < bh */
114				c->x = sx + mw;
115				c->y = sy + bh;
116				c->w = w - 2;
117				c->h = sh - 2 - bh;
118			}
119			resize(c, False, TopLeft);
120			i++;
121		}
122		else
123			ban(c);
124	}
125	if(!sel || (sel && !sel->tags[tsel])) {
126		if((sel = getnext(clients))) {
127			higher(sel);
128			focus(sel);
129		}
130		else
131			XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
132	}
133	drawall();
134}
135
136Client *
137getnext(Client *c)
138{
139	for(; c && !c->tags[tsel]; c = c->next);
140	return c;
141}
142
143Client *
144getprev(Client *c)
145{
146	for(; c && !c->tags[tsel]; c = c->prev);
147	return c;
148}
149
150void
151initrregs()
152{
153	unsigned int i;
154	regex_t *reg;
155
156	if(rreg)
157		return;
158	len = sizeof(rule) / sizeof(rule[0]);
159	rreg = emallocz(len * sizeof(RReg));
160
161	for(i = 0; i < len; i++) {
162		if(rule[i].clpattern) {
163			reg = emallocz(sizeof(regex_t));
164			if(regcomp(reg, rule[i].clpattern, 0))
165				free(reg);
166			else
167				rreg[i].clregex = reg;
168		}
169		if(rule[i].tpattern) {
170			reg = emallocz(sizeof(regex_t));
171			if(regcomp(reg, rule[i].tpattern, 0))
172				free(reg);
173			else
174				rreg[i].tregex = reg;
175		}
176	}
177}
178
179void
180replacetag(Arg *arg)
181{
182	int i;
183
184	if(!sel)
185		return;
186
187	for(i = 0; i < ntags; i++)
188		sel->tags[i] = False;
189	appendtag(arg);
190}
191
192void
193settags(Client *c)
194{
195	char classinst[256];
196	unsigned int i, j;
197	regmatch_t tmp;
198	Bool matched = False;
199	XClassHint ch;
200
201	if(XGetClassHint(dpy, c->win, &ch)) {
202		snprintf(classinst, sizeof(classinst), "%s:%s",
203				ch.res_class ? ch.res_class : "",
204				ch.res_name ? ch.res_name : "");
205		for(i = 0; !matched && i < len; i++)
206			if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
207				c->isfloat = rule[i].isfloat;
208				for(j = 0; rreg[i].tregex && j < ntags; j++) {
209					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
210						matched = True;
211						c->tags[j] = True;
212					}
213				}
214			}
215		if(ch.res_class)
216			XFree(ch.res_class);
217		if(ch.res_name)
218			XFree(ch.res_name);
219	}
220	if(!matched)
221		c->tags[tsel] = True;
222}
223
224void
225togglemode(Arg *arg)
226{
227	arrange = arrange == dofloat ? dotile : dofloat;
228	arrange(NULL);
229}
230
231void
232view(Arg *arg)
233{
234	tsel = arg->i;
235	arrange(NULL);
236	drawall();
237}
238
239void
240viewnext(Arg *arg)
241{
242	arg->i = (tsel < ntags-1) ? tsel+1 : 0;
243	view(arg);
244}
245
246void
247viewprev(Arg *arg)
248{
249	arg->i = (tsel > 0) ? tsel-1 : ntags-1;
250	view(arg);
251}