JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
cleaned the CUSTOMIZE flags
[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
7 #include <regex.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <X11/Xutil.h>
12
13 /* static */
14
15 typedef struct {
16         const char *pattern;
17         char *tags[TLast];
18         Bool isfloat;
19 } Rule;
20
21 /* CUSTOMIZE */ 
22 static Rule rule[] = {
23         /* class:instance       tags                            isfloat */
24         { "Firefox.*",          { [Twww] = "www" },             False },
25         { "Gimp.*",             { 0 },                          True},
26 };
27
28 char *tags[TLast] = {
29         [Tscratch] = "scratch",
30         [Tdev] = "dev",
31         [Twww] = "www",
32         [Twork] = "work",
33 };
34
35 void (*arrange)(Arg *) = dotile;
36
37 /* END CUSTOMIZE */
38
39 /* extern */
40
41 void
42 appendtag(Arg *arg)
43 {
44         if(!sel)
45                 return;
46
47         sel->tags[arg->i] = tags[arg->i];
48         arrange(NULL);
49 }
50
51 void
52 dofloat(Arg *arg)
53 {
54         Client *c;
55
56         for(c = clients; c; c = c->next) {
57                 c->ismax = False;
58                 if(c->tags[tsel]) {
59                         resize(c, True, TopLeft);
60                 }
61                 else
62                         ban(c);
63         }
64         if(sel && !sel->tags[tsel]) {
65                 if((sel = getnext(clients, tsel))) {
66                         higher(sel);
67                         focus(sel);
68                 }
69         }
70         drawall();
71 }
72
73 void
74 dotile(Arg *arg)
75 {
76         int n, i, w, h;
77         Client *c;
78
79         w = sw - mw;
80         for(n = 0, c = clients; c; c = c->next)
81                 if(c->tags[tsel] && !c->isfloat)
82                         n++;
83
84         if(n > 1)
85                 h = (sh - bh) / (n - 1);
86         else
87                 h = sh - bh;
88
89         for(i = 0, c = clients; c; c = c->next) {
90                 c->ismax = False;
91                 if(c->tags[tsel]) {
92                         if(c->isfloat) {
93                                 higher(c);
94                                 resize(c, True, TopLeft);
95                                 continue;
96                         }
97                         if(n == 1) {
98                                 c->x = sx;
99                                 c->y = sy + bh;
100                                 c->w = sw - 2 * c->border;
101                                 c->h = sh - 2 * c->border - bh;
102                         }
103                         else if(i == 0) {
104                                 c->x = sx;
105                                 c->y = sy + bh;
106                                 c->w = mw - 2 * c->border;
107                                 c->h = sh - 2 * c->border - bh;
108                         }
109                         else if(h > bh) {
110                                 c->x = sx + mw;
111                                 c->y = sy + (i - 1) * h + bh;
112                                 c->w = w - 2 * c->border;
113                                 c->h = h - 2 * c->border;
114                         }
115                         else { /* fallback if h < bh */
116                                 c->x = sx + mw;
117                                 c->y = sy + bh;
118                                 c->w = w - 2 * c->border;
119                                 c->h = sh - 2 * c->border - bh;
120                         }
121                         resize(c, False, TopLeft);
122                         i++;
123                 }
124                 else
125                         ban(c);
126         }
127         if(!sel || (sel && !sel->tags[tsel])) {
128                 if((sel = getnext(clients, tsel))) {
129                         higher(sel);
130                         focus(sel);
131                 }
132         }
133         drawall();
134 }
135
136 Client *
137 getnext(Client *c, unsigned int t)
138 {
139         for(; c && !c->tags[t]; c = c->next);
140         return c;
141 }
142
143 void
144 heretag(Arg *arg)
145 {
146         int i;
147         Client *c;
148
149         if(arg->i == tsel)
150                 return;
151
152         if(!(c = getnext(clients, arg->i)))
153                 return;
154
155         for(i = 0; i < TLast; i++)
156                 c->tags[i] = NULL;
157         c->tags[tsel] = tags[tsel];
158         pop(c);
159         focus(c);
160 }
161
162 void
163 replacetag(Arg *arg)
164 {
165         int i;
166
167         if(!sel)
168                 return;
169
170         for(i = 0; i < TLast; i++)
171                 sel->tags[i] = NULL;
172         appendtag(arg);
173 }
174
175 void
176 settags(Client *c)
177 {
178         char classinst[256];
179         static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
180         unsigned int i, j;
181         regex_t regex;
182         regmatch_t tmp;
183         Bool matched = False;
184         XClassHint ch;
185
186         if(!len) {
187                 c->tags[tsel] = tags[tsel];
188                 return;
189         }
190
191         if(XGetClassHint(dpy, c->win, &ch)) {
192                 snprintf(classinst, sizeof(classinst), "%s:%s",
193                                 ch.res_class ? ch.res_class : "",
194                                 ch.res_name ? ch.res_name : "");
195                 for(i = 0; !matched && i < len; i++) {
196                         if(!regcomp(&regex, rule[i].pattern, 0)) {
197                                 if(!regexec(&regex, classinst, 1, &tmp, 0)) {
198                                         for(j = 0; j < TLast; j++) {
199                                                 if(rule[i].tags[j])
200                                                         matched = True;
201                                                 c->tags[j] = rule[i].tags[j];
202                                         }
203                                         c->isfloat = rule[i].isfloat;
204                                 }
205                                 regfree(&regex);
206                         }
207                 }
208                 if(ch.res_class)
209                         XFree(ch.res_class);
210                 if(ch.res_name)
211                         XFree(ch.res_name);
212         }
213         if(!matched)
214                 c->tags[tsel] = tags[tsel];
215 }
216
217 void
218 togglemode(Arg *arg)
219 {
220         arrange = arrange == dofloat ? dotile : dofloat;
221         arrange(NULL);
222 }
223
224 void
225 view(Arg *arg)
226 {
227         tsel = arg->i;
228         arrange(NULL);
229         drawall();
230 }