diff options
author | Philip Wittamore <philip@wittamore.com> | 2025-06-08 22:00:43 +0200 |
---|---|---|
committer | Philip Wittamore <philip@wittamore.com> | 2025-06-08 22:00:43 +0200 |
commit | 81757c235ff8e112b4baabdd1ff23409426e9c98 (patch) | |
tree | ef213566ac3c17bf3d7795b0597f254791bd219e /src/status.c | |
download | dwmblocks-async-81757c235ff8e112b4baabdd1ff23409426e9c98.tar.gz dwmblocks-async-81757c235ff8e112b4baabdd1ff23409426e9c98.tar.bz2 dwmblocks-async-81757c235ff8e112b4baabdd1ff23409426e9c98.zip |
update
Diffstat (limited to 'src/status.c')
-rw-r--r-- | src/status.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/status.c b/src/status.c new file mode 100644 index 0000000..cf0911a --- /dev/null +++ b/src/status.c @@ -0,0 +1,78 @@ +#include "status.h" + +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +#include "block.h" +#include "config.h" +#include "util.h" +#include "x11.h" + +static bool has_status_changed(const status *const status) { + return strcmp(status->current, status->previous) != 0; +} + +status status_new(const block *const blocks, + const unsigned short block_count) { + status status = { + .current = {[0] = '\0'}, + .previous = {[0] = '\0'}, + + .blocks = blocks, + .block_count = block_count, + }; + + return status; +} + +bool status_update(status *const status) { + (void)strncpy(status->previous, status->current, LEN(status->current)); + status->current[0] = '\0'; + + for (unsigned short i = 0; i < status->block_count; ++i) { + const block *const block = &status->blocks[i]; + + if (strlen(block->output) > 0) { +#if LEADING_DELIMITER + (void)strncat(status->current, DELIMITER, LEN(DELIMITER)); +#else + if (status->current[0] != '\0') { + (void)strncat(status->current, DELIMITER, LEN(DELIMITER)); + } +#endif + +#if CLICKABLE_BLOCKS + if (block->signal > 0) { + const char signal[] = {(char)block->signal, '\0'}; + (void)strncat(status->current, signal, LEN(signal)); + } +#endif + + (void)strncat(status->current, block->icon, LEN(block->output)); + (void)strncat(status->current, block->output, LEN(block->output)); + } + } + +#if TRAILING_DELIMITER + if (status->current[0] != '\0') { + (void)strncat(status->current, DELIMITER, LEN(DELIMITER)); + } +#endif + + return has_status_changed(status); +} + +int status_write(const status *const status, const bool is_debug_mode, + x11_connection *const connection) { + if (is_debug_mode) { + (void)printf("%s\n", status->current); + return 0; + } + + if (x11_set_root_name(connection, status->current) != 0) { + return 1; + } + + return 0; +} |