JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
implement multi-tag selection through button3 click on the specific tag
[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 void (*arrange)(Arg *) = DEFMODE;
34
35 /* extern */
36
37 void
38 appendtag(Arg *arg)
39 {
40         if(!sel)
41                 return;
42
43         sel->tags[arg->i] = True;
44         arrange(NULL);
45 }
46
47 void
48 dofloat(Arg *arg)
49 {
50         Client *c;
51
52         for(c = clients; c; c = c->next) {
53                 c->ismax = False;
54                 if(isvisible(c)) {
55                         resize(c, True, TopLeft);
56                 }
57                 else
58                         ban(c);
59         }
60         if((sel = getnext(clients))) {
61                 higher(sel);
62                 focus(sel);
63         }
64         else
65                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
66         drawall();
67 }
68
69 void
70 dotile(Arg *arg)
71 {
72         int n, i, w, h;
73         Client *c;
74
75         w = sw - mw;
76         for(n = 0, c = clients; c; c = c->next)
77                 if(isvisible(c) && !c->isfloat)
78                         n++;
79
80         if(n > 1)
81                 h = (sh - bh) / (n - 1);
82         else
83                 h = sh - bh;
84
85         for(i = 0, c = clients; c; c = c->next) {
86                 c->ismax = False;
87                 if(isvisible(c)) {
88                         if(c->isfloat) {
89                                 higher(c);
90                                 resize(c, True, TopLeft);
91                                 continue;
92                         }
93                         if(n == 1) {
94                                 c->x = sx;
95                                 c->y = sy + bh;
96                                 c->w = sw - 2;
97                                 c->h = sh - 2 - bh;
98                         }
99                         else if(i == 0) {
100                                 c->x = sx;
101                                 c->y = sy + bh;
102                                 c->w = mw - 2;
103                                 c->h = sh - 2 - bh;
104                         }
105                         else if(h > bh) {
106                                 c->x = sx + mw;
107                                 c->y = sy + (i - 1) * h + bh;
108                                 c->w = w - 2;
109                                 if(i + 1 == n)
110                                         c->h = sh - c->y - 2;
111                                 else
112                                         c->h = h - 2;
113                         }
114                         else { /* fallback if h < bh */
115                                 c->x = sx + mw;
116                                 c->y = sy + bh;
117                                 c->w = w - 2;
118                                 c->h = sh - 2 - bh;
119                         }
120                         resize(c, False, TopLeft);
121                         i++;
122                 }
123                 else
124                         ban(c);
125         }
126         if((sel = getnext(clients))) {
127                 higher(sel);
128                 focus(sel);
129         }
130         else
131                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
132         drawall();
133 }
134
135 Client *
136 getnext(Client *c)
137 {
138         for(; c && !isvisible(c); c = c->next);
139         return c;
140 }
141
142 Client *
143 getprev(Client *c)
144 {
145         for(; c && !isvisible(c); c = c->prev);
146         return c;
147 }
148
149 void
150 initrregs()
151 {
152         unsigned int i;
153         regex_t *reg;
154
155         if(rreg)
156                 return;
157         len = sizeof(rule) / sizeof(rule[0]);
158         rreg = emallocz(len * sizeof(RReg));
159
160         for(i = 0; i < len; i++) {
161                 if(rule[i].clpattern) {
162                         reg = emallocz(sizeof(regex_t));
163                         if(regcomp(reg, rule[i].clpattern, 0))
164                                 free(reg);
165                         else
166                                 rreg[i].clregex = reg;
167                 }
168                 if(rule[i].tpattern) {
169                         reg = emallocz(sizeof(regex_t));
170                         if(regcomp(reg, rule[i].tpattern, 0))
171                                 free(reg);
172                         else
173                                 rreg[i].tregex = reg;
174                 }
175         }
176 }
177
178 Bool
179 isvisible(Client *c)
180 {
181         unsigned int i;
182
183         for(i = 0; i < ntags; i++)
184                 if(c->tags[i] && tsel[i])
185                         return True;
186         return False;
187 }
188
189 void
190 replacetag(Arg *arg)
191 {
192         int i;
193
194         if(!sel)
195                 return;
196
197         for(i = 0; i < ntags; i++)
198                 sel->tags[i] = False;
199         appendtag(arg);
200 }
201
202 void
203 settags(Client *c)
204 {
205         char classinst[256];
206         unsigned int i, j;
207         regmatch_t tmp;
208         Bool matched = False;
209         XClassHint ch;
210
211         if(XGetClassHint(dpy, c->win, &ch)) {
212                 snprintf(classinst, sizeof(classinst), "%s:%s",
213                                 ch.res_class ? ch.res_class : "",
214                                 ch.res_name ? ch.res_name : "");
215                 for(i = 0; !matched && i < len; i++)
216                         if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
217                                 c->isfloat = rule[i].isfloat;
218                                 for(j = 0; rreg[i].tregex && j < ntags; j++) {
219                                         if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
220                                                 matched = True;
221                                                 c->tags[j] = True;
222                                         }
223                                 }
224                         }
225                 if(ch.res_class)
226                         XFree(ch.res_class);
227                 if(ch.res_name)
228                         XFree(ch.res_name);
229         }
230         if(!matched)
231                 for(i = 0; i < ntags; i++)
232                         c->tags[i] = tsel[i];
233 }
234
235 void
236 togglemode(Arg *arg)
237 {
238         arrange = arrange == dofloat ? dotile : dofloat;
239         arrange(NULL);
240 }
241
242 void
243 view(Arg *arg)
244 {
245         unsigned int i;
246
247         for(i = 0; i < ntags; i++)
248                 tsel[i] = False;
249         tsel[arg->i] = True;
250         arrange(NULL);
251         drawall();
252 }
253
254 void
255 viewnext(Arg *arg)
256 {
257         unsigned int i;
258
259         for(i = 0; !tsel[i]; i++);
260         arg->i = (i < ntags-1) ? i+1 : 0;
261         view(arg);
262 }
263
264 void
265 viewprev(Arg *arg)
266 {
267         unsigned int i;
268
269         for(i = 0; !tsel[i]; i++);
270         arg->i = (i > 0) ? i-1 : ntags-1;
271         view(arg);
272 }