From c282f86559f3c7858e34888c1fa0204c22ede89c Mon Sep 17 00:00:00 2001 From: elbachir-one Date: Sat, 21 Sep 2024 22:59:53 +0100 Subject: [PATCH] Allowing manual switch of monocle mode. --- config.def.h | 7 ++--- dwm.c | 72 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/config.def.h b/config.def.h index 9efa774..32fafc9 100644 --- a/config.def.h +++ b/config.def.h @@ -5,6 +5,7 @@ static const unsigned int borderpx = 1; /* border pixel of windows */ static const unsigned int snap = 32; /* snap pixel */ static const int showbar = 1; /* 0 means no bar */ static const int topbar = 1; /* 0 means bottom bar */ +static const int monoclemode = 4; /* automatically switch to monocle mode after opening 4 windows */ static const char *fonts[] = { "monospace:size=10" }; static const char dmenufont[] = "monospace:size=10"; static const char col_gray1[] = "#222222"; @@ -40,8 +41,8 @@ static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen win static const Layout layouts[] = { /* symbol arrange function */ { "[]=", tile }, /* first entry is default */ - { "><>", NULL }, /* no layout function means floating behavior */ { "[M]", monocle }, + { "><>", NULL }, /* no layout function means floating behavior */ }; /* key definitions */ @@ -75,8 +76,8 @@ static const Key keys[] = { { MODKEY, XK_Tab, view, {0} }, { MODKEY|ShiftMask, XK_c, killclient, {0} }, { MODKEY, XK_t, setlayout, {.v = &layouts[0]} }, - { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, - { MODKEY, XK_m, setlayout, {.v = &layouts[2]} }, + { MODKEY, XK_m, setlayout, {.v = &layouts[1]} }, + { MODKEY, XK_f, setlayout, {.v = &layouts[2]} }, { 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 67c6b2b..9561896 100644 --- a/dwm.c +++ b/dwm.c @@ -130,6 +130,7 @@ struct Monitor { Monitor *next; Window barwin; const Layout *lt[2]; + int manualswitch; }; typedef struct { @@ -227,6 +228,7 @@ static void updatetitle(Client *c); static void updatewindowtype(Client *c); static void updatewmhints(Client *c); static void view(const Arg *arg); +static int visibleclientcount(Monitor *m); static Client *wintoclient(Window w); static Monitor *wintomon(Window w); static int xerror(Display *dpy, XErrorEvent *ee); @@ -382,15 +384,31 @@ applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) void arrange(Monitor *m) { - if (m) - showhide(m->stack); - else for (m = mons; m; m = m->next) + if (!m) + for (m = mons; m; m = m->next) + showhide(m->stack); + else showhide(m->stack); - if (m) { - arrangemon(m); - restack(m); - } else for (m = mons; m; m = m->next) - arrangemon(m); + + for (Monitor *mon = (m ? m : mons); mon; mon = (m ? NULL : mon->next)) { + unsigned int n = visibleclientcount(mon); + + if (!mon->manualswitch) { + if (n >= monoclemode && mon->lt[mon->sellt]->arrange != monocle) { + setlayout(&(Arg) {.v = &layouts[1]}); + } else if (n < monoclemode && mon->lt[mon->sellt]->arrange == monocle) { + setlayout(&(Arg) {.v = &layouts[0]}); + } + } + + if (mon->manualswitch && (n < monoclemode || n >= monoclemode)) { + mon->manualswitch = 0; + } + + arrangemon(mon); + if (!m) + restack(mon); + } } void @@ -1510,15 +1528,22 @@ setfullscreen(Client *c, int fullscreen) void setlayout(const Arg *arg) { - if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) - selmon->sellt ^= 1; - if (arg && arg->v) - selmon->lt[selmon->sellt] = (Layout *)arg->v; - strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); - if (selmon->sel) - arrange(selmon); - else - drawbar(selmon); + if (!arg || !arg->v) + return; + + Layout *newlayout = (Layout *)arg->v; + if (newlayout != selmon->lt[selmon->sellt]) { + selmon->lt[selmon->sellt] = newlayout; + strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof(selmon->ltsymbol)); + selmon->ltsymbol[sizeof(selmon->ltsymbol) - 1] = '\0'; + + selmon->manualswitch = 1; + + if (selmon->sel) + arrange(selmon); + else + drawbar(selmon); + } } /* arg > 1.0 will set mfact absolutely */ @@ -2062,6 +2087,19 @@ view(const Arg *arg) arrange(selmon); } +int +visibleclientcount(Monitor *m) +{ + unsigned int count = 0; + Client *c; + for (c = m->clients; c; c = c->next) { + if (ISVISIBLE(c)) { + count++; + } + } + return count; +} + Client * wintoclient(Window w) { -- 2.46.0