aewl

view tag.c @ 297:6db0d04d292a

Added tag 0.9 for changeset fae61afa861755636c4a1070694209ace8efbb6c
author Anselm R.Garbe <arg@10ksloc.org>
date Tue, 15 Aug 2006 10:25:30 +0200
parents 5e5e5392c7cb
children 71f02d14dce1
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 restack();
55 }
56 else
57 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
58 }
60 void
61 dotile(Arg *arg)
62 {
63 int h, i, n, w;
64 Client *c;
66 w = sw - mw;
67 for(n = 0, c = clients; c; c = c->next)
68 if(isvisible(c) && !c->isfloat)
69 n++;
71 if(n > 1)
72 h = (sh - bh) / (n - 1);
73 else
74 h = sh - bh;
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 }
125 Client *
126 getnext(Client *c)
127 {
128 for(; c && !isvisible(c); c = c->next);
129 return c;
130 }
132 Client *
133 getprev(Client *c)
134 {
135 for(; c && !isvisible(c); c = c->prev);
136 return c;
137 }
139 void
140 initrregs()
141 {
142 unsigned int i;
143 regex_t *reg;
145 if(rreg)
146 return;
147 len = sizeof(rule) / sizeof(rule[0]);
148 rreg = emallocz(len * sizeof(RReg));
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 }
168 Bool
169 isvisible(Client *c)
170 {
171 unsigned int i;
173 for(i = 0; i < ntags; i++)
174 if(c->tags[i] && seltag[i])
175 return True;
176 return False;
177 }
179 void
180 restack()
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;
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 }
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 }
231 void
232 settags(Client *c)
233 {
234 char classinst[256];
235 unsigned int i, j;
236 regmatch_t tmp;
237 Bool matched = False;
238 XClassHint ch;
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 }
264 void
265 tag(Arg *arg)
266 {
267 unsigned int i;
269 if(!sel)
270 return;
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 }
280 void
281 togglemode(Arg *arg)
282 {
283 arrange = arrange == dofloat ? dotile : dofloat;
284 arrange(NULL);
285 }
287 void
288 toggletag(Arg *arg)
289 {
290 unsigned int i;
292 if(!sel)
293 return;
295 sel->tags[arg->i] = !sel->tags[arg->i];
296 for(i = 0; i < ntags && !sel->tags[i]; i++);
297 if(i == ntags)
298 sel->tags[arg->i] = True;
299 settitle(sel);
300 if(!isvisible(sel))
301 arrange(NULL);
302 }
305 void
306 toggleview(Arg *arg)
307 {
308 unsigned int i;
310 seltag[arg->i] = !seltag[arg->i];
311 for(i = 0; i < ntags && !seltag[i]; i++);
312 if(i == ntags)
313 seltag[arg->i] = True; /* cannot toggle last view */
314 arrange(NULL);
315 }
317 void
318 view(Arg *arg)
319 {
320 unsigned int i;
322 for(i = 0; i < ntags; i++)
323 seltag[i] = False;
324 seltag[arg->i] = True;
325 arrange(NULL);
326 }