diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/block.h | 29 | ||||
-rw-r--r-- | include/cli.h | 12 | ||||
-rw-r--r-- | include/main.h | 16 | ||||
-rw-r--r-- | include/signal-handler.h | 33 | ||||
-rw-r--r-- | include/status.h | 31 | ||||
-rw-r--r-- | include/timer.h | 21 | ||||
-rw-r--r-- | include/util.h | 28 | ||||
-rw-r--r-- | include/watcher.h | 28 | ||||
-rw-r--r-- | include/x11.h | 13 |
9 files changed, 211 insertions, 0 deletions
diff --git a/include/block.h b/include/block.h new file mode 100644 index 0000000..c4f8d54 --- /dev/null +++ b/include/block.h @@ -0,0 +1,29 @@ +#ifndef BLOCK_H +#define BLOCK_H + +#include <stdbool.h> +#include <stdint.h> +#include <sys/types.h> + +#include "config.h" +#include "util.h" + +typedef struct { + const char *const icon; + 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; + +block block_new(const char *const icon, const char *const command, + const unsigned int interval, const int signal); +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); + +#endif // BLOCK_H diff --git a/include/cli.h b/include/cli.h new file mode 100644 index 0000000..2f93f62 --- /dev/null +++ b/include/cli.h @@ -0,0 +1,12 @@ +#ifndef CLI_H +#define CLI_H + +#include <stdbool.h> + +typedef struct { + bool is_debug_mode; +} cli_arguments; + +cli_arguments cli_parse_arguments(const char* const argv[], const int argc); + +#endif // CLI_H diff --git a/include/main.h b/include/main.h new file mode 100644 index 0000000..b37a6b1 --- /dev/null +++ b/include/main.h @@ -0,0 +1,16 @@ +#ifndef MAIN_H +#define MAIN_H + +#include <signal.h> + +#include "config.h" +#include "util.h" + +#define REFRESH_SIGNAL SIGUSR1 + +// Utilise C's adjacent string concatenation to count the number of blocks. +#define X(...) "." +enum { BLOCK_COUNT = LEN(BLOCKS(X)) - 1 }; +#undef X + +#endif // MAIN_H diff --git a/include/signal-handler.h b/include/signal-handler.h new file mode 100644 index 0000000..da2d471 --- /dev/null +++ b/include/signal-handler.h @@ -0,0 +1,33 @@ +#ifndef SIGNAL_HANDLER_H +#define SIGNAL_HANDLER_H + +#include <signal.h> + +#include "block.h" +#include "timer.h" + +typedef sigset_t signal_set; +typedef int (*signal_refresh_callback)(block* const blocks, + const unsigned short block_count); +typedef int (*signal_timer_callback)(block* const blocks, + const unsigned short block_code, + timer* const timer); + +typedef struct { + int fd; + const signal_refresh_callback refresh_callback; + const signal_timer_callback timer_callback; + + block* const blocks; + const unsigned short block_count; +} signal_handler; + +signal_handler signal_handler_new( + block* const blocks, const unsigned short block_count, + 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); + +#endif // SIGNAL_HANDLER_H diff --git a/include/status.h b/include/status.h new file mode 100644 index 0000000..48fb3d8 --- /dev/null +++ b/include/status.h @@ -0,0 +1,31 @@ +#ifndef STATUS_H +#define STATUS_H + +#include <stdbool.h> + +#include "block.h" +#include "config.h" +#include "main.h" +#include "util.h" +#include "x11.h" + +typedef struct { +#define STATUS_LENGTH \ + ((BLOCK_COUNT * (MEMBER_LENGTH(block, output) - 1) + CLICKABLE_BLOCKS) + \ + (BLOCK_COUNT - 1 + LEADING_DELIMITER + TRAILING_DELIMITER) * \ + (LEN(DELIMITER) - 1) + \ + 1) + char current[STATUS_LENGTH]; + char previous[STATUS_LENGTH]; +#undef STATUS_LENGTH + + const block* const blocks; + const unsigned short block_count; +} status; + +status status_new(const block* const blocks, const unsigned short block_count); +bool status_update(status* const status); +int status_write(const status* const status, const bool is_debug_mode, + x11_connection* const connection); + +#endif // STATUS_H diff --git a/include/timer.h b/include/timer.h new file mode 100644 index 0000000..1ec7f75 --- /dev/null +++ b/include/timer.h @@ -0,0 +1,21 @@ +#ifndef TIMER_H +#define TIMER_H + +#include <signal.h> +#include <stdbool.h> + +#include "block.h" + +#define TIMER_SIGNAL SIGALRM + +typedef struct { + unsigned int time; + const unsigned int tick; + const unsigned int reset_value; +} timer; + +timer timer_new(const block *const blocks, const unsigned short block_count); +int timer_arm(timer *const timer); +bool timer_must_run_block(const timer *const timer, const block *const block); + +#endif // TIMER_H diff --git a/include/util.h b/include/util.h new file mode 100644 index 0000000..a3bdcce --- /dev/null +++ b/include/util.h @@ -0,0 +1,28 @@ +#ifndef UTIL_H +#define UTIL_H + +#include <stddef.h> + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define LEN(arr) (sizeof(arr) / sizeof((arr)[0])) +#define BIT(n) (1 << (n)) + +// NOLINTBEGIN(bugprone-macro-parentheses) +#define MEMBER_SIZE(type, member) sizeof(((type*)NULL)->member) +#define MEMBER_LENGTH(type, member) \ + (MEMBER_SIZE(type, member) / MEMBER_SIZE(type, member[0])) +// NOLINTEND(bugprone-macro-parentheses) + +#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); + +#endif // UTIL_H diff --git a/include/watcher.h b/include/watcher.h new file mode 100644 index 0000000..ff31809 --- /dev/null +++ b/include/watcher.h @@ -0,0 +1,28 @@ +#ifndef WATCHER_H +#define WATCHER_H + +#include <poll.h> +#include <stdbool.h> + +#include "block.h" +#include "main.h" + +enum watcher_fd_index { + SIGNAL_FD = BLOCK_COUNT, + WATCHER_FD_COUNT, +}; + +typedef struct pollfd watcher_fd; + +typedef struct { + watcher_fd fds[WATCHER_FD_COUNT]; + unsigned short active_blocks[BLOCK_COUNT]; + unsigned short active_block_count; + bool got_signal; +} watcher; + +int watcher_init(watcher *const watcher, const block *const blocks, + const unsigned short block_count, const int signal_fd); +int watcher_poll(watcher *const watcher, const int timeout_ms); + +#endif // WATCHER_H diff --git a/include/x11.h b/include/x11.h new file mode 100644 index 0000000..6faaced --- /dev/null +++ b/include/x11.h @@ -0,0 +1,13 @@ +#ifndef X11_H +#define X11_H + +#include <xcb/xcb.h> + +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); + +#endif // X11_H |