slstatus

My fork of https://tools.suckless.org/slstatus/
git clone https://git.inz.fi/slstatus
Log | Files | Refs | README | LICENSE

commit 641b8671c652d4d628ad83999d84962f22390993
parent 4c4d53c669a3f09e2e1b4b9a92736085840d6c91
Author: Santtu Lakkala <inz@inz.fi>
Date:   Wed, 23 Feb 2022 08:51:41 +0200

Add common display access for modules

As some modules use X connection, they would need to connect-disconnect
on every refresh. Instead use a shared Display.

Diffstat:
Mcomponents/keyboard_indicators.c | 3+--
Mslstatus.c | 6++----
Mutil.c | 18++++++++++++++++++
Mutil.h | 3+++
4 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c @@ -22,12 +22,11 @@ keyboard_indicators(const char *fmt) int togglecase, isset; char key; - if (!(dpy = XOpenDisplay(NULL))) { + if (!(dpy = getdisplay())) { warn("XOpenDisplay: Failed to open display"); return NULL; } XGetKeyboardControl(dpy, &state); - XCloseDisplay(dpy); fmtlen = strnlen(fmt, 4); for (i = n = 0; i < fmtlen; i++) { diff --git a/slstatus.c b/slstatus.c @@ -77,7 +77,7 @@ main(int argc, char *argv[]) act.sa_flags |= SA_RESTART; sigaction(SIGUSR1, &act, NULL); - if (!sflag && !(dpy = XOpenDisplay(NULL))) { + if (!sflag && !(dpy = getdisplay())) { die("XOpenDisplay: Failed to open display"); } @@ -132,10 +132,8 @@ main(int argc, char *argv[]) if (!sflag) { XStoreName(dpy, DefaultRootWindow(dpy), NULL); - if (XCloseDisplay(dpy) < 0) { - die("XCloseDisplay: Failed to close display"); - } } + closedisplay(); return 0; } diff --git a/util.c b/util.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <X11/Xlib.h> #include <errno.h> #include <stdarg.h> #include <stdint.h> @@ -9,6 +10,7 @@ #include "util.h" char *argv0; +static Display *dpy = NULL; static void verr(const char *fmt, va_list ap) @@ -144,3 +146,19 @@ pscanf(const char *path, const char *fmt, ...) return (n == EOF) ? -1 : n; } + +Display * +getdisplay(void) +{ + if (dpy) + return dpy; + return dpy = XOpenDisplay(NULL); +} + +void +closedisplay(void) +{ + if (dpy) + XCloseDisplay(dpy); + dpy = NULL; +} diff --git a/util.h b/util.h @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <stdint.h> #include <stdarg.h> +#include <X11/Xlib.h> extern char buf[1024]; @@ -16,3 +17,5 @@ int esnprintf(char *str, size_t size, const char *fmt, ...); const char *bprintf(const char *fmt, ...); const char *fmt_human(uintmax_t num, int base); int pscanf(const char *path, const char *fmt, ...); +Display *getdisplay(void); +void closedisplay(void);