refactor: Add clang-tidy checks and simplify codebase

This commit is contained in:
Utkarsh Verma
2023-11-16 09:08:29 +05:30
parent f18100c2ee
commit c166ffe0b4
17 changed files with 130 additions and 78 deletions

View File

@@ -141,7 +141,7 @@ int block_update(block *const block) {
return 1;
}
(void)strcpy(block->output, buffer);
(void)strncpy(block->output, buffer, LEN(buffer));
return 0;
}

View File

@@ -86,46 +86,30 @@ static int event_loop(block *const blocks, const unsigned short block_count,
return 1;
}
watcher watcher = watcher_new(blocks, block_count);
if (watcher_init(&watcher, signal_handler->fd) != 0) {
watcher watcher;
if (watcher_init(&watcher, blocks, block_count, signal_handler->fd) != 0) {
return 1;
}
status status = status_new(blocks, block_count);
bool is_alive = true;
while (is_alive) {
const int event_count = watcher_poll(&watcher, -1);
if (event_count == -1) {
if (watcher_poll(&watcher, -1) != 0) {
return 1;
}
int i = 0;
for (unsigned short j = 0; j < WATCHER_FD_COUNT; ++j) {
if (i == event_count) {
break;
}
if (watcher.got_signal) {
is_alive = signal_handler_process(signal_handler, &timer) == 0;
}
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);
for (unsigned short i = 0; i < watcher.active_block_count; ++i) {
(void)block_update(&blocks[watcher.active_blocks[i]]);
}
const bool has_status_changed = status_update(&status);
if (has_status_changed) {
if (status_write(&status, is_debug_mode, connection) != 0) {
return 1;
}
if (has_status_changed &&
status_write(&status, is_debug_mode, connection) != 0) {
return 1;
}
}

View File

@@ -6,6 +6,7 @@
#include "block.h"
#include "config.h"
#include "util.h"
#include "x11.h"
static bool has_status_changed(const status *const status) {
@@ -26,7 +27,7 @@ status status_new(const block *const blocks,
}
bool status_update(status *const status) {
(void)strcpy(status->previous, status->current);
(void)strncpy(status->previous, status->current, LEN(status->current));
status->current[0] = '\0';
for (unsigned short i = 0; i < status->block_count; ++i) {
@@ -34,27 +35,27 @@ bool status_update(status *const status) {
if (strlen(block->output) > 0) {
#if LEADING_DELIMITER
(void)strcat(status->current, DELIMITER);
(void)strncat(status->current, DELIMITER, LEN(DELIMITER));
#else
if (status->current[0] != '\0') {
(void)strcat(status->current, DELIMITER);
(void)strncat(status->current, DELIMITER, LEN(DELIMITER));
}
#endif
#if CLICKABLE_BLOCKS
if (block->signal > 0) {
const char signal[] = {(char)block->signal, '\0'};
(void)strcat(status->current, signal);
(void)strncat(status->current, signal, LEN(signal));
}
#endif
(void)strcat(status->current, block->output);
(void)strncat(status->current, block->output, LEN(block->output));
}
}
#if TRAILING_DELIMITER
if (status->current[0] != '\0') {
(void)strcat(status->current, DELIMITER);
(void)strcat(status->current, DELIMITER, LEN(DELIMITER));
}
#endif

View File

@@ -24,7 +24,7 @@ size_t truncate_utf8_string(char* const buffer, const size_t size,
unsigned short skip = 1;
// Multibyte unicode character
// Multibyte unicode character.
if ((ch & UTF8_MULTIBYTE_BIT) != 0) {
// Skip continuation bytes.
ch <<= 1;

View File

@@ -8,20 +8,16 @@
#include "block.h"
#include "util.h"
watcher watcher_new(const block* const blocks,
const unsigned short block_count) {
watcher watcher = {
.blocks = blocks,
.block_count = block_count,
};
return watcher;
static bool watcher_fd_is_readable(const watcher_fd* const watcher_fd) {
return (watcher_fd->revents & POLLIN) != 0;
}
int watcher_init(watcher* const watcher, const int signal_fd) {
int watcher_init(watcher* const watcher, const block* const blocks,
const unsigned short block_count, const int signal_fd) {
if (signal_fd == -1) {
fprintf(stderr,
"error: invalid signal file descriptor passed to watcher\n");
(void)fprintf(
stderr,
"error: invalid signal file descriptor passed to watcher\n");
return 1;
}
@@ -29,10 +25,10 @@ int watcher_init(watcher* const watcher, const int signal_fd) {
fd->fd = signal_fd;
fd->events = POLLIN;
for (unsigned short i = 0; i < watcher->block_count; ++i) {
const int block_fd = watcher->blocks[i].pipe[READ_END];
for (unsigned short i = 0; i < block_count; ++i) {
const int block_fd = blocks[i].pipe[READ_END];
if (block_fd == -1) {
fprintf(
(void)fprintf(
stderr,
"error: invalid block file descriptors passed to watcher\n");
return 1;
@@ -47,17 +43,27 @@ int watcher_init(watcher* const watcher, const int signal_fd) {
}
int watcher_poll(watcher* watcher, const int timeout_ms) {
const int event_count = poll(watcher->fds, LEN(watcher->fds), timeout_ms);
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 1;
}
return event_count;
}
watcher->got_signal = watcher_fd_is_readable(&watcher->fds[SIGNAL_FD]);
bool watcher_fd_is_readable(const watcher_fd* const watcher_fd) {
return (watcher_fd->revents & POLLIN) != 0;
watcher->active_block_count = event_count - (int)watcher->got_signal;
unsigned short i = 0;
unsigned short j = 0;
while (i < event_count && j < LEN(watcher->active_blocks)) {
if (watcher_fd_is_readable(&watcher->fds[j])) {
watcher->active_blocks[i] = j;
++i;
}
++j;
}
return 0;
}

View File

@@ -8,7 +8,7 @@
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");
(void)fprintf(stderr, "error: could not connect to X server\n");
return NULL;
}