Xinqi Bao's Git

made tag/view/toggle{tag,view} work on pointer to tags-array, there was the need...
[dwm.git] / tag.c
1 /* See LICENSE file for copyright and license details. */
2 #include "dwm.h"
3 #include <regex.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xatom.h>
8 #include <X11/Xutil.h>
9
10 /* static */
11
12 typedef struct {
13 const char *prop;
14 const char *tags;
15 Bool isfloating;
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 static char prop[512];
29
30 static void
31 persistconfig(Client *c) {
32 unsigned int i;
33
34 for(i = 0; i < ntags && i < sizeof prop - 1; i++)
35 prop[i] = c->tags[i] ? '1' : '0';
36 if(i < sizeof prop - 1)
37 prop[i++] = c->isfloating ? '1' : '0';
38 prop[i] = '\0';
39 XChangeProperty(dpy, c->win, dwmconfig, XA_STRING, 8,
40 PropModeReplace, (unsigned char *)prop, i);
41 }
42
43 static unsigned int
44 idxoftag(const char *tag) {
45 unsigned int i;
46
47 for(i = 0; i < ntags; i++)
48 if(tags[i] == tag)
49 return i;
50 return 0;
51 }
52
53 /* extern */
54
55 void
56 compileregs(void) {
57 unsigned int i;
58 regex_t *reg;
59
60 if(regs)
61 return;
62 nrules = sizeof rule / sizeof rule[0];
63 regs = emallocz(nrules * sizeof(Regs));
64 for(i = 0; i < nrules; i++) {
65 if(rule[i].prop) {
66 reg = emallocz(sizeof(regex_t));
67 if(regcomp(reg, rule[i].prop, REG_EXTENDED))
68 free(reg);
69 else
70 regs[i].propregex = reg;
71 }
72 if(rule[i].tags) {
73 reg = emallocz(sizeof(regex_t));
74 if(regcomp(reg, rule[i].tags, REG_EXTENDED))
75 free(reg);
76 else
77 regs[i].tagregex = reg;
78 }
79 }
80 }
81
82 Bool
83 isvisible(Client *c) {
84 unsigned int i;
85
86 for(i = 0; i < ntags; i++)
87 if(c->tags[i] && seltag[i])
88 return True;
89 return False;
90 }
91
92 void
93 settags(Client *c, Client *trans) {
94 unsigned int i, j;
95 regmatch_t tmp;
96 Bool matched = trans != NULL;
97 XClassHint ch = { 0 };
98 XTextProperty name;
99
100 if(matched) {
101 for(i = 0; i < ntags; i++)
102 c->tags[i] = trans->tags[i];
103 }
104 else {
105 /* check if window has set a property */
106 name.nitems = 0;
107 XGetTextProperty(dpy, c->win, &name, dwmconfig);
108 if(name.nitems && name.encoding == XA_STRING) {
109 strncpy(prop, (char *)name.value, sizeof prop - 1);
110 prop[sizeof prop - 1] = '\0';
111 XFree(name.value);
112 for(i = 0; i < ntags && i < sizeof prop - 1 && prop[i] != '\0'; i++)
113 if((c->tags[i] = prop[i] == '1'))
114 matched = True;
115 if(i < sizeof prop - 1 && prop[i] != '\0')
116 c->isfloating = prop[i] == '1';
117 }
118 }
119 if(!matched) {
120 /* rule matching */
121 XGetClassHint(dpy, c->win, &ch);
122 snprintf(prop, sizeof prop, "%s:%s:%s",
123 ch.res_class ? ch.res_class : "",
124 ch.res_name ? ch.res_name : "", c->name);
125 for(i = 0; i < nrules; i++)
126 if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
127 c->isfloating = rule[i].isfloating;
128 for(j = 0; regs[i].tagregex && j < ntags; j++) {
129 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
130 matched = True;
131 c->tags[j] = True;
132 }
133 }
134 }
135 if(ch.res_class)
136 XFree(ch.res_class);
137 if(ch.res_name)
138 XFree(ch.res_name);
139 }
140 if(!matched)
141 for(i = 0; i < ntags; i++)
142 c->tags[i] = seltag[i];
143 persistconfig(c);
144 }
145
146 void
147 tag(const char *arg) {
148 unsigned int i;
149
150 if(!sel)
151 return;
152 for(i = 0; i < ntags; i++)
153 sel->tags[i] = arg == NULL;
154 i = idxoftag(arg);
155 if(i >= 0 && i < ntags)
156 sel->tags[i] = True;
157 persistconfig(sel);
158 arrange();
159 }
160
161 void
162 togglefloating(const char *arg) {
163 if(!sel || isfloating())
164 return;
165 sel->isfloating = !sel->isfloating;
166 if(sel->isfloating) {
167 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
168 persistconfig(sel);
169 }
170 arrange();
171 }
172
173 void
174 toggletag(const char *arg) {
175 unsigned int i, j;
176
177 if(!sel)
178 return;
179 i = idxoftag(arg);
180 sel->tags[i] = !sel->tags[i];
181 for(j = 0; j < ntags && !sel->tags[j]; j++);
182 if(j == ntags)
183 sel->tags[i] = True;
184 persistconfig(sel);
185 arrange();
186 }
187
188 void
189 toggleview(const char *arg) {
190 unsigned int i, j;
191
192 i = idxoftag(arg);
193 seltag[i] = !seltag[i];
194 for(j = 0; j < ntags && !seltag[j]; j++);
195 if(j == ntags)
196 seltag[i] = True; /* cannot toggle last view */
197 arrange();
198 }
199
200 void
201 view(const char *arg) {
202 unsigned int i;
203
204 for(i = 0; i < ntags; i++)
205 seltag[i] = arg == NULL;
206 i = idxoftag(arg);
207 if(i >= 0 && i < ntags)
208 seltag[i] = True;
209 arrange();
210 }