all repos — dwm @ 72707c2fae68f5eba6ea97cbf356bfb968c8a15d

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
  7#include <regex.h>
  8#include <stdio.h>
  9#include <string.h>
 10#include <sys/types.h>
 11#include <X11/Xutil.h>
 12
 13/* static */
 14
 15typedef struct {
 16	const char *pattern;
 17	char *tags[TLast];
 18	Bool isfloat;
 19} Rule;
 20
 21/* CUSTOMIZE */ 
 22static Rule rule[] = {
 23	/* class:instance	tags				isfloat */
 24	{ "Firefox.*",		{ [Twww] = "www" },		False },
 25	{ "Gimp.*",		{ 0 },				True},
 26};
 27
 28char *tags[TLast] = {
 29	[Tscratch] = "scratch",
 30	[Tdev] = "dev",
 31	[Twww] = "www",
 32	[Twork] = "work",
 33};
 34
 35void (*arrange)(Arg *) = dotile;
 36
 37/* END CUSTOMIZE */
 38
 39/* extern */
 40
 41void
 42appendtag(Arg *arg)
 43{
 44	if(!sel)
 45		return;
 46
 47	sel->tags[arg->i] = tags[arg->i];
 48	arrange(NULL);
 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(c->tags[tsel]) {
 59			resize(c, True, TopLeft);
 60		}
 61		else
 62			ban(c);
 63	}
 64	if(sel && !sel->tags[tsel]) {
 65		if((sel = getnext(clients, tsel))) {
 66			higher(sel);
 67			focus(sel);
 68		}
 69	}
 70	drawall();
 71}
 72
 73void
 74dotile(Arg *arg)
 75{
 76	int n, i, w, h;
 77	Client *c;
 78
 79	w = sw - mw;
 80	for(n = 0, c = clients; c; c = c->next)
 81		if(c->tags[tsel] && !c->isfloat)
 82			n++;
 83
 84	if(n > 1)
 85		h = (sh - bh) / (n - 1);
 86	else
 87		h = sh - bh;
 88
 89	for(i = 0, c = clients; c; c = c->next) {
 90		c->ismax = False;
 91		if(c->tags[tsel]) {
 92			if(c->isfloat) {
 93				higher(c);
 94				resize(c, True, TopLeft);
 95				continue;
 96			}
 97			if(n == 1) {
 98				c->x = sx;
 99				c->y = sy + bh;
100				c->w = sw - 2 * c->border;
101				c->h = sh - 2 * c->border - bh;
102			}
103			else if(i == 0) {
104				c->x = sx;
105				c->y = sy + bh;
106				c->w = mw - 2 * c->border;
107				c->h = sh - 2 * c->border - bh;
108			}
109			else if(h > bh) {
110				c->x = sx + mw;
111				c->y = sy + (i - 1) * h + bh;
112				c->w = w - 2 * c->border;
113				c->h = h - 2 * c->border;
114			}
115			else { /* fallback if h < bh */
116				c->x = sx + mw;
117				c->y = sy + bh;
118				c->w = w - 2 * c->border;
119				c->h = sh - 2 * c->border - bh;
120			}
121			resize(c, False, TopLeft);
122			i++;
123		}
124		else
125			ban(c);
126	}
127	if(!sel || (sel && !sel->tags[tsel])) {
128		if((sel = getnext(clients, tsel))) {
129			higher(sel);
130			focus(sel);
131		}
132	}
133	drawall();
134}
135
136Client *
137getnext(Client *c, unsigned int t)
138{
139	for(; c && !c->tags[t]; 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
151heretag(Arg *arg)
152{
153	int i;
154	Client *c;
155
156	if(arg->i == tsel)
157		return;
158
159	if(!(c = getnext(clients, arg->i)))
160		return;
161
162	for(i = 0; i < TLast; i++)
163		c->tags[i] = NULL;
164	c->tags[tsel] = tags[tsel];
165	pop(c);
166	focus(c);
167}
168
169void
170replacetag(Arg *arg)
171{
172	int i;
173
174	if(!sel)
175		return;
176
177	for(i = 0; i < TLast; i++)
178		sel->tags[i] = NULL;
179	appendtag(arg);
180}
181
182void
183settags(Client *c)
184{
185	char classinst[256];
186	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
187	unsigned int i, j;
188	regex_t regex;
189	regmatch_t tmp;
190	Bool matched = False;
191	XClassHint ch;
192
193	if(!len) {
194		c->tags[tsel] = tags[tsel];
195		return;
196	}
197
198	if(XGetClassHint(dpy, c->win, &ch)) {
199		snprintf(classinst, sizeof(classinst), "%s:%s",
200				ch.res_class ? ch.res_class : "",
201				ch.res_name ? ch.res_name : "");
202		for(i = 0; !matched && i < len; i++) {
203			if(!regcomp(&regex, rule[i].pattern, 0)) {
204				if(!regexec(&regex, classinst, 1, &tmp, 0)) {
205					for(j = 0; j < TLast; j++) {
206						if(rule[i].tags[j])
207							matched = True;
208						c->tags[j] = rule[i].tags[j];
209					}
210					c->isfloat = rule[i].isfloat;
211				}
212				regfree(&regex);
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] = tags[tsel];
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}