aewl

annotate tag.c @ 380:4bf79305d675

this algorithm seems to keep order for any scenario
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 29 Aug 2006 09:57:57 +0200
parents a1901753deef
children b00cc483d13b
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@142 36 getnext(Client *c)
garbeam@75 37 {
arg@261 38 for(; c && !isvisible(c); c = c->next);
garbeam@76 39 return c;
garbeam@75 40 }
garbeam@75 41
arg@127 42 Client *
arg@127 43 getprev(Client *c)
arg@127 44 {
arg@261 45 for(; c && !isvisible(c); c = c->prev);
arg@127 46 return c;
arg@127 47 }
arg@127 48
garbeam@75 49 void
arg@191 50 initrregs()
arg@191 51 {
arg@191 52 unsigned int i;
arg@191 53 regex_t *reg;
arg@191 54
arg@191 55 if(rreg)
arg@191 56 return;
arg@191 57 len = sizeof(rule) / sizeof(rule[0]);
arg@191 58 rreg = emallocz(len * sizeof(RReg));
arg@191 59
arg@191 60 for(i = 0; i < len; i++) {
arg@191 61 if(rule[i].clpattern) {
arg@191 62 reg = emallocz(sizeof(regex_t));
arg@191 63 if(regcomp(reg, rule[i].clpattern, 0))
arg@191 64 free(reg);
arg@191 65 else
arg@191 66 rreg[i].clregex = reg;
arg@191 67 }
arg@191 68 if(rule[i].tpattern) {
arg@191 69 reg = emallocz(sizeof(regex_t));
arg@191 70 if(regcomp(reg, rule[i].tpattern, 0))
arg@191 71 free(reg);
arg@191 72 else
arg@191 73 rreg[i].tregex = reg;
arg@191 74 }
arg@191 75 }
arg@191 76 }
arg@191 77
arg@270 78 void
garbeam@76 79 settags(Client *c)
garbeam@76 80 {
arg@336 81 char prop[512];
arg@191 82 unsigned int i, j;
arg@114 83 regmatch_t tmp;
garbeam@76 84 Bool matched = False;
arg@114 85 XClassHint ch;
garbeam@76 86
garbeam@76 87 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];
garbeam@76 109 }
garbeam@76 110
garbeam@76 111 void
arg@284 112 tag(Arg *arg)
arg@284 113 {
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@284 122 settitle(sel);
arg@379 123 detach(sel);
arg@379 124 attach(sel);
arg@287 125 if(!isvisible(sel))
arg@287 126 arrange(NULL);
arg@340 127 else
arg@340 128 drawstatus();
arg@284 129 }
arg@284 130
arg@284 131 void
arg@284 132 toggletag(Arg *arg)
arg@284 133 {
arg@284 134 unsigned int i;
arg@284 135
arg@284 136 if(!sel)
arg@284 137 return;
arg@284 138
arg@284 139 sel->tags[arg->i] = !sel->tags[arg->i];
arg@284 140 for(i = 0; i < ntags && !sel->tags[i]; i++);
arg@284 141 if(i == ntags)
arg@284 142 sel->tags[arg->i] = True;
arg@284 143 settitle(sel);
arg@379 144 detach(sel);
arg@379 145 attach(sel);
arg@287 146 if(!isvisible(sel))
arg@287 147 arrange(NULL);
arg@342 148 else
arg@342 149 drawstatus();
arg@284 150 }