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