diff --git a/GNUmakefile b/GNUmakefile index edeb833..e6fdda3 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -8,7 +8,7 @@ X11LIBS := $(shell pkg-config --libs x11) all: dwmblocks sigdwmblocks xgetrootname -dwmblocks: dwmblocks.c blocks.h +dwmblocks: dwmblocks.c config.h block.h ${CC} -o $@ -Wno-missing-field-initializers -Wno-unused-parameter ${CFLAGS} ${X11CFLAGS} $< ${X11LIBS} sigdwmblocks: sigdwmblocks.c diff --git a/README.md b/README.md index 81708a5..5b0c0ce 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ signaling, clickability, cursor hinting and color. # Modifying blocks -Blocks are added and removed by editing [blocks.h](blocks.h) file. Read it for -more info. +Blocks are added and removed by editing [config.h](config.h) file. Some other +configurations can also be done through the file. Read it for more info. > Change the PATH macro defined at the top of the file. It should point to the > folder where your scripts are saved. diff --git a/block.h b/block.h new file mode 100644 index 0000000..4eb15b7 --- /dev/null +++ b/block.h @@ -0,0 +1,9 @@ +typedef struct { + char *const pathu; + char *const pathc; + const int interval; + const int signal; + char curtext[CMDOUTLENGTH + 1 + DELIMITERLENGTH]; + char prvtext[CMDOUTLENGTH + 1]; + int len; +} Block; diff --git a/blocks.h b/config.h similarity index 89% rename from blocks.h rename to config.h index 1dad7f8..66faa25 100644 --- a/blocks.h +++ b/config.h @@ -1,5 +1,12 @@ +/* macro for conveniently specifying pathu and pathc below */ #define PATH(name) "/home/ashish/.local/projects/dwmblocks/blocks/"name +/* buffer size for capturing output of the programs used for updating blocks */ +#define CMDOUTLENGTH 50 + +/* buffer size for status text */ +#define STATUSLENGTH 256 + /* DELIMITERENDCHAR must be less than 32. * At max, DELIMITERENDCHAR - 1 number of clickable blocks will be allowed. * Raw characters larger than DELIMITERENDCHAR and smaller than ' ' in ASCII @@ -11,6 +18,12 @@ dwm.c and color codes in your pathu programs. */ #define DELIMITERENDCHAR 10 +/* delimiter specified as an array of characters + * don't remove DELIMITERENDCHAR at the end */ +static const char delimiter[] = { ' ', ' ', ' ', DELIMITERENDCHAR }; + +#include "block.h" + /* If interval of a block is set to 0, the block will only be updated once at startup. * If interval is set to a negative value, the block will never be updated in @@ -39,7 +52,3 @@ static Block blocks[] = { { PATH("battery.sh"), PATH("battery_button.sh"), 30, 3}, { NULL } /* just to mark the end of the array */ }; - -/* delimiter specified as an array of characters - * don't remove DELIMITERENDCHAR at the end */ -static const char delimiter[] = { ' ', ' ', ' ', DELIMITERENDCHAR }; diff --git a/dwmblocks.c b/dwmblocks.c index 2a31e93..89e19da 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -9,24 +9,12 @@ #include #include -#define CMDOUTLENGTH 50 -#define STATUSLENGTH 256 - #define NILL INT_MIN #define LOCKFILE "/tmp/dwmblocks.pid" #define DELIMITERLENGTH sizeof delimiter -typedef struct { - char *const pathu; - char *const pathc; - const int interval; - const int signal; - char curcmdout[CMDOUTLENGTH + 1]; - char prvcmdout[CMDOUTLENGTH + 1]; -} Block; - -#include "blocks.h" +#include "config.h" static void buttonhandler(int sig, siginfo_t *info, void *ucontext); static void cleanup(); @@ -39,8 +27,8 @@ static void updateblock(Block *block, int sigval); static void updatestatus(); static void writepid(); -static int dirty; static char statustext[STATUSLENGTH + DELIMITERLENGTH]; +static Block *dirtyblock; static Display *dpy; static sigset_t blocksigmask; @@ -79,9 +67,9 @@ cleanup() void setroot() { - if (dirty) { + if (dirtyblock) { updatestatus(); - dirty = 0; + dirtyblock = NULL; XStoreName(dpy, DefaultRootWindow(dpy), statustext); XSync(dpy, False); } @@ -230,17 +218,33 @@ updateblock(Block *block, int sigval) close(fd[1]); do - rd = read(fd[0], block->curcmdout + trd, CMDOUTLENGTH - trd); + rd = read(fd[0], block->curtext + trd, CMDOUTLENGTH - trd); while (rd > 0 && (trd += rd) < CMDOUTLENGTH); if (rd == -1) { perror("updateblock - read"); exit(1); } close(fd[0]); - block->curcmdout[block->curcmdout[trd - 1] == '\n' ? --trd : trd] = '\0'; - if (memcmp(block->curcmdout, block->prvcmdout, trd) != 0) { - memcpy(block->prvcmdout, block->curcmdout, trd); - dirty = 1; + if (trd == 0) { + if (block->prvtext[0] != '\0') { + block->prvtext[0] = '\0'; + if (!dirtyblock || block < dirtyblock) + dirtyblock = block; + } + block->len = 0; + } else { + if (block->curtext[trd - 1] == '\n') + trd--; + block->curtext[trd++] = block->signal; + if (memcmp(block->curtext, block->prvtext, trd) != 0) { + memcpy(block->prvtext, block->curtext, trd); + if (!dirtyblock || block < dirtyblock) + dirtyblock = block; + } + if (!block->pathc) + trd--; + memcpy(block->curtext + trd, delimiter, DELIMITERLENGTH); + block->len = trd + DELIMITERLENGTH; } } } @@ -250,21 +254,17 @@ void updatestatus() { char *s = statustext; - size_t len; - for (Block *block = blocks; block->pathu; block++) { - if ((len = strlen(block->curcmdout)) == 0) - continue; - memcpy(s, block->curcmdout, len); - s += len; - if (block->pathc) - *(s++) = block->signal; - memcpy(s, delimiter, DELIMITERLENGTH); - s += DELIMITERLENGTH; + for (Block *block = blocks; block < dirtyblock; block++) + s += block->len; + for (Block *block = dirtyblock; block->pathu; block++) { + memcpy(s, block->curtext, block->len); + s += block->len; } if (s != statustext) - s -= DELIMITERLENGTH; - *s = '\0'; + *(s - DELIMITERLENGTH) = '\0'; + else + *s = '\0'; } void