dwm

[fork] dynamic window manager
Log | Files | Refs | README | LICENSE

commit 4a8db016ab56d3c0527334b1b1d1224c9e147eb4
parent f081e123764bfcb2cd88e102c530357917160807
Author: hhvn <dev@hhvn.uk>
Date:   Fri, 15 Apr 2022 18:48:54 +0100

Remove unused layouts + change layout handling

Diffstat:
Mconfig.h | 23++++-------------------
Mdwm.c | 247++++++++++++++++++++-----------------------------------------------------------
2 files changed, 65 insertions(+), 205 deletions(-)

diff --git a/config.h b/config.h @@ -39,15 +39,8 @@ static const float mfact = 0.55; /* factor of master area size [0.05..0.95] static const int nmaster = 1; /* number of clients in master area */ static const int resizehints = 0; /* 1 means respect size hints in tiled resizals */ -static const Layout layouts[] = { - /* symbol arrange function */ - { "|=", tile }, /* first entry is default */ - { ".:", NULL }, /* no layout function means floating behavior */ - { "M", monocle }, - { "@", spiral }, - { "#=", dwindle }, - { "=#=", centeredmaster }, -}; +static Layout layout = { tileicon, tile }; +static Layout altlayout = { monocleicon, monocle }; /* key definitions */ #define MODKEY Mod4Mask @@ -57,15 +50,13 @@ static const Layout layouts[] = { { MODKEY|ControlMask, KEY, toggleview, {.ui = 1 << TAG} }, \ { MODKEY|ShiftMask, KEY, tag, {.ui = 1 << TAG} }, \ { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui = 1 << TAG} }, -#define LAYOUT(KEY, LAYOUT) \ - { MODKEY|AltMask, KEY, setlayout, {.v = &layouts[LAYOUT]} }, static Key keys[] = { /* modifier key function argument */ { MODKEY|ShiftMask, XK_Return, zoom, {0} }, { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, { MODKEY, XK_Tab, view, {0} }, - { MODKEY, XK_f, togglefullscr, {0} }, + { MODKEY, XK_f, togglefullscr, {0} }, { MODKEY, XK_h, setmfact, {.f = -0.05} }, { MODKEY, XK_i, incnmaster, {.i = +1 } }, { MODKEY, XK_j, focusstack, {.i = +1 } }, @@ -81,13 +72,7 @@ static Key keys[] = { { MODKEY, XK_period, focusmon, {.i = +1 } }, { MODKEY|ShiftMask, XK_comma, tagmon, {.i = -1 } }, { MODKEY|ShiftMask, XK_period, tagmon, {.i = +1 } }, - /* Layout */ - LAYOUT( XK_1, 0) - LAYOUT( XK_2, 1) - LAYOUT( XK_3, 3) - LAYOUT( XK_4, 4) - LAYOUT( XK_5, 5) - LAYOUT( XK_0, 2) + { MODKEY, XK_slash, swaplt, {0} }, /* Tags */ TAGKEYS( XK_1, 0) TAGKEYS( XK_2, 1) diff --git a/dwm.c b/dwm.c @@ -113,13 +113,12 @@ typedef struct { } Key; typedef struct { - const char *symbol; + int (*drawicon)(int); void (*arrange)(Monitor *); } Layout; typedef struct Pertag Pertag; struct Monitor { - char ltsymbol[16]; float mfact; int nmaster; int num; @@ -130,7 +129,6 @@ struct Monitor { int wx, wy, ww, wh; /* window area */ int gappx; /* gaps between windows */ unsigned int seltags; - unsigned int sellt; unsigned int tagset[2]; int showbar; int topbar; @@ -139,7 +137,7 @@ struct Monitor { Client *stack; Monitor *next; Window barwin; - const Layout *lt[2]; + Layout *lt; Pertag *pertag; }; @@ -192,7 +190,6 @@ static void killclient(const Arg *arg); static void manage(Window w, XWindowAttributes *wa); static void mappingnotify(XEvent *e); static void maprequest(XEvent *e); -static void monocle(Monitor *m); static void motionnotify(XEvent *e); static Client *nexttagged(Client *c); static Client *nexttiled(Client *c); @@ -211,7 +208,7 @@ static void setclientstate(Client *c, long state); static void setfocus(Client *c); static void setfullscreen(Client *c, int fullscreen); //static void setgaps(const Arg *arg); -static void setlayout(const Arg *arg); +static void swaplt(const Arg *arg); static void setmfact(const Arg *arg); static void setup(void); static void seturgent(Client *c, int urg); @@ -219,7 +216,6 @@ static void showhide(Client *c); static void sigchld(int unused); static void tag(const Arg *arg); static void tagmon(const Arg *arg); -static void tile(Monitor *); //static void togglebar(const Arg *arg); static void togglefloating(const Arg *arg); static void togglefullscr(const Arg *arg); @@ -245,10 +241,10 @@ static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); -static void centeredmaster(Monitor *m); -static void spiral(Monitor *mon); -static void dwindle(Monitor *mon); -static void fibonacci(Monitor *mon, int s); +static void monocle(Monitor *m); +static int monocleicon(int x); +static void tile(Monitor *); +static int tileicon(int x); /* variables */ static const char broken[] = "broken"; @@ -290,8 +286,7 @@ struct Pertag { unsigned int curtag, prevtag; /* current and previous tag */ int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */ float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */ - unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */ - const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */ + Layout *ltidxs[LENGTH(tags) + 1]; /* selected layout for each tag */ }; /* compile-time check if all tags fit into an unsigned int bit array. */ @@ -369,7 +364,7 @@ applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) *h = bh; if (*w < bh) *w = bh; - if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) { + if (resizehints || c->isfloating || !c->mon->lt->arrange) { /* see last two sentences in ICCCM 4.1.2.3 */ baseismin = c->basew == c->minw && c->baseh == c->minh; if (!baseismin) { /* temporarily remove base dimensions */ @@ -420,9 +415,8 @@ arrange(Monitor *m) void arrangemon(Monitor *m) { - strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol); - if (m->lt[m->sellt]->arrange) - m->lt[m->sellt]->arrange(m); + if (m->lt->arrange) + m->lt->arrange(m); } void @@ -465,12 +459,12 @@ void cleanup(void) { Arg a = {.ui = ~0}; - Layout foo = { "", NULL }; + Layout foo = { NULL, NULL }; Monitor *m; size_t i; view(&a); - selmon->lt[selmon->sellt] = &foo; + selmon->lt = &foo; for (m = mons; m; m = m->next) while (m->stack) unmanage(m->stack, 0); @@ -582,7 +576,7 @@ configurerequest(XEvent *e) if ((c = wintoclient(ev->window))) { if (ev->value_mask & CWBorderWidth) c->bw = ev->border_width; - else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) { + else if (c->isfloating || !selmon->lt->arrange) { m = c->mon; if (ev->value_mask & CWX) { c->oldx = c->x; @@ -636,9 +630,7 @@ createmon(void) m->showbar = showbar; m->topbar = topbar; m->gappx = gappx; - m->lt[0] = &layouts[0]; - m->lt[1] = &layouts[1 % LENGTH(layouts)]; - strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); + m->lt = &layout; m->pertag = ecalloc(1, sizeof(Pertag)); m->pertag->curtag = m->pertag->prevtag = 1; @@ -646,9 +638,7 @@ createmon(void) m->pertag->nmasters[i] = m->nmaster; m->pertag->mfacts[i] = m->mfact; - m->pertag->ltidxs[i][0] = m->lt[0]; - m->pertag->ltidxs[i][1] = m->lt[1]; - m->pertag->sellts[i] = m->sellt; + m->pertag->ltidxs[i] = m->lt; } return m; @@ -817,7 +807,6 @@ drawbar(Monitor *m) int boxs = drw->fonts->h / 9; int boxw = drw->fonts->h / 6 + 2; unsigned int i, occ = 0, urg = 0; - char layout[16]; Client *c; XGetWindowAttributes(dpy, m->barwin, &attr); @@ -849,10 +838,8 @@ drawbar(Monitor *m) x += w; } - snprintf(layout, sizeof(layout), "%s%d", m->ltsymbol, n); - w = blw = TEXTW(layout); drw_setscheme(drw, scheme[SchemeNorm]); - x = drw_text(drw, x, 0, w, bh, lrpad / 2, layout, 0); + x = m->lt->drawicon(x); if ((w = m->ww - sw - x) > bh) { if (n > 0) { @@ -1252,6 +1239,24 @@ monocle(Monitor *m) resize(c, m->wx + m->gappx, m->wy + m->gappx, (m->ww - 2 * c->bw) - 2*m->gappx, (m->wh - 2 * c->bw) - 2*m->gappx, 0); } +int +monocleicon(int x) { + int pad = 3; + int h = bh - 3 *pad; + int w = h * 1.5; + int tw = lrpad + h * 2 + pad; + + drw_rect(drw, x, 0, tw, bh, 1, 1); + drw_setscheme(drw, scheme[SchemeInact]); + drw_rect(drw, x + lrpad/2, pad, w, h, 1, 0); + drw_setscheme(drw, scheme[SchemeNorm]); + drw_rect(drw, x + lrpad/2 + pad - 1, pad * 2 - 1, w, h, 1, 1); + drw_setscheme(drw, scheme[SchemeSel]); + drw_rect(drw, x + lrpad/2 + pad, pad * 2, w, h, 1, 0); + drw_setscheme(drw, scheme[SchemeNorm]); + return x + tw; +} + void motionnotify(XEvent *e) { @@ -1383,9 +1388,9 @@ restack(Monitor *m) XWindowChanges wc; drawbar(m); if (!m->sel) return; - if (m->sel->isfloating || !m->lt[m->sellt]->arrange) + if (m->sel->isfloating || !m->lt->arrange) XRaiseWindow(dpy, m->sel->win); - if (m->lt[m->sellt]->arrange) { + if (m->lt->arrange) { wc.stack_mode = Below; wc.sibling = m->barwin; for (c = m->stack; c; c = c->snext) @@ -1531,26 +1536,10 @@ setfullscreen(Client *c, int fullscreen) } } -/* -void -setgaps(const Arg *arg) -{ - if ((arg->i == 0) || (selmon->gappx + arg->i < 0)) - selmon->gappx = 0; - else - selmon->gappx += arg->i; - arrange(selmon); -} -*/ - void -setlayout(const Arg *arg) +swaplt(const Arg *arg) { - if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) - selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1; - if (arg && arg->v) - selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v; - strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); + selmon->lt = selmon->pertag->ltidxs[selmon->pertag->curtag] = selmon->lt == &layout ? &altlayout : &layout; if (selmon->sel) arrange(selmon); else @@ -1563,7 +1552,7 @@ setmfact(const Arg *arg) { float f; - if (!arg || !selmon->lt[selmon->sellt]->arrange) + if (!arg || !selmon->lt->arrange) return; f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; if (f < 0.05 || f > 0.95) @@ -1666,7 +1655,7 @@ showhide(Client *c) if (ISVISIBLE(c)) { /* show clients top down */ XMoveWindow(dpy, c->win, c->x, c->y); - if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen) + if ((!c->mon->lt->arrange || c->isfloating) && !c->isfullscreen) resize(c, c->x, c->y, c->w, c->h, 0); showhide(c->snext); } else { @@ -1730,6 +1719,23 @@ tile(Monitor *m) } } +int +tileicon(int x) { + int opad = 3, ipad = 1; + int w = (bh - 2 * opad) / 2; + int tw = lrpad + 2 * w + ipad; + + drw_rect(drw, x, 0, tw, bh, 1, 1); + drw_setscheme(drw, scheme[SchemeInact]); + drw_rect(drw, x + lrpad/2, opad, w, 2 * w + ipad, 1, 0); + drw_rect(drw, x + lrpad/2 + w + ipad, opad, w, w, 1, 0); + drw_setscheme(drw, scheme[SchemeSel]); + drw_rect(drw, x + lrpad/2 + w + ipad, opad + w + ipad, w, w, 1, 0); + drw_setscheme(drw, scheme[SchemeNorm]); + + return x + tw; +} + void togglefloating(const Arg *arg) { @@ -1790,9 +1796,7 @@ toggleview(const Arg *arg) /* apply settings for this view */ selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; - selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; - selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; - selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; + selmon->lt = selmon->pertag->ltidxs[selmon->pertag->curtag]; focus(NULL); arrange(selmon); @@ -2112,9 +2116,7 @@ view(const Arg *arg) selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; - selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; - selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; - selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; + selmon->lt = selmon->pertag->ltidxs[selmon->pertag->curtag]; focus(NULL); arrange(selmon); @@ -2191,7 +2193,7 @@ zoom(const Arg *arg) { Client *c = selmon->sel; - if (!selmon->lt[selmon->sellt]->arrange + if (!selmon->lt->arrange || (selmon->sel && selmon->sel->isfloating)) return; if (c == nexttiled(selmon->clients)) @@ -2223,130 +2225,3 @@ main(int argc, char *argv[]) XCloseDisplay(dpy); return exitval; } - -static void -fibonacci(Monitor *mon, int s) { - unsigned int i, n, nx, ny, nw, nh; - Client *c; - - for(n = 0, c = nexttiled(mon->clients); c; c = nexttiled(c->next), n++); - if(n == 0) - return; - - nx = mon->wx; - ny = 0; - nw = mon->ww; - nh = mon->wh - gappx; - - for(i = 0, c = nexttiled(mon->clients); c; c = nexttiled(c->next)) { - if((i % 2 && nh / 2 > 2 * c->bw) - || (!(i % 2) && nw / 2 > 2 * c->bw)) { - if(i < n - 1) { - if(i % 2) - nh /= 2; - else - nw /= 2; - if((i % 4) == 2 && !s) - nx += nw; - else if((i % 4) == 3 && !s) - ny += nh; - } - if((i % 4) == 0) { - if(s) - ny += nh; - else - ny -= nh; - } - else if((i % 4) == 1) - nx += nw; - else if((i % 4) == 2) - ny += nh; - else if((i % 4) == 3) { - if(s) - nx += nw; - else - nx -= nw; - } - if(i == 0) - { - if(n != 1) - nw = mon->ww * mon->mfact; - ny = mon->wy; - } - else if(i == 1) - nw = mon->ww - nw - gappx; - i++; - } - resize(c, nx + mon->gappx, ny + mon->gappx, nw - (2 * c->bw) - mon->gappx, nh - 2*c->bw - mon->gappx, False); - } -} - -static void -dwindle(Monitor *mon) { - fibonacci(mon, 1); -} - -static void -spiral(Monitor *mon) { - fibonacci(mon, 0); -} - -void -centeredmaster(Monitor *m) -{ - unsigned int i, n, h, mw, mx, my, oty, ety, tw, mult; - Client *c; - - /* count number of clients in the selected monitor */ - for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); - if (n == 0) - return; - - /* initialize areas */ - mw = m->ww; - mx = 0; - my = 0; - tw = mw; - - if (n > m->nmaster) { - /* go mfact box in the center if more than nmaster clients */ - mw = m->nmaster ? m->ww * m->mfact : 0; - tw = m->ww - mw; - - if (n - m->nmaster > 1) { - /* only one client */ - mx = (m->ww - mw) / 2; - tw = (m->ww - mw) / 2; - } - } - - oty = 0; - ety = 0; - for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { - if (i < m->nmaster) { - /* nmaster clients are stacked vertically, in the center - * of the screen */ - mult = (i < m->nmaster - 1) ? 1 : 0; - - h = (m->wh - my) / (MIN(n, m->nmaster) - i); - resize(c, m->wx + mx + gappx, m->wy + my + gappx, mw - 2*c->bw - 2*gappx, - h - 2*c->bw - (2+mult)*gappx, 0); - my += HEIGHT(c) + mult*gappx; - } else { - /* stack clients are stacked vertically */ - mult = (i < n - 2) ? 1 : 0; - - if ((i - m->nmaster) % 2 ) { - h = (m->wh - ety) / ( (1 + n - i) / 2); - resize(c, m->wx + gappx, m->wy + ety + gappx, tw - 2*c->bw - gappx, - h - 2*c->bw - (2+mult)*gappx, 0); - ety += HEIGHT(c) + mult*gappx; - } else { - h = (m->wh - oty) / ((1 + n - i) / 2); - resize(c, m->wx + mx + mw, m->wy + oty + gappx, - tw - 2*c->bw - gappx, h - 2*c->bw - (2+mult)*gappx, 0); - oty += HEIGHT(c) + mult*gappx; - } - } - } -}