From f8c84c40410e223dcd037ed3a9fba0208ed3411f Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Sun, 10 Jul 2022 22:02:35 +0200 Subject: [PATCH] =?UTF-8?q?Applay=20the=20centeredmaster=20layout=20?= =?UTF-8?q?=F0=9F=A5=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 + keys/keys.c | 4 + layouts/centeredmaster.c | 102 +++++++++++++ layouts/centeredmaster.h | 10 ++ layouts/layouts.h | 15 +- .../dwm-centeredmaster-20160719-56a31dc.diff | 142 ++++++++++++++++++ 6 files changed, 270 insertions(+), 6 deletions(-) create mode 100644 layouts/centeredmaster.c create mode 100644 layouts/centeredmaster.h create mode 100644 patches/dwm-centeredmaster-20160719-56a31dc.diff diff --git a/README.md b/README.md index a54a179..2d196c0 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ - [sticky](https://dwm.suckless.org/patches/sticky) - [noborder](https://dwm.suckless.org/patches/noborder) - [fibonacci layouts](https://dwm.suckless.org/patches/fibonacci) +- [centeredmaster](https://dwm.suckless.org/patches/centeredmaster) ### Keys | Keys | Function | @@ -51,6 +52,8 @@ | modkey + s -> g | Use the grid layout | | modkey + s -> r | Use the spial layout (part from fibonacci layouts) | | modkey + s -> shift + r | Use the dwindle layout (part from fibonacci layouts) | +| modkey + s -> c | Use the centerd master layout | +| modkey + s -> shift + s | Use the centerd floating master layout | | modkey + s -> space | Toggle between current layout and tile layout | | modkey + shift + s | Toggle sticky mode | | modkey + alt + f | Toggle floating window | diff --git a/keys/keys.c b/keys/keys.c index 59f2784..7548a3d 100644 --- a/keys/keys.c +++ b/keys/keys.c @@ -87,6 +87,10 @@ static Keychord keychords[] = { {2, {{MODKEY, XK_s}, {0, XK_r}}, setlayout, {.v = &layouts[4]} }, // Dwindle layout {2, {{MODKEY, XK_s}, {ShiftMask, XK_r}}, setlayout, {.v = &layouts[5]} }, + // Centerd master layout + {2, {{MODKEY, XK_s}, {0, XK_c}}, setlayout, {.v = &layouts[6]} }, + // Centerd floating master layout + {2, {{MODKEY, XK_s}, {ShiftMask, XK_c}}, setlayout, {.v = &layouts[7]} }, // Toggle between current layout and tile layout {2, {{MODKEY, XK_s}, {0, XK_space}}, setlayout, {0} }, diff --git a/layouts/centeredmaster.c b/layouts/centeredmaster.c new file mode 100644 index 0000000..16f2a67 --- /dev/null +++ b/layouts/centeredmaster.c @@ -0,0 +1,102 @@ +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); + } +} diff --git a/layouts/centeredmaster.h b/layouts/centeredmaster.h new file mode 100644 index 0000000..d7a7e4b --- /dev/null +++ b/layouts/centeredmaster.h @@ -0,0 +1,10 @@ +#ifndef centeredmaster_H +#define centeredmaster_H + +static void centeredmaster(Monitor *m); +static void centeredfloatingmaster(Monitor *m); + +#include "centeredmaster.c" + + +#endif // !centeredmaster.h diff --git a/layouts/layouts.h b/layouts/layouts.h index 33aa977..1313517 100644 --- a/layouts/layouts.h +++ b/layouts/layouts.h @@ -6,16 +6,19 @@ #include "monocle.c" #include "grid.c" #include "fibonacci.c" +#include "centeredmaster.h" // Layouts array static const Layout layouts[] = { /* symbol arrange function */ - { "[]=", tile }, /* first entry is default */ - { "><>", NULL }, /* no layout function means floating behavior */ - { "[M]", monocle }, - { "HHH", grid }, - { "[@]", spiral }, - { "[\\]", dwindle }, + { "[]=", tile }, /* first entry is default */ + { "><>", NULL }, /* no layout function means floating behavior */ + { "[M]", monocle }, // 2 + { "HHH", grid }, // 3 + { "[@]", spiral }, // 4 + { "[\\]", dwindle }, // 5 + { "|M|", centeredmaster }, // 6 + { ">M>", centeredfloatingmaster }, // 7 }; #endif // !layouts.h diff --git a/patches/dwm-centeredmaster-20160719-56a31dc.diff b/patches/dwm-centeredmaster-20160719-56a31dc.diff new file mode 100644 index 0000000..1968e61 --- /dev/null +++ b/patches/dwm-centeredmaster-20160719-56a31dc.diff @@ -0,0 +1,142 @@ +diff --git a/config.def.h b/config.def.h +index fd77a07..f025619 100644 +--- a/config.def.h ++++ b/config.def.h +@@ -41,6 +41,8 @@ static const Layout layouts[] = { + { "[]=", tile }, /* first entry is default */ + { "><>", NULL }, /* no layout function means floating behavior */ + { "[M]", monocle }, ++ { "|M|", centeredmaster }, ++ { ">M>", centeredfloatingmaster }, + }; + + /* key definitions */ +@@ -76,6 +78,8 @@ static Key keys[] = { + { MODKEY, XK_t, setlayout, {.v = &layouts[0]} }, + { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, + { MODKEY, XK_m, setlayout, {.v = &layouts[2]} }, ++ { MODKEY, XK_u, setlayout, {.v = &layouts[3]} }, ++ { MODKEY, XK_o, setlayout, {.v = &layouts[4]} }, + { MODKEY, XK_space, setlayout, {0} }, + { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, + { MODKEY, XK_0, view, {.ui = ~0 } }, +diff --git a/dwm.c b/dwm.c +index b2bc9bd..9ecabae 100644 +--- a/dwm.c ++++ b/dwm.c +@@ -234,6 +234,8 @@ 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 centeredfloatingmaster(Monitor *m); + + /* variables */ + static const char broken[] = "broken"; +@@ -2138,3 +2140,106 @@ main(int argc, char *argv[]) + XCloseDisplay(dpy); + return EXIT_SUCCESS; + } ++ ++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); ++ } ++}