From bc84d094cd26d7c91f9fbf7e79ce223917171b2c Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Tue, 24 Oct 2023 08:44:31 +0530 Subject: [PATCH] Rewrite the entire code-base The following changes are focused upon: - Modularity - Doing away with globals - No heap allocations - Better command line interface - Switch from Xlib to XCB - More verbose type definitions - Implement a single-file config by utilising X-macros --- .clang-format | 6 +- .clangd | 11 +- .gitignore | 2 +- Makefile | 30 ++-- README.md | 27 ++-- config.c | 19 --- config.h | 31 +++- inc/bar.h | 15 -- inc/block.h | 17 --- inc/util.h | 8 - inc/x11.h | 5 - include/block.h | 24 +++ include/cli.h | 10 ++ include/main.h | 14 ++ include/signal-handler.h | 22 +++ include/status.h | 25 ++++ include/timer.h | 14 ++ include/util.h | 22 +++ include/watcher.h | 22 +++ include/x11.h | 10 ++ src/bar.c | 62 -------- src/block.c | 161 ++++++++++++++------ src/cli.c | 30 ++++ src/main.c | 313 +++++++++++++++++++++------------------ src/signal-handler.c | 119 +++++++++++++++ src/status.c | 74 +++++++++ src/timer.c | 56 +++++++ src/util.c | 61 ++++---- src/watcher.c | 53 +++++++ src/x11.c | 51 +++++-- 30 files changed, 918 insertions(+), 396 deletions(-) delete mode 100644 config.c delete mode 100644 inc/bar.h delete mode 100644 inc/block.h delete mode 100644 inc/util.h delete mode 100644 inc/x11.h create mode 100644 include/block.h create mode 100644 include/cli.h create mode 100644 include/main.h create mode 100644 include/signal-handler.h create mode 100644 include/status.h create mode 100644 include/timer.h create mode 100644 include/util.h create mode 100644 include/watcher.h create mode 100644 include/x11.h delete mode 100644 src/bar.c create mode 100644 src/cli.c create mode 100644 src/signal-handler.c create mode 100644 src/status.c create mode 100644 src/timer.c create mode 100644 src/watcher.c diff --git a/.clang-format b/.clang-format index d9be3e2..299675f 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,8 @@ BasedOnStyle: Google IndentWidth: 4 +InsertBraces: true ColumnLimit: 79 -AlignArrayOfStructures: Left -AlignConsecutiveMacros: true +AlignConsecutiveMacros: Consecutive AllowShortFunctionsOnASingleLine: None +AllowShortLoopsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: Never diff --git a/.clangd b/.clangd index 6c32dd2..d5ba524 100644 --- a/.clangd +++ b/.clangd @@ -1,6 +1,5 @@ -CompileFlags: - Add: - - "-I." - - "-I./inc" - - "-I.." - - "-I../inc" +Diagnostics: + UnusedIncludes: Strict + MissingIncludes: Strict + Includes: + IgnoreHeader: bits/getopt_core.h diff --git a/.gitignore b/.gitignore index 9056e50..902f3c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ build/ .cache/ -dwmblocks +compile_commands.json diff --git a/Makefile b/Makefile index 6fab7cd..930e4b0 100644 --- a/Makefile +++ b/Makefile @@ -3,18 +3,20 @@ BIN := dwmblocks BUILD_DIR := build SRC_DIR := src -INC_DIR := inc +INC_DIR := include VERBOSE := 0 +LIBS := xcb-atom PREFIX := /usr/local -CFLAGS := -Wall -Wextra -Ofast -I. -I$(INC_DIR) -CFLAGS += -Wall -Wextra -Wno-missing-field-initializers -LDLIBS := -lX11 +CFLAGS := -Ofast -I. -I$(INC_DIR) +CFLAGS += -DBINARY=\"$(BIN)\" -D_POSIX_C_SOURCE=200809L +CFLAGS += -Wall -Wpedantic -Wextra -Wswitch-enum +CFLAGS += $(shell pkg-config --cflags $(LIBS)) +LDLIBS := $(shell pkg-config --libs $(LIBS)) -VPATH := $(SRC_DIR) -OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(wildcard $(SRC_DIR)/*.c)) -OBJS += $(patsubst %.c,$(BUILD_DIR)/%.o,$(wildcard *.c)) +SRCS := $(wildcard $(SRC_DIR)/*.c) +OBJS := $(subst $(SRC_DIR)/,$(BUILD_DIR)/,$(SRCS:.c=.o)) INSTALL_DIR := $(DESTDIR)$(PREFIX)/bin @@ -26,17 +28,15 @@ endif all: $(BUILD_DIR)/$(BIN) -$(BUILD_DIR)/$(BIN): $(OBJS) - $(PRINTF) "LD" $@ - $Q$(LINK.o) $^ $(LDLIBS) -o $@ - -$(BUILD_DIR)/%.o: %.c config.h | $(BUILD_DIR) +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c config.h + $Qmkdir -p $(@D) $(PRINTF) "CC" $@ $Q$(COMPILE.c) -o $@ $< -$(BUILD_DIR): - $(PRINTF) "MKDIR" $@ - $Qmkdir -p $@ + +$(BUILD_DIR)/$(BIN): $(OBJS) + $(PRINTF) "LD" $@ + $Q$(LINK.o) $^ $(LDLIBS) -o $@ clean: $(PRINTF) "CLEAN" $(BUILD_DIR) diff --git a/README.md b/README.md index 2006457..674e3e3 100644 --- a/README.md +++ b/README.md @@ -85,15 +85,14 @@ dwmblocks & ### Modifying the blocks -You can define your status bar blocks in `config.c`: +You can define your status bar blocks in `config.h`: ```c -Block blocks[] = { +#define BLOCKS(X) \ ... - {"volume", 0, 5}, - {"date", 1800, 1}, + X("volume", 0, 5), \ + X("date", 1800, 1), \ ... -} ``` Each block has the following properties: @@ -107,17 +106,21 @@ Each block has the following properties: Apart from defining the blocks, features can be toggled through `config.h`: ```c -// Maximum possible length of output from block, expressed in number of characters. -#define CMDLENGTH 50 +// String used to delimit block outputs in the status. +#define DELIMITER " " -// The status bar's delimiter that appears in between each block. -#define DELIMITER " " +// Maximum number of Unicode characters that a block can output. +#define MAX_BLOCK_OUTPUT_LENGTH 45 -// Adds a leading delimiter to the status bar, useful for powerline. -#define LEADING_DELIMITER 1 -// Enable clickability for blocks. See the "Clickable blocks" section below. +// Control whether blocks are clickable. #define CLICKABLE_BLOCKS 1 + +// Control whether a leading delimiter should be prepended to the status. +#define LEADING_DELIMITER 0 + +// Control whether a trailing delimiter should be appended to the status. +#define TRAILING_DELIMITER 0 ``` ### Signalling changes diff --git a/config.c b/config.c deleted file mode 100644 index 991bae3..0000000 --- a/config.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "config.h" - -#include "block.h" -#include "util.h" - -Block blocks[] = { - {"sb-mail", 600, 1 }, - {"sb-music", 0, 2 }, - {"sb-disk", 1800, 3 }, - {"sb-memory", 10, 4 }, - {"sb-loadavg", 5, 5 }, - {"sb-mic", 0, 6 }, - {"sb-record", 0, 7 }, - {"sb-volume", 0, 8 }, - {"sb-battery", 5, 9 }, - {"sb-date", 1, 10}, -}; - -const unsigned short blockCount = LEN(blocks); diff --git a/config.h b/config.h index 235ee71..4e619d8 100644 --- a/config.h +++ b/config.h @@ -1,6 +1,29 @@ #pragma once -#define CLICKABLE_BLOCKS 1 // Enable clickability for blocks -#define CMDLENGTH 45 // Trim block output to this length -#define DELIMITER " " // Delimiter string used to separate blocks -#define LEADING_DELIMITER 0 // Whether a leading separator should be used +// String used to delimit block outputs in the status. +#define DELIMITER " " + +// Maximum number of Unicode characters that a block can output. +#define MAX_BLOCK_OUTPUT_LENGTH 45 + +// Control whether blocks are clickable. +#define CLICKABLE_BLOCKS 1 + +// Control whether a leading delimiter should be prepended to the status. +#define LEADING_DELIMITER 0 + +// Control whether a trailing delimiter should be appended to the status. +#define TRAILING_DELIMITER 0 + +// Define blocks for the status feed as X(cmd, interval, signal). +#define BLOCKS(X) \ + X("sb-mail", 600, 1) \ + X("sb-music", 0, 2) \ + X("sb-disk", 1800, 3) \ + X("sb-memory", 10, 4) \ + X("sb-loadavg", 5, 5) \ + X("sb-mic", 0, 6) \ + X("sb-record", 0, 7) \ + X("sb-volume", 0, 8) \ + X("sb-battery", 5, 9) \ + X("sb-date", 1, 10) diff --git a/inc/bar.h b/inc/bar.h deleted file mode 100644 index 377886e..0000000 --- a/inc/bar.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "block.h" -#include "config.h" -#include "util.h" - -typedef struct { - char *current; - char *previous; -} BarStatus; - -extern unsigned short debugMode; - -void initStatus(BarStatus *); -void freeStatus(BarStatus *); -void writeStatus(BarStatus *); diff --git a/inc/block.h b/inc/block.h deleted file mode 100644 index f0d264c..0000000 --- a/inc/block.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "config.h" - -typedef struct { - const char *command; - const unsigned int interval; - const unsigned int signal; - int pipe[2]; - char output[CMDLENGTH * 4 + 1]; -} Block; - -extern Block blocks[]; -extern const unsigned short blockCount; - -void execBlock(const Block *, const char *); -void execBlocks(unsigned int); -void updateBlock(Block *); diff --git a/inc/util.h b/inc/util.h deleted file mode 100644 index bfc030d..0000000 --- a/inc/util.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#define LEN(arr) (sizeof(arr) / sizeof(arr[0])) -#define MAX(a, b) (a > b ? a : b) - -int gcd(int, int); -void closePipe(int[2]); -void trimUTF8(char*, unsigned int); diff --git a/inc/x11.h b/inc/x11.h deleted file mode 100644 index abb4f0f..0000000 --- a/inc/x11.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -int setupX(); -int closeX(); -void setXRootName(char *); diff --git a/include/block.h b/include/block.h new file mode 100644 index 0000000..88e1956 --- /dev/null +++ b/include/block.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +#include "config.h" +#include "util.h" + +typedef struct { + const char *const command; + const unsigned int interval; + const int signal; + + int pipe[PIPE_FD_COUNT]; + char output[MAX_BLOCK_OUTPUT_LENGTH * UTF8_MAX_BYTE_COUNT + 1]; + pid_t fork_pid; +} block; + +int block_init(block *const block); +int block_deinit(block *const block); +int block_execute(block *const block, const uint8_t button); +int block_update(block *const block); +bool block_must_run(const block *const block, const unsigned int time); diff --git a/include/cli.h b/include/cli.h new file mode 100644 index 0000000..7cb218d --- /dev/null +++ b/include/cli.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +typedef struct { + bool is_debug_mode; +} cli_arguments; + +int cli_init(cli_arguments* const args, const char* const argv[], + const int argc); diff --git a/include/main.h b/include/main.h new file mode 100644 index 0000000..c6ad61b --- /dev/null +++ b/include/main.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include "block.h" +#include "config.h" +#include "util.h" + +// Utilise C's adjacent string concatenation to count the number of blocks. +#define X(...) "." +extern block blocks[LEN(BLOCKS(X)) - 1]; +#undef X + +#define REFRESH_SIGNAL SIGUSR1 diff --git a/include/signal-handler.h b/include/signal-handler.h new file mode 100644 index 0000000..d397f2b --- /dev/null +++ b/include/signal-handler.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +#include "timer.h" + +typedef sigset_t signal_set; +typedef int (*signal_refresh_callback)(void); +typedef int (*signal_timer_callback)(timer* const timer); + +typedef struct { + int fd; + const signal_refresh_callback refresh_callback; + const signal_timer_callback timer_callback; +} signal_handler; + +signal_handler signal_handler_new( + const signal_refresh_callback refresh_callback, + const signal_timer_callback timer_callback); +int signal_handler_init(signal_handler* const handler); +int signal_handler_deinit(signal_handler* const handler); +int signal_handler_process(signal_handler* const handler, timer* const timer); diff --git a/include/status.h b/include/status.h new file mode 100644 index 0000000..1d22388 --- /dev/null +++ b/include/status.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include "block.h" +#include "config.h" +#include "main.h" +#include "util.h" +#include "x11.h" + +typedef struct { +#define STATUS_LENGTH \ + ((LEN(blocks) * (MEMBER_LENGTH(block, output) - 1) + CLICKABLE_BLOCKS) + \ + (LEN(blocks) - 1 + LEADING_DELIMITER + TRAILING_DELIMITER) * \ + (LEN(DELIMITER) - 1) + \ + 1) + char current[STATUS_LENGTH]; + char previous[STATUS_LENGTH]; +#undef STATUS_LENGTH +} status; + +status status_new(void); +bool status_update(status* const status); +int status_write(const status* const status, const bool is_debug_mode, + x11_connection* const connection); diff --git a/include/timer.h b/include/timer.h new file mode 100644 index 0000000..9f0abf7 --- /dev/null +++ b/include/timer.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#define TIMER_SIGNAL SIGALRM + +typedef struct { + unsigned int time; + const unsigned int tick; + const unsigned int reset_value; +} timer; + +timer timer_new(void); +int timer_arm(timer *const timer); diff --git a/include/util.h b/include/util.h new file mode 100644 index 0000000..692d1ce --- /dev/null +++ b/include/util.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define LEN(arr) (sizeof(arr) / sizeof(arr[0])) +#define BIT(n) (1 << (n)) +#define MEMBER_SIZE(type, member) sizeof(((type*)NULL)->member) +#define MEMBER_LENGTH(type, member) \ + (MEMBER_SIZE(type, member) / MEMBER_SIZE(type, member[0])) + +#define UTF8_MAX_BYTE_COUNT 4 + +enum pipe_fd_index { + READ_END, + WRITE_END, + PIPE_FD_COUNT, +}; + +unsigned int gcd(unsigned int a, unsigned int b); +size_t truncate_utf8_string(char* const buffer, const size_t size, + const size_t char_limit); diff --git a/include/watcher.h b/include/watcher.h new file mode 100644 index 0000000..23bc82d --- /dev/null +++ b/include/watcher.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include "main.h" +#include "util.h" + +typedef enum { + SIGNAL_FD = LEN(blocks), + WATCHER_FD_COUNT, +} watcher_fd_index; + +typedef struct pollfd watcher_fd; + +typedef struct { + watcher_fd fds[WATCHER_FD_COUNT]; +} watcher; + +int watcher_init(watcher *const watcher, const int signal_fd); +int watcher_poll(watcher *const watcher, const int timeout_ms); +bool watcher_fd_is_readable(const watcher_fd *const watcher_fd); diff --git a/include/x11.h b/include/x11.h new file mode 100644 index 0000000..6110876 --- /dev/null +++ b/include/x11.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +typedef xcb_connection_t x11_connection; + +x11_connection* x11_connection_open(void); +void x11_connection_close(x11_connection* const connection); +int x11_set_root_name(x11_connection* const connection, + const char* const name); diff --git a/src/bar.c b/src/bar.c deleted file mode 100644 index 9f8261c..0000000 --- a/src/bar.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "bar.h" - -#include -#include -#include - -#include "block.h" -#include "x11.h" - -void initStatus(BarStatus *status) { - const unsigned int statusLength = - (blockCount * (LEN(blocks[0].output) - 1)) + - (blockCount - 1 + LEADING_DELIMITER) * (LEN(DELIMITER) - 1); - - status->current = (char *)malloc(statusLength); - status->previous = (char *)malloc(statusLength); - status->current[0] = '\0'; - status->previous[0] = '\0'; -} - -void freeStatus(BarStatus *status) { - free(status->current); - free(status->previous); -} - -int updateStatus(BarStatus *status) { - strcpy(status->previous, status->current); - status->current[0] = '\0'; - - for (int i = 0; i < blockCount; i++) { - Block *block = blocks + i; - - if (strlen(block->output)) { -#if LEADING_DELIMITER - strcat(status->current, DELIMITER); -#else - if (status->current[0]) strcat(status->current, DELIMITER); -#endif - -#if CLICKABLE_BLOCKS - if (!debugMode && block->signal) { - char signal[] = {block->signal, '\0'}; - strcat(status->current, signal); - } -#endif - - strcat(status->current, block->output); - } - } - return strcmp(status->current, status->previous); -} - -void writeStatus(BarStatus *status) { - // Only write out if status has changed - if (!updateStatus(status)) return; - - if (debugMode) { - printf("%s\n", status->current); - return; - } - setXRootName(status->current); -} diff --git a/src/block.c b/src/block.c index ae50ea5..7e2a809 100644 --- a/src/block.c +++ b/src/block.c @@ -1,72 +1,147 @@ #include "block.h" -#define _GNU_SOURCE +#include +#include +#include +#include #include #include #include +#include +#include #include +#include "config.h" #include "util.h" -static int execLock = 0; +int block_init(block *const block) { + if (pipe(block->pipe) != 0) { + (void)fprintf(stderr, + "error: could not create a pipe for \"%s\" block\n", + block->command); + return 1; + } -void execBlock(const Block *block, const char *button) { - unsigned short i = block - blocks; + block->fork_pid = -1; - // Ensure only one child process exists per block at an instance - if (execLock & 1 << i) return; - // Lock execution of block until current instance finishes execution - execLock |= 1 << i; + return 0; +} - if (fork() == 0) { - close(block->pipe[0]); - dup2(block->pipe[1], STDOUT_FILENO); - close(block->pipe[1]); +int block_deinit(block *const block) { + int status = close(block->pipe[READ_END]); + status |= close(block->pipe[WRITE_END]); + if (status != 0) { + (void)fprintf(stderr, "error: could not close \"%s\" block's pipe\n", + block->command); + return 1; + } - if (button) setenv("BLOCK_BUTTON", button, 1); + return 0; +} - FILE *file = popen(block->command, "r"); - if (!file) { - printf("\n"); +int block_execute(block *const block, const uint8_t button) { + // Ensure only one child process exists per block at an instance. + if (block->fork_pid != -1) { + return 0; + } + + block->fork_pid = fork(); + if (block->fork_pid == -1) { + (void)fprintf( + stderr, "error: could not create a subprocess for \"%s\" block\n", + block->command); + return 1; + } + + if (block->fork_pid == 0) { + const int write_fd = block->pipe[WRITE_END]; + int status = close(block->pipe[READ_END]); + + if (button != 0) { + char button_str[4]; + (void)snprintf(button_str, LEN(button_str), "%hhu", button); + status |= setenv("BLOCK_BUTTON", button_str, 1); + } + + const char null = '\0'; + if (status != 0) { + (void)write(write_fd, &null, sizeof(null)); exit(EXIT_FAILURE); } - // Buffer will hold both '\n' and '\0' - char buffer[LEN(block->output) + 1]; - if (fgets(buffer, LEN(buffer), file) == NULL) { - // Send an empty line in case of no output - printf("\n"); - exit(EXIT_SUCCESS); + FILE *const file = popen(block->command, "r"); + if (file == NULL) { + (void)write(write_fd, &null, sizeof(null)); + exit(EXIT_FAILURE); } - pclose(file); - // Trim to the max possible UTF-8 capacity - trimUTF8(buffer, LEN(buffer)); + // Ensure null-termination since fgets() will leave buffer untouched on + // no output. + char buffer[LEN(block->output)] = {[0] = null}; + (void)fgets(buffer, LEN(buffer), file); + + // Remove trailing newlines. + const size_t length = strcspn(buffer, "\n"); + buffer[length] = null; + + // Exit if command execution failed or if file could not be closed. + if (pclose(file) != 0) { + (void)write(write_fd, &null, sizeof(null)); + exit(EXIT_FAILURE); + } + + const size_t output_size = + truncate_utf8_string(buffer, LEN(buffer), MAX_BLOCK_OUTPUT_LENGTH); + (void)write(write_fd, buffer, output_size); - printf("%s\n", buffer); exit(EXIT_SUCCESS); } + + return 0; } -void execBlocks(unsigned int time) { - for (int i = 0; i < blockCount; i++) { - const Block *block = blocks + i; - if (time == 0 || - (block->interval != 0 && time % block->interval == 0)) { - execBlock(block, NULL); - } - } -} - -void updateBlock(Block *block) { +int block_update(block *const block) { char buffer[LEN(block->output)]; - int bytesRead = read(block->pipe[0], buffer, LEN(buffer)); - // String from pipe will always end with '\n' - buffer[bytesRead - 1] = '\0'; + const ssize_t bytes_read = + read(block->pipe[READ_END], buffer, LEN(buffer)); + if (bytes_read == -1) { + (void)fprintf(stderr, + "error: could not fetch output of \"%s\" block\n", + block->command); + return 2; + } - strcpy(block->output, buffer); + // Collect exit-status of the subprocess to avoid zombification. + int fork_status = 0; + if (waitpid(block->fork_pid, &fork_status, 0) == -1) { + (void)fprintf(stderr, + "error: could not obtain exit status for \"%s\" block\n", + block->command); + return 2; + } + block->fork_pid = -1; - // Remove execution lock for the current block - execLock &= ~(1 << (block - blocks)); + if (fork_status != 0) { + (void)fprintf(stderr, + "error: \"%s\" block exited with non-zero status\n", + block->command); + return 1; + } + + (void)strcpy(block->output, buffer); + + return 0; +} + +bool block_must_run(const block *const block, const unsigned int time) { + if (time == 0) { + return true; + } + + if (block->interval == 0) { + return false; + } + + return time % block->interval == 0; } diff --git a/src/cli.c b/src/cli.c new file mode 100644 index 0000000..5aa7df3 --- /dev/null +++ b/src/cli.c @@ -0,0 +1,30 @@ +#include "cli.h" + +#include +#include +#include + +int cli_init(cli_arguments *const args, const char *const argv[], + const int argc) { + args->is_debug_mode = false; + + int opt = -1; + opterr = 0; // Suppress getopt's built-in invalid opt message + while ((opt = getopt(argc, (char *const *)argv, "dh")) != -1) { + switch (opt) { + case 'd': + args->is_debug_mode = true; + break; + case 'h': + // fall through + case '?': + (void)fprintf(stderr, "error: unknown option `-%c'\n", optopt); + // fall through + default: + (void)fprintf(stderr, "usage: %s [-d]\n", BINARY); + return 1; + } + } + + return 0; +} diff --git a/src/main.c b/src/main.c index 8b87b63..629effd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,157 +1,180 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include +#include "main.h" + +#include +#include -#include "bar.h" #include "block.h" +#include "cli.h" +#include "config.h" +#include "signal-handler.h" +#include "status.h" +#include "timer.h" #include "util.h" +#include "watcher.h" #include "x11.h" -static unsigned short statusContinue = 1; -unsigned short debugMode = 0; -static int epollFD, signalFD; -static unsigned int timerTick = 0, maxInterval = 1; +#define BLOCK(cmd, period, sig) \ + { \ + .command = cmd, \ + .interval = period, \ + .signal = sig, \ + }, -void signalHandler() { - struct signalfd_siginfo info; - read(signalFD, &info, sizeof(info)); - unsigned int signal = info.ssi_signo; +block blocks[] = {BLOCKS(BLOCK)}; +#undef BLOCK - static unsigned int timer = 0; - switch (signal) { - case SIGALRM: - // Schedule the next timer event and execute blocks - alarm(timerTick); - execBlocks(timer); - - // Wrap `timer` to the interval [1, `maxInterval`] - timer = (timer + timerTick - 1) % maxInterval + 1; - return; - case SIGUSR1: - // Update all blocks on receiving SIGUSR1 - execBlocks(0); - return; - } - - for (int j = 0; j < blockCount; j++) { - const Block *block = blocks + j; - if (block->signal == signal - SIGRTMIN) { - char button[4]; // value can't be more than 255; - sprintf(button, "%d", info.ssi_int & 0xff); - execBlock(block, button); - break; +static int init_blocks(void) { + for (unsigned short i = 0; i < LEN(blocks); ++i) { + block *const block = &blocks[i]; + if (block_init(block) != 0) { + return 1; } } -} - -void termHandler() { - statusContinue = 0; -} - -void setupSignals() { - sigset_t handledSignals; - sigemptyset(&handledSignals); - sigaddset(&handledSignals, SIGUSR1); - sigaddset(&handledSignals, SIGALRM); - - // Append all block signals to `handledSignals` - for (int i = 0; i < blockCount; i++) - if (blocks[i].signal > 0) - sigaddset(&handledSignals, SIGRTMIN + blocks[i].signal); - - // Create a signal file descriptor for epoll to watch - signalFD = signalfd(-1, &handledSignals, 0); - - // Block all realtime and handled signals - for (int i = SIGRTMIN; i <= SIGRTMAX; i++) sigaddset(&handledSignals, i); - sigprocmask(SIG_BLOCK, &handledSignals, NULL); - - // Handle termination signals - signal(SIGINT, termHandler); - signal(SIGTERM, termHandler); - - // Avoid zombie subprocesses - struct sigaction signalAction; - signalAction.sa_handler = SIG_DFL; - sigemptyset(&signalAction.sa_mask); - signalAction.sa_flags = SA_NOCLDWAIT; - sigaction(SIGCHLD, &signalAction, 0); -} - -void statusLoop() { - // Update all blocks initially - raise(SIGALRM); - - BarStatus status; - initStatus(&status); - struct epoll_event events[blockCount + 1]; - while (statusContinue) { - int eventCount = epoll_wait(epollFD, events, LEN(events), 100); - for (int i = 0; i < eventCount; i++) { - unsigned short id = events[i].data.u32; - if (id < blockCount) { - updateBlock(blocks + id); - } else { - signalHandler(); - } - } - - if (eventCount != -1) writeStatus(&status); - } - freeStatus(&status); -} - -void init() { - epollFD = epoll_create(blockCount); - struct epoll_event event = { - .events = EPOLLIN, - }; - - for (int i = 0; i < blockCount; i++) { - // Append each block's pipe's read end to `epollFD` - pipe(blocks[i].pipe); - event.data.u32 = i; - epoll_ctl(epollFD, EPOLL_CTL_ADD, blocks[i].pipe[0], &event); - - // Calculate the max interval and tick size for the timer - if (blocks[i].interval) { - maxInterval = MAX(blocks[i].interval, maxInterval); - timerTick = gcd(blocks[i].interval, timerTick); - } - } - - setupSignals(); - - // Watch signal file descriptor as well - event.data.u32 = blockCount; - epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event); -} - -int main(const int argc, const char *argv[]) { - if (setupX()) { - fprintf(stderr, "%s\n", "dwmblocks: Failed to open display"); - return 1; - } - - for (int i = 0; i < argc; i++) { - if (!strcmp("-d", argv[i])) { - debugMode = 1; - break; - } - } - - init(); - statusLoop(); - - if (closeX()) - fprintf(stderr, "%s\n", "dwmblocks: Failed to close display"); - - close(epollFD); - close(signalFD); - for (int i = 0; i < blockCount; i++) closePipe(blocks[i].pipe); return 0; } + +static int deinit_blocks(void) { + for (unsigned short i = 0; i < LEN(blocks); ++i) { + block *const block = &blocks[i]; + if (block_deinit(block) != 0) { + return 1; + } + } + + return 0; +} + +static int execute_blocks(const unsigned int time) { + for (unsigned short i = 0; i < LEN(blocks); ++i) { + block *const block = &blocks[i]; + if (!block_must_run(block, time)) { + continue; + } + + if (block_execute(&blocks[i], 0) != 0) { + return 1; + } + } + + return 0; +} + +static int trigger_event(timer *const timer) { + if (execute_blocks(timer->time) != 0) { + return 1; + } + + if (timer_arm(timer) != 0) { + return 1; + } + + return 0; +} + +static int refresh_callback(void) { + if (execute_blocks(0) != 0) { + return 1; + } + + return 0; +} + +static int event_loop(const bool is_debug_mode, + x11_connection *const connection, + signal_handler *const signal_handler) { + timer timer = timer_new(); + + // Kickstart the event loop with an initial execution. + if (trigger_event(&timer) != 0) { + return 1; + } + + watcher watcher; + if (watcher_init(&watcher, signal_handler->fd) != 0) { + return 1; + } + + status status = status_new(); + bool is_alive = true; + while (is_alive) { + const int event_count = watcher_poll(&watcher, -1); + if (event_count == -1) { + return 1; + } + + int i = 0; + for (unsigned short j = 0; j < WATCHER_FD_COUNT; ++j) { + if (i == event_count) { + break; + } + + const watcher_fd *const watcher_fd = &watcher.fds[j]; + if (!watcher_fd_is_readable(watcher_fd)) { + continue; + } + + ++i; + + if (j == SIGNAL_FD) { + is_alive = signal_handler_process(signal_handler, &timer) == 0; + continue; + } + + block *const block = &blocks[j]; + (void)block_update(block); + } + + const bool has_status_changed = status_update(&status); + if (has_status_changed) { + if (status_write(&status, is_debug_mode, connection) != 0) { + return 1; + } + } + } + + return 0; +} + +int main(const int argc, const char *const argv[]) { + cli_arguments cli_args; + if (cli_init(&cli_args, argv, argc) != 0) { + return 1; + } + + x11_connection *const connection = x11_connection_open(); + if (connection == NULL) { + return 1; + } + + int status = 0; + if (init_blocks() != 0) { + status = 1; + goto x11_close; + } + + signal_handler signal_handler = + signal_handler_new(refresh_callback, trigger_event); + if (signal_handler_init(&signal_handler) != 0) { + status = 1; + goto deinit_blocks; + } + + if (event_loop(cli_args.is_debug_mode, connection, &signal_handler) != 0) { + status = 1; + } + + if (signal_handler_deinit(&signal_handler) != 0) { + status = 1; + } + +deinit_blocks: + if (deinit_blocks() != 0) { + status = 1; + } + +x11_close: + x11_connection_close(connection); + + return status; +} diff --git a/src/signal-handler.c b/src/signal-handler.c new file mode 100644 index 0000000..2a9c1ee --- /dev/null +++ b/src/signal-handler.c @@ -0,0 +1,119 @@ +#include "signal-handler.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "block.h" +#include "main.h" +#include "timer.h" +#include "util.h" + +typedef struct signalfd_siginfo signal_info; + +signal_handler signal_handler_new( + const signal_refresh_callback refresh_callback, + const signal_timer_callback timer_callback) { + signal_handler handler = { + .refresh_callback = refresh_callback, + .timer_callback = timer_callback, + }; + + return handler; +} + +int signal_handler_init(signal_handler *const handler) { + signal_set set; + (void)sigemptyset(&set); + + // Handle user-generated signal for refreshing the status. + (void)sigaddset(&set, REFRESH_SIGNAL); + + // Handle SIGALRM generated by the timer. + (void)sigaddset(&set, TIMER_SIGNAL); + + // Handle termination signals. + (void)sigaddset(&set, SIGINT); + (void)sigaddset(&set, SIGTERM); + + for (unsigned short i = 0; i < LEN(blocks); ++i) { + const block *const block = &blocks[i]; + if (blocks->signal > 0) { + if (sigaddset(&set, SIGRTMIN + block->signal) != 0) { + (void)fprintf( + stderr, + "error: invalid or unsupported signal specified for " + "\"%s\" block\n", + block->command); + return 1; + } + } + } + + // Create a signal file descriptor for epoll to watch. + handler->fd = signalfd(-1, &set, 0); + if (handler->fd == -1) { + (void)fprintf(stderr, + "error: could not create file descriptor for signals\n"); + return 1; + } + + // Block all realtime and handled signals. + for (int i = SIGRTMIN; i <= SIGRTMAX; ++i) { + (void)sigaddset(&set, i); + } + (void)sigprocmask(SIG_BLOCK, &set, NULL); + + return 0; +} + +int signal_handler_deinit(signal_handler *const handler) { + if (close(handler->fd) != 0) { + (void)fprintf(stderr, + "error: could not close signal file descriptor\n"); + } + + return 0; +} + +int signal_handler_process(signal_handler *const handler, timer *const timer) { + signal_info info; + const ssize_t bytes_read = read(handler->fd, &info, sizeof(info)); + if (bytes_read == -1) { + (void)fprintf(stderr, "error: could not read info of incoming signal"); + return 1; + } + + const int signal = (int)info.ssi_signo; + switch (signal) { + case TIMER_SIGNAL: + if (handler->timer_callback(timer) != 0) { + return 1; + } + return 0; + case REFRESH_SIGNAL: + if (handler->refresh_callback() != 0) { + return 1; + } + return 0; + case SIGTERM: + // fall through + case SIGINT: + return 1; + } + + for (unsigned short i = 0; i < LEN(blocks); ++i) { + block *const block = blocks + i; + if (block->signal == signal - SIGRTMIN) { + const uint8_t button = (uint8_t)info.ssi_int; + block_execute(block, button); + break; + } + } + + return 0; +} diff --git a/src/status.c b/src/status.c new file mode 100644 index 0000000..f540f56 --- /dev/null +++ b/src/status.c @@ -0,0 +1,74 @@ +#include "status.h" + +#include +#include +#include + +#include "block.h" +#include "config.h" +#include "main.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(void) { + status status = { + .current = {[0] = '\0'}, + .previous = {[0] = '\0'}, + }; + + return status; +} + +bool status_update(status *const status) { + (void)strcpy(status->previous, status->current); + status->current[0] = '\0'; + + for (unsigned short i = 0; i < LEN(blocks); ++i) { + const block *const block = &blocks[i]; + + if (strlen(block->output) > 0) { +#if LEADING_DELIMITER + (void)strcat(status->current, DELIMITER); +#else + if (status->current[0] != '\0') { + (void)strcat(status->current, DELIMITER); + } +#endif + +#if CLICKABLE_BLOCKS + if (block->signal > 0) { + const char signal[] = {(char)block->signal, '\0'}; + (void)strcat(status->current, signal); + } +#endif + + (void)strcat(status->current, block->output); + } + } + +#if TRAILING_DELIMITER + if (status->current[0] != '\0') { + (void)strcat(status->current, 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; +} diff --git a/src/timer.c b/src/timer.c new file mode 100644 index 0000000..b50b946 --- /dev/null +++ b/src/timer.c @@ -0,0 +1,56 @@ +#include "timer.h" + +#include +#include +#include + +#include "block.h" +#include "main.h" +#include "util.h" + +static unsigned int compute_tick(void) { + unsigned int tick = 0; + + for (unsigned short i = 0; i < LEN(blocks); ++i) { + const block *const block = &blocks[i]; + tick = gcd(block->interval, tick); + } + + return tick; +} + +static unsigned int compute_reset_value(void) { + unsigned int reset_value = 1; + + for (unsigned short i = 0; i < LEN(blocks); ++i) { + const block *const block = &blocks[i]; + reset_value = MAX(block->interval, reset_value); + } + + return reset_value; +} + +timer timer_new(void) { + timer timer = { + .time = 0, + .tick = compute_tick(), + .reset_value = compute_reset_value(), + }; + + return timer; +} + +int timer_arm(timer *const timer) { + errno = 0; + (void)alarm(timer->tick); + + if (errno != 0) { + (void)fprintf(stderr, "error: could not arm timer\n"); + return 1; + } + + // Wrap `time` to the interval [1, reset_value]. + timer->time = (timer->time + timer->tick) % timer->reset_value + 1; + + return 0; +} diff --git a/src/util.c b/src/util.c index 157e626..1648f5a 100644 --- a/src/util.c +++ b/src/util.c @@ -1,41 +1,50 @@ #include "util.h" -#include +#include +#define UTF8_MULTIBYTE_BIT BIT(7) -int gcd(int a, int b) { - int temp; +unsigned int gcd(unsigned int a, unsigned int b) { while (b > 0) { - temp = a % b; + const unsigned int temp = a % b; a = b; b = temp; } + return a; } -void closePipe(int pipe[2]) { - close(pipe[0]); - close(pipe[1]); -} - -void trimUTF8(char* buffer, unsigned int size) { - int length = (size - 1) / 4; - int count = 0, j = 0; - char ch = buffer[j]; - while (ch != '\0' && ch != '\n' && count < length) { - // Skip continuation bytes, if any - int skip = 1; - while ((ch & 0xc0) > 0x80) { - ch <<= 1; - skip++; +size_t truncate_utf8_string(char* const buffer, const size_t size, + const size_t char_limit) { + size_t char_count = 0; + size_t i = 0; + while (char_count < char_limit) { + char ch = buffer[i]; + if (ch == '\0') { + break; } - j += skip; - ch = buffer[j]; - count++; + unsigned short skip = 1; + + // Multibyte unicode character + if ((ch & UTF8_MULTIBYTE_BIT) != 0) { + // Skip continuation bytes. + ch <<= 1; + while ((ch & UTF8_MULTIBYTE_BIT) != 0) { + ch <<= 1; + ++skip; + } + } + + // Avoid buffer overflow. + if (i + skip >= size) { + break; + } + + ++char_count; + i += skip; } - // Trim trailing newline and spaces - buffer[j] = ' '; - while (j >= 0 && buffer[j] == ' ') j--; - buffer[j + 1] = '\0'; + buffer[i] = '\0'; + + return i + 1; } diff --git a/src/watcher.c b/src/watcher.c new file mode 100644 index 0000000..f544089 --- /dev/null +++ b/src/watcher.c @@ -0,0 +1,53 @@ +#include "watcher.h" + +#include +#include +#include +#include + +#include "main.h" +#include "util.h" + +int watcher_init(watcher* const watcher, const int signal_fd) { + if (signal_fd == -1) { + fprintf(stderr, + "error: invalid signal file descriptor passed to watcher\n"); + return 1; + } + + watcher_fd* const fd = &watcher->fds[SIGNAL_FD]; + fd->fd = signal_fd; + fd->events = POLLIN; + + for (unsigned short i = 0; i < LEN(blocks); ++i) { + const int block_fd = blocks[i].pipe[READ_END]; + if (block_fd == -1) { + fprintf( + stderr, + "error: invalid block file descriptors passed to watcher\n"); + return 1; + } + + watcher_fd* const fd = &watcher->fds[i]; + fd->fd = block_fd; + fd->events = POLLIN; + } + + return 0; +} + +int watcher_poll(watcher* watcher, const int timeout_ms) { + const int event_count = poll(watcher->fds, LEN(watcher->fds), timeout_ms); + + // Don't return non-zero status for signal interruptions. + if (event_count == -1 && errno != EINTR) { + (void)fprintf(stderr, "error: watcher could not poll blocks\n"); + return -1; + } + + return event_count; +} + +bool watcher_fd_is_readable(const watcher_fd* const watcher_fd) { + return (watcher_fd->revents & POLLIN) != 0; +} diff --git a/src/x11.c b/src/x11.c index 63c5d14..7335d5e 100644 --- a/src/x11.c +++ b/src/x11.c @@ -1,25 +1,44 @@ #include "x11.h" -#include +#include +#include +#include +#include -static Display *display; -static Window rootWindow; +x11_connection *x11_connection_open(void) { + xcb_connection_t *const connection = xcb_connect(NULL, NULL); + if (xcb_connection_has_error(connection)) { + (void)fprintf(stderr, "error: could not connect to the X server\n"); + return NULL; + } -int setupX() { - display = XOpenDisplay(NULL); - if (!display) { + return connection; +} + +void x11_connection_close(xcb_connection_t *const connection) { + xcb_disconnect(connection); +} + +int x11_set_root_name(x11_connection *const connection, const char *name) { + xcb_screen_t *const screen = + xcb_setup_roots_iterator(xcb_get_setup(connection)).data; + const xcb_window_t root_window = screen->root; + + const unsigned short name_format = 8; + const xcb_void_cookie_t cookie = xcb_change_property( + connection, XCB_PROP_MODE_REPLACE, root_window, XCB_ATOM_WM_NAME, + XCB_ATOM_STRING, name_format, strlen(name), name); + + xcb_generic_error_t *error = xcb_request_check(connection, cookie); + if (error != NULL) { + (void)fprintf(stderr, "error: could not set X root name\n"); + return 1; + } + + if (xcb_flush(connection) <= 0) { + (void)fprintf(stderr, "error: could not flush X output buffer\n"); return 1; } - rootWindow = DefaultRootWindow(display); return 0; } - -int closeX() { - return XCloseDisplay(display); -} - -void setXRootName(char *str) { - XStoreName(display, rootWindow, str); - XFlush(display); -}