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;
32 static Regexps *regexps = 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));
62 compileregexps(void) {
68 len = sizeof rule / sizeof rule[0];
69 regexps = emallocz(len * sizeof(Regexps));
70 for(i = 0; i < len; i++) {
72 reg = emallocz(sizeof(regex_t));
73 if(regcomp(reg, rule[i].prop, REG_EXTENDED))
76 regexps[i].propregex = reg;
79 reg = emallocz(sizeof(regex_t));
80 if(regcomp(reg, rule[i].tags, REG_EXTENDED))
83 regexps[i].tagregex = reg;
92 for(c = clients; c; c = c->next) {
95 XMoveWindow(dpy, c->win, c->x, c->y);
97 resize(c, c->x, c->y, c->w, c->h, True);
101 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
104 if(!sel || !isvisible(sel)) {
105 for(c = stack; c && !isvisible(c); c = c->snext);
112 unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
115 for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
118 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
119 mw = (n > nmaster) ? (waw * master) / 1000 : waw;
120 th = (n > nmaster) ? wah / (n - nmaster) : 0;
123 for(i = 0, c = clients; c; c = c->next)
126 XMoveWindow(dpy, c->win, c->x, c->y);
135 nw = mw - 2 * BORDERPX;
136 nh = mh - 2 * BORDERPX;
138 else { /* tile window */
140 nw = tw - 2 * BORDERPX;
141 if(th > 2 * BORDERPX) {
142 ny += (i - nmaster) * th;
143 nh = th - 2 * BORDERPX;
145 else /* fallback if th <= 2 * BORDERPX */
146 nh = wah - 2 * BORDERPX;
148 resize(c, nx, ny, nw, nh, False);
153 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
155 if(!sel || !isvisible(sel)) {
156 for(c = stack; c && !isvisible(c); c = c->snext);
163 incnmaster(Arg *arg) {
164 if((arrange == dofloat) || (nmaster + arg->i < 1)
165 || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
175 isvisible(Client *c) {
178 for(i = 0; i < ntags; i++)
179 if(c->tags[i] && seltag[i])
185 resizemaster(Arg *arg) {
186 if(arrange != dotile)
191 if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
192 || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
207 if(sel->isfloat || arrange == dofloat)
208 XRaiseWindow(dpy, sel->win);
209 if(arrange != dofloat) {
211 XLowerWindow(dpy, sel->win);
212 for(c = nextmanaged(clients); c; c = nextmanaged(c->next)) {
215 XLowerWindow(dpy, c->win);
219 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
223 settags(Client *c, Client *trans) {
227 Bool matched = trans != NULL;
228 XClassHint ch = { 0 };
231 for(i = 0; i < ntags; i++)
232 c->tags[i] = trans->tags[i];
234 XGetClassHint(dpy, c->win, &ch);
235 snprintf(prop, sizeof prop, "%s:%s:%s",
236 ch.res_class ? ch.res_class : "",
237 ch.res_name ? ch.res_name : "", c->name);
238 for(i = 0; i < len; i++)
239 if(regexps[i].propregex && !regexec(regexps[i].propregex, prop, 1, &tmp, 0)) {
240 c->isfloat = rule[i].isfloat;
241 for(j = 0; regexps[i].tagregex && j < ntags; j++) {
242 if(!regexec(regexps[i].tagregex, tags[j], 1, &tmp, 0)) {
254 for(i = 0; i < ntags; i++)
255 c->tags[i] = seltag[i];
264 for(i = 0; i < ntags; i++)
265 sel->tags[i] = (arg->i == -1) ? True : False;
266 if(arg->i >= 0 && arg->i < ntags)
267 sel->tags[arg->i] = True;
272 togglefloat(Arg *arg) {
273 if(!sel || arrange == dofloat)
275 sel->isfloat = !sel->isfloat;
280 toggletag(Arg *arg) {
285 sel->tags[arg->i] = !sel->tags[arg->i];
286 for(i = 0; i < ntags && !sel->tags[i]; i++);
288 sel->tags[arg->i] = True;
293 togglemode(Arg *arg) {
294 arrange = (arrange == dofloat) ? dotile : dofloat;
302 toggleview(Arg *arg) {
305 seltag[arg->i] = !seltag[arg->i];
306 for(i = 0; i < ntags && !seltag[i]; i++);
308 seltag[arg->i] = True; /* cannot toggle last view */
316 for(i = 0; i < ntags; i++)
317 seltag[i] = (arg->i == -1) ? True : False;
318 if(arg->i >= 0 && arg->i < ntags)
319 seltag[arg->i] = True;
330 if(sel->isfloat || (arrange == dofloat)) {
334 for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
337 if((c = sel) == nextmanaged(clients))
338 if(!(c = nextmanaged(c->next)))