From 7d97b36e5f50013251be023797bc4746ce67f5bb Mon Sep 17 00:00:00 2001 From: Owain Ainsworth Date: Tue, 25 Aug 2009 01:44:40 +0000 Subject: Reduce duplciation of code for checking modifiers in key/mouse bindings. shaves a bunch of bytes. ok okan@ --- app/cwm/conf.c | 78 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/app/cwm/conf.c b/app/cwm/conf.c index b2b202919..b88770d22 100644 --- a/app/cwm/conf.c +++ b/app/cwm/conf.c @@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $Id: conf.c,v 1.68 2009/08/25 01:42:05 oga Exp $ + * $Id: conf.c,v 1.69 2009/08/25 01:44:39 oga Exp $ */ #include "headers.h" @@ -437,36 +437,40 @@ conf_ungrab(struct conf *c, struct keybinding *kb) xu_key_ungrab(sc->rootwin, kb->modmask, kb->keysym); } +struct { + char chr; + int mask; +} bind_mods[] = { + { 'C', ControlMask }, + { 'M', Mod1Mask }, + { '4', Mod4Mask }, + { 'S', ShiftMask }, +}; + void conf_bindname(struct conf *c, char *name, char *binding) { struct keybinding *current_binding; - char *substring; + char *substring, *tmp; int iter; current_binding = xcalloc(1, sizeof(*current_binding)); - if (strchr(name, 'C') != NULL && - strchr(name, 'C') < strchr(name, '-')) - current_binding->modmask |= ControlMask; - - if (strchr(name, 'M') != NULL && - strchr(name, 'M') < strchr(name, '-')) - current_binding->modmask |= Mod1Mask; - - if (strchr(name, '4') != NULL && - strchr(name, '4') < strchr(name, '-')) - current_binding->modmask |= Mod4Mask; - - if (strchr(name, 'S') != NULL && - strchr(name, 'S') < strchr(name, '-')) - current_binding->modmask |= ShiftMask; - - substring = strchr(name, '-') + 1; + if ((substring = strchr(name, '-')) != NULL) { + for (iter = 0; iter < (sizeof(bind_mods) / + sizeof(bind_mods[0])); iter++) { + if ((tmp = strchr(name, bind_mods[iter].chr)) != + NULL && tmp < substring) { + current_binding->modmask |= + bind_mods[iter].mask; + } + } - /* If there is no '-' in name, continue as is */ - if (strchr(name, '-') == NULL) + /* skip past the modifiers */ + substring++; + } else { substring = name; + } if (substring[0] == '[' && substring[strlen(substring)-1] == ']') { @@ -552,31 +556,25 @@ void conf_mousebind(struct conf *c, char *name, char *binding) { struct mousebinding *current_binding; - char *substring; + char *substring, *tmp; const char *errstr; int iter; current_binding = xcalloc(1, sizeof(*current_binding)); - if (strchr(name, 'C') != NULL && - strchr(name, 'C') < strchr(name, '-')) - current_binding->modmask |= ControlMask; - - if (strchr(name, 'M') != NULL && - strchr(name, 'M') < strchr(name, '-')) - current_binding->modmask |= Mod1Mask; - - if (strchr(name, 'S') != NULL && - strchr(name, 'S') < strchr(name, '-')) - current_binding->modmask |= ShiftMask; - - if (strchr(name, '4') != NULL && - strchr(name, '4') < strchr(name, '-')) - current_binding->modmask |= Mod4Mask; - - substring = strchr(name, '-') + 1; + if ((substring = strchr(name, '-')) != NULL) { + for (iter = 0; iter < (sizeof(bind_mods) / + sizeof(bind_mods[0])); iter++) { + if ((tmp = strchr(name, bind_mods[iter].chr)) != + NULL && tmp < substring) { + current_binding->modmask |= + bind_mods[iter].mask; + } + } - if (strchr(name, '-') == NULL) + /* skip past the modifiers */ + substring++; + } else substring = name; current_binding->button = strtonum(substring, 1, 3, &errstr); -- cgit v1.2.3