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