From 3d55e4b10ffcb11378b0f2770b91358b0eef948a Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Sun, 17 Oct 2021 07:28:15 +0530 Subject: [PATCH] Make block clickability optional --- README.md | 26 ++++++++++++++++++-------- config.h | 1 + main.c | 18 ++++++++++++++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8cd1444..46c78c0 100644 --- a/README.md +++ b/README.md @@ -84,32 +84,42 @@ static Block blocks[] = { } ``` -Apart from that you can also modify the block delimiters and width of each block as shown. +Apart from that you can also modify the following parameters to suit your needs. ```c // Maximum possible length of output from block, expressed in number of characters. #define CMDLENGTH 50 +// The status bar's delimiter which appears in between each block. #define DELIMITER " " -// Adds a trailing delimiter to the statusbar, useful for powerline +// Adds a trailing delimiter to the statusbar, useful for powerline. #define TRAILING_DELIMITER + +// Enable clickability for blocks. Needs `dwm` to be patched appropriately. +// See the "Clickable blocks" section below. +#define CLICKABLE_BLOCKS ``` ### Signalling changes -Most statusbars constantly rerun every script every several seconds to update. This is an option here, but a superior choice is giving your module a signal that you can signal to it to update on a relevant event, rather than having it rerun idly. +Most statusbars constantly rerun every script every several seconds to update. This is an option here, but a superior choice is giving your block a signal that you can signal to it to update on a relevant event, rather than having it rerun idly. -For example, the volume module has the update signal 5 by default. Thus, running `pkill -RTMIN+5 dwmblocks` will update it. +For example, the volume block has the update signal 5 by default. Thus, running `pkill -RTMIN+5 dwmblocks` will update it. You can also run `kill -39 $(pidof dwmblocks)` which will have the same effect, but is faster. Just add 34 to your typical signal number. -My volume module *never* updates on its own, instead I have this command run along side my volume shortcuts in `dwm` to only update it when relevant. +My volume block *never* updates on its own, instead I have this command run along side my volume shortcuts in `dwm` to only update it when relevant. -Note that all modules must have different signal numbers. +Note that all blocks must have different signal numbers. -### Clickable modules +### Clickable blocks Like `i3blocks`, this build allows you to build in additional actions into your scripts in response to click events. You can check out [my statusbar scripts](https://github.com/UtkarshVerma/dotfiles/tree/main/.local/bin/statusbar) as references for using the `$BLOCK_BUTTON` variable. -For this feature to work, you need `dwm` to be patched with [statuscmd](https://dwm.suckless.org/patches/statuscmd/). +To use this feature, define the `CLICKABLE_BLOCKS` feature macro in your `config.h`. +```c +#define CLICKABLE_BLOCKS +``` + +Apart from that, you need `dwm` to be patched with [statuscmd](https://dwm.suckless.org/patches/statuscmd/). Because `dwmblocks-async` creates a child process, it messes up the way the original `statuscmd` patch gets the PID of statusbar. It is necessary to modify the following lines in the definition of `getstatusbarpid()`. diff --git a/config.h b/config.h index b7e8dc2..5facba9 100644 --- a/config.h +++ b/config.h @@ -1,6 +1,7 @@ #define CMDLENGTH 50 #define DELIMITER "<" #define TRAILING_DELIMITER +#define CLICKABLE_BLOCKS const Block blocks[] = { BLOCK("sb-mail", 1800, 17) diff --git a/main.c b/main.c index 3403174..3b4f038 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,7 @@ #define POLL_INTERVAL 50 #define LEN(arr) (sizeof(arr) / sizeof(arr[0])) -#define BLOCK(cmd, interval, signal) {"echo \"_$(" cmd ")\"", interval, signal}, +#define BLOCK(cmd, interval, signal) {"echo \"$(" cmd ")\"", interval, signal}, typedef struct { const char* command; const unsigned int interval; @@ -20,11 +20,19 @@ typedef struct { } Block; #include "config.h" +#ifdef CLICKABLE_BLOCKS +#undef CLICKABLE_BLOCKS +#define CLICKABLE_BLOCKS 1 +#else +#undef CLICKABLE_BLOCKS +#define CLICKABLE_BLOCKS 0 +#endif + static Display* dpy; static int screen; static Window root; static unsigned short int statusContinue = 1; -static char outputs[LEN(blocks)][CMDLENGTH + 2]; +static char outputs[LEN(blocks)][CMDLENGTH + 1 + CLICKABLE_BLOCKS]; static char statusBar[2][LEN(blocks) * ((LEN(outputs[0]) - 1) + (LEN(DELIMITER) - 1)) + 1]; static struct epoll_event event, events[LEN(blocks) + 2]; static int pipes[LEN(blocks)][2]; @@ -93,17 +101,19 @@ void updateBlock(int i) { char ch; while (read(pipes[i][0], &ch, 1) == 1 && ch != '\n') ; - } else if (bytesRead == 2) { + } else if (bytesRead == 1) { output[0] = '\0'; return; } +#if CLICKABLE_BLOCKS if (blocks[i].signal > 0) { output[0] = blocks[i].signal; output++; } +#endif - strcpy(output, buffer + 1); + strcpy(output, buffer); } void debug() {