dwm-autoswitch-20240921-c282f865.diff (5015B)
1 From c282f86559f3c7858e34888c1fa0204c22ede89c Mon Sep 17 00:00:00 2001 2 From: elbachir-one <bachiralfa@gmail.com> 3 Date: Sat, 21 Sep 2024 22:59:53 +0100 4 Subject: [PATCH] Allowing manual switch of monocle mode. 5 6 --- 7 config.def.h | 7 ++--- 8 dwm.c | 72 +++++++++++++++++++++++++++++++++++++++------------- 9 2 files changed, 59 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..9561896 100644 46 --- a/dwm.c 47 +++ b/dwm.c 48 @@ -130,6 +130,7 @@ struct Monitor { 49 Monitor *next; 50 Window barwin; 51 const Layout *lt[2]; 52 + int manualswitch; 53 }; 54 55 typedef struct { 56 @@ -227,6 +228,7 @@ static void updatetitle(Client *c); 57 static void updatewindowtype(Client *c); 58 static void updatewmhints(Client *c); 59 static void view(const Arg *arg); 60 +static int visibleclientcount(Monitor *m); 61 static Client *wintoclient(Window w); 62 static Monitor *wintomon(Window w); 63 static int xerror(Display *dpy, XErrorEvent *ee); 64 @@ -382,15 +384,31 @@ applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) 65 void 66 arrange(Monitor *m) 67 { 68 - if (m) 69 - showhide(m->stack); 70 - else for (m = mons; m; m = m->next) 71 + if (!m) 72 + for (m = mons; m; m = m->next) 73 + showhide(m->stack); 74 + else 75 showhide(m->stack); 76 - if (m) { 77 - arrangemon(m); 78 - restack(m); 79 - } else for (m = mons; m; m = m->next) 80 - arrangemon(m); 81 + 82 + for (Monitor *mon = (m ? m : mons); mon; mon = (m ? NULL : mon->next)) { 83 + unsigned int n = visibleclientcount(mon); 84 + 85 + if (!mon->manualswitch) { 86 + if (n >= monoclemode && mon->lt[mon->sellt]->arrange != monocle) { 87 + setlayout(&(Arg) {.v = &layouts[1]}); 88 + } else if (n < monoclemode && mon->lt[mon->sellt]->arrange == monocle) { 89 + setlayout(&(Arg) {.v = &layouts[0]}); 90 + } 91 + } 92 + 93 + if (mon->manualswitch && (n < monoclemode || n >= monoclemode)) { 94 + mon->manualswitch = 0; 95 + } 96 + 97 + arrangemon(mon); 98 + if (!m) 99 + restack(mon); 100 + } 101 } 102 103 void 104 @@ -1510,15 +1528,22 @@ setfullscreen(Client *c, int fullscreen) 105 void 106 setlayout(const Arg *arg) 107 { 108 - if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) 109 - selmon->sellt ^= 1; 110 - if (arg && arg->v) 111 - selmon->lt[selmon->sellt] = (Layout *)arg->v; 112 - strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); 113 - if (selmon->sel) 114 - arrange(selmon); 115 - else 116 - drawbar(selmon); 117 + if (!arg || !arg->v) 118 + return; 119 + 120 + Layout *newlayout = (Layout *)arg->v; 121 + if (newlayout != selmon->lt[selmon->sellt]) { 122 + selmon->lt[selmon->sellt] = newlayout; 123 + strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof(selmon->ltsymbol)); 124 + selmon->ltsymbol[sizeof(selmon->ltsymbol) - 1] = '\0'; 125 + 126 + selmon->manualswitch = 1; 127 + 128 + if (selmon->sel) 129 + arrange(selmon); 130 + else 131 + drawbar(selmon); 132 + } 133 } 134 135 /* arg > 1.0 will set mfact absolutely */ 136 @@ -2062,6 +2087,19 @@ view(const Arg *arg) 137 arrange(selmon); 138 } 139 140 +int 141 +visibleclientcount(Monitor *m) 142 +{ 143 + unsigned int count = 0; 144 + Client *c; 145 + for (c = m->clients; c; c = c->next) { 146 + if (ISVISIBLE(c)) { 147 + count++; 148 + } 149 + } 150 + return count; 151 +} 152 + 153 Client * 154 wintoclient(Window w) 155 { 156 -- 157 2.46.0 158