dwm-autoswitch-20240919-5096afab.diff (4709B)
1 From 5096afab5ccc582ba8c25f795e44a5e4c4761925 Mon Sep 17 00:00:00 2001 2 From: elbachir-one <bachiralfa@gmail.com> 3 Date: Thu, 19 Sep 2024 05:28:42 +0100 4 Subject: [PATCH] Automatically switch to monocle mode after opening N window 5 6 --- 7 config.def.h | 7 +++--- 8 dwm.c | 62 ++++++++++++++++++++++++++++++++++++++-------------- 9 2 files changed, 49 insertions(+), 20 deletions(-) 10 11 diff --git a/config.def.h b/config.def.h 12 index 9efa774..32fafc9 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -5,6 +5,7 @@ static const unsigned int borderpx = 1; /* border pixel of windows */ 16 static const unsigned int snap = 32; /* snap pixel */ 17 static const int showbar = 1; /* 0 means no bar */ 18 static const int topbar = 1; /* 0 means bottom bar */ 19 +static const int monoclemode = 4; /* automatically switch to monocle mode after opening 4 windows */ 20 static const char *fonts[] = { "monospace:size=10" }; 21 static const char dmenufont[] = "monospace:size=10"; 22 static const char col_gray1[] = "#222222"; 23 @@ -40,8 +41,8 @@ static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen win 24 static const Layout layouts[] = { 25 /* symbol arrange function */ 26 { "[]=", tile }, /* first entry is default */ 27 - { "><>", NULL }, /* no layout function means floating behavior */ 28 { "[M]", monocle }, 29 + { "><>", NULL }, /* no layout function means floating behavior */ 30 }; 31 32 /* key definitions */ 33 @@ -75,8 +76,8 @@ static const Key keys[] = { 34 { MODKEY, XK_Tab, view, {0} }, 35 { MODKEY|ShiftMask, XK_c, killclient, {0} }, 36 { MODKEY, XK_t, setlayout, {.v = &layouts[0]} }, 37 - { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, 38 - { MODKEY, XK_m, setlayout, {.v = &layouts[2]} }, 39 + { MODKEY, XK_m, setlayout, {.v = &layouts[1]} }, 40 + { MODKEY, XK_f, setlayout, {.v = &layouts[2]} }, 41 { MODKEY, XK_space, setlayout, {0} }, 42 { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, 43 { MODKEY, XK_0, view, {.ui = ~0 } }, 44 diff --git a/dwm.c b/dwm.c 45 index 67c6b2b..f8add6d 100644 46 --- a/dwm.c 47 +++ b/dwm.c 48 @@ -227,6 +227,7 @@ static void updatetitle(Client *c); 49 static void updatewindowtype(Client *c); 50 static void updatewmhints(Client *c); 51 static void view(const Arg *arg); 52 +static int visibleclientcount(Monitor *m); 53 static Client *wintoclient(Window w); 54 static Monitor *wintomon(Window w); 55 static int xerror(Display *dpy, XErrorEvent *ee); 56 @@ -382,15 +383,25 @@ applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) 57 void 58 arrange(Monitor *m) 59 { 60 - if (m) 61 - showhide(m->stack); 62 - else for (m = mons; m; m = m->next) 63 + if (!m) 64 + for (m = mons; m; m = m->next) 65 + showhide(m->stack); 66 + else 67 showhide(m->stack); 68 - if (m) { 69 - arrangemon(m); 70 - restack(m); 71 - } else for (m = mons; m; m = m->next) 72 - arrangemon(m); 73 + 74 + for (Monitor *mon = (m ? m : mons); mon; mon = (m ? NULL : mon->next)) { 75 + unsigned int n = visibleclientcount(mon); 76 + 77 + if (n >= monoclemode && mon->lt[mon->sellt]->arrange != monocle) 78 + setlayout(&(Arg) {.v = &layouts[1]}); 79 + 80 + if (n < monoclemode && mon->lt[mon->sellt]->arrange == monocle) 81 + setlayout(&(Arg) {.v = &layouts[0]}); 82 + 83 + arrangemon(mon); 84 + if (!m) 85 + restack(mon); 86 + } 87 } 88 89 void 90 @@ -1510,15 +1521,19 @@ setfullscreen(Client *c, int fullscreen) 91 void 92 setlayout(const Arg *arg) 93 { 94 - if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) 95 - selmon->sellt ^= 1; 96 - if (arg && arg->v) 97 - selmon->lt[selmon->sellt] = (Layout *)arg->v; 98 - strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); 99 - if (selmon->sel) 100 - arrange(selmon); 101 - else 102 - drawbar(selmon); 103 + if (!arg || !arg->v) 104 + return; 105 + 106 + Layout *newlayout = (Layout *)arg->v; 107 + if (newlayout != selmon->lt[selmon->sellt]) { 108 + selmon->lt[selmon->sellt] = newlayout; 109 + strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof(selmon->ltsymbol)); 110 + selmon->ltsymbol[sizeof(selmon->ltsymbol) - 1] = '\0'; 111 + if (selmon->sel) 112 + arrange(selmon); 113 + else 114 + drawbar(selmon); 115 + } 116 } 117 118 /* arg > 1.0 will set mfact absolutely */ 119 @@ -2062,6 +2077,19 @@ view(const Arg *arg) 120 arrange(selmon); 121 } 122 123 +int 124 +visibleclientcount(Monitor *m) 125 +{ 126 + unsigned int count = 0; 127 + Client *c; 128 + for (c = m->clients; c; c = c->next) { 129 + if (ISVISIBLE(c)) { 130 + count++; 131 + } 132 + } 133 + return count; 134 +} 135 + 136 Client * 137 wintoclient(Window w) 138 { 139 -- 140 2.46.0 141