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