1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
10 #include <X11/Xutil.h>
12 void (*arrange)(void) = DEFMODE;
13 unsigned int master = MASTER;
14 unsigned int nmaster = NMASTER;
19 const char *clpattern;
32 static RReg *rreg = NULL;
33 static unsigned int len = 0;
36 nextmanaged(Client *c) {
37 for(; c && (c->isfloat || !isvisible(c)); c = c->next);
42 togglemax(Client *c) {
47 if((c->ismax = !c->ismax)) {
52 resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
55 resize(c, c->rx, c->ry, c->rw, c->rh, True);
56 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
70 attachstack(Client *c) {
76 compileregexps(void) {
82 len = sizeof rule / sizeof rule[0];
83 rreg = emallocz(len * sizeof(RReg));
84 for(i = 0; i < len; i++) {
85 if(rule[i].clpattern) {
86 reg = emallocz(sizeof(regex_t));
87 if(regcomp(reg, rule[i].clpattern, REG_EXTENDED))
90 rreg[i].clregex = reg;
92 if(rule[i].tpattern) {
93 reg = emallocz(sizeof(regex_t));
94 if(regcomp(reg, rule[i].tpattern, REG_EXTENDED))
105 c->prev->next = c->next;
107 c->next->prev = c->prev;
110 c->next = c->prev = NULL;
114 detachstack(Client *c) {
116 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
124 for(c = clients; c; c = c->next) {
127 XMoveWindow(dpy, c->win, c->x, c->y);
129 resize(c, c->x, c->y, c->w, c->h, True);
133 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
136 if(!sel || !isvisible(sel)) {
137 for(c = stack; c && !isvisible(c); c = c->snext);
144 unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
147 for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
150 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
151 mw = (n > nmaster) ? (waw * master) / 1000 : waw;
152 th = (n > nmaster) ? wah / (n - nmaster) : 0;
155 for(i = 0, c = clients; c; c = c->next)
158 XMoveWindow(dpy, c->win, c->x, c->y);
167 nw = mw - 2 * BORDERPX;
168 nh = mh - 2 * BORDERPX;
170 else { /* tile window */
172 nw = tw - 2 * BORDERPX;
173 if(th > 2 * BORDERPX) {
174 ny += (i - nmaster) * th;
175 nh = th - 2 * BORDERPX;
177 else /* fallback if th <= 2 * BORDERPX */
178 nh = wah - 2 * BORDERPX;
180 resize(c, nx, ny, nw, nh, False);
185 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
187 if(!sel || !isvisible(sel)) {
188 for(c = stack; c && !isvisible(c); c = c->snext);
195 focusnext(Arg *arg) {
200 for(c = sel->next; c && !isvisible(c); c = c->next);
202 for(c = clients; c && !isvisible(c); c = c->next);
210 focusprev(Arg *arg) {
215 for(c = sel->prev; c && !isvisible(c); c = c->prev);
217 for(c = clients; c && c->next; c = c->next);
218 for(; c && !isvisible(c); c = c->prev);
227 getclient(Window w) {
230 for(c = clients; c; c = c->next)
237 incnmaster(Arg *arg) {
238 if((arrange == dofloat) || (nmaster + arg->i < 1)
239 || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
249 isvisible(Client *c) {
252 for(i = 0; i < ntags; i++)
253 if(c->tags[i] && seltag[i])
259 resizemaster(Arg *arg) {
260 if(arrange != dotile)
265 if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
266 || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
281 if(sel->isfloat || arrange == dofloat)
282 XRaiseWindow(dpy, sel->win);
283 if(arrange != dofloat) {
285 XLowerWindow(dpy, sel->win);
286 for(c = nextmanaged(clients); c; c = nextmanaged(c->next)) {
289 XLowerWindow(dpy, c->win);
293 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
297 settags(Client *c, Client *trans) {
301 Bool matched = trans != NULL;
302 XClassHint ch = { 0 };
305 for(i = 0; i < ntags; i++)
306 c->tags[i] = trans->tags[i];
308 XGetClassHint(dpy, c->win, &ch);
309 snprintf(prop, sizeof prop, "%s:%s:%s",
310 ch.res_class ? ch.res_class : "",
311 ch.res_name ? ch.res_name : "", c->name);
312 for(i = 0; i < len; i++)
313 if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
314 c->isfloat = rule[i].isfloat;
315 for(j = 0; rreg[i].tregex && j < ntags; j++) {
316 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
328 for(i = 0; i < ntags; i++)
329 c->tags[i] = seltag[i];
338 for(i = 0; i < ntags; i++)
339 sel->tags[i] = (arg->i == -1) ? True : False;
340 if(arg->i >= 0 && arg->i < ntags)
341 sel->tags[arg->i] = True;
346 togglefloat(Arg *arg) {
347 if(!sel || arrange == dofloat)
349 sel->isfloat = !sel->isfloat;
354 toggletag(Arg *arg) {
359 sel->tags[arg->i] = !sel->tags[arg->i];
360 for(i = 0; i < ntags && !sel->tags[i]; i++);
362 sel->tags[arg->i] = True;
367 togglemode(Arg *arg) {
368 arrange = (arrange == dofloat) ? dotile : dofloat;
376 toggleview(Arg *arg) {
379 seltag[arg->i] = !seltag[arg->i];
380 for(i = 0; i < ntags && !seltag[i]; i++);
382 seltag[arg->i] = True; /* cannot toggle last view */
390 for(i = 0; i < ntags; i++)
391 seltag[i] = (arg->i == -1) ? True : False;
392 if(arg->i >= 0 && arg->i < ntags)
393 seltag[arg->i] = True;
404 if(sel->isfloat || (arrange == dofloat)) {
408 for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
411 if((c = sel) == nextmanaged(clients))
412 if(!(c = nextmanaged(c->next)))