dwm-meillo

view tag.c @ 287:5e5e5392c7cb

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