garbeam@75: /* garbeam@75: * (C)opyright MMVI Anselm R. Garbe garbeam@75: * See LICENSE file for license details. garbeam@75: */ garbeam@76: #include "dwm.h" arg@114: #include arg@114: #include arg@191: #include garbeam@75: #include arg@114: #include garbeam@75: #include garbeam@75: garbeam@76: arg@114: typedef struct { arg@191: const char *clpattern; arg@191: const char *tpattern; arg@114: Bool isfloat; arg@114: } Rule; arg@114: arg@191: typedef struct { arg@191: regex_t *clregex; arg@191: regex_t *tregex; arg@191: } RReg; arg@191: arg@191: /* static */ arg@191: arg@146: TAGS arg@146: RULES garbeam@84: arg@191: static RReg *rreg = NULL; arg@191: static unsigned int len = 0; arg@191: arg@156: void (*arrange)(Arg *) = DEFMODE; arg@125: arg@125: /* extern */ arg@125: garbeam@76: void garbeam@76: appendtag(Arg *arg) garbeam@75: { garbeam@76: if(!sel) garbeam@76: return; garbeam@75: arg@173: sel->tags[arg->i] = True; garbeam@75: arrange(NULL); garbeam@75: } garbeam@75: garbeam@75: void garbeam@75: dofloat(Arg *arg) garbeam@75: { garbeam@75: Client *c; garbeam@75: garbeam@75: for(c = clients; c; c = c->next) { arg@124: c->ismax = False; arg@261: if(isvisible(c)) { arg@99: resize(c, True, TopLeft); garbeam@95: } garbeam@75: else garbeam@75: ban(c); garbeam@75: } arg@194: if((sel = getnext(clients))) { arg@194: higher(sel); arg@194: focus(sel); garbeam@75: } arg@194: else arg@194: XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); garbeam@75: drawall(); garbeam@75: } garbeam@75: garbeam@75: void garbeam@75: dotile(Arg *arg) garbeam@75: { arg@123: int n, i, w, h; garbeam@75: Client *c; garbeam@75: garbeam@75: w = sw - mw; garbeam@75: for(n = 0, c = clients; c; c = c->next) arg@261: if(isvisible(c) && !c->isfloat) garbeam@75: n++; garbeam@75: garbeam@75: if(n > 1) garbeam@75: h = (sh - bh) / (n - 1); garbeam@75: else garbeam@75: h = sh - bh; garbeam@75: garbeam@75: for(i = 0, c = clients; c; c = c->next) { arg@124: c->ismax = False; arg@261: if(isvisible(c)) { garbeam@80: if(c->isfloat) { garbeam@75: higher(c); arg@99: resize(c, True, TopLeft); garbeam@75: continue; garbeam@75: } garbeam@75: if(n == 1) { arg@115: c->x = sx; arg@115: c->y = sy + bh; arg@164: c->w = sw - 2; arg@164: c->h = sh - 2 - bh; garbeam@75: } garbeam@75: else if(i == 0) { arg@115: c->x = sx; arg@115: c->y = sy + bh; arg@164: c->w = mw - 2; arg@164: c->h = sh - 2 - bh; garbeam@75: } arg@104: else if(h > bh) { arg@115: c->x = sx + mw; arg@115: c->y = sy + (i - 1) * h + bh; arg@164: c->w = w - 2; arg@240: if(i + 1 == n) arg@240: c->h = sh - c->y - 2; arg@240: else arg@240: c->h = h - 2; garbeam@75: } arg@104: else { /* fallback if h < bh */ arg@115: c->x = sx + mw; arg@115: c->y = sy + bh; arg@164: c->w = w - 2; arg@164: c->h = sh - 2 - bh; arg@104: } arg@99: resize(c, False, TopLeft); garbeam@75: i++; garbeam@75: } garbeam@75: else garbeam@75: ban(c); garbeam@75: } arg@194: if((sel = getnext(clients))) { arg@194: higher(sel); arg@194: focus(sel); garbeam@75: } arg@194: else arg@194: XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); garbeam@75: drawall(); garbeam@75: } garbeam@75: garbeam@76: Client * arg@142: getnext(Client *c) garbeam@75: { arg@261: for(; c && !isvisible(c); c = c->next); garbeam@76: return c; garbeam@75: } garbeam@75: arg@127: Client * arg@127: getprev(Client *c) arg@127: { arg@261: for(; c && !isvisible(c); c = c->prev); arg@127: return c; arg@127: } arg@127: garbeam@75: void arg@191: initrregs() arg@191: { arg@191: unsigned int i; arg@191: regex_t *reg; arg@191: arg@191: if(rreg) arg@191: return; arg@191: len = sizeof(rule) / sizeof(rule[0]); arg@191: rreg = emallocz(len * sizeof(RReg)); arg@191: arg@191: for(i = 0; i < len; i++) { arg@191: if(rule[i].clpattern) { arg@191: reg = emallocz(sizeof(regex_t)); arg@191: if(regcomp(reg, rule[i].clpattern, 0)) arg@191: free(reg); arg@191: else arg@191: rreg[i].clregex = reg; arg@191: } arg@191: if(rule[i].tpattern) { arg@191: reg = emallocz(sizeof(regex_t)); arg@191: if(regcomp(reg, rule[i].tpattern, 0)) arg@191: free(reg); arg@191: else arg@191: rreg[i].tregex = reg; arg@191: } arg@191: } arg@191: } arg@191: arg@261: Bool arg@261: isvisible(Client *c) arg@261: { arg@261: unsigned int i; arg@261: arg@261: for(i = 0; i < ntags; i++) arg@261: if(c->tags[i] && tsel[i]) arg@261: return True; arg@261: return False; arg@261: } arg@261: arg@191: void garbeam@75: replacetag(Arg *arg) garbeam@75: { garbeam@75: int i; arg@123: garbeam@75: if(!sel) garbeam@75: return; garbeam@75: arg@178: for(i = 0; i < ntags; i++) arg@173: sel->tags[i] = False; garbeam@75: appendtag(arg); garbeam@75: } garbeam@75: garbeam@76: void garbeam@76: settags(Client *c) garbeam@76: { arg@114: char classinst[256]; arg@191: unsigned int i, j; arg@114: regmatch_t tmp; garbeam@76: Bool matched = False; arg@114: XClassHint ch; garbeam@76: garbeam@76: if(XGetClassHint(dpy, c->win, &ch)) { arg@114: snprintf(classinst, sizeof(classinst), "%s:%s", arg@114: ch.res_class ? ch.res_class : "", arg@114: ch.res_name ? ch.res_name : ""); arg@191: for(i = 0; !matched && i < len; i++) arg@191: if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) { arg@191: c->isfloat = rule[i].isfloat; arg@191: for(j = 0; rreg[i].tregex && j < ntags; j++) { arg@191: if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) { arg@191: matched = True; arg@191: c->tags[j] = True; arg@191: } garbeam@76: } arg@114: } garbeam@76: if(ch.res_class) garbeam@76: XFree(ch.res_class); garbeam@76: if(ch.res_name) garbeam@76: XFree(ch.res_name); garbeam@76: } garbeam@76: if(!matched) arg@261: for(i = 0; i < ntags; i++) arg@261: c->tags[i] = tsel[i]; garbeam@76: } garbeam@76: garbeam@76: void arg@124: togglemode(Arg *arg) arg@124: { arg@124: arrange = arrange == dofloat ? dotile : dofloat; arg@124: arrange(NULL); arg@124: } arg@124: arg@124: void garbeam@76: view(Arg *arg) garbeam@76: { arg@261: unsigned int i; arg@261: arg@261: for(i = 0; i < ntags; i++) arg@261: tsel[i] = False; arg@261: tsel[arg->i] = True; garbeam@76: arrange(NULL); garbeam@76: drawall(); garbeam@76: } arg@144: arg@144: void arg@144: viewnext(Arg *arg) arg@144: { arg@261: unsigned int i; arg@261: arg@261: for(i = 0; !tsel[i]; i++); arg@261: arg->i = (i < ntags-1) ? i+1 : 0; arg@144: view(arg); arg@144: } arg@144: arg@144: void arg@144: viewprev(Arg *arg) arg@144: { arg@261: unsigned int i; arg@261: arg@261: for(i = 0; !tsel[i]; i++); arg@261: arg->i = (i > 0) ? i-1 : ntags-1; arg@144: view(arg); arg@144: }