aewl

annotate tag.c @ 692:8d3720e06c89

Jukka, thanks for being faster than me!
author Anselm R. Garbe <arg@suckless.org>
date Mon, 15 Jan 2007 19:01:57 +0100
parents e90bf387bf6f
children 39b941868ce3
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII 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@581 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@609 58 if(regcomp(reg, rule[i].clpattern, REG_EXTENDED))
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@609 65 if(regcomp(reg, rule[i].tpattern, REG_EXTENDED))
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@581 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@609 89 for(i = 0; 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];
garbeam@76 107 }
garbeam@76 108
garbeam@76 109 void
arg@461 110 tag(Arg *arg) {
arg@284 111 unsigned int i;
arg@284 112
arg@284 113 if(!sel)
arg@284 114 return;
arg@284 115 for(i = 0; i < ntags; i++)
arg@594 116 sel->tags[i] = (arg->i == -1) ? True : False;
arg@611 117 if(arg->i >= 0 && arg->i < ntags)
arg@611 118 sel->tags[arg->i] = True;
arg@533 119 arrange();
arg@284 120 }
arg@284 121
arg@284 122 void
arg@461 123 toggletag(Arg *arg) {
arg@284 124 unsigned int i;
arg@284 125
arg@284 126 if(!sel)
arg@284 127 return;
arg@284 128 sel->tags[arg->i] = !sel->tags[arg->i];
arg@284 129 for(i = 0; i < ntags && !sel->tags[i]; i++);
arg@284 130 if(i == ntags)
arg@284 131 sel->tags[arg->i] = True;
arg@533 132 arrange();
arg@284 133 }