JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed view-change bug reported on the list
[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(c->tags[tsel]) {
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(c->tags[tsel] && !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(c->tags[tsel]) {
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                                 c->h = h - 2;
110                         }
111                         else { /* fallback if h < bh */
112                                 c->x = sx + mw;
113                                 c->y = sy + bh;
114                                 c->w = w - 2;
115                                 c->h = sh - 2 - bh;
116                         }
117                         resize(c, False, TopLeft);
118                         i++;
119                 }
120                 else
121                         ban(c);
122         }
123         if((sel = getnext(clients))) {
124                 higher(sel);
125                 focus(sel);
126         }
127         else
128                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
129         drawall();
130 }
131
132 Client *
133 getnext(Client *c)
134 {
135         for(; c && !c->tags[tsel]; c = c->next);
136         return c;
137 }
138
139 Client *
140 getprev(Client *c)
141 {
142         for(; c && !c->tags[tsel]; c = c->prev);
143         return c;
144 }
145
146 void
147 initrregs()
148 {
149         unsigned int i;
150         regex_t *reg;
151
152         if(rreg)
153                 return;
154         len = sizeof(rule) / sizeof(rule[0]);
155         rreg = emallocz(len * sizeof(RReg));
156
157         for(i = 0; i < len; i++) {
158                 if(rule[i].clpattern) {
159                         reg = emallocz(sizeof(regex_t));
160                         if(regcomp(reg, rule[i].clpattern, 0))
161                                 free(reg);
162                         else
163                                 rreg[i].clregex = reg;
164                 }
165                 if(rule[i].tpattern) {
166                         reg = emallocz(sizeof(regex_t));
167                         if(regcomp(reg, rule[i].tpattern, 0))
168                                 free(reg);
169                         else
170                                 rreg[i].tregex = reg;
171                 }
172         }
173 }
174
175 void
176 replacetag(Arg *arg)
177 {
178         int i;
179
180         if(!sel)
181                 return;
182
183         for(i = 0; i < ntags; i++)
184                 sel->tags[i] = False;
185         appendtag(arg);
186 }
187
188 void
189 settags(Client *c)
190 {
191         char classinst[256];
192         unsigned int i, j;
193         regmatch_t tmp;
194         Bool matched = False;
195         XClassHint ch;
196
197         if(XGetClassHint(dpy, c->win, &ch)) {
198                 snprintf(classinst, sizeof(classinst), "%s:%s",
199                                 ch.res_class ? ch.res_class : "",
200                                 ch.res_name ? ch.res_name : "");
201                 for(i = 0; !matched && i < len; i++)
202                         if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
203                                 c->isfloat = rule[i].isfloat;
204                                 for(j = 0; rreg[i].tregex && j < ntags; j++) {
205                                         if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
206                                                 matched = True;
207                                                 c->tags[j] = True;
208                                         }
209                                 }
210                         }
211                 if(ch.res_class)
212                         XFree(ch.res_class);
213                 if(ch.res_name)
214                         XFree(ch.res_name);
215         }
216         if(!matched)
217                 c->tags[tsel] = True;
218 }
219
220 void
221 togglemode(Arg *arg)
222 {
223         arrange = arrange == dofloat ? dotile : dofloat;
224         arrange(NULL);
225 }
226
227 void
228 view(Arg *arg)
229 {
230         tsel = arg->i;
231         arrange(NULL);
232         drawall();
233 }
234
235 void
236 viewnext(Arg *arg)
237 {
238         arg->i = (tsel < ntags-1) ? tsel+1 : 0;
239         view(arg);
240 }
241
242 void
243 viewprev(Arg *arg)
244 {
245         arg->i = (tsel > 0) ? tsel-1 : ntags-1;
246         view(arg);
247 }