Xinqi Bao's Git

several changes, made togglemax extern and separated it from zoom() - moved zoom...
[dwm.git] / tag.c
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <regex.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <X11/Xutil.h>
9
10 /* static */
11
12 typedef struct {
13 const char *prop;
14 const char *tags;
15 Bool isversatile;
16 } Rule;
17
18 typedef struct {
19 regex_t *propregex;
20 regex_t *tagregex;
21 } Regs;
22
23 TAGS
24 RULES
25
26 static Regs *regs = NULL;
27 static unsigned int nrules = 0;
28
29 /* extern */
30
31 void
32 compileregs(void) {
33 unsigned int i;
34 regex_t *reg;
35
36 if(regs)
37 return;
38 nrules = sizeof rule / sizeof rule[0];
39 regs = emallocz(nrules * sizeof(Regs));
40 for(i = 0; i < nrules; i++) {
41 if(rule[i].prop) {
42 reg = emallocz(sizeof(regex_t));
43 if(regcomp(reg, rule[i].prop, REG_EXTENDED))
44 free(reg);
45 else
46 regs[i].propregex = reg;
47 }
48 if(rule[i].tags) {
49 reg = emallocz(sizeof(regex_t));
50 if(regcomp(reg, rule[i].tags, REG_EXTENDED))
51 free(reg);
52 else
53 regs[i].tagregex = reg;
54 }
55 }
56 }
57
58 Bool
59 isvisible(Client *c) {
60 unsigned int i;
61
62 for(i = 0; i < ntags; i++)
63 if(c->tags[i] && seltag[i])
64 return True;
65 return False;
66 }
67
68 void
69 settags(Client *c, Client *trans) {
70 char prop[512];
71 unsigned int i, j;
72 regmatch_t tmp;
73 Bool matched = trans != NULL;
74 XClassHint ch = { 0 };
75
76 if(matched)
77 for(i = 0; i < ntags; i++)
78 c->tags[i] = trans->tags[i];
79 else {
80 XGetClassHint(dpy, c->win, &ch);
81 snprintf(prop, sizeof prop, "%s:%s:%s",
82 ch.res_class ? ch.res_class : "",
83 ch.res_name ? ch.res_name : "", c->name);
84 for(i = 0; i < nrules; i++)
85 if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
86 c->isversatile = rule[i].isversatile;
87 for(j = 0; regs[i].tagregex && j < ntags; j++) {
88 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
89 matched = True;
90 c->tags[j] = True;
91 }
92 }
93 }
94 if(ch.res_class)
95 XFree(ch.res_class);
96 if(ch.res_name)
97 XFree(ch.res_name);
98 }
99 if(!matched)
100 for(i = 0; i < ntags; i++)
101 c->tags[i] = seltag[i];
102 }
103
104 void
105 tag(Arg arg) {
106 unsigned int i;
107
108 if(!sel)
109 return;
110 for(i = 0; i < ntags; i++)
111 sel->tags[i] = (arg.i == -1) ? True : False;
112 if(arg.i >= 0 && arg.i < ntags)
113 sel->tags[arg.i] = True;
114 lt->arrange();
115 }
116
117 void
118 toggletag(Arg arg) {
119 unsigned int i;
120
121 if(!sel)
122 return;
123 sel->tags[arg.i] = !sel->tags[arg.i];
124 for(i = 0; i < ntags && !sel->tags[i]; i++);
125 if(i == ntags)
126 sel->tags[arg.i] = True;
127 lt->arrange();
128 }
129
130 void
131 toggleview(Arg arg) {
132 unsigned int i;
133
134 seltag[arg.i] = !seltag[arg.i];
135 for(i = 0; i < ntags && !seltag[i]; i++);
136 if(i == ntags)
137 seltag[arg.i] = True; /* cannot toggle last view */
138 lt->arrange();
139 }
140
141 void
142 view(Arg arg) {
143 unsigned int i;
144
145 for(i = 0; i < ntags; i++)
146 seltag[i] = (arg.i == -1) ? True : False;
147 if(arg.i >= 0 && arg.i < ntags)
148 seltag[arg.i] = True;
149 lt->arrange();
150 }