Commit: eb159931ddb30aa72871470b74ddeb4c0b6c4e3c
Parent: fddb602f76eee4e02e62b8586f4d59e5805b4e84
Author: Randy Palamar
Date: Sat, 27 Mar 2021 15:51:47 -0600
implement updateblock() and updatestatus() functions
this avoids excessive updates of the status text and calls to Xlib
functions.
since this code is a modified version of what is found in dsblocks I
added the author to LICENSE (they use the same license)
Diffstat:
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -1,6 +1,7 @@
ISC License (ISC)
© 2019-2021 Randy Palamar <palamar@ualberta.ca>
+© 2021 Ashish Kumar Yadav
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
diff --git a/status.c b/status.c
@@ -15,6 +15,7 @@ static int dflag = 0;
char buf[BLOCKLEN - BLOCKPAD];
static Display *dpy;
+static struct Block *dirty;
static void
terminate(int signo)
@@ -23,14 +24,39 @@ terminate(int signo)
}
static void
-setstatus(char *str)
+updateblock(struct Block *b)
{
+ b->len = b->fn(b);
+ if (memcmp(b->curstr, b->prevstr, b->len)) {
+ memcpy(b->prevstr, b->curstr, b->len);
+ if (!dirty || b < dirty)
+ dirty = b;
+ }
+}
+
+static void
+updatestatus(void)
+{
+ static char status[STATUSLEN];
+ struct Block *b;
+ char *s = status;
+
+ for (b = blks; b < dirty; b++)
+ s += b->len;
+
+ for (; b->fn; b++) {
+ memcpy(s, b->curstr, b->len);
+ s += b->len;
+ }
+ s[0] = '\0';
+ dirty = NULL;
+
if (dflag) {
- puts(str);
+ puts(status);
return;
}
- XStoreName(dpy, DefaultRootWindow(dpy), str);
+ XStoreName(dpy, DefaultRootWindow(dpy), status);
XSync(dpy, False);
}
@@ -38,8 +64,6 @@ int
main(int argc, char *argv[])
{
struct sigaction sa;
- char status[STATUSLEN];
- size_t len;
struct Block *b;
memset(&sa, 0, sizeof(sa));
@@ -60,13 +84,12 @@ main(int argc, char *argv[])
if (!dflag && !(dpy = XOpenDisplay(NULL)))
die("XOpenDisplay: can't open display\n");
- for (len = 0; !done; sleep(1), len = 0) {
+ for (; !done; sleep(1))
for (b = blks; b->fn; b++) {
- b->len = b->fn(b);
- len += snprintf(status + len, sizeof(status) - len, "%s", b->curstr);
+ updateblock(b);
+ if (dirty)
+ updatestatus();
}
- setstatus(status);
- }
if (!dflag) {
XStoreName(dpy, DefaultRootWindow(dpy), NULL);