diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-01 17:21:45 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-01 17:21:45 -0500 |
| commit | 58e18d86a798b4487375f19bcfcdcf9283a56f6e (patch) | |
| tree | 6c0e2100559d2e79e4ab032da1993df64800bf2d | |
| parent | 18a8ecffd287035362338ecf27dc99a462593bd4 (diff) | |
| download | dwm-58e18d86a798b4487375f19bcfcdcf9283a56f6e.tar.gz dwm-58e18d86a798b4487375f19bcfcdcf9283a56f6e.tar.xz | |
Add a new centered layout to dwm.
Allow the scaling factor to be adjusted on the fly.
| -rw-r--r-- | config.h | 11 | ||||
| -rw-r--r-- | dwm.c | 291 | ||||
| -rw-r--r-- | layouts.c | 226 | ||||
| -rw-r--r-- | util.c | 52 | ||||
| -rw-r--r-- | util.h | 2 |
5 files changed, 351 insertions, 231 deletions
@@ -53,11 +53,10 @@ static const Rule rules[] = { /* layout(s) */ static const float mfact = 0.55; /* factor of master area size [0.05..0.95] */ +static const float cfact = 0.5; /* factor of center area size [0.05..0.95] */ static const int nmaster = 1; /* number of clients in master area */ static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */ -#include "layouts.c" - static const Layout layouts[] = { /* symbol arrange function */ { "[]=", tile }, /* first entry is default */ @@ -68,7 +67,7 @@ static const Layout layouts[] = { { "HHH", grid }, { "[@]", spiral }, { "[\\]", dwindle }, - { "C", layoutCenter }, + { "[C]", layoutCenter }, }; /* key definitions */ @@ -86,6 +85,8 @@ static const Layout layouts[] = { #define DMENU_FLAGS "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4 + + /* commands */ static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ static const char *dmenucmd[] = { "dmenu_run", DMENU_FLAGS, NULL }; @@ -129,6 +130,8 @@ static Key keys[] = { { MODKEY|ShiftMask, XK_j, incnmaster, {.i = -1 } }, { MODKEY, XK_h, setmfact, {.f = -0.05} }, { MODKEY, XK_l, setmfact, {.f = +0.05} }, + { MODKEY, XK_equal, setcfact, {.f = +0.05} }, + { MODKEY, XK_minus, setcfact, {.f = -0.05} }, { MODKEY|ShiftMask, XK_f, zoom, {0} }, { MODKEY, XK_Tab, view, {0} }, { MODKEY|ShiftMask, XK_q, killclient, {0} }, @@ -142,7 +145,7 @@ static Key keys[] = { { MODKEY, XK_g, setlayout, {.v = &layouts[5]} }, { MODKEY, XK_v, setlayout, {.v = &layouts[6]} }, { MODKEY|ShiftMask, XK_v, setlayout, {.v = &layouts[7]} }, - { MODKEY|Mod1Mask, XK_v, setlayout, {.v = &layouts[8]} }, + { MODKEY, XK_c, setlayout, {.v = &layouts[8]} }, { MODKEY, XK_space, setlayout, {0} }, { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, { MODKEY, XK_0, view, {.ui = ~0 } }, @@ -43,7 +43,6 @@ #include "drw.h" #include "util.h" -#include "util2.c" /* macros */ #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) @@ -131,6 +130,7 @@ struct Monitor { Monitor *next; Window barwin; const Layout *lt[2]; + float cFact; }; typedef struct { @@ -203,6 +203,7 @@ static void setfocus(Client *c); static void setfullscreen(Client *c, int fullscreen); static void setlayout(const Arg *arg); static void setmfact(const Arg *arg); +static void setcfact(const Arg *arg); static void setup(void); static void seturgent(Client *c, int urg); static void showhide(Client *c); @@ -236,8 +237,13 @@ 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 grid(Monitor *m); +static void fibonacci(Monitor *mon, int s); +static void dwindle(Monitor *m); +static void spiral(Monitor *m); static void centeredmaster(Monitor *m); static void centeredfloatingmaster(Monitor *m); +static void layoutCenter(Monitor *m); /* variables */ static const char broken[] = "broken"; @@ -645,6 +651,7 @@ createmon(void) m->topbar = topbar; m->lt[0] = &layouts[0]; m->lt[1] = &layouts[1 % LENGTH(layouts)]; + m->cFact = cfact; strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); return m; } @@ -1531,6 +1538,21 @@ setmfact(const Arg *arg) arrange(selmon); } +/* arg > 1.0 will set mfact absolutely */ +void +setcfact(const Arg *arg) +{ + float f; + + if (!arg || !selmon->lt[selmon->sellt]->arrange) + return; + f = arg->f < 1.0 ? arg->f + selmon->cFact : arg->f - 1.0; + if (f < 0.05 || f > 0.95) + return; + selmon->cFact = f; + arrange(selmon); +} + void setup(void) { @@ -2186,3 +2208,270 @@ togglefullscr(const Arg *arg) if(selmon->sel) setfullscreen(selmon->sel, !selmon->sel->isfullscreen); } + + + +void +grid(Monitor *m) { + unsigned int i, n, cx, cy, cw, ch, aw, ah, cols, rows; + Client *c; + + for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) + 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 / (rows ? rows : 1); + cw = m->ww / (cols ? cols : 1); + for(i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) { + cx = m->wx + (i / rows) * cw; + cy = m->wy + (i % rows) * ch; + /* adjust height/width of last row/column's windows */ + ah = ((i + 1) % rows == 0) ? m->wh - ch * rows : 0; + aw = (i >= rows * (cols - 1)) ? m->ww - cw * cols : 0; + resize(c, cx, cy, cw - 2 * c->bw + aw, ch - 2 * c->bw + ah, False); + i++; + } +} + +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; + + 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; + i++; + } + resize(c, nx, ny, nw - 2 * c->bw, nh - 2 * c->bw, False); + } +} + +void +dwindle(Monitor *mon) { + fibonacci(mon, 1); +} + +void +spiral(Monitor *mon) { + fibonacci(mon, 0); +} + +void +centeredmaster(Monitor *m) +{ + unsigned int i, n, h, mw, mx, my, oty, ety, tw; + 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 */ + h = (m->wh - my) / (MIN(n, m->nmaster) - i); + resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw), + h - (2*c->bw), 0); + my += HEIGHT(c); + } else { + /* stack clients are stacked vertically */ + if ((i - m->nmaster) % 2 ) { + h = (m->wh - ety) / ( (1 + n - i) / 2); + resize(c, m->wx, m->wy + ety, tw - (2*c->bw), + h - (2*c->bw), 0); + ety += HEIGHT(c); + } else { + h = (m->wh - oty) / ((1 + n - i) / 2); + resize(c, m->wx + mx + mw, m->wy + oty, + tw - (2*c->bw), h - (2*c->bw), 0); + oty += HEIGHT(c); + } + } +} + +void +centeredfloatingmaster(Monitor *m) +{ + unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx; + 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 nmaster area */ + if (n > m->nmaster) { + /* go mfact box in the center if more than nmaster clients */ + if (m->ww > m->wh) { + mw = m->nmaster ? m->ww * m->mfact : 0; + mh = m->nmaster ? m->wh * 0.9 : 0; + } else { + mh = m->nmaster ? m->wh * m->mfact : 0; + mw = m->nmaster ? m->ww * 0.9 : 0; + } + mx = mxo = (m->ww - mw) / 2; + my = myo = (m->wh - mh) / 2; + } else { + /* go fullscreen if all clients are in the master area */ + mh = m->wh; + mw = m->ww; + mx = mxo = 0; + my = myo = 0; + } + + for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) + if (i < m->nmaster) { + /* nmaster clients are stacked horizontally, in the center + * of the screen */ + w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i); + resize(c, m->wx + mx, m->wy + my, w - (2*c->bw), + mh - (2*c->bw), 0); + mx += WIDTH(c); + } else { + /* stack clients are stacked horizontally */ + w = (m->ww - tx) / (n - i); + resize(c, m->wx + tx, m->wy, w - (2*c->bw), + m->wh - (2*c->bw), 0); + tx += WIDTH(c); + } +} + +void +printArea(int x, int y, int w, int h) +{ + printf("Pos: %dx%d WxH: %dx%d\n", + x, y, w, h); +} + +void +printResizeInfo(Client *c, int x, int y, int w, int h, int throwaway) +{ + printf("Client: '%s' Pos: %dx%d WxH: %dx%d\n", + c->name, x, y, w, h); +} + +/* Simply a copy of the default layout for the moment.... */ +void +layoutCenterFact(Monitor *m, double fact) +{ + unsigned int i, n, h, mw, my, ty; + Client *c; + + + // double fact = 0.9; + unsigned int lx, ly, lw, lh; + + lx = (m->ww - (m->ww * fact))/2; + ly = (m->wh - (m->wh * fact))/2; + lw = m->ww * fact; + lh = m->wh * fact; + + printf("Screen size: "); printArea(m->mx, m->my, m->mw, m->mh); + printf("Window area: "); printArea(m->wx, m->wy, m->ww, m->wh); + printf("Local area: "); printArea(lx, ly, lw, lh); + + for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); + if (n == 0) + return; + + printf("Number of clients: %d\n", n); + + if (n > m->nmaster) + mw = m->nmaster ? lw * m->mfact : 0; + else + mw = lw; + for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) + if (i < m->nmaster) { + h = (lh - my) / (MIN(n, m->nmaster) - i); + resize(c, lx, ly + my, mw - (2*c->bw), h - (2*c->bw), 0); + printResizeInfo(c, lx, ly + my, mw - (2*c->bw), h - (2*c->bw), 0); + if (my + HEIGHT(c) < lh) + my += HEIGHT(c); + } else { + h = (lh - ty) / (n - i); + resize(c, lx + mw, ly + ty, lw - mw - (2*c->bw), h - (2*c->bw), 0); + printResizeInfo(c, lx + mw, ly + ty, lw - mw - (2*c->bw), h - (2*c->bw), 0); + if (ty + HEIGHT(c) < lh) + ty += HEIGHT(c); + } +} + +void +layoutCenter(Monitor *m) +{ + layoutCenterFact(m, m->cFact); +} + diff --git a/layouts.c b/layouts.c deleted file mode 100644 index 3e1d0e2..0000000 --- a/layouts.c +++ /dev/null @@ -1,226 +0,0 @@ -void -grid(Monitor *m) { - unsigned int i, n, cx, cy, cw, ch, aw, ah, cols, rows; - Client *c; - - for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) - 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 / (rows ? rows : 1); - cw = m->ww / (cols ? cols : 1); - for(i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) { - cx = m->wx + (i / rows) * cw; - cy = m->wy + (i % rows) * ch; - /* adjust height/width of last row/column's windows */ - ah = ((i + 1) % rows == 0) ? m->wh - ch * rows : 0; - aw = (i >= rows * (cols - 1)) ? m->ww - cw * cols : 0; - resize(c, cx, cy, cw - 2 * c->bw + aw, ch - 2 * c->bw + ah, False); - i++; - } -} - -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; - - 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; - i++; - } - resize(c, nx, ny, nw - 2 * c->bw, nh - 2 * c->bw, False); - } -} - -void -dwindle(Monitor *mon) { - fibonacci(mon, 1); -} - -void -spiral(Monitor *mon) { - fibonacci(mon, 0); -} - -void -centeredmaster(Monitor *m) -{ - unsigned int i, n, h, mw, mx, my, oty, ety, tw; - 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 */ - h = (m->wh - my) / (MIN(n, m->nmaster) - i); - resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw), - h - (2*c->bw), 0); - my += HEIGHT(c); - } else { - /* stack clients are stacked vertically */ - if ((i - m->nmaster) % 2 ) { - h = (m->wh - ety) / ( (1 + n - i) / 2); - resize(c, m->wx, m->wy + ety, tw - (2*c->bw), - h - (2*c->bw), 0); - ety += HEIGHT(c); - } else { - h = (m->wh - oty) / ((1 + n - i) / 2); - resize(c, m->wx + mx + mw, m->wy + oty, - tw - (2*c->bw), h - (2*c->bw), 0); - oty += HEIGHT(c); - } - } -} - -void -centeredfloatingmaster(Monitor *m) -{ - unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx; - 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 nmaster area */ - if (n > m->nmaster) { - /* go mfact box in the center if more than nmaster clients */ - if (m->ww > m->wh) { - mw = m->nmaster ? m->ww * m->mfact : 0; - mh = m->nmaster ? m->wh * 0.9 : 0; - } else { - mh = m->nmaster ? m->wh * m->mfact : 0; - mw = m->nmaster ? m->ww * 0.9 : 0; - } - mx = mxo = (m->ww - mw) / 2; - my = myo = (m->wh - mh) / 2; - } else { - /* go fullscreen if all clients are in the master area */ - mh = m->wh; - mw = m->ww; - mx = mxo = 0; - my = myo = 0; - } - - for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) - if (i < m->nmaster) { - /* nmaster clients are stacked horizontally, in the center - * of the screen */ - w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i); - resize(c, m->wx + mx, m->wy + my, w - (2*c->bw), - mh - (2*c->bw), 0); - mx += WIDTH(c); - } else { - /* stack clients are stacked horizontally */ - w = (m->ww - tx) / (n - i); - resize(c, m->wx + tx, m->wy, w - (2*c->bw), - m->wh - (2*c->bw), 0); - tx += WIDTH(c); - } -} - -/* Simply a copy of the default layout for the moment.... */ -void -layoutCenter(Monitor *m) -{ - unsigned int i, n, h, mw, my, ty; - Client *c; - - for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); - if (n == 0) - return; - - if (n > m->nmaster) - mw = m->nmaster ? m->ww * m->mfact : 0; - else - mw = m->ww; - for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) - if (i < m->nmaster) { - h = (m->wh - my) / (MIN(n, m->nmaster) - i); - resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0); - if (my + HEIGHT(c) < m->wh) - my += HEIGHT(c); - } else { - h = (m->wh - ty) / (n - i); - resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0); - if (ty + HEIGHT(c) < m->wh) - ty += HEIGHT(c); - } -} @@ -1,8 +1,12 @@ /* See LICENSE file for copyright and license details. */ +#include <err.h> +#include <fcntl.h> +#include <regex.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "util.h" @@ -33,3 +37,51 @@ die(const char *fmt, ...) { exit(1); } + + +int +readEnv(FILE *fh) +{ + int ret; + char name[1024], value[8192]; + + while (1) { + if ((ret = fscanf(fh, "%1023[^=]=%8191[^\n]\n", &name, &value)) == EOF) + break; + else if (ret == 0) + break; + + fprintf(stderr, "Setting: '%s' = '%s'\n", name, value); + setenv(name, value, 1); + } + + return 0; +} + +int +reMatch(const char *regex, const char *str) { + char reErr[1024] = {0}; + regex_t *re = calloc(1, sizeof(regex_t)); + int rc = 0; + + if (!re) + return -1; + + rc = regcomp(re, regex, REG_EXTENDED|REG_ICASE|REG_NOSUB); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex compile err: %s %s\n", regex, reErr); + return -1; + } + + rc = regexec(re, str, 0, NULL, 0); + regfree(re); + if (rc != 0) { + regerror(rc, re, reErr, 1024); + fprintf(stderr, "Regex match error: %s -> %s : %s\n", + regex, str, reErr); + return -1; + } + + return rc; +} @@ -6,3 +6,5 @@ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); +int readEnv(FILE *fh); +int reMatch(const char *regex, const char *str); |
