From 2fee408d6fe0964e245dc0bae90027baa13b159a Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Wed, 11 May 2022 19:22:08 +1000 Subject: Improve plugin loading architecture This should be better, maybe the variable names could be better though. Init is more extensible, we take in a 'board_data' struct on the plugin end that has everything it needs (mainly function pointers for now). Then, a 'board' struct is given back to the main Clak runtime, with everything it needs to know (again, mainly function pointers). It is a bit weird that the board is not stored with Clak but as a pointer to the dynamically loaded bit, but not sure. --- clak.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'clak.c') diff --git a/clak.c b/clak.c index 145709f..444165f 100644 --- a/clak.c +++ b/clak.c @@ -2,13 +2,15 @@ #include #include #include +#include -#include "clak.h" #include "platform/platform.h" #include "board/board.h" #define VOLUME 0.15 +struct board *board = NULL; + void do_exit(int code) { keyboard_unhook(); @@ -17,17 +19,16 @@ void do_exit(int code) } void on_clean_exit(void) { do_exit(0); } -fn_board_on_down board_on_down; -fn_board_on_up board_on_up; - void keyboard_on_down(void) { - board_on_down(); + assert(board != NULL); + board->on_down(); } void keyboard_on_up(void) { - board_on_up(); + assert(board != NULL); + board->on_up(); } int main(int argc, char **argv) @@ -39,10 +40,11 @@ int main(int argc, char **argv) // TODO: List valid boards. return 1; } - if (!load_board(argv[1], &board_on_down, &board_on_up)) { + if ((board = load_board(argv[1])) == NULL) { + printf("Failed to load board, exiting.\n"); return 1; } - fprintf(stderr, "Loaded board: %s\n", argv[1]); + fprintf(stderr, "Loaded board: %s\n", board->name); if (!sound_init(VOLUME)) { printf("ERROR: Could not initialise sound system.\n"); -- cgit