From d402ae8f69e255af610bb12b82f8233cfafef49a Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 2 Jan 2008 12:44:18 +0100 Subject: [PATCH] optimize rule usage --- awesome.c | 9 +++++---- config.c | 10 ++++------ config.h | 9 +++------ rules.c | 59 +++++++++++++++++++++++-------------------------------- rules.h | 2 +- 5 files changed, 38 insertions(+), 51 deletions(-) diff --git a/awesome.c b/awesome.c index 48ad307d..acdd4e9b 100644 --- a/awesome.c +++ b/awesome.c @@ -117,10 +117,11 @@ cleanup() for(r = globalconf.rules; r; r = rn) { rn = r->next; - p_delete(&r->prop); - p_delete(&r->tags); - p_delete(&r->propregex); - p_delete(&r->tagregex); + p_delete(&r->prop_r); + p_delete(&r->tags_r); + p_delete(&r->xpropval_r); + p_delete(&r->icon); + p_delete(&r->xprop); p_delete(&r); } diff --git a/config.c b/config.c index b4fa0943..1db5e0c6 100644 --- a/config.c +++ b/config.c @@ -710,14 +710,14 @@ config_parse(const char *confpatharg) for(i = 0; i < cfg_size(cfg_rules, "rule"); i++) { cfgsectmp = cfg_getnsec(cfg_rules, "rule", i); - rule->prop = a_strdup(cfg_getstr(cfgsectmp, "name")); - rule->tags = a_strdup(cfg_getstr(cfgsectmp, "tags")); + rule->prop_r = rules_compile_regex(cfg_getstr(cfgsectmp, "name")); + 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->isfloating = cfg_getbool(cfgsectmp, "float"); rule->screen = cfg_getint(cfgsectmp, "screen"); 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()) rule->screen = 0; @@ -730,8 +730,6 @@ config_parse(const char *confpatharg) else globalconf.rules = NULL; - compileregs(globalconf.rules); - /* Mouse: root window click bindings */ globalconf.buttons.root = parse_mouse_bindings(cfg_mouse, "root", True); diff --git a/config.h b/config.h index 49138d5b..0e7139cb 100644 --- a/config.h +++ b/config.h @@ -40,17 +40,14 @@ enum typedef struct Rule Rule; struct Rule { - char *prop; - char *tags; char *icon; char *xprop; - char *xprop_val; int screen; Bool isfloating; Bool not_master; - regex_t *propregex; - regex_t *tagregex; - regex_t *xpropvalregex; + regex_t *prop_r; + regex_t *tags_r; + regex_t *xpropval_r; Rule *next; }; diff --git a/rules.c b/rules.c index c633cdc8..8e2800d8 100644 --- a/rules.c +++ b/rules.c @@ -25,8 +25,8 @@ extern AwesomeConf globalconf; -static regex_t * -compile_regex(char *val) +regex_t * +rules_compile_regex(char *val) { regex_t *reg; @@ -42,19 +42,6 @@ compile_regex(char *val) 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 client_match_rule(Client *c, Rule *r) { @@ -62,35 +49,39 @@ client_match_rule(Client *c, Rule *r) regmatch_t tmp; ssize_t len; XClassHint ch = { 0, 0 }; - Bool ret; + Bool ret = False; - /* first try to match on name */ - XGetClassHint(globalconf.display, c->win, &ch); + if(r->prop_r) + { + /* first try to match on name */ + XGetClassHint(globalconf.display, c->win, &ch); - len = a_strlen(ch.res_class) + a_strlen(ch.res_name) + a_strlen(c->name); - prop = p_new(char, len + 3); + len = a_strlen(ch.res_class) + a_strlen(ch.res_name) + a_strlen(c->name); + prop = p_new(char, len + 3); - /* rule matching */ - snprintf(prop, len + 3, "%s:%s:%s", - ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name); + /* rule matching */ + snprintf(prop, len + 3, "%s:%s:%s", + 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); - if(ch.res_class) - XFree(ch.res_class); - if(ch.res_name) - XFree(ch.res_name); - - if(ret) - return True; + if(ch.res_class) + XFree(ch.res_class); + if(ch.res_name) + XFree(ch.res_name); + if(ret) + return True; + } + if(r->xprop + && r->xpropval_r && xgettextprop(globalconf.display, c->win, XInternAtom(globalconf.display, r->xprop, False), buf, ssizeof(buf))) - ret = (r->xpropvalregex && !regexec(r->xpropvalregex, buf, 1, &tmp, 0)); + ret = !regexec(r->xpropval_r, buf, 1, &tmp, 0); return ret; } @@ -100,7 +91,7 @@ is_tag_match_rules(Tag *t, Rule *r) { 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 False; diff --git a/rules.h b/rules.h index 5ef97107..390b76a4 100644 --- a/rules.h +++ b/rules.h @@ -26,7 +26,7 @@ #define RULE_NOSCREEN -1 -void compileregs(Rule *); +regex_t * rules_compile_regex(char *); Bool client_match_rule(Client *, Rule *); Bool is_tag_match_rules(Tag *, Rule *);