all repos — dwm @ 0dfe729f90069de070495c82ddc72cc476a4f3ab

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