dwm-meillo

annotate tag.c @ 535:13ef0d218c67

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