aewl

diff tag.c @ 114:dfa5cd0969a6

implemented regexp matching for rules
author arg@10ksloc.org
date Wed, 19 Jul 2006 17:42:08 +0200
parents 3a708f113f55
children 329fd7dae530
line diff
     1.1 --- a/tag.c	Wed Jul 19 16:38:39 2006 +0200
     1.2 +++ b/tag.c	Wed Jul 19 17:42:08 2006 +0200
     1.3 @@ -4,15 +4,25 @@
     1.4   */
     1.5  #include "dwm.h"
     1.6  
     1.7 +#include <regex.h>
     1.8 +#include <stdio.h>
     1.9  #include <string.h>
    1.10 +#include <sys/types.h>
    1.11  #include <X11/Xutil.h>
    1.12  
    1.13  /* static */
    1.14  
    1.15 +typedef struct {
    1.16 +	const char *pattern;
    1.17 +	char *tags[TLast];
    1.18 +	Bool isfloat;
    1.19 +} Rule;
    1.20 +
    1.21  /* CUSTOMIZE */ 
    1.22  static Rule rule[] = {
    1.23 -	/* class			instance	tags						isfloat */
    1.24 -	{ "Firefox-bin",	"firefox-bin",	{ [Twww] = "www" },			False },
    1.25 +	/* class			instance	tags		isfloat */
    1.26 +	{ "Firefox.*",	{ [Twww] = "www" },			False },
    1.27 +	{ "Gimp.*",		{ 0 },						True},
    1.28  };
    1.29  
    1.30  /* extern */
    1.31 @@ -164,10 +174,13 @@
    1.32  void
    1.33  settags(Client *c)
    1.34  {
    1.35 -	XClassHint ch;
    1.36 +	char classinst[256];
    1.37  	static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
    1.38  	unsigned int i, j;
    1.39 +	regex_t regex;
    1.40 +	regmatch_t tmp;
    1.41  	Bool matched = False;
    1.42 +	XClassHint ch;
    1.43  
    1.44  	if(!len) {
    1.45  		c->tags[tsel] = tags[tsel];
    1.46 @@ -175,24 +188,27 @@
    1.47  	}
    1.48  
    1.49  	if(XGetClassHint(dpy, c->win, &ch)) {
    1.50 -		if(ch.res_class && ch.res_name) {
    1.51 -			for(i = 0; i < len; i++)
    1.52 -				if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
    1.53 -					&& !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
    1.54 -				{
    1.55 -					for(j = 0; j < TLast; j++)
    1.56 +		snprintf(classinst, sizeof(classinst), "%s:%s",
    1.57 +				ch.res_class ? ch.res_class : "",
    1.58 +				ch.res_name ? ch.res_name : "");
    1.59 +		for(i = 0; !matched && i < len; i++) {
    1.60 +			if(!regcomp(&regex, rule[i].pattern, 0)) {
    1.61 +				if(!regexec(&regex, classinst, 1, &tmp, 0)) {
    1.62 +					for(j = 0; j < TLast; j++) {
    1.63 +						if(rule[i].tags[j])
    1.64 +							matched = True;
    1.65  						c->tags[j] = rule[i].tags[j];
    1.66 +					}
    1.67  					c->isfloat = rule[i].isfloat;
    1.68 -					matched = True;
    1.69 -					break;
    1.70  				}
    1.71 +				regfree(&regex);
    1.72 +			}
    1.73  		}
    1.74  		if(ch.res_class)
    1.75  			XFree(ch.res_class);
    1.76  		if(ch.res_name)
    1.77  			XFree(ch.res_name);
    1.78  	}
    1.79 -
    1.80  	if(!matched)
    1.81  		c->tags[tsel] = tags[tsel];
    1.82  }