aewl

view tag.c @ 276:9a0a351dd910

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