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