[doc] generate rc skeleton based on the order found in common/configopts
[awesome.git] / rules.c
blob4aa8f15724c27bd25e9c2dac10b5f2dc7f19a17d
1 /*
2 * rules.c - rules management
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <X11/Xutil.h>
24 #include "rules.h"
25 #include "common/xutil.h"
27 extern AwesomeConf globalconf;
29 regex_t *
30 rules_compile_regex(char *val)
32 regex_t *reg;
34 if(val)
36 reg = p_new(regex_t, 1);
37 if(regcomp(reg, val, REG_EXTENDED))
38 p_delete(&reg);
39 else
40 return reg;
43 return NULL;
46 static Bool
47 client_match_rule(Client *c, Rule *r)
49 char *prop, buf[512];
50 regmatch_t tmp;
51 ssize_t len;
52 XClassHint ch = { 0, 0 };
53 Bool ret = False;
55 if(r->prop_r)
57 /* first try to match on name */
58 XGetClassHint(globalconf.display, c->win, &ch);
60 len = a_strlen(ch.res_class) + a_strlen(ch.res_name) + a_strlen(c->name);
61 prop = p_new(char, len + 3);
63 /* rule matching */
64 snprintf(prop, len + 3, "%s:%s:%s",
65 ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name);
67 ret = !regexec(r->prop_r, prop, 1, &tmp, 0);
69 p_delete(&prop);
71 if(ch.res_class)
72 XFree(ch.res_class);
73 if(ch.res_name)
74 XFree(ch.res_name);
76 if(ret)
77 return True;
80 if(r->xprop
81 && r->xpropval_r
82 && xgettextprop(globalconf.display, c->win,
83 XInternAtom(globalconf.display, r->xprop, False),
84 buf, ssizeof(buf)))
85 ret = !regexec(r->xpropval_r, buf, 1, &tmp, 0);
87 return ret;
90 Bool
91 tag_match_rule(Tag *t, Rule *r)
93 regmatch_t tmp;
95 if(r->tags_r && !regexec(r->tags_r, t->name, 1, &tmp, 0))
96 return True;
98 return False;
101 Rule *
102 rule_matching_client(Client *c)
104 Rule *r;
105 for(r = globalconf.rules; r; r = r->next)
106 if(client_match_rule(c, r))
107 return r;
109 return NULL;
112 Fuzzy
113 rules_get_fuzzy_from_str(const char *str)
115 if(!a_strcmp(str, "true") || !a_strcmp(str, "yes"))
116 return Yes;
117 else if(!a_strcmp(str, "false") || !a_strcmp(str, "no"))
118 return No;
120 return Maybe;
123 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80