dwm-statuscmd-status2d-20210405-60bb3df.diff (5695B)
1 From 05f9b3c45d4267d52724b2a76f333ba00353cab3 Mon Sep 17 00:00:00 2001 2 From: Daniel Bylinka <daniel.bylinka@gmail.com> 3 Date: Fri, 2 Apr 2021 19:04:58 +0200 4 Subject: [PATCH] [statuscmd] status2d compatibility 5 6 --- 7 config.def.h | 6 +++- 8 dwm.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++---- 9 2 files changed, 83 insertions(+), 6 deletions(-) 10 11 diff --git a/config.def.h b/config.def.h 12 index 1c0b587..154a59b 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -54,6 +54,8 @@ static const Layout layouts[] = { 16 /* helper for spawning shell commands in the pre dwm-5.0 fashion */ 17 #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } } 18 19 +#define STATUSBAR "dwmblocks" 20 + 21 /* commands */ 22 static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ 23 static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; 24 @@ -103,7 +105,9 @@ static Button buttons[] = { 25 { ClkLtSymbol, 0, Button1, setlayout, {0} }, 26 { ClkLtSymbol, 0, Button3, setlayout, {.v = &layouts[2]} }, 27 { ClkWinTitle, 0, Button2, zoom, {0} }, 28 - { ClkStatusText, 0, Button2, spawn, {.v = termcmd } }, 29 + { ClkStatusText, 0, Button1, sigstatusbar, {.i = 1} }, 30 + { ClkStatusText, 0, Button2, sigstatusbar, {.i = 2} }, 31 + { ClkStatusText, 0, Button3, sigstatusbar, {.i = 3} }, 32 { ClkClientWin, MODKEY, Button1, movemouse, {0} }, 33 { ClkClientWin, MODKEY, Button2, togglefloating, {0} }, 34 { ClkClientWin, MODKEY, Button3, resizemouse, {0} }, 35 diff --git a/dwm.c b/dwm.c 36 index acbe6c9..ba478b9 100644 37 --- a/dwm.c 38 +++ b/dwm.c 39 @@ -173,6 +173,7 @@ static void focusstack(const Arg *arg); 40 static Atom getatomprop(Client *c, Atom prop); 41 static int getrootptr(int *x, int *y); 42 static long getstate(Window w); 43 +static pid_t getstatusbarpid(); 44 static int gettextprop(Window w, Atom atom, char *text, unsigned int size); 45 static void grabbuttons(Client *c, int focused); 46 static void grabkeys(void); 47 @@ -207,6 +208,7 @@ static void setup(void); 48 static void seturgent(Client *c, int urg); 49 static void showhide(Client *c); 50 static void sigchld(int unused); 51 +static void sigstatusbar(const Arg *arg); 52 static void spawn(const Arg *arg); 53 static void tag(const Arg *arg); 54 static void tagmon(const Arg *arg); 55 @@ -239,6 +241,9 @@ static void zoom(const Arg *arg); 56 /* variables */ 57 static const char broken[] = "broken"; 58 static char stext[1024]; 59 +static int statussig; 60 +static int statusw; 61 +static pid_t statuspid = -1; 62 static int screen; 63 static int sw, sh; /* X display screen geometry width, height */ 64 static int bh, blw = 0; /* bar geometry */ 65 @@ -441,9 +446,34 @@ buttonpress(XEvent *e) 66 arg.ui = 1 << i; 67 } else if (ev->x < x + blw) 68 click = ClkLtSymbol; 69 - else if (ev->x > selmon->ww - (int)TEXTW(stext)) 70 + else if (ev->x > selmon->ww - statusw) { 71 + x = selmon->ww - statusw; 72 click = ClkStatusText; 73 - else 74 + 75 + char *text, *s, ch; 76 + statussig = 0; 77 + for (text = s = stext; *s && x <= ev->x; s++) { 78 + if ((unsigned char)(*s) < ' ') { 79 + ch = *s; 80 + *s = '\0'; 81 + x += TEXTW(text) - lrpad; 82 + *s = ch; 83 + text = s + 1; 84 + if (x >= ev->x) 85 + break; 86 + statussig = ch; 87 + } else if (*s == '^') { 88 + *s = '\0'; 89 + x += TEXTW(text) - lrpad; 90 + *s = '^'; 91 + if (*(++s) == 'f') 92 + x += atoi(++s); 93 + while (*(s++) != '^'); 94 + text = s; 95 + s--; 96 + } 97 + } 98 + } else 99 click = ClkWinTitle; 100 } else if ((c = wintoclient(ev->window))) { 101 focus(c); 102 @@ -696,7 +726,7 @@ dirtomon(int dir) 103 104 int 105 drawstatusbar(Monitor *m, int bh, char* stext) { 106 - int ret, i, w, x, len; 107 + int ret, i, j, w, x, len; 108 short isCode = 0; 109 char *text; 110 char *p; 111 @@ -705,7 +735,12 @@ drawstatusbar(Monitor *m, int bh, char* stext) { 112 if (!(text = (char*) malloc(sizeof(char)*len))) 113 die("malloc"); 114 p = text; 115 - memcpy(text, stext, len); 116 + 117 + i = -1, j = 0; 118 + while (stext[++i]) 119 + if ((unsigned char)stext[i] >= ' ') 120 + text[j++] = stext[i]; 121 + text[j] = '\0'; 122 123 /* compute width of the status text */ 124 w = 0; 125 @@ -813,7 +848,7 @@ drawbar(Monitor *m) 126 127 /* draw status first so it can be overdrawn by tags later */ 128 if (m == selmon) { /* status is only drawn on selected monitor */ 129 - tw = m->ww - drawstatusbar(m, bh, stext); 130 + tw = statusw = m->ww - drawstatusbar(m, bh, stext); 131 } 132 133 for (c = m->clients; c; c = c->next) { 134 @@ -979,6 +1014,30 @@ getatomprop(Client *c, Atom prop) 135 return atom; 136 } 137 138 +pid_t 139 +getstatusbarpid() 140 +{ 141 + char buf[32], *str = buf, *c; 142 + FILE *fp; 143 + 144 + if (statuspid > 0) { 145 + snprintf(buf, sizeof(buf), "/proc/%u/cmdline", statuspid); 146 + if ((fp = fopen(buf, "r"))) { 147 + fgets(buf, sizeof(buf), fp); 148 + while ((c = strchr(str, '/'))) 149 + str = c + 1; 150 + fclose(fp); 151 + if (!strcmp(str, STATUSBAR)) 152 + return statuspid; 153 + } 154 + } 155 + if (!(fp = popen("pidof -s "STATUSBAR, "r"))) 156 + return -1; 157 + fgets(buf, sizeof(buf), fp); 158 + pclose(fp); 159 + return strtoul(buf, NULL, 10); 160 +} 161 + 162 int 163 getrootptr(int *x, int *y) 164 { 165 @@ -1745,6 +1804,20 @@ sigchld(int unused) 166 while (0 < waitpid(-1, NULL, WNOHANG)); 167 } 168 169 +void 170 +sigstatusbar(const Arg *arg) 171 +{ 172 + union sigval sv; 173 + 174 + if (!statussig) 175 + return; 176 + sv.sival_int = arg->i; 177 + if ((statuspid = getstatusbarpid()) <= 0) 178 + return; 179 + 180 + sigqueue(statuspid, SIGRTMIN+statussig, sv); 181 +} 182 + 183 void 184 spawn(const Arg *arg) 185 { 186 -- 187 2.31.0 188