From 58e18d86a798b4487375f19bcfcdcf9283a56f6e Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 1 Jan 2023 17:21:45 -0500 Subject: Add a new centered layout to dwm. Allow the scaling factor to be adjusted on the fly. --- dwm.c | 291 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 290 insertions(+), 1 deletion(-) (limited to 'dwm.c') diff --git a/dwm.c b/dwm.c index a129a57..95477b6 100644 --- a/dwm.c +++ b/dwm.c @@ -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); +} + -- cgit v1.2.3