all repos — dwm @ dc5d967ee61046f899b3b49daeb9268c8161844a

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
 28/* extern */
 29
 30/* CUSTOMIZE */
 31char *tags[TLast] = {
 32	[Tscratch] = "scratch",
 33	[Tdev] = "dev",
 34	[Twww] = "www",
 35	[Twork] = "work",
 36};
 37void (*arrange)(Arg *) = dotile;
 38
 39void
 40appendtag(Arg *arg)
 41{
 42	if(!sel)
 43		return;
 44
 45	sel->tags[arg->i] = tags[arg->i];
 46	arrange(NULL);
 47}
 48
 49void
 50dofloat(Arg *arg)
 51{
 52	Client *c;
 53
 54	arrange = dofloat;
 55	for(c = clients; c; c = c->next) {
 56		if(c->tags[tsel]) {
 57			resize(c, True, TopLeft);
 58		}
 59		else
 60			ban(c);
 61	}
 62	if(sel && !sel->tags[tsel]) {
 63		if((sel = getnext(clients, tsel))) {
 64			higher(sel);
 65			focus(sel);
 66		}
 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	arrange = dotile;
 79	for(n = 0, c = clients; c; c = c->next)
 80		if(c->tags[tsel] && !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		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 * c->border;
 99				c->h = sh - 2 * c->border - bh;
100			}
101			else if(i == 0) {
102				c->x = sx;
103				c->y = sy + bh;
104				c->w = mw - 2 * c->border;
105				c->h = sh - 2 * c->border - 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 * c->border;
111				c->h = h - 2 * c->border;
112			}
113			else { /* fallback if h < bh */
114				c->x = sx + mw;
115				c->y = sy + bh;
116				c->w = w - 2 * c->border;
117				c->h = sh - 2 * c->border - 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, tsel))) {
127			higher(sel);
128			focus(sel);
129		}
130	}
131	drawall();
132}
133
134Client *
135getnext(Client *c, unsigned int t)
136{
137	for(; c && !c->tags[t]; c = c->next);
138	return c;
139}
140
141void
142heretag(Arg *arg)
143{
144	int i;
145	Client *c;
146
147	if(arg->i == tsel)
148		return;
149
150	if(!(c = getnext(clients, arg->i)))
151		return;
152
153	for(i = 0; i < TLast; i++)
154		c->tags[i] = NULL;
155	c->tags[tsel] = tags[tsel];
156	pop(c);
157	focus(c);
158}
159
160void
161replacetag(Arg *arg)
162{
163	int i;
164
165	if(!sel)
166		return;
167
168	for(i = 0; i < TLast; i++)
169		sel->tags[i] = NULL;
170	appendtag(arg);
171}
172
173void
174settags(Client *c)
175{
176	char classinst[256];
177	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
178	unsigned int i, j;
179	regex_t regex;
180	regmatch_t tmp;
181	Bool matched = False;
182	XClassHint ch;
183
184	if(!len) {
185		c->tags[tsel] = tags[tsel];
186		return;
187	}
188
189	if(XGetClassHint(dpy, c->win, &ch)) {
190		snprintf(classinst, sizeof(classinst), "%s:%s",
191				ch.res_class ? ch.res_class : "",
192				ch.res_name ? ch.res_name : "");
193		for(i = 0; !matched && i < len; i++) {
194			if(!regcomp(&regex, rule[i].pattern, 0)) {
195				if(!regexec(&regex, classinst, 1, &tmp, 0)) {
196					for(j = 0; j < TLast; j++) {
197						if(rule[i].tags[j])
198							matched = True;
199						c->tags[j] = rule[i].tags[j];
200					}
201					c->isfloat = rule[i].isfloat;
202				}
203				regfree(&regex);
204			}
205		}
206		if(ch.res_class)
207			XFree(ch.res_class);
208		if(ch.res_name)
209			XFree(ch.res_name);
210	}
211	if(!matched)
212		c->tags[tsel] = tags[tsel];
213}
214
215void
216view(Arg *arg)
217{
218	tsel = arg->i;
219	arrange(NULL);
220	drawall();
221}