aewl

view tag.c @ 319:94d2d7658673

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