all repos — dwm @ 95e56ffc0d91f55dcb138b25b6788378971485a5

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