From a689ba41dadad0093f7545dd8999d41a165ceb98 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Tue, 15 Aug 2023 20:45:39 +1000 Subject: Initial refactor of plugin system (darwin only) Darwin only for now since that's what I'm on, will migrate the others a bit later. Trying out a different plugin system that probably makes more sense as an interface to each other. Might shuffle things around even more later. This is based somewhat on Tsoding's audio visualiser plugin system, I thought that was way more elegant (the 'state' object), as opposed to whatever the hell we were doing. --- board/board.h | 22 ++++++++++++++-------- board/simple_board.h | 28 ++++++++++++---------------- clak.c | 19 ++++++++++++------- platform/darwin.c | 10 ++++------ platform/platform.h | 2 +- 5 files changed, 43 insertions(+), 38 deletions(-) diff --git a/board/board.h b/board/board.h index 36d39fb..0c95b9c 100644 --- a/board/board.h +++ b/board/board.h @@ -1,16 +1,22 @@ #ifndef CLAK_BOARD_H_ #define CLAK_BOARD_H_ -struct board { - void (*on_down)(void); - void (*on_up)(void); +struct board_state; + +typedef void (*board_init_fn_t)(struct board_state *board_state); +typedef void (*key_event_fn_t)(struct board_state *board_state); + +typedef void (*sound_play_fn_t)(unsigned char *buffer, unsigned int buffer_len); + +struct board_state { + /* Populated by plugin */ + key_event_fn_t on_down; + key_event_fn_t on_up; char *name; -}; -struct board_data { - void (*sound_play)(unsigned char *buffer, unsigned int buffer_len); + /* Runtime from main */ + board_init_fn_t board_init; + sound_play_fn_t sound_play; }; -typedef struct board *(*fn_board_init)(struct board_data data); - #endif /* CLAK_BOARD_H_ */ diff --git a/board/simple_board.h b/board/simple_board.h index f0997c7..1a264de 100644 --- a/board/simple_board.h +++ b/board/simple_board.h @@ -1,36 +1,32 @@ #include "board.h" -static struct board_data board_data; - #define WAV_LEN(W) PREPROC_CONCAT(W,_len) #define PREPROC_CONCAT(A, B) A ## B -void board_on_down(void) +void board_on_down(struct board_state *state) { + (void) state; #ifdef BOARD_DOWN_WAV - board_data.sound_play(BOARD_DOWN_WAV, WAV_LEN(BOARD_DOWN_WAV)); + state->sound_play(BOARD_DOWN_WAV, WAV_LEN(BOARD_DOWN_WAV)); #endif } -void board_on_up(void) +void board_on_up(struct board_state *state) { + (void) state; #ifdef BOARD_UP_WAV - board_data.sound_play(BOARD_UP_WAV, WAV_LEN(BOARD_UP_WAV)); + state->sound_play(BOARD_UP_WAV, WAV_LEN(BOARD_UP_WAV)); #endif } -static struct board board = { - .on_down = &board_on_down, - .on_up = &board_on_up, +void board_init(struct board_state *state) +{ #ifdef BOARD_NAME - .name = BOARD_NAME, + state->name = BOARD_NAME; #else - .name = "Generic Board", + state->name = "Generic Board"; #endif -}; -struct board *board_init(struct board_data data) -{ - board_data = data; - return &board; + state->on_down = &board_on_down; + state->on_up = &board_on_up; } diff --git a/clak.c b/clak.c index f7208e7..3c484bc 100644 --- a/clak.c +++ b/clak.c @@ -9,7 +9,7 @@ #define VOLUME 0.15 -struct board *board = NULL; +struct board_state state = {0}; void do_exit(int code) { @@ -21,14 +21,14 @@ void on_clean_exit(void) { do_exit(0); } void keyboard_on_down(void) { - assert(board != NULL); - board->on_down(); + assert(state.on_down != NULL); + state.on_down(&state); } void keyboard_on_up(void) { - assert(board != NULL); - board->on_up(); + assert(state.on_up != NULL); + state.on_up(&state); } int main(int argc, char **argv) @@ -40,12 +40,17 @@ int main(int argc, char **argv) // TODO: List valid boards. return 1; } + + /* Initialise board state */ + state.sound_play = &sound_play; /* Inject platform-specific impl */ + fprintf(stderr, "Loading board...\n"); - if ((board = load_board(argv[1])) == NULL) { + if (!load_board(&state, argv[1])) { printf("Failed to load board, exiting.\n"); return 1; } - fprintf(stderr, "Loaded board: %s\n", board->name); + state.board_init(&state); + fprintf(stderr, "Loaded board: %s\n", state.name); fprintf(stderr, "Initialisating sound system...\n"); if (!sound_init(VOLUME)) { diff --git a/platform/darwin.c b/platform/darwin.c index 3189a5d..2ffdbe3 100644 --- a/platform/darwin.c +++ b/platform/darwin.c @@ -45,7 +45,7 @@ void enter_idle(void) } /* TODO: probably also merge into a *nix module */ -struct board *load_board(char *board_name) +bool load_board(struct board_state *state, char *board_name) { char so_name[100]; int r = snprintf(so_name, 100, "./board/%s.dylib", board_name); @@ -60,16 +60,14 @@ struct board *load_board(char *board_name) return false; } - fn_board_init board_init = dlsym(board_module, "board_init"); + board_init_fn_t board_init = dlsym(board_module, "board_init"); if (board_init == NULL) { printf("ERROR: No board initialisation function could be loaded.\n"); return false; } + state->board_init = board_init; - struct board_data data = { - .sound_play = &sound_play - }; - return board_init(data); + return true; } /* https://developer.apple.com/documentation/iokit/iohidvaluecallback */ diff --git a/platform/platform.h b/platform/platform.h index ca07789..34f1200 100644 --- a/platform/platform.h +++ b/platform/platform.h @@ -5,7 +5,7 @@ #include "../board/board.h" -struct board *load_board(char *board_name); +bool load_board(struct board_state *state, char *board_name); bool sound_init(float volume); void sound_play(unsigned char *buffer, unsigned int buffer_len); -- cgit