Bug fix, cleanup; rewrote updatestatus, removed -d

This commit is contained in:
Ashish Kumar Yadav 2021-02-09 11:38:40 +05:30
parent 9bdfdb6c99
commit 2c5ba57da2
3 changed files with 34 additions and 87 deletions

View file

@ -5,7 +5,7 @@ signaling, clickability, cursor hinting and color.
# Usage # Usage
`dwmblocks [-d <delimiter>]` `dwmblocks`
# Modifying blocks # Modifying blocks

View file

@ -39,5 +39,6 @@ static Block blocks[] = {
{ NULL } /* just to mark the end of the array */ { NULL } /* just to mark the end of the array */
}; };
/* default delimiter string */ /* delimiter specified as an array of characters
#define DELIMITER " " * don't remove DELIMITERENDCHAR at the end */
static const char delimiter[] = { ' ', ' ', ' ', DELIMITERENDCHAR };

View file

@ -9,18 +9,21 @@
#include <unistd.h> #include <unistd.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#define CMDLENGTH 50 #define CMDOUTLENGTH 50
#define STTLENGTH 256 #define STATUSLENGTH 256
#define NILL INT_MIN #define NILL INT_MIN
#define LOCKFILE "/tmp/dwmblocks.pid" #define LOCKFILE "/tmp/dwmblocks.pid"
#define DELIMITERLENGTH sizeof delimiter
typedef struct { typedef struct {
char *const pathu; char *const pathu;
char *const pathc; char *const pathc;
const int interval; const int interval;
const int signal; const int signal;
char cmdoutcur[CMDLENGTH + 1]; char curcmdout[CMDOUTLENGTH + 1];
char cmdoutprv[CMDLENGTH + 1]; char prvcmdout[CMDOUTLENGTH + 1];
} Block; } Block;
#include "blocks.h" #include "blocks.h"
@ -36,9 +39,7 @@ static void updateblock(Block *block, int sigval);
static int updatestatus(); static int updatestatus();
static void writepid(); static void writepid();
static char statustext[STTLENGTH]; static char statustext[STATUSLENGTH + DELIMITERLENGTH];
static char *delim;
static size_t delimlength;
static Display *dpy; static Display *dpy;
static sigset_t blocksigmask; static sigset_t blocksigmask;
@ -227,8 +228,8 @@ updateblock(Block *block, int sigval)
close(fd[1]); close(fd[1]);
do do
rd = read(fd[0], block->cmdoutcur + trd, CMDLENGTH - trd); rd = read(fd[0], block->curcmdout + trd, CMDOUTLENGTH - trd);
while (rd > 0 && (trd += rd) < CMDLENGTH); while (rd > 0 && (trd += rd) < CMDOUTLENGTH);
if (rd == -1) { if (rd == -1) {
perror("updateblock - read"); perror("updateblock - read");
exit(1); exit(1);
@ -243,87 +244,39 @@ int
updatestatus() updatestatus()
{ {
char *s = statustext; char *s = statustext;
char *c, *p; /* for cmdoutcur and cmdoutprv */ char *c, *p;
const char *d; /* for delimiter */
Block *block = blocks; Block *block = blocks;
/* checking half of the function */ /* checking half of the function */
/* find the first non-empty block */
for (;; block++) {
/* all blocks are empty */
if (!block->pathu)
return 0;
/* contents of the block changed */
if (*block->cmdoutcur != *block->cmdoutprv)
goto update0;
/* skip delimiter handler for the first non-empty block */
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0')
goto skipdelimc;
}
/* main loop */
for (; block->pathu; block++) { for (; block->pathu; block++) {
/* contents of the block changed */ c = block->curcmdout, p = block->prvcmdout;
if (*block->cmdoutcur != *block->cmdoutprv) for (; *c == *p && *c != '\n' && *c != '\0'; c++, p++);
goto update1; s += c - block->curcmdout;
/* delimiter handler */ if (*c != *p)
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0') goto update;
s += delimlength; if (c == block->curcmdout)
/* skip over empty blocks */
else
continue; continue;
skipdelimc: if (block->pathc /* && block->signal */)
/* checking for the first byte has been done */
c = block->cmdoutcur + 1, p = block->cmdoutprv + 1;
for (; *c != '\n' && *c != '\0'; c++, p++)
/* contents of the block changed */
if (*c != *p) {
s += c - block->cmdoutcur;
goto update2;
}
s += c - block->cmdoutcur;
/* byte containing info about signal number for the block */
if (block->pathc && block->signal)
s++; s++;
s += DELIMITERLENGTH;
} }
return 0; return 0;
/* updating half of the function */ /* updating half of the function */
/* find the first non-empty block */
for (;; block++) {
/* all blocks are empty */
if (!block->pathu)
return 1;
update0:
/* don't add delimiter before the first non-empty block */
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0')
goto skipdelimu;
*block->cmdoutprv = *block->cmdoutcur;
}
/* main loop */
for (; block->pathu; block++) { for (; block->pathu; block++) {
update1: c = block->curcmdout, p = block->prvcmdout;
/* delimiter handler */ update:
if (*block->cmdoutcur != '\n' && *block->cmdoutcur != '\0') { for (; *p = *c, *c != '\n' && *c != '\0'; c++, p++)
d = delim;
while (*d != '\0')
*(s++) = *(d++);
*(s++) = DELIMITERENDCHAR;
/* skip over empty blocks */
} else {
*block->cmdoutprv = *block->cmdoutcur;
continue;
}
skipdelimu:
c = block->cmdoutcur, p = block->cmdoutprv;
update2:
do {
*(s++) = *c; *(s++) = *c;
*p = *c; if (c == block->curcmdout)
c++, p++; continue;
} while (*c != '\n' && *c != '\0'); if (block->pathc /* && block->signal */)
if (block->pathc && block->signal)
*(s++) = block->signal; *(s++) = block->signal;
memcpy(s, delimiter, DELIMITERLENGTH);
s += DELIMITERLENGTH;
} }
if (s != statustext)
s -= DELIMITERLENGTH;
*s = '\0'; *s = '\0';
return 1; return 1;
} }
@ -363,18 +316,11 @@ writepid()
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
writepid();
if (argc == 3 && strcmp(argv[1], "-d") == 0) {
delim = argv[2];
delimlength = strlen(delim) + 1;
} else {
delim = DELIMITER;
delimlength = sizeof DELIMITER;
}
if (!(dpy = XOpenDisplay(NULL))) { if (!(dpy = XOpenDisplay(NULL))) {
fputs("Error: could not open display.\n", stderr); fputs("Error: could not open display.\n", stderr);
return 1; return 1;
} }
writepid();
setupsignals(); setupsignals();
statusloop(); statusloop();
cleanup(); cleanup();