Cleanup; further optimization of updatestatus and updateblock

This commit is contained in:
Ashish Kumar Yadav 2021-02-10 21:02:27 +05:30
parent a58b4d6a80
commit bc0f134dda
5 changed files with 58 additions and 40 deletions

View file

@ -8,7 +8,7 @@ X11LIBS := $(shell pkg-config --libs x11)
all: dwmblocks sigdwmblocks xgetrootname 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} ${CC} -o $@ -Wno-missing-field-initializers -Wno-unused-parameter ${CFLAGS} ${X11CFLAGS} $< ${X11LIBS}
sigdwmblocks: sigdwmblocks.c sigdwmblocks: sigdwmblocks.c

View file

@ -9,8 +9,8 @@ signaling, clickability, cursor hinting and color.
# Modifying blocks # Modifying blocks
Blocks are added and removed by editing [blocks.h](blocks.h) file. Read it for Blocks are added and removed by editing [config.h](config.h) file. Some other
more info. 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 > Change the PATH macro defined at the top of the file. It should point to the
> folder where your scripts are saved. > folder where your scripts are saved.

9
block.h Normal file
View file

@ -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;

View file

@ -1,5 +1,12 @@
/* macro for conveniently specifying pathu and pathc below */
#define PATH(name) "/home/ashish/.local/projects/dwmblocks/blocks/"name #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. /* DELIMITERENDCHAR must be less than 32.
* At max, DELIMITERENDCHAR - 1 number of clickable blocks will be allowed. * At max, DELIMITERENDCHAR - 1 number of clickable blocks will be allowed.
* Raw characters larger than DELIMITERENDCHAR and smaller than ' ' in ASCII * Raw characters larger than DELIMITERENDCHAR and smaller than ' ' in ASCII
@ -11,6 +18,12 @@
dwm.c and color codes in your pathu programs. */ dwm.c and color codes in your pathu programs. */
#define DELIMITERENDCHAR 10 #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 /* If interval of a block is set to 0, the block will only be updated once at
startup. startup.
* If interval is set to a negative value, the block will never be updated in * 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}, { PATH("battery.sh"), PATH("battery_button.sh"), 30, 3},
{ NULL } /* just to mark the end of the array */ { 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 };

View file

@ -9,24 +9,12 @@
#include <unistd.h> #include <unistd.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#define CMDOUTLENGTH 50
#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 #define DELIMITERLENGTH sizeof delimiter
typedef struct { #include "config.h"
char *const pathu;
char *const pathc;
const int interval;
const int signal;
char curcmdout[CMDOUTLENGTH + 1];
char prvcmdout[CMDOUTLENGTH + 1];
} Block;
#include "blocks.h"
static void buttonhandler(int sig, siginfo_t *info, void *ucontext); static void buttonhandler(int sig, siginfo_t *info, void *ucontext);
static void cleanup(); static void cleanup();
@ -39,8 +27,8 @@ static void updateblock(Block *block, int sigval);
static void updatestatus(); static void updatestatus();
static void writepid(); static void writepid();
static int dirty;
static char statustext[STATUSLENGTH + DELIMITERLENGTH]; static char statustext[STATUSLENGTH + DELIMITERLENGTH];
static Block *dirtyblock;
static Display *dpy; static Display *dpy;
static sigset_t blocksigmask; static sigset_t blocksigmask;
@ -79,9 +67,9 @@ cleanup()
void void
setroot() setroot()
{ {
if (dirty) { if (dirtyblock) {
updatestatus(); updatestatus();
dirty = 0; dirtyblock = NULL;
XStoreName(dpy, DefaultRootWindow(dpy), statustext); XStoreName(dpy, DefaultRootWindow(dpy), statustext);
XSync(dpy, False); XSync(dpy, False);
} }
@ -230,17 +218,33 @@ updateblock(Block *block, int sigval)
close(fd[1]); close(fd[1]);
do 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); while (rd > 0 && (trd += rd) < CMDOUTLENGTH);
if (rd == -1) { if (rd == -1) {
perror("updateblock - read"); perror("updateblock - read");
exit(1); exit(1);
} }
close(fd[0]); close(fd[0]);
block->curcmdout[block->curcmdout[trd - 1] == '\n' ? --trd : trd] = '\0'; if (trd == 0) {
if (memcmp(block->curcmdout, block->prvcmdout, trd) != 0) { if (block->prvtext[0] != '\0') {
memcpy(block->prvcmdout, block->curcmdout, trd); block->prvtext[0] = '\0';
dirty = 1; 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,20 +254,16 @@ void
updatestatus() updatestatus()
{ {
char *s = statustext; char *s = statustext;
size_t len;
for (Block *block = blocks; block->pathu; block++) { for (Block *block = blocks; block < dirtyblock; block++)
if ((len = strlen(block->curcmdout)) == 0) s += block->len;
continue; for (Block *block = dirtyblock; block->pathu; block++) {
memcpy(s, block->curcmdout, len); memcpy(s, block->curtext, block->len);
s += len; s += block->len;
if (block->pathc)
*(s++) = block->signal;
memcpy(s, delimiter, DELIMITERLENGTH);
s += DELIMITERLENGTH;
} }
if (s != statustext) if (s != statustext)
s -= DELIMITERLENGTH; *(s - DELIMITERLENGTH) = '\0';
else
*s = '\0'; *s = '\0';
} }