Get rid of all global variables

This commit is contained in:
Utkarsh Verma
2023-10-25 20:00:08 +05:30
parent bc84d094cd
commit 10f7d9b8db
13 changed files with 121 additions and 69 deletions

View File

@@ -17,6 +17,8 @@ typedef struct {
pid_t fork_pid;
} block;
block block_new(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);

View File

@@ -2,13 +2,12 @@
#include <signal.h>
#include "block.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(...) "."
extern block blocks[LEN(BLOCKS(X)) - 1];
enum { BLOCK_COUNT = LEN(BLOCKS(X)) - 1 };
#undef X
#define REFRESH_SIGNAL SIGUSR1

View File

@@ -2,19 +2,27 @@
#include <bits/types/sigset_t.h>
#include "block.h"
#include "timer.h"
typedef sigset_t signal_set;
typedef int (*signal_refresh_callback)(void);
typedef int (*signal_timer_callback)(timer* const timer);
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);

View File

@@ -10,16 +10,19 @@
typedef struct {
#define STATUS_LENGTH \
((LEN(blocks) * (MEMBER_LENGTH(block, output) - 1) + CLICKABLE_BLOCKS) + \
(LEN(blocks) - 1 + LEADING_DELIMITER + TRAILING_DELIMITER) * \
((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(void);
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);

View File

@@ -2,6 +2,8 @@
#include <signal.h>
#include "block.h"
#define TIMER_SIGNAL SIGALRM
typedef struct {
@@ -10,5 +12,5 @@ typedef struct {
const unsigned int reset_value;
} timer;
timer timer_new(void);
timer timer_new(const block *const blocks, const unsigned short block_count);
int timer_arm(timer *const timer);

View File

@@ -3,11 +3,11 @@
#include <stdbool.h>
#include <sys/poll.h>
#include "block.h"
#include "main.h"
#include "util.h"
typedef enum {
SIGNAL_FD = LEN(blocks),
SIGNAL_FD = BLOCK_COUNT,
WATCHER_FD_COUNT,
} watcher_fd_index;
@@ -15,8 +15,13 @@ typedef struct pollfd watcher_fd;
typedef struct {
watcher_fd fds[WATCHER_FD_COUNT];
const block *const blocks;
const unsigned short block_count;
} watcher;
watcher watcher_new(const block *const blocks,
const unsigned short block_count);
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);