aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-05-11 19:22:08 +1000
committerNicholas Tay <nick@windblume.net>2022-05-11 19:22:08 +1000
commit2fee408d6fe0964e245dc0bae90027baa13b159a (patch)
tree8ff7cd648f2e9331b6e86ac3dfeb7c4e07df8f99
parent6a1e6a7d6b6a3d987f2fccb3c06f4f5da071f504 (diff)
downloadclak-2fee408d6fe0964e245dc0bae90027baa13b159a.tar.gz
clak-2fee408d6fe0964e245dc0bae90027baa13b159a.tar.bz2
clak-2fee408d6fe0964e245dc0bae90027baa13b159a.zip
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.
-rw-r--r--board/board.c8
-rw-r--r--board/board.h14
-rw-r--r--board/mxblue/board.c14
-rw-r--r--board/quack/board.c14
-rw-r--r--board/simple_board.h33
-rw-r--r--clak.c18
-rw-r--r--clak.h8
-rw-r--r--platform/platform.h5
-rw-r--r--platform/win32.c22
9 files changed, 69 insertions, 67 deletions
diff --git a/board/board.c b/board/board.c
deleted file mode 100644
index 0410aef..0000000
--- a/board/board.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include "board.h"
-
-static fn_sound_play sound_play;
-
-void board_init(fn_sound_play sp)
-{
- sound_play = sp;
-} \ No newline at end of file
diff --git a/board/board.h b/board/board.h
index 18f6afa..c7dfc1f 100644
--- a/board/board.h
+++ b/board/board.h
@@ -1,12 +1,16 @@
#ifndef CLAK_BOARD_H_
#define CLAK_BOARD_H_
-#include <stdbool.h>
+struct board {
+ void (*on_down)(void);
+ void (*on_up)(void);
+ char *name;
+};
-#include "../clak.h"
+struct board_data {
+ void (*sound_play)(unsigned char *buffer);
+};
-typedef void (*fn_board_init)(fn_sound_play sp);
-typedef void (*fn_board_on_down)(void);
-typedef void (*fn_board_on_up)(void);
+typedef struct board *(*fn_board_init)(struct board_data data);
#endif /* CLAK_BOARD_H_ */ \ No newline at end of file
diff --git a/board/mxblue/board.c b/board/mxblue/board.c
index 14277db..fb0456d 100644
--- a/board/mxblue/board.c
+++ b/board/mxblue/board.c
@@ -1,12 +1,4 @@
#include "sound.h"
-
-#include "../board.c"
-
-void board_on_down(void)
-{
- sound_play(mxblue_board_down_wav);
-}
-
-void board_on_up(void)
-{
-} \ No newline at end of file
+#define BOARD_NAME "Cherry MX Blue"
+#define BOARD_DOWN_WAV mxblue_board_down_wav
+#include "../simple_board.h" \ No newline at end of file
diff --git a/board/quack/board.c b/board/quack/board.c
index 893e366..9a58e2f 100644
--- a/board/quack/board.c
+++ b/board/quack/board.c
@@ -1,12 +1,4 @@
#include "sound.h"
-
-#include "../board.c"
-
-void board_on_down(void)
-{
- sound_play(quack_board_down_wav);
-}
-
-void board_on_up(void)
-{
-} \ No newline at end of file
+#define BOARD_NAME "Quack Quack"
+#define BOARD_DOWN_WAV quack_board_down_wav
+#include "../simple_board.h" \ No newline at end of file
diff --git a/board/simple_board.h b/board/simple_board.h
new file mode 100644
index 0000000..9d9836b
--- /dev/null
+++ b/board/simple_board.h
@@ -0,0 +1,33 @@
+#include "board.h"
+
+static struct board_data board_data;
+
+void board_on_down(void)
+{
+#ifdef BOARD_DOWN_WAV
+ board_data.sound_play(BOARD_DOWN_WAV);
+#endif
+}
+
+void board_on_up(void)
+{
+#ifdef BOARD_UP_WAV
+ board_data.sound_play(BOARD_UP_WAV);
+#endif
+}
+
+static struct board board = {
+ .on_down = &board_on_down,
+ .on_up = &board_on_up,
+#ifdef BOARD_NAME
+ .name = BOARD_NAME,
+#else
+ .name = "Generic Board",
+#endif
+};
+
+struct board *board_init(struct board_data data)
+{
+ board_data = data;
+ return &board;
+} \ No newline at end of file
diff --git a/clak.c b/clak.c
index 145709f..444165f 100644
--- a/clak.c
+++ b/clak.c
@@ -2,13 +2,15 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
+#include <assert.h>
-#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");
diff --git a/clak.h b/clak.h
deleted file mode 100644
index c700a12..0000000
--- a/clak.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef CLAK_H_
-#define CLAK_H_
-
-#include <stdbool.h>
-
-typedef void (*fn_sound_play)(unsigned char *buffer);
-
-#endif /* CLAK_H_ */ \ No newline at end of file
diff --git a/platform/platform.h b/platform/platform.h
index fd9b9bb..a5e5db4 100644
--- a/platform/platform.h
+++ b/platform/platform.h
@@ -5,11 +5,14 @@
#include "../board/board.h"
-bool load_board(char *board_name, fn_board_on_down *on_down, fn_board_on_down *on_up);
+struct board *load_board(char *board_name);
+
bool sound_init(float volume);
void sound_play(unsigned char *buffer);
+
void keyboard_unhook(void);
bool keyboard_hook(void);
+
void enter_idle(void);
#endif /* CLAK_PLATFORM_H_ */ \ No newline at end of file
diff --git a/platform/win32.c b/platform/win32.c
index cf22718..1dccc7b 100644
--- a/platform/win32.c
+++ b/platform/win32.c
@@ -82,7 +82,7 @@ void enter_idle(void)
}
}
-bool load_board(char *board_name, fn_board_on_down *on_down, fn_board_on_down *on_up)
+struct board *load_board(char *board_name)
{
char dll_name[100];
int r = snprintf(dll_name, 100, "./board/%s.dll", board_name);
@@ -96,23 +96,15 @@ bool load_board(char *board_name, fn_board_on_down *on_down, fn_board_on_down *o
printf("ERROR: Could not load board module.\n");
return false;
}
+ // HACK: Cast to void function ptr then cast back, otherwise types are incompatible to the compiler.
fn_board_init board_init = (fn_board_init) (void (*)(void)) GetProcAddress(board_module, "board_init");
if (board_init == NULL) {
- printf("ERROR: No board down function could be loaded.\n");
+ printf("ERROR: No board initialisation function could be loaded.\n");
return false;
}
- board_init(&sound_play);
- *on_down = (fn_board_on_down) GetProcAddress(board_module, "board_on_down");
- if (on_down == NULL) {
- printf("ERROR: No board down function could be loaded.\n");
- return false;
- }
- *on_up = (fn_board_on_up) GetProcAddress(board_module, "board_on_up");
- if (on_up == NULL) {
- printf("ERROR: No board up function could be loaded.\n");
- return false;
- }
-
- return true;
+ struct board_data data = {
+ .sound_play = &sound_play
+ };
+ return board_init(data);
} \ No newline at end of file