aewl

view tag.c @ 306:71f02d14dce1

added general centralization rule for new clients (works around various borken apps)
author Anselm R.Garbe <arg@10ksloc.org>
date Wed, 16 Aug 2006 15:57:36 +0200
parents 6562340b9ffc
children ec8a66a2b9cc
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 if(sel)
285 arrange(NULL);
286 else
287 drawstatus();
288 }
290 void
291 toggletag(Arg *arg)
292 {
293 unsigned int i;
295 if(!sel)
296 return;
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 }
308 void
309 toggleview(Arg *arg)
310 {
311 unsigned int i;
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 }
320 void
321 view(Arg *arg)
322 {
323 unsigned int i;
325 for(i = 0; i < ntags; i++)
326 seltag[i] = False;
327 seltag[arg->i] = True;
328 arrange(NULL);
329 }