From 5270088f730a7e30155a642dcd5c4e9a80055d7a Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Sun, 8 May 2022 01:35:55 +1000 Subject: More messing around with plugin system Just more attempts, but I think I'm gonna switch over to dynamic .so/.dll loading. That would be pretty fun to check out. --- .gitignore | 2 +- Makefile | 20 ++------------------ board/generate.sh | 8 ++++++++ board/mxblue/board.h | 3 +++ board/quack/board.h | 3 +++ board/simple.h | 9 +++++++++ boards.h | 6 ++++++ clak.c | 14 ++++++++------ 8 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 board/generate.sh create mode 100644 board/mxblue/board.h create mode 100644 board/quack/board.h create mode 100644 board/simple.h create mode 100644 boards.h diff --git a/.gitignore b/.gitignore index 5529293..dbfb8f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ clak clak.exe board/boards.h -board/*/board.h \ No newline at end of file +board/*/sound.h \ No newline at end of file diff --git a/Makefile b/Makefile index bd016b7..bf2bd8f 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,9 @@ NAME = clak PREFIX = $(HOME)/.local -BOARDS = mxblue quack - CC = gcc CFLAGS += -std=c99 -Wall -Wextra -Wshadow -Werror -pedantic -BOARD_FILES = $(addsuffix /board.h,$(addprefix board/,$(BOARDS))) - ifeq ($(OS),Windows_NT) LDLIBS = -lWinmm PLATFORM = win32 @@ -20,21 +16,9 @@ endif default: $(NAME) -$(NAME): $(NAME).c platform/$(PLATFORM).c $(BOARD_FILES) board/boards.h +$(NAME): $(NAME).c platform/$(PLATFORM).c boards.h $(CC) $(CFLAGS) $(NAME).c platform/$(PLATFORM).c $(LDLIBS) -o $(NAME) -board/boards.h: - printf "#ifndef BOARD_DEFAULTS_H\n#define BOARD_DEFAULTS_H\n\n" > board/boards.h - for board in $(BOARDS); do echo "#include \"$$board/board.h\"" >> board/boards.h; done - printf "\n#define BOARDS " >> board/boards.h - for board in $(BOARDS); do printf "BOARD(\"$$board\", board_$${board}_board_down_wav) " >> board/boards.h; done - printf "\n\n#endif /* BOARD_DEFAULTS_H */" >> board/boards.h - -$(BOARD_FILES): %.h: %_down.wav - xxd -i "$<" "$@" - clean: rm -f *.o - rm -f $(NAME) - rm -f board/boards.h - rm -f $(BOARD_FILES) \ No newline at end of file + rm -f $(NAME) \ No newline at end of file diff --git a/board/generate.sh b/board/generate.sh new file mode 100644 index 0000000..6d8d593 --- /dev/null +++ b/board/generate.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +BOARDS='mxblue quack' + +cd "$(dirname "$0")" +for board in $BOARDS; do + xxd -i $board/board_down.wav $board/sound.h +done \ No newline at end of file diff --git a/board/mxblue/board.h b/board/mxblue/board.h new file mode 100644 index 0000000..7d17f3f --- /dev/null +++ b/board/mxblue/board.h @@ -0,0 +1,3 @@ +#include "./sound.h" +#include "../simple.h" +SIMPLE_BOARD(mxblue) \ No newline at end of file diff --git a/board/quack/board.h b/board/quack/board.h new file mode 100644 index 0000000..64ba4cb --- /dev/null +++ b/board/quack/board.h @@ -0,0 +1,3 @@ +#include "./sound.h" +#include "../simple.h" +SIMPLE_BOARD(quack) \ No newline at end of file diff --git a/board/simple.h b/board/simple.h new file mode 100644 index 0000000..c2b211b --- /dev/null +++ b/board/simple.h @@ -0,0 +1,9 @@ +#ifndef CLAK_SIMPLE_H_ +#define CLAK_SIMPLE_H_ + +extern void sound_play(unsigned char *buffer); +#define SIMPLE_BOARD(name) \ + void name ## _on_down(void) { sound_play( name ## _board_down_wav ); } \ + void name ## _on_up(void) { sound_play( name ## _board_down_wav ); } + +#endif /* CLAK_SIMPLE_H_ */ \ No newline at end of file diff --git a/boards.h b/boards.h new file mode 100644 index 0000000..9090570 --- /dev/null +++ b/boards.h @@ -0,0 +1,6 @@ +#include "board/mxblue/board.h" +#include "board/quack/board.h" + +#define BOARDS \ + BOARD(mxblue) \ + BOARD(quack) diff --git a/clak.c b/clak.c index eace40d..7a19f4a 100644 --- a/clak.c +++ b/clak.c @@ -6,14 +6,15 @@ #define VOLUME 0.15 -#include "board/boards.h" -#define BOARD(str, buf) { .name = str, .wav = buf }, +#define BOARD(x) { .name = #x, .on_down = x ## _on_down, .on_up = x ## _on_up }, +#include "boards.h" #include "platform/platform.h" typedef struct { char *name; - unsigned char* wav; + void (*on_down)(void); + void (*on_up)(void); } Board; Board boards[] = { @@ -44,13 +45,14 @@ void keyboard_on_down(void) { if (board == NULL) return; - sound_play(board->wav); + board->on_down(); } void keyboard_on_up(void) { - // if (board == NULL) - // return; + if (board == NULL) + return; + board->on_up(); } int main(int argc, char **argv) -- cgit