diff tag.c @ 0:7024076fa948

initial add
author paulo@localhost
date Sun, 22 Mar 2009 23:26:35 -0700
parents
children ba504f41828f
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tag.c	Sun Mar 22 23:26:35 2009 -0700
     1.3 @@ -0,0 +1,166 @@
     1.4 +/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
     1.5 + * See LICENSE file for license details.
     1.6 + */
     1.7 +#include "dwm.h"
     1.8 +#include <regex.h>
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include <X11/Xutil.h>
    1.12 +
    1.13 +/* static */
    1.14 +
    1.15 +typedef struct {
    1.16 +	const char *prop;
    1.17 +	const char *tags;
    1.18 +	Bool isversatile;
    1.19 +} Rule;
    1.20 +
    1.21 +typedef struct {
    1.22 +	regex_t *propregex;
    1.23 +	regex_t *tagregex;
    1.24 +} Regs;
    1.25 +
    1.26 +TAGS
    1.27 +RULES
    1.28 +
    1.29 +static Regs *regs = NULL;
    1.30 +static unsigned int nrules = 0;
    1.31 +static unsigned int lastview = 0;
    1.32 +
    1.33 +/* extern */
    1.34 +
    1.35 +void
    1.36 +compileregs(void) {
    1.37 +	unsigned int i;
    1.38 +	regex_t *reg;
    1.39 +
    1.40 +	if(regs)
    1.41 +		return;
    1.42 +	nrules = sizeof rule / sizeof rule[0];
    1.43 +	regs = emallocz(nrules * sizeof(Regs));
    1.44 +	for(i = 0; i < nrules; i++) {
    1.45 +		if(rule[i].prop) {
    1.46 +			reg = emallocz(sizeof(regex_t));
    1.47 +			if(regcomp(reg, rule[i].prop, REG_EXTENDED))
    1.48 +				free(reg);
    1.49 +			else
    1.50 +				regs[i].propregex = reg;
    1.51 +		}
    1.52 +		if(rule[i].tags) {
    1.53 +			reg = emallocz(sizeof(regex_t));
    1.54 +			if(regcomp(reg, rule[i].tags, REG_EXTENDED))
    1.55 +				free(reg);
    1.56 +			else
    1.57 +				regs[i].tagregex = reg;
    1.58 +		}
    1.59 +	}
    1.60 +}
    1.61 +
    1.62 +Bool
    1.63 +isvisible(Client *c) {
    1.64 +	unsigned int i;
    1.65 +
    1.66 +	for(i = 0; i < ntags; i++)
    1.67 +		if(c->tags[i] && seltag[i])
    1.68 +			return True;
    1.69 +	return False;
    1.70 +}
    1.71 +
    1.72 +void
    1.73 +settags(Client *c, Client *trans) {
    1.74 +	char prop[512];
    1.75 +	unsigned int i, j;
    1.76 +	regmatch_t tmp;
    1.77 +	Bool matched = trans != NULL;
    1.78 +	XClassHint ch = { 0 };
    1.79 +
    1.80 +	if(matched)
    1.81 +		for(i = 0; i < ntags; i++)
    1.82 +			c->tags[i] = trans->tags[i];
    1.83 +	else {
    1.84 +		XGetClassHint(dpy, c->win, &ch);
    1.85 +		snprintf(prop, sizeof prop, "%s:%s:%s",
    1.86 +				ch.res_class ? ch.res_class : "",
    1.87 +				ch.res_name ? ch.res_name : "", c->name);
    1.88 +		for(i = 0; i < nrules; i++)
    1.89 +			if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
    1.90 +				c->isversatile = rule[i].isversatile;
    1.91 +				for(j = 0; regs[i].tagregex && j < ntags; j++) {
    1.92 +					if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
    1.93 +						matched = True;
    1.94 +						c->tags[j] = True;
    1.95 +					}
    1.96 +				}
    1.97 +			}
    1.98 +		if(ch.res_class)
    1.99 +			XFree(ch.res_class);
   1.100 +		if(ch.res_name)
   1.101 +			XFree(ch.res_name);
   1.102 +	}
   1.103 +	if(!matched)
   1.104 +		for(i = 0; i < ntags; i++)
   1.105 +			c->tags[i] = seltag[i];
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +tag(Arg *arg) {
   1.110 +	unsigned int i;
   1.111 +
   1.112 +	if(!sel)
   1.113 +		return;
   1.114 +	for(i = 0; i < ntags; i++)
   1.115 +		sel->tags[i] = (arg->i == -1) ? True : False;
   1.116 +	if(arg->i >= 0 && arg->i < ntags)
   1.117 +		sel->tags[arg->i] = True;
   1.118 +	lt->arrange();
   1.119 +}
   1.120 +
   1.121 +void
   1.122 +toggletag(Arg *arg) {
   1.123 +	unsigned int i;
   1.124 +
   1.125 +	if(!sel)
   1.126 +		return;
   1.127 +	sel->tags[arg->i] = !sel->tags[arg->i];
   1.128 +	for(i = 0; i < ntags && !sel->tags[i]; i++);
   1.129 +	if(i == ntags)
   1.130 +		sel->tags[arg->i] = True;
   1.131 +	lt->arrange();
   1.132 +}
   1.133 +
   1.134 +void
   1.135 +toggleview(Arg *arg) {
   1.136 +	unsigned int i;
   1.137 +
   1.138 +	seltag[arg->i] = !seltag[arg->i];
   1.139 +	for(i = 0; i < ntags && !seltag[i]; i++);
   1.140 +	if(i == ntags)
   1.141 +		seltag[arg->i] = True; /* cannot toggle last view */
   1.142 +	lt->arrange();
   1.143 +}
   1.144 +
   1.145 +void
   1.146 +view(Arg *arg) {
   1.147 +	unsigned int i;
   1.148 +	Arg a;
   1.149 +	
   1.150 +	a.i = 0;
   1.151 +	setlayout(&a); /* back to default layout */
   1.152 +
   1.153 +	for(i = 0; i < ntags; i++) {
   1.154 +		if (seltag[i] == True)
   1.155 +			lastview = i;
   1.156 +		seltag[i] = (arg->i == -1) ? True : False;
   1.157 +	}
   1.158 +	if(arg->i >= 0 && arg->i < ntags)
   1.159 +		seltag[arg->i] = True;
   1.160 +	lt->arrange();
   1.161 +}
   1.162 +
   1.163 +void
   1.164 +last_view(Arg *arg) {
   1.165 +	Arg a;
   1.166 +
   1.167 +	a.i = lastview;
   1.168 +	view(&a);
   1.169 +}