/* This C code handles all layouts. * You may choose to remove layouts here if you don't find them useful. * * Luckily for you though, I've added simple options to disable them from getting compiled into the code at all. * To change options, edit toggle.h. */ #if LAYOUT_TILE static void tile(Monitor *m) { unsigned int i, n; int oh, ov, ih, iv; int mx = 0, my = 0, mh = 0, mw = 0; int sx = 0, sy = 0, sh = 0, sw = 0; float mfacts, sfacts; int mrest, srest; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; sx = mx = m->wx + ov; sy = my = m->wy + oh; mh = m->wh - 2*oh - ih * (MIN(n, m->mastercount) - 1); sh = m->wh - 2*oh - ih * (n - m->mastercount - 1); sw = mw = m->ww - 2*ov; if (m->mastercount && n > m->mastercount) { sw = (mw - iv) * (1 - m->mfact); mw = mw - iv - sw; sx = mx + mw + iv; } getfacts(m, mh, sh, &mfacts, &sfacts, &mrest, &srest); for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) if (i < m->mastercount) { resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0); my += HEIGHT(c) + ih; } else { resize(c, sx, sy, sw - (2*c->bw), sh * (c->cfact / sfacts) + ((i - m->mastercount) < srest ? 1 : 0) - (2*c->bw), 0); sy += HEIGHT(c) + ih; } } #endif #if LAYOUT_MONOCLE void monocle(Monitor *m) { int oh, ov, ih, iv; Client *c; if (monocleclientcount && !monoclecount) { unsigned int n = 0; getgaps(m, &oh, &ov, &ih, &iv, &n); for (c = m->clients; c; c = c->next) if (ISVISIBLE(c)) n++; if (n > 0) /* override layout symbol */ snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n); } for (c = m->stack; c && (!ISVISIBLE(c) || c->isfloating); c = c->snext); if (c && !c->isfloating) { XMoveWindow(dpy, c->win, m->wx, m->wy); resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0); c = c->snext; } for (; c; c = c->snext) if (!c->isfloating && ISVISIBLE(c)) XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y); } #endif #if LAYOUT_GRID void grid(Monitor *m) { unsigned int i, n; int cx, cy, cw, ch, cc, cr, chrest, cwrest, cols, rows; int oh, ov, ih, iv; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); /* grid dimensions */ for (rows = 0; rows <= n/2; rows++) if (rows*rows >= n) break; cols = (rows && (rows - 1) * rows >= n) ? rows - 1 : rows; /* window geoms (cell height/width) */ ch = (m->wh - 2*oh - ih * (rows - 1)) / (rows ? rows : 1); cw = (m->ww - 2*ov - iv * (cols - 1)) / (cols ? cols : 1); chrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows; cwrest = (m->ww - 2*ov - iv * (cols - 1)) - cw * cols; for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { cc = i / rows; cr = i % rows; cx = m->wx + ov + cc * (cw + iv) + MIN(cc, cwrest); cy = m->wy + oh + cr * (ch + ih) + MIN(cr, chrest); resize(c, cx, cy, cw + (cc < cwrest ? 1 : 0) - 2*c->bw, ch + (cr < chrest ? 1 : 0) - 2*c->bw, False); } } #endif #if LAYOUT_FIBO void fibonacci(Monitor *m, int s) { unsigned int i, n; int nx, ny, nw, nh; int oh, ov, ih, iv; int nv, hrest = 0, wrest = 0, r = 1; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; nx = m->wx + ov; ny = m->wy + oh; nw = m->ww - 2*ov; nh = m->wh - 2*oh; for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) { if (r) { if ((i % 2 && (nh - ih) / 2 <= (bh + 2*c->bw)) || (!(i % 2) && (nw - iv) / 2 <= (bh + 2*c->bw))) { r = 0; } if (r && i < n - 1) { if (i % 2) { nv = (nh - ih) / 2; hrest = nh - 2*nv - ih; nh = nv; } else { nv = (nw - iv) / 2; wrest = nw - 2*nv - iv; nw = nv; } if ((i % 4) == 2 && !s) nx += nw + iv; else if ((i % 4) == 3 && !s) ny += nh + ih; } if ((i % 4) == 0) { if (s) { ny += nh + ih; nh += hrest; } else { nh -= hrest; ny -= nh + ih; } } else if ((i % 4) == 1) { nx += nw + iv; nw += wrest; } else if ((i % 4) == 2) { ny += nh + ih; nh += hrest; if (i < n - 1) nw += wrest; } else if ((i % 4) == 3) { if (s) { nx += nw + iv; nw -= wrest; } else { nw -= wrest; nx -= nw + iv; nh += hrest; } } if (i == 0) { if (n != 1) { nw = (m->ww - iv - 2*ov) - (m->ww - iv - 2*ov) * (1 - m->mfact); wrest = 0; } ny = m->wy + oh; } else if (i == 1) nw = m->ww - nw - iv - 2*ov; i++; } resize(c, nx, ny, nw - (2*c->bw), nh - (2*c->bw), False); } } #endif #if LAYOUT_DWINDLE void dwindle(Monitor *m) { fibonacci(m, 1); } #endif #if LAYOUT_SPIRAL void spiral(Monitor *m) { fibonacci(m, 0); } #endif #if LAYOUT_BSTACK static void bstack(Monitor *m) { unsigned int i, n; int oh, ov, ih, iv; int mx = 0, my = 0, mh = 0, mw = 0; int sx = 0, sy = 0, sh = 0, sw = 0; float mfacts, sfacts; int mrest, srest; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; sx = mx = m->wx + ov; sy = my = m->wy + oh; sh = mh = m->wh - 2*oh; mw = m->ww - 2*ov - iv * (MIN(n, m->mastercount) - 1); sw = m->ww - 2*ov - iv * (n - m->mastercount - 1); if (m->mastercount && n > m->mastercount) { sh = (mh - ih) * (1 - m->mfact); mh = mh - ih - sh; sx = mx; sy = my + mh + ih; } getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest); for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { if (i < m->mastercount) { resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); mx += WIDTH(c) + iv; } else { resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - m->mastercount) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0); sx += WIDTH(c) + iv; } } } #endif #if LAYOUT_BSTACKH static void bstackhoriz(Monitor *m) { unsigned int i, n; int oh, ov, ih, iv; int mx = 0, my = 0, mh = 0, mw = 0; int sx = 0, sy = 0, sh = 0, sw = 0; float mfacts, sfacts; int mrest, srest; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; sx = mx = m->wx + ov; sy = my = m->wy + oh; mh = m->wh - 2*oh; sh = m->wh - 2*oh - ih * (n - m->mastercount - 1); mw = m->ww - 2*ov - iv * (MIN(n, m->mastercount) - 1); sw = m->ww - 2*ov; if (m->mastercount && n > m->mastercount) { sh = (mh - ih) * (1 - m->mfact); mh = mh - ih - sh; sy = my + mh + ih; sh = m->wh - mh - 2*oh - ih * (n - m->mastercount); } getfacts(m, mw, sh, &mfacts, &sfacts, &mrest, &srest); for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { if (i < m->mastercount) { resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); mx += WIDTH(c) + iv; } else { resize(c, sx, sy, sw - (2*c->bw), sh * (c->cfact / sfacts) + ((i - m->mastercount) < srest ? 1 : 0) - (2*c->bw), 0); sy += HEIGHT(c) + ih; } } } #endif #if LAYOUT_HGRID void horizgrid(Monitor *m) { Client *c; unsigned int n, i; int oh, ov, ih, iv; int mx = 0, my = 0, mh = 0, mw = 0; int sx = 0, sy = 0, sh = 0, sw = 0; int ntop, nbottom = 1; float mfacts = 0, sfacts = 0; int mrest, srest, mtotal = 0, stotal = 0; /* Count windows */ getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; if (n <= 2) ntop = n; else { ntop = n / 2; nbottom = n - ntop; } sx = mx = m->wx + ov; sy = my = m->wy + oh; sh = mh = m->wh - 2*oh; sw = mw = m->ww - 2*ov; if (n > ntop) { sh = (mh - ih) / 2; mh = mh - ih - sh; sy = my + mh + ih; mw = m->ww - 2*ov - iv * (ntop - 1); sw = m->ww - 2*ov - iv * (nbottom - 1); } /* calculate facts */ for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) if (i < ntop) mfacts += c->cfact; else sfacts += c->cfact; for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) if (i < ntop) mtotal += mh * (c->cfact / mfacts); else stotal += sw * (c->cfact / sfacts); mrest = mh - mtotal; srest = sw - stotal; for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) if (i < ntop) { resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); mx += WIDTH(c) + iv; } else { resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - ntop) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0); sx += WIDTH(c) + iv; } } #endif #if LAYOUT_DGRID void dynamicgrid(Monitor *m) { unsigned int n; int ri = 0, ci = 0; /* counters */ int oh, ov, ih, iv; /* vanitygap settings */ unsigned int cx, cy, cw, ch; /* client geometry */ unsigned int uw = 0, uh = 0, uc = 0; /* utilization trackers */ unsigned int cols, rows = m->mastercount + 1; Client *c; /* count clients */ getgaps(m, &oh, &ov, &ih, &iv, &n); /* nothing to do here */ if (n == 0) return; /* force 2 clients to always split vertically */ if (FORCE_VSPLIT && n == 2) rows = 1; /* never allow empty rows */ if (n < rows) rows = n; /* define first row */ cols = n / rows; uc = cols; cy = m->wy + oh; ch = (m->wh - 2*oh - ih*(rows - 1)) / rows; uh = ch; for (c = nexttiled(m->clients); c; c = nexttiled(c->next), ci++) { if (ci == cols) { uw = 0; ci = 0; ri++; /* next row */ cols = (n - uc) / (rows - ri); uc += cols; cy = m->wy + oh + uh + ih; uh += ch + ih; } cx = m->wx + ov + uw; cw = (m->ww - 2*ov - uw) / (cols - ci); uw += cw + iv; resize(c, cx, cy, cw - (2*c->bw), ch - (2*c->bw), 0); } } #endif #if LAYOUT_TATAMI void tatami(Monitor *m) { unsigned int i, n, nx, ny, nw, nh, mats, tc, tnx, tny, tnw, tnh; Client *c; for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); if(n == 0) return; nx = m->wx; ny = 0; nw = m->ww; nh = m->wh; c = nexttiled(m->clients); if(n != 1) nw = m->ww * m->mfact; ny = m->wy; resize(c, nx, ny, nw - 2 * c->bw, nh - 2 * c->bw, False); c = nexttiled(c->next); nx += nw; nw = m->ww - nw; if(n>1) { tc = n-1; mats = tc/5; nh/=(mats + (tc % 5 > 0)); for(i = 0; c && (i < (tc % 5)); c = nexttiled(c->next)) { tnw=nw; tnx=nx; tnh=nh; tny=ny; switch(tc - (mats*5)) { case 1://fill break; case 2://up and down if((i % 5) == 0) //up tnh/=2; else if((i % 5) == 1) //down { tnh/=2; tny += nh/2; } break; case 3://bottom, up-left and up-right if((i % 5) == 0) //up-left { tnw = nw/2; tnh = (2*nh)/3; } else if((i % 5) == 1)//up-right { tnx += nw/2; tnw = nw/2; tnh = (2*nh)/3; } else if((i % 5) == 2)//bottom { tnh = nh/3; tny += (2*nh)/3; } break; case 4://bottom, left, right and top if((i % 5) == 0) //top { tnh = (nh)/4; } else if((i % 5) == 1)//left { tnw = nw/2; tny += nh/4; tnh = (nh)/2; } else if((i % 5) == 2)//right { tnx += nw/2; tnw = nw/2; tny += nh/4; tnh = (nh)/2; } else if((i % 5) == 3)//bottom { tny += (3*nh)/4; tnh = (nh)/4; } break; } ++i; resize(c, tnx, tny, tnw - 2 * c->bw, tnh - 2 * c->bw, False); } ++mats; for(i = 0; c && (mats>0); c = nexttiled(c->next)) { if((i%5)==0) { --mats; if(((tc % 5) > 0)||(i>=5)) ny+=nh; } tnw=nw; tnx=nx; tnh=nh; tny=ny; switch(i % 5) { case 0: //top-left-vert tnw = (nw)/3; tnh = (nh*2)/3; break; case 1: //top-right-hor tnx += (nw)/3; tnw = (nw*2)/3; tnh = (nh)/3; break; case 2: //center tnx += (nw)/3; tnw = (nw)/3; tny += (nh)/3; tnh = (nh)/3; break; case 3: //bottom-right-vert tnx += (nw*2)/3; tnw = (nw)/3; tny += (nh)/3; tnh = (nh*2)/3; break; case 4: //(oldest) bottom-left-hor tnw = (2*nw)/3; tny += (2*nh)/3; tnh = (nh)/3; break; default: break; } ++i; //i%=5; resize(c, tnx, tny, tnw - 2 * c->bw, tnh - 2 * c->bw, False); } } } #endif #if LAYOUT_CM void centeredmaster(Monitor *m) { unsigned int i, n; int oh, ov, ih, iv; int mx = 0, my = 0, mh = 0, mw = 0; int lx = 0, ly = 0, lw = 0, lh = 0; int rx = 0, ry = 0, rw = 0, rh = 0; float mfacts = 0, lfacts = 0, rfacts = 0; int mtotal = 0, ltotal = 0, rtotal = 0; int mrest = 0, lrest = 0, rrest = 0; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; /* initialize areas */ mx = m->wx + ov; my = m->wy + oh; mh = m->wh - 2*oh - ih * ((!m->mastercount ? n : MIN(n, m->mastercount)) - 1); mw = m->ww - 2*ov; lh = m->wh - 2*oh - ih * (((n - m->mastercount) / 2) - 1); rh = m->wh - 2*oh - ih * (((n - m->mastercount) / 2) - ((n - m->mastercount) % 2 ? 0 : 1)); if (m->mastercount && n > m->mastercount) { /* go mfact box in the center if more than mastercount clients */ if (n - m->mastercount > 1) { /* ||<-S->|<---M--->|<-S->|| */ mw = (m->ww - 2*ov - 2*iv) * m->mfact; lw = (m->ww - mw - 2*ov - 2*iv) / 2; rw = (m->ww - mw - 2*ov - 2*iv) - lw; mx += lw + iv; } else { /* ||<---M--->|<-S->|| */ mw = (mw - iv) * m->mfact; lw = 0; rw = m->ww - mw - iv - 2*ov; } lx = m->wx + ov; ly = m->wy + oh; rx = mx + mw + iv; ry = m->wy + oh; } /* calculate facts */ for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) { if (!m->mastercount || n < m->mastercount) mfacts += c->cfact; else if ((n - m->mastercount) % 2) lfacts += c->cfact; // total factor of left hand stack area else rfacts += c->cfact; // total factor of right hand stack area } for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) if (!m->mastercount || n < m->mastercount) mtotal += mh * (c->cfact / mfacts); else if ((n - m->mastercount) % 2) ltotal += lh * (c->cfact / lfacts); else rtotal += rh * (c->cfact / rfacts); mrest = mh - mtotal; lrest = lh - ltotal; rrest = rh - rtotal; for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { if (!m->mastercount || i < m->mastercount) { /* mastercount clients are stacked vertically, in the center of the screen */ resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0); my += HEIGHT(c) + ih; } else { /* stack clients are stacked vertically */ if ((i - m->mastercount) % 2 ) { resize(c, lx, ly, lw - (2*c->bw), lh * (c->cfact / lfacts) + ((i - 2*m->mastercount) < 2*lrest ? 1 : 0) - (2*c->bw), 0); ly += HEIGHT(c) + ih; } else { resize(c, rx, ry, rw - (2*c->bw), rh * (c->cfact / rfacts) + ((i - 2*m->mastercount) < 2*rrest ? 1 : 0) - (2*c->bw), 0); ry += HEIGHT(c) + ih; } } } } #endif #if LAYOUT_CFM void centeredfloatingmaster(Monitor *m) { unsigned int i, n; float mfacts, sfacts; float mivf = 1.0; // master inner vertical gap factor int oh, ov, ih, iv, mrest, srest; int mx = 0, my = 0, mh = 0, mw = 0; int sx = 0, sy = 0, sh = 0, sw = 0; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; sx = mx = m->wx + ov; sy = my = m->wy + oh; sh = mh = m->wh - 2*oh; mw = m->ww - 2*ov - iv*(n - 1); sw = m->ww - 2*ov - iv*(n - m->mastercount - 1); if (m->mastercount && n > m->mastercount) { mivf = 0.8; /* go mfact box in the center if more than mastercount clients */ if (m->ww > m->wh) { mw = m->ww * m->mfact - iv*mivf*(MIN(n, m->mastercount) - 1); mh = m->wh * 0.9 - 2 * oh; } else { mw = m->ww * 0.9 - iv*mivf*(MIN(n, m->mastercount) - 1); mh = m->wh * m->mfact; } mx = m->wx + (m->ww - mw) / 2; my = m->wy + (m->wh - mh) / 2; sx = m->wx + ov; sy = m->wy + oh; sh = m->wh - 2*oh; } getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest); for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) if (i < m->mastercount) { /* mastercount clients are stacked horizontally, in the center of the screen */ resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0); mx += WIDTH(c) + iv*mivf; } else { /* stack clients are stacked horizontally */ resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - m->mastercount) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0); sx += WIDTH(c) + iv; } } #endif #if LAYOUT_DECK void deck(Monitor *m) { unsigned int i, n; int oh, ov, ih, iv; int mx = 0, my = 0, mh = 0, mw = 0; int sx = 0, sy = 0, sh = 0, sw = 0; float mfacts, sfacts; int mrest, srest; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); if (n == 0) return; sx = mx = m->wx + ov; sy = my = m->wy + oh; sh = mh = m->wh - 2*oh - ih * (MIN(n, m->mastercount) - 1); sw = mw = m->ww - 2*ov; if (m->mastercount && n > m->mastercount) { sw = (mw - iv) * (1 - m->mfact); mw = mw - iv - sw; sx = mx + mw + iv; sh = m->wh - 2*oh; } getfacts(m, mh, sh, &mfacts, &sfacts, &mrest, &srest); if (n - m->mastercount > 0 && deckcount) /* override layout symbol */ snprintf(m->ltsymbol, sizeof m->ltsymbol, deckformat, n - m->mastercount); for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) if (i < m->mastercount) { resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0); my += HEIGHT(c) + ih; } else { resize(c, sx, sy, sw - (2*c->bw), sh - (2*c->bw), 0); } } #endif #if LAYOUT_CUSTOM enum node_type_t { ND_NULL, /* Containers */ ND_MONOCLE, ND_HORIZONTAL_LR, ND_HORIZONTAL_RL, ND_VERTICAL_UD, ND_VERTICAL_DU, ND_VOID, /* Elements */ ND_CLIENT, ND_CLIENT_NUM, ND_CLIENT_NTH, ND_CLIENT_CLASS, ND_CLIENT_FLOAT, ND_CLIENT_EMPTY, ND_REST, }; typedef struct node_t node_t; struct node_t { enum node_type_t type; float weight; int x, y, w, h; int f; unsigned n; unsigned margin; char *s; Client *c; struct node_t *branch; struct node_t *next; }; struct client_ref_t { Client *c; struct client_ref_t *next; }; static node_t *s_layout_scheme; node_t* alloc_node(enum node_type_t type) { node_t *node = (node_t*) malloc(sizeof(node_t)); memset(node, 0, sizeof(node_t)); node->type = type; return node; } node_t* clone_node(node_t *n) { if (n == NULL) return NULL; node_t *node = alloc_node(n->type); node->weight = n->weight; node->x = n->x; node->y = n->y; node->w = n->w; node->h = n->h; node->f = n->f; node->n = n->n; node->margin = n->margin; node->s = n->s; node->c = n->c; node->next = NULL; node->branch = NULL; return node; } int is_nested(node_t *node) { return node->type == ND_HORIZONTAL_LR || node->type == ND_HORIZONTAL_RL || node->type == ND_VERTICAL_UD || node->type == ND_VERTICAL_DU || node->type == ND_MONOCLE; } int is_terminal(char c) { return c == ' ' || c == '\t' || c == '(' || c == ')' || c == '\0'; } void free_node(node_t *node) { for ( node_t *n = node; n != NULL; ) { if (is_nested(n)) free_node(n->branch); node_t *ns = n->next; if (n->s) free(n->s); free(n); n = ns; } } node_t* reverse_node(node_t *node) { node_t *a = node, *b = NULL; if (a != NULL) { b = a->next; a->next = NULL; } while (b != NULL) { node_t *nx = b->next; b->next = a; a = b; b = nx; } return (b ? b : a); } void node_length(node_t *node, unsigned *len, float *weight) { unsigned n = 0; float w = 0.0; for (; node != NULL; node = node->next) { if (!node->f) { n ++; w += (node->weight == 0 ? 1 : node->weight); } } if (len != NULL) *len = n; if (weight != NULL) *weight = w; } struct client_ref_t* copy_clients(Client *clients) { struct client_ref_t head; head.next = NULL; struct client_ref_t *tail = &head; for (Client *c = nexttiled(clients); c != NULL; c = nexttiled(c->next)) { tail->next = (struct client_ref_t*) malloc(sizeof(struct client_ref_t)); tail = tail->next; tail->next = NULL; tail->c = c; } return head.next; } void free_clients(struct client_ref_t *clients) { struct client_ref_t *nxt = NULL; while (clients != NULL) { nxt = clients->next; free(clients); clients = nxt; } } struct s_recur_analyze_ret { node_t *head; node_t *tail; } s_recur_analyze(struct client_ref_t **clients, node_t *node) { struct client_ref_t *c = *clients; unsigned i = 0; /* a single client, just assign a client. */ if (node->type == ND_CLIENT) { struct s_recur_analyze_ret ret; ret.tail = ret.head = clone_node(node); *clients = c->next; ret.head->c = c->c; return ret; } /* An empty slot. */ if (node->type == ND_CLIENT_EMPTY) { struct s_recur_analyze_ret ret; ret.tail = ret.head = clone_node(node); ret.tail->c = NULL; ret.tail->type = ND_CLIENT; return ret; } /* pick 'n' client from the list top. */ if (node->type == ND_CLIENT_NTH) { struct client_ref_t *prev = NULL; struct s_recur_analyze_ret ret; ret.head = ret.tail = NULL; for ( i = 0, c = *clients; i < node->n && c != NULL; i ++, c = c->next) { prev = c; } if (c != NULL) { if (prev == NULL) *clients = c->next; else prev->next = c->next; ret.tail = ret.head = clone_node(node); ret.head->type = ND_CLIENT; ret.head->c = c->c; } return ret; } if (node->type == ND_CLIENT_CLASS) { struct s_recur_analyze_ret ret; return ret; } /* Fixed number of clients. */ if (node->type == ND_CLIENT_NUM) { struct s_recur_analyze_ret ret; node_t head, *p = &head; head.next = NULL; for ( i = 0, c = *clients; i < node->n && c != NULL; i ++, c = c->next ) { p->next = clone_node(node); p = p->next; p->type = ND_CLIENT; p->c = c->c; } *clients = c; ret.head = head.next; ret.tail = p; return ret; } /* All leftover client. */ if (node->type == ND_REST) { struct s_recur_analyze_ret ret; node_t head, *p = &head; head.next = NULL; for (c = *clients; c != NULL; c = c->next) { p->next = clone_node(node); p = p->next; p->type = ND_CLIENT; p->c = c->c; } *clients = c; ret.head = head.next; ret.tail = p; return ret; } if (node->type == ND_NULL) { struct s_recur_analyze_ret ret; ret.head = NULL; ret.tail = NULL; return ret; } /* In case the element is a container */ if (is_nested(node)) { struct s_recur_analyze_ret ret; ret.head = clone_node(node); ret.tail = ret.head; struct s_recur_analyze_ret x; x.head = ret.head; x.tail = x.head; node_t branch, *tail = &branch; branch.next = NULL; node_t *n = NULL; /* For reversed containers the order must be reversed */ if (node->type == ND_HORIZONTAL_RL || node->type == ND_VERTICAL_DU) { n = reverse_node(node->branch); } else { n = node->branch; } for (; *clients != NULL && n != NULL; n = n->next ) { x = s_recur_analyze(clients, n); /* Attach the received tree to the tail of the previous element */ if (x.head != NULL) { tail->next = x.head; tail = x.tail; } } if (node->type == ND_HORIZONTAL_RL || node->type == ND_VERTICAL_DU) { ret.head->branch = reverse_node(branch.next); } else { ret.head->branch = branch.next; } return ret; } struct s_recur_analyze_ret ret; ret.head = NULL; ret.tail = NULL; return ret; } struct frame_t { int x, y, w, h; }; void s_recur_resize(node_t *node, struct frame_t frame) { if (node == NULL) return; if (node->type == ND_CLIENT) { if (node->c != NULL) { if (node->f) resize(node->c, node->x, node->y, node->w, node->h, 0); else resize(node->c, frame.x + node->margin, frame.y + node->margin, frame.w - 2 * node->margin - 2 * node->c->bw, frame.h - 2 * node->margin - 2 * node->c->bw, 0); } return; } if (node->type == ND_VERTICAL_UD || node->type == ND_VERTICAL_DU) { unsigned len = 0; float wgt = 0.0; int delta = 0; float avg_wgt = 1; frame.x += node->margin; frame.y += node->margin; frame.w -= 2 * node->margin; frame.h -= 2 * node->margin; node_length(node->branch, &len, &wgt); if (len != 0) { delta = frame.h / len; avg_wgt = wgt / len; } for (node_t *n = node->branch; n != NULL; n = n->next) { if (!n->f) { frame.h = (n->weight == 0 ? 1 : n->weight) / avg_wgt * delta; s_recur_resize(n, frame); frame.y += frame.h; } else { s_recur_resize(n, frame); } } return; } if (node->type == ND_HORIZONTAL_RL || node->type == ND_HORIZONTAL_LR) { unsigned len = 0; float wgt = 0.0; int delta = 0; float avg_wgt = 1; frame.x += node->margin; frame.y += node->margin; frame.w -= 2 * node->margin; frame.h -= 2 * node->margin; node_length(node->branch, &len, &wgt); if (len != 0) { delta = frame.w / len; avg_wgt = wgt / len; } for (node_t *n = node->branch; n != NULL; n = n->next) { if (!n->f) { frame.w = (n->weight == 0 ? 1 : n->weight) / avg_wgt * delta; s_recur_resize(n, frame); frame.x += frame.w; } else { s_recur_resize(n, frame); } } return; } if (node->type == ND_MONOCLE) { frame.x += node->margin; frame.y += node->margin; frame.w -= 2 * node->margin; frame.h -= 2 * node->margin; for (node_t *n = node->branch; n != NULL; n = n->next) s_recur_resize(n, frame); } } /* Main layout function. */ void custom(Monitor *m) { /* Need to clone the client stack, as we might need to pull items from it. */ struct client_ref_t *clients = copy_clients(m->clients), *clients_root = clients; if (s_layout_scheme == NULL) return; struct s_recur_analyze_ret ret = s_recur_analyze(&clients, s_layout_scheme); struct frame_t frame; frame.x = m->wx; frame.y = m->wy; frame.w = m->ww; frame.h = m->wh; s_recur_resize(ret.head, frame); /* Free the resources we allocated. */ free_clients(clients_root); free_node(ret.head); } /* tokenize string */ typedef struct string_token_t string_token_t; struct string_token_t { char token[32]; struct string_token_t *next; }; string_token_t* parse_string(char *str, unsigned *i) { unsigned char escape = 0; unsigned j = 0; string_token_t *ret = (string_token_t*) malloc(sizeof(string_token_t)); ret->next = NULL; while (str[*i] != '\0' && j < sizeof(ret->token) - 1) { if (str[*i] == '\\' && !escape) { escape = 1; (*i) ++; continue; } if (str[*i] == '"' && !escape) { break; } ret->token[j++] = str[(*i)++]; escape = 0; } ret->token[j] = '\0'; if (str[*i] == '\0') (*i) --; return ret; } struct string_token_t* tokenize_string(char *str) { struct string_token_t head; struct string_token_t *node = &head; head.next = NULL; unsigned word_start = UINT_MAX; unsigned len = 0; for (unsigned i = 0;; i ++) { switch (str[i]) { /* end of line */ case '\0': if (word_start != UINT_MAX) { node->next = (struct string_token_t*) malloc(sizeof(struct string_token_t)); node = node->next; node->next = NULL; len = MIN(i - word_start, sizeof(node->token) - 1); strncpy(node->token, &str[word_start], len); node->token[len] = '\0'; } return head.next; /* comment */ case ';': return head.next; /* beginning of a string */ case '"': node->next = parse_string(str, &i); if (node->next) node = node->next; case ' ': case '\t': case '(': case ')': if (word_start != UINT_MAX) { node->next = (struct string_token_t*) malloc(sizeof(struct string_token_t)); node = node->next; node->next = NULL; len = MIN(i - word_start, sizeof(node->token) - 1); strncpy(node->token, &str[word_start], len); node->token[len] = '\0'; word_start = UINT_MAX; } if (str[i] == '(' || str[i] == ')') { node->next = (struct string_token_t*) malloc(sizeof(struct string_token_t)); node = node->next; node->next = NULL; node->token[0] = str[i]; node->token[1] = '\0'; } break; // A piece of a word default: if (word_start == UINT_MAX) word_start = i; } } return NULL; } /* Parse s-expression to node_t structure */ node_t* parse_sexp(string_token_t **token) { node_t *head = NULL; node_t branch, *p = &branch; branch.next = NULL; string_token_t *t = *token; while (t != NULL) { if (strcmp(t->token, ")") == 0) { t = t->next; break; } if (strcmp(t->token, "(") == 0) { t = t->next; *token = t; if (head == NULL) { head = parse_sexp(token); } else { p->next = parse_sexp(token); if (p->next) p = p->next; } t = *token; continue; } /* client slots * single client */ if (strcmp(t->token, "c") == 0 || strcmp(t->token, "client") == 0) { if (head == NULL) { head = alloc_node(ND_CLIENT); } else { p->next = alloc_node(ND_CLIENT); p = p->next; } t = t->next; continue; } /* Empty viewport */ if (strcmp(t->token, "e") == 0 || strcmp(t->token, "empty") == 0) { if (head == NULL) { head = alloc_node(ND_CLIENT_EMPTY); } else { p->next = alloc_node(ND_CLIENT_EMPTY); p = p->next; } t = t->next; continue; } /* the rest of the clients */ if (strcmp(t->token, "...") == 0 || strcmp(t->token, "rest") == 0) { if (head == NULL) { head = alloc_node(ND_REST); } else { p->next = alloc_node(ND_REST); p = p->next; } t = t->next; continue; } /* choose the client by class */ if (strcmp(t->token, "class") == 0) { if (head == NULL) { head = alloc_node(ND_CLIENT_CLASS); if (t->next) { t = t->next; head->s = strdup(t->token); } } else { p->next = alloc_node(ND_CLIENT_CLASS); p = p->next; if (t->next) { t = t->next; p->s = strdup(t->token); } } t = t->next; continue; } /* 'n' client */ unsigned long n = 0; char *endp = NULL; n = strtoul(t->token, &endp, 10); if (is_terminal(*endp)) { if (head == NULL) { head = alloc_node(ND_CLIENT_NTH); head->n = n; } else { p->next = alloc_node(ND_CLIENT_NTH); p->next->n = n; p = p->next; } t = t->next; continue; } /* max number of clients */ if (strcmp(t->token, "max") == 0) { if (head == NULL) { head = alloc_node(ND_CLIENT_NUM); t = t->next; if (t != NULL) { head->n = (unsigned) atoi(t->token); t = t->next; } } continue; } /* parameters * weight */ if ((strcmp(t->token, "w:") == 0 || strcmp(t->token, ":w") == 0 || strcmp(t->token, "weight:") == 0 || strcmp(t->token, ":weight") == 0) && head != NULL) { t = t->next; if (t != NULL) { head->weight = (float) atof(t->token); t = t->next; } continue; } /* margin */ if (((strcmp(t->token, "m:") == 0) || strcmp(t->token, ":m") == 0 || strcmp(t->token, "margin:") == 0 || strcmp(t->token, ":margin") == 0) && head != NULL) { t = t->next; if (t != NULL) { head->margin = (unsigned) atoi(t->token); t = t->next; } continue; } /* floating geometry */ if ((strcmp(t->token, "f:") == 0 || strcmp(t->token, ":f") == 0 || strcmp(t->token, "float:") == 0 || strcmp(t->token, ":float") == 0) && head != NULL) { head->f = 1; t = t->next; if (t != NULL) { head->x = atoi(t->token); t = t->next; } if (t != NULL) { head->y = atoi(t->token); t = t->next; } if (t != NULL) { head->w = atoi(t->token); t = t->next; } if (t != NULL) { head->h = atoi(t->token); t = t->next; } continue; } /* containers */ if ((strcmp(t->token, "h") == 0 || strcmp(t->token, "horizontal") == 0) && head == NULL) head = alloc_node(ND_HORIZONTAL_LR); if ((strcmp(t->token, "hr") == 0 || strcmp(t->token, "h-reversed") == 0) && head == NULL) head = alloc_node(ND_HORIZONTAL_RL); if ((strcmp(t->token, "v") == 0 || strcmp(t->token, "vertical") == 0) && head == NULL) head = alloc_node(ND_VERTICAL_UD); if ((strcmp(t->token, "vr") == 0 || strcmp(t->token, "v-reversed") == 0) && head == NULL) head = alloc_node(ND_VERTICAL_UD); if ((strcmp(t->token, "m") == 0 || strcmp(t->token, "monocle") == 0) && head == NULL) head = alloc_node(ND_MONOCLE); t = t->next; } if (head) head->branch = branch.next; *token = t; return head; } void set_s_layout(const Arg *arg) { FILE *pp, *hf; char pathbuf[1024]; char *home = getenv("HOME"); if (home != NULL) { snprintf(pathbuf, 1023, "%s/" CUSTOM_HISTFILE, home); pathbuf[1023] = '\0'; /* make sure the history file exists */ hf = fopen(CUSTOM_HISTFILE, "a"); fclose(hf); system("sort " CUSTOM_HISTFILE " | uniq > " CUSTOM_HISTFILE "~"); system("mv " CUSTOM_HISTFILE "~ " CUSTOM_HISTFILE); pp = popen(RUN_CUSTOM_LAYOUT CUSTOM_HISTFILE, "r"); } else { pp = popen(RUN_CUSTOM_LAYOUT, "r"); } if (!pp) return; char buf[2048]; buf[2048] = '\0'; fgets(buf, 1024, pp); fclose(pp); if (buf[0] == '\0') return; hf = fopen(CUSTOM_HISTFILE, "a"); fprintf(hf, "%s", buf); fclose(hf); if (s_layout_scheme != NULL) { free_node(s_layout_scheme); s_layout_scheme = NULL; } struct string_token_t *token_root = tokenize_string(buf), *token = token_root; s_layout_scheme = parse_sexp(&token); /* Free the token list */ while (token_root != NULL) { token = token_root->next; free(token_root); token_root = token; } } #endif