dwm-meillo

annotate tag.c @ 494:36f9bb3da949

small fix
author arg@mmvi
date Tue, 26 Sep 2006 13:30:30 +0200
parents 9d23330a5268
children 651f2c868b31
rev   line source
garbeam@75 1 /*
garbeam@75 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@75 3 * See LICENSE file for license details.
garbeam@75 4 */
garbeam@76 5 #include "dwm.h"
arg@114 6 #include <regex.h>
arg@114 7 #include <stdio.h>
arg@191 8 #include <stdlib.h>
garbeam@75 9 #include <string.h>
arg@114 10 #include <sys/types.h>
garbeam@75 11 #include <X11/Xutil.h>
garbeam@75 12
garbeam@76 13
arg@114 14 typedef struct {
arg@191 15 const char *clpattern;
arg@191 16 const char *tpattern;
arg@114 17 Bool isfloat;
arg@114 18 } Rule;
arg@114 19
arg@191 20 typedef struct {
arg@191 21 regex_t *clregex;
arg@191 22 regex_t *tregex;
arg@191 23 } RReg;
arg@191 24
arg@191 25 /* static */
arg@191 26
arg@146 27 TAGS
arg@146 28 RULES
garbeam@84 29
arg@191 30 static RReg *rreg = NULL;
arg@191 31 static unsigned int len = 0;
arg@191 32
arg@125 33 /* extern */
arg@125 34
garbeam@76 35 Client *
arg@461 36 getnext(Client *c) {
arg@261 37 for(; c && !isvisible(c); c = c->next);
garbeam@76 38 return c;
garbeam@75 39 }
garbeam@75 40
arg@127 41 Client *
arg@461 42 getprev(Client *c) {
arg@261 43 for(; c && !isvisible(c); c = c->prev);
arg@127 44 return c;
arg@127 45 }
arg@127 46
garbeam@75 47 void
arg@487 48 initrregs(void) {
arg@191 49 unsigned int i;
arg@191 50 regex_t *reg;
arg@191 51
arg@191 52 if(rreg)
arg@191 53 return;
arg@191 54 len = sizeof(rule) / sizeof(rule[0]);
arg@191 55 rreg = emallocz(len * sizeof(RReg));
arg@191 56
arg@191 57 for(i = 0; i < len; i++) {
arg@191 58 if(rule[i].clpattern) {
arg@191 59 reg = emallocz(sizeof(regex_t));
arg@191 60 if(regcomp(reg, rule[i].clpattern, 0))
arg@191 61 free(reg);
arg@191 62 else
arg@191 63 rreg[i].clregex = reg;
arg@191 64 }
arg@191 65 if(rule[i].tpattern) {
arg@191 66 reg = emallocz(sizeof(regex_t));
arg@191 67 if(regcomp(reg, rule[i].tpattern, 0))
arg@191 68 free(reg);
arg@191 69 else
arg@191 70 rreg[i].tregex = reg;
arg@191 71 }
arg@191 72 }
arg@191 73 }
arg@191 74
arg@270 75 void
arg@461 76 settags(Client *c, Client *trans) {
arg@336 77 char prop[512];
arg@191 78 unsigned int i, j;
arg@114 79 regmatch_t tmp;
arg@431 80 Bool matched = trans != NULL;
arg@114 81 XClassHint ch;
garbeam@76 82
arg@431 83 if(matched) {
arg@431 84 for(i = 0; i < ntags; i++)
arg@431 85 c->tags[i] = trans->tags[i];
arg@431 86 }
arg@431 87 else if(XGetClassHint(dpy, c->win, &ch)) {
arg@336 88 snprintf(prop, sizeof(prop), "%s:%s:%s",
arg@114 89 ch.res_class ? ch.res_class : "",
arg@336 90 ch.res_name ? ch.res_name : "", c->name);
arg@191 91 for(i = 0; !matched && i < len; i++)
arg@336 92 if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
arg@191 93 c->isfloat = rule[i].isfloat;
arg@191 94 for(j = 0; rreg[i].tregex && j < ntags; j++) {
arg@191 95 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
arg@191 96 matched = True;
arg@191 97 c->tags[j] = True;
arg@191 98 }
garbeam@76 99 }
arg@114 100 }
garbeam@76 101 if(ch.res_class)
garbeam@76 102 XFree(ch.res_class);
garbeam@76 103 if(ch.res_name)
garbeam@76 104 XFree(ch.res_name);
garbeam@76 105 }
garbeam@76 106 if(!matched)
arg@261 107 for(i = 0; i < ntags; i++)
arg@262 108 c->tags[i] = seltag[i];
arg@441 109 for(c->weight = 0; c->weight < ntags && !c->tags[c->weight]; c->weight++);
garbeam@76 110 }
garbeam@76 111
garbeam@76 112 void
arg@461 113 tag(Arg *arg) {
arg@284 114 unsigned int i;
arg@284 115
arg@284 116 if(!sel)
arg@284 117 return;
arg@284 118
arg@284 119 for(i = 0; i < ntags; i++)
arg@284 120 sel->tags[i] = False;
arg@284 121 sel->tags[arg->i] = True;
arg@441 122 sel->weight = arg->i;
arg@393 123 arrange(NULL);
arg@284 124 }
arg@284 125
arg@284 126 void
arg@461 127 toggletag(Arg *arg) {
arg@284 128 unsigned int i;
arg@284 129
arg@284 130 if(!sel)
arg@284 131 return;
arg@284 132
arg@284 133 sel->tags[arg->i] = !sel->tags[arg->i];
arg@284 134 for(i = 0; i < ntags && !sel->tags[i]; i++);
arg@284 135 if(i == ntags)
arg@284 136 sel->tags[arg->i] = True;
arg@441 137 sel->weight = (i == ntags) ? arg->i : i;
arg@393 138 arrange(NULL);
arg@284 139 }