all repos — dwm @ 01b151f5f85b7b488ab6859e0404b64926df16ad

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	Client *c;
 75	int n, i, w, h;
 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	if(!sel)
165		return;
166
167	for(i = 0; i < TLast; i++)
168		sel->tags[i] = NULL;
169	appendtag(arg);
170}
171
172void
173settags(Client *c)
174{
175	char classinst[256];
176	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
177	unsigned int i, j;
178	regex_t regex;
179	regmatch_t tmp;
180	Bool matched = False;
181	XClassHint ch;
182
183	if(!len) {
184		c->tags[tsel] = tags[tsel];
185		return;
186	}
187
188	if(XGetClassHint(dpy, c->win, &ch)) {
189		snprintf(classinst, sizeof(classinst), "%s:%s",
190				ch.res_class ? ch.res_class : "",
191				ch.res_name ? ch.res_name : "");
192		for(i = 0; !matched && i < len; i++) {
193			if(!regcomp(&regex, rule[i].pattern, 0)) {
194				if(!regexec(&regex, classinst, 1, &tmp, 0)) {
195					for(j = 0; j < TLast; j++) {
196						if(rule[i].tags[j])
197							matched = True;
198						c->tags[j] = rule[i].tags[j];
199					}
200					c->isfloat = rule[i].isfloat;
201				}
202				regfree(&regex);
203			}
204		}
205		if(ch.res_class)
206			XFree(ch.res_class);
207		if(ch.res_name)
208			XFree(ch.res_name);
209	}
210	if(!matched)
211		c->tags[tsel] = tags[tsel];
212}
213
214void
215view(Arg *arg)
216{
217	tsel = arg->i;
218	arrange(NULL);
219	drawall();
220}