tag.c (view raw)
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
14typedef struct {
15 const char *clpattern;
16 const char *tpattern;
17 Bool isfloat;
18} Rule;
19
20typedef struct {
21 regex_t *clregex;
22 regex_t *tregex;
23} RReg;
24
25/* static */
26
27TAGS
28RULES
29
30static RReg *rreg = NULL;
31static unsigned int len = 0;
32
33void (*arrange)(Arg *) = DEFMODE;
34
35/* extern */
36
37void
38dofloat(Arg *arg)
39{
40 Client *c;
41
42 for(c = clients; c; c = c->next) {
43 c->ismax = False;
44 if(isvisible(c)) {
45 resize(c, True, TopLeft);
46 }
47 else
48 ban(c);
49 }
50 if(!sel || !isvisible(sel))
51 sel = getnext(clients);
52 if(sel) {
53 focus(sel);
54 restack();
55 }
56 else
57 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
58}
59
60void
61dotile(Arg *arg)
62{
63 int h, i, n, w;
64 Client *c;
65
66 w = sw - mw;
67 for(n = 0, c = clients; c; c = c->next)
68 if(isvisible(c) && !c->isfloat)
69 n++;
70
71 if(n > 1)
72 h = (sh - bh) / (n - 1);
73 else
74 h = sh - bh;
75
76 for(i = 0, c = clients; c; c = c->next) {
77 c->ismax = False;
78 if(isvisible(c)) {
79 if(c->isfloat) {
80 resize(c, True, TopLeft);
81 continue;
82 }
83 if(n == 1) {
84 c->x = sx;
85 c->y = sy + bh;
86 c->w = sw - 2;
87 c->h = sh - 2 - bh;
88 }
89 else if(i == 0) {
90 c->x = sx;
91 c->y = sy + bh;
92 c->w = mw - 2;
93 c->h = sh - 2 - bh;
94 }
95 else if(h > bh) {
96 c->x = sx + mw;
97 c->y = sy + (i - 1) * h + bh;
98 c->w = w - 2;
99 if(i + 1 == n)
100 c->h = sh - c->y - 2;
101 else
102 c->h = h - 2;
103 }
104 else { /* fallback if h < bh */
105 c->x = sx + mw;
106 c->y = sy + bh;
107 c->w = w - 2;
108 c->h = sh - 2 - bh;
109 }
110 resize(c, False, TopLeft);
111 i++;
112 }
113 else
114 ban(c);
115 }
116 if(!sel || !isvisible(sel))
117 sel = getnext(clients);
118 if(sel)
119 focus(sel);
120 else
121 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
122 restack();
123}
124
125Client *
126getnext(Client *c)
127{
128 for(; c && !isvisible(c); c = c->next);
129 return c;
130}
131
132Client *
133getprev(Client *c)
134{
135 for(; c && !isvisible(c); c = c->prev);
136 return c;
137}
138
139void
140initrregs()
141{
142 unsigned int i;
143 regex_t *reg;
144
145 if(rreg)
146 return;
147 len = sizeof(rule) / sizeof(rule[0]);
148 rreg = emallocz(len * sizeof(RReg));
149
150 for(i = 0; i < len; i++) {
151 if(rule[i].clpattern) {
152 reg = emallocz(sizeof(regex_t));
153 if(regcomp(reg, rule[i].clpattern, 0))
154 free(reg);
155 else
156 rreg[i].clregex = reg;
157 }
158 if(rule[i].tpattern) {
159 reg = emallocz(sizeof(regex_t));
160 if(regcomp(reg, rule[i].tpattern, 0))
161 free(reg);
162 else
163 rreg[i].tregex = reg;
164 }
165 }
166}
167
168Bool
169isvisible(Client *c)
170{
171 unsigned int i;
172
173 for(i = 0; i < ntags; i++)
174 if(c->tags[i] && seltag[i])
175 return True;
176 return False;
177}
178
179void
180restack()
181{
182 static unsigned int nwins = 0;
183 static Window *wins = NULL;
184 unsigned int f, fi, m, mi, n;
185 Client *c;
186 XEvent ev;
187
188 for(f = 0, m = 0, c = clients; c; c = c->next)
189 if(isvisible(c)) {
190 if(c->isfloat || arrange == dofloat)
191 f++;
192 else
193 m++;
194 }
195 if(!(n = 2 * (f + m))) {
196 drawstatus();
197 return;
198 }
199 if(nwins < n) {
200 nwins = n;
201 wins = erealloc(wins, nwins * sizeof(Window));
202 }
203
204 fi = 0;
205 mi = 2 * f;
206 if(sel->isfloat || arrange == dofloat) {
207 wins[fi++] = sel->title;
208 wins[fi++] = sel->win;
209 }
210 else {
211 wins[mi++] = sel->title;
212 wins[mi++] = sel->win;
213 }
214 for(c = clients; c; c = c->next)
215 if(isvisible(c) && c != sel) {
216 if(c->isfloat || arrange == dofloat) {
217 wins[fi++] = c->title;
218 wins[fi++] = c->win;
219 }
220 else {
221 wins[mi++] = c->title;
222 wins[mi++] = c->win;
223 }
224 }
225 XRestackWindows(dpy, wins, n);
226 drawall();
227 XSync(dpy, False);
228 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
229}
230
231void
232settags(Client *c)
233{
234 char classinst[256];
235 unsigned int i, j;
236 regmatch_t tmp;
237 Bool matched = False;
238 XClassHint ch;
239
240 if(XGetClassHint(dpy, c->win, &ch)) {
241 snprintf(classinst, sizeof(classinst), "%s:%s",
242 ch.res_class ? ch.res_class : "",
243 ch.res_name ? ch.res_name : "");
244 for(i = 0; !matched && i < len; i++)
245 if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
246 c->isfloat = rule[i].isfloat;
247 for(j = 0; rreg[i].tregex && j < ntags; j++) {
248 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
249 matched = True;
250 c->tags[j] = True;
251 }
252 }
253 }
254 if(ch.res_class)
255 XFree(ch.res_class);
256 if(ch.res_name)
257 XFree(ch.res_name);
258 }
259 if(!matched)
260 for(i = 0; i < ntags; i++)
261 c->tags[i] = seltag[i];
262}
263
264void
265tag(Arg *arg)
266{
267 unsigned int i;
268
269 if(!sel)
270 return;
271
272 for(i = 0; i < ntags; i++)
273 sel->tags[i] = False;
274 sel->tags[arg->i] = True;
275 settitle(sel);
276 if(!isvisible(sel))
277 arrange(NULL);
278}
279
280void
281togglemode(Arg *arg)
282{
283 arrange = arrange == dofloat ? dotile : dofloat;
284 if(sel)
285 arrange(NULL);
286 else
287 drawstatus();
288}
289
290void
291toggletag(Arg *arg)
292{
293 unsigned int i;
294
295 if(!sel)
296 return;
297
298 sel->tags[arg->i] = !sel->tags[arg->i];
299 for(i = 0; i < ntags && !sel->tags[i]; i++);
300 if(i == ntags)
301 sel->tags[arg->i] = True;
302 settitle(sel);
303 if(!isvisible(sel))
304 arrange(NULL);
305}
306
307
308void
309toggleview(Arg *arg)
310{
311 unsigned int i;
312
313 seltag[arg->i] = !seltag[arg->i];
314 for(i = 0; i < ntags && !seltag[i]; i++);
315 if(i == ntags)
316 seltag[arg->i] = True; /* cannot toggle last view */
317 arrange(NULL);
318}
319
320void
321view(Arg *arg)
322{
323 unsigned int i;
324
325 for(i = 0; i < ntags; i++)
326 seltag[i] = False;
327 seltag[arg->i] = True;
328 arrange(NULL);
329}