optimize rule usage

This commit is contained in:
Julien Danjou 2008-01-02 12:44:18 +01:00
parent 346a383258
commit d402ae8f69
5 changed files with 38 additions and 51 deletions

View File

@ -117,10 +117,11 @@ cleanup()
for(r = globalconf.rules; r; r = rn) for(r = globalconf.rules; r; r = rn)
{ {
rn = r->next; rn = r->next;
p_delete(&r->prop); p_delete(&r->prop_r);
p_delete(&r->tags); p_delete(&r->tags_r);
p_delete(&r->propregex); p_delete(&r->xpropval_r);
p_delete(&r->tagregex); p_delete(&r->icon);
p_delete(&r->xprop);
p_delete(&r); p_delete(&r);
} }

View File

@ -710,14 +710,14 @@ config_parse(const char *confpatharg)
for(i = 0; i < cfg_size(cfg_rules, "rule"); i++) for(i = 0; i < cfg_size(cfg_rules, "rule"); i++)
{ {
cfgsectmp = cfg_getnsec(cfg_rules, "rule", i); cfgsectmp = cfg_getnsec(cfg_rules, "rule", i);
rule->prop = a_strdup(cfg_getstr(cfgsectmp, "name")); rule->prop_r = rules_compile_regex(cfg_getstr(cfgsectmp, "name"));
rule->tags = a_strdup(cfg_getstr(cfgsectmp, "tags")); rule->tags_r = rules_compile_regex(cfg_getstr(cfgsectmp, "tags"));
rule->xprop = a_strdup(cfg_getstr(cfgsectmp, "xproperty_name"));
rule->xpropval_r = rules_compile_regex(cfg_getstr(cfgsectmp, "xproperty_value"));
rule->icon = a_strdup(cfg_getstr(cfgsectmp, "icon")); rule->icon = a_strdup(cfg_getstr(cfgsectmp, "icon"));
rule->isfloating = cfg_getbool(cfgsectmp, "float"); rule->isfloating = cfg_getbool(cfgsectmp, "float");
rule->screen = cfg_getint(cfgsectmp, "screen"); rule->screen = cfg_getint(cfgsectmp, "screen");
rule->not_master = cfg_getbool(cfgsectmp, "not_master"); rule->not_master = cfg_getbool(cfgsectmp, "not_master");
rule->xprop = a_strdup(cfg_getstr(cfgsectmp, "xproperty_name"));
rule->xprop_val = a_strdup(cfg_getstr(cfgsectmp, "xproperty_value"));
if(rule->screen >= get_screen_count()) if(rule->screen >= get_screen_count())
rule->screen = 0; rule->screen = 0;
@ -730,8 +730,6 @@ config_parse(const char *confpatharg)
else else
globalconf.rules = NULL; globalconf.rules = NULL;
compileregs(globalconf.rules);
/* Mouse: root window click bindings */ /* Mouse: root window click bindings */
globalconf.buttons.root = parse_mouse_bindings(cfg_mouse, "root", True); globalconf.buttons.root = parse_mouse_bindings(cfg_mouse, "root", True);

View File

@ -40,17 +40,14 @@ enum
typedef struct Rule Rule; typedef struct Rule Rule;
struct Rule struct Rule
{ {
char *prop;
char *tags;
char *icon; char *icon;
char *xprop; char *xprop;
char *xprop_val;
int screen; int screen;
Bool isfloating; Bool isfloating;
Bool not_master; Bool not_master;
regex_t *propregex; regex_t *prop_r;
regex_t *tagregex; regex_t *tags_r;
regex_t *xpropvalregex; regex_t *xpropval_r;
Rule *next; Rule *next;
}; };

29
rules.c
View File

@ -25,8 +25,8 @@
extern AwesomeConf globalconf; extern AwesomeConf globalconf;
static regex_t * regex_t *
compile_regex(char *val) rules_compile_regex(char *val)
{ {
regex_t *reg; regex_t *reg;
@ -42,19 +42,6 @@ compile_regex(char *val)
return NULL; return NULL;
} }
void
compileregs(Rule *rules)
{
Rule *r;
for(r = rules; r; r = r->next)
{
r->propregex = compile_regex(r->prop);
r->tagregex = compile_regex(r->tags);
r->xpropvalregex = compile_regex(r->xprop_val);
}
}
Bool Bool
client_match_rule(Client *c, Rule *r) client_match_rule(Client *c, Rule *r)
{ {
@ -62,8 +49,10 @@ client_match_rule(Client *c, Rule *r)
regmatch_t tmp; regmatch_t tmp;
ssize_t len; ssize_t len;
XClassHint ch = { 0, 0 }; XClassHint ch = { 0, 0 };
Bool ret; Bool ret = False;
if(r->prop_r)
{
/* first try to match on name */ /* first try to match on name */
XGetClassHint(globalconf.display, c->win, &ch); XGetClassHint(globalconf.display, c->win, &ch);
@ -74,7 +63,7 @@ client_match_rule(Client *c, Rule *r)
snprintf(prop, len + 3, "%s:%s:%s", snprintf(prop, len + 3, "%s:%s:%s",
ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name); ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name);
ret = (r->propregex && !regexec(r->propregex, prop, 1, &tmp, 0)); ret = !regexec(r->prop_r, prop, 1, &tmp, 0);
p_delete(&prop); p_delete(&prop);
@ -85,12 +74,14 @@ client_match_rule(Client *c, Rule *r)
if(ret) if(ret)
return True; return True;
}
if(r->xprop if(r->xprop
&& r->xpropval_r
&& xgettextprop(globalconf.display, c->win, && xgettextprop(globalconf.display, c->win,
XInternAtom(globalconf.display, r->xprop, False), XInternAtom(globalconf.display, r->xprop, False),
buf, ssizeof(buf))) buf, ssizeof(buf)))
ret = (r->xpropvalregex && !regexec(r->xpropvalregex, buf, 1, &tmp, 0)); ret = !regexec(r->xpropval_r, buf, 1, &tmp, 0);
return ret; return ret;
} }
@ -100,7 +91,7 @@ is_tag_match_rules(Tag *t, Rule *r)
{ {
regmatch_t tmp; regmatch_t tmp;
if(r->tagregex && !regexec(r->tagregex, t->name, 1, &tmp, 0)) if(r->tags_r && !regexec(r->tags_r, t->name, 1, &tmp, 0))
return True; return True;
return False; return False;

View File

@ -26,7 +26,7 @@
#define RULE_NOSCREEN -1 #define RULE_NOSCREEN -1
void compileregs(Rule *); regex_t * rules_compile_regex(char *);
Bool client_match_rule(Client *, Rule *); Bool client_match_rule(Client *, Rule *);
Bool is_tag_match_rules(Tag *, Rule *); Bool is_tag_match_rules(Tag *, Rule *);