2007-11-13 21:40:33 +01:00
|
|
|
/*
|
|
|
|
* rules.c - rules management
|
|
|
|
*
|
|
|
|
* Copyright © 2007 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rules.h"
|
2008-01-02 12:23:01 +01:00
|
|
|
#include "xutil.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.h"
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2007-12-27 23:10:43 +01:00
|
|
|
extern AwesomeConf globalconf;
|
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
regex_t *
|
|
|
|
rules_compile_regex(char *val)
|
2008-01-02 12:23:01 +01:00
|
|
|
{
|
|
|
|
regex_t *reg;
|
|
|
|
|
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
reg = p_new(regex_t, 1);
|
|
|
|
if(regcomp(reg, val, REG_EXTENDED))
|
|
|
|
p_delete(®);
|
|
|
|
else
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-12 22:59:13 +01:00
|
|
|
static Bool
|
2007-11-13 21:40:33 +01:00
|
|
|
client_match_rule(Client *c, Rule *r)
|
|
|
|
{
|
2008-01-02 12:23:01 +01:00
|
|
|
char *prop, buf[512];
|
2007-11-13 21:40:33 +01:00
|
|
|
regmatch_t tmp;
|
|
|
|
ssize_t len;
|
|
|
|
XClassHint ch = { 0, 0 };
|
2008-01-02 12:44:18 +01:00
|
|
|
Bool ret = False;
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
if(r->prop_r)
|
|
|
|
{
|
|
|
|
/* first try to match on name */
|
|
|
|
XGetClassHint(globalconf.display, c->win, &ch);
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
len = a_strlen(ch.res_class) + a_strlen(ch.res_name) + a_strlen(c->name);
|
|
|
|
prop = p_new(char, len + 3);
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
/* rule matching */
|
|
|
|
snprintf(prop, len + 3, "%s:%s:%s",
|
|
|
|
ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name);
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
ret = !regexec(r->prop_r, prop, 1, &tmp, 0);
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
p_delete(&prop);
|
2007-11-13 21:40:33 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
if(ch.res_class)
|
|
|
|
XFree(ch.res_class);
|
|
|
|
if(ch.res_name)
|
|
|
|
XFree(ch.res_name);
|
2008-01-02 12:23:01 +01:00
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
if(ret)
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2008-01-02 12:23:01 +01:00
|
|
|
if(r->xprop
|
2008-01-02 12:44:18 +01:00
|
|
|
&& r->xpropval_r
|
2008-01-06 22:53:40 +01:00
|
|
|
&& xgettextprop(c->win,
|
2008-01-02 12:23:01 +01:00
|
|
|
XInternAtom(globalconf.display, r->xprop, False),
|
|
|
|
buf, ssizeof(buf)))
|
2008-01-02 12:44:18 +01:00
|
|
|
ret = !regexec(r->xpropval_r, buf, 1, &tmp, 0);
|
2008-01-02 12:23:01 +01:00
|
|
|
|
2007-11-13 21:40:33 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bool
|
2008-01-12 22:49:27 +01:00
|
|
|
tag_match_rule(Tag *t, Rule *r)
|
2007-11-13 21:40:33 +01:00
|
|
|
{
|
|
|
|
regmatch_t tmp;
|
|
|
|
|
2008-01-02 12:44:18 +01:00
|
|
|
if(r->tags_r && !regexec(r->tags_r, t->name, 1, &tmp, 0))
|
2007-11-13 21:40:33 +01:00
|
|
|
return True;
|
|
|
|
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2008-01-12 22:59:13 +01:00
|
|
|
Rule *
|
|
|
|
rule_matching_client(Client *c)
|
|
|
|
{
|
|
|
|
Rule *r;
|
|
|
|
for(r = globalconf.rules; r; r = r->next)
|
|
|
|
if(client_match_rule(c, r))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-07 00:36:45 +01:00
|
|
|
RuleFloat
|
|
|
|
rules_get_float_from_str(const char *str)
|
|
|
|
{
|
|
|
|
if(!a_strcmp(str, "true") || !a_strcmp(str, "yes"))
|
|
|
|
return Float;
|
|
|
|
else if(!a_strcmp(str, "false") || !a_strcmp(str, "no"))
|
|
|
|
return Tile;
|
|
|
|
|
|
|
|
return Auto;
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|