all repos — dwm @ e8389a4cc0f1c35bcb7e7646102bd6d6a830207e

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 <stdlib.h>
  9#include <string.h>
 10#include <sys/types.h>
 11#include <X11/Xutil.h>
 12
 13
 14typedef struct {
 15	const char *clpattern;
 16	const char *tpattern;
 17	Bool isfloat;
 18} Rule;
 19
 20typedef struct {
 21	regex_t *clregex;
 22	regex_t *tregex;
 23} RReg;
 24
 25/* static */
 26
 27TAGS
 28RULES
 29
 30static RReg *rreg = NULL;
 31static unsigned int len = 0;
 32
 33/* extern */
 34
 35Client *
 36getnext(Client *c)
 37{
 38	for(; c && !isvisible(c); c = c->next);
 39	return c;
 40}
 41
 42Client *
 43getprev(Client *c)
 44{
 45	for(; c && !isvisible(c); c = c->prev);
 46	return c;
 47}
 48
 49void
 50initrregs()
 51{
 52	unsigned int i;
 53	regex_t *reg;
 54
 55	if(rreg)
 56		return;
 57	len = sizeof(rule) / sizeof(rule[0]);
 58	rreg = emallocz(len * sizeof(RReg));
 59
 60	for(i = 0; i < len; i++) {
 61		if(rule[i].clpattern) {
 62			reg = emallocz(sizeof(regex_t));
 63			if(regcomp(reg, rule[i].clpattern, 0))
 64				free(reg);
 65			else
 66				rreg[i].clregex = reg;
 67		}
 68		if(rule[i].tpattern) {
 69			reg = emallocz(sizeof(regex_t));
 70			if(regcomp(reg, rule[i].tpattern, 0))
 71				free(reg);
 72			else
 73				rreg[i].tregex = reg;
 74		}
 75	}
 76}
 77
 78void
 79settags(Client *c, Client *trans)
 80{
 81	char prop[512];
 82	unsigned int i, j;
 83	regmatch_t tmp;
 84	Bool matched = trans != NULL;
 85	XClassHint ch;
 86
 87	if(matched) {
 88		for(i = 0; i < ntags; i++)
 89			c->tags[i] = trans->tags[i];
 90	}
 91	else if(XGetClassHint(dpy, c->win, &ch)) {
 92		snprintf(prop, sizeof(prop), "%s:%s:%s",
 93				ch.res_class ? ch.res_class : "",
 94				ch.res_name ? ch.res_name : "", c->name);
 95		for(i = 0; !matched && i < len; i++)
 96			if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
 97				c->isfloat = rule[i].isfloat;
 98				for(j = 0; rreg[i].tregex && j < ntags; j++) {
 99					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
100						matched = True;
101						c->tags[j] = True;
102					}
103				}
104			}
105		if(ch.res_class)
106			XFree(ch.res_class);
107		if(ch.res_name)
108			XFree(ch.res_name);
109	}
110	if(!matched)
111		for(i = 0; i < ntags; i++)
112			c->tags[i] = seltag[i];
113	if(!c->isfloat)
114		for(c->weight = 0; c->weight < ntags && !c->tags[c->weight]; c->weight++);
115}
116
117void
118tag(Arg *arg)
119{
120	unsigned int i;
121
122	if(!sel)
123		return;
124
125	for(i = 0; i < ntags; i++)
126		sel->tags[i] = False;
127	sel->tags[arg->i] = True;
128	if(!sel->isfloat)
129		sel->weight = arg->i;
130	arrange(NULL);
131}
132
133void
134toggletag(Arg *arg)
135{
136	unsigned int i;
137
138	if(!sel)
139		return;
140
141	sel->tags[arg->i] = !sel->tags[arg->i];
142	for(i = 0; i < ntags && !sel->tags[i]; i++);
143	if(i == ntags)
144		sel->tags[arg->i] = True;
145	if(!sel->isfloat)
146		sel->weight = (i == ntags) ? arg->i : i;
147	arrange(NULL);
148}