From bafa106f114cf4a8495cc3af28fa31f8150f2088 Mon Sep 17 00:00:00 2001 From: Gavan Fantom Date: Sun, 29 Apr 2007 11:50:00 +0000 Subject: [PATCH] Move toward a plugin API --- src/lsi/Makefile | 4 ++-- src/lsi/beatdetect.c | 4 +++- src/lsi/beatdetect.h | 2 +- src/lsi/dmx.c | 3 ++- src/lsi/dmx.h | 2 +- src/lsi/main.c | 41 ++++++++++++++++++++++++++++++----------- src/lsi/midi.c | 3 ++- src/lsi/midi.h | 2 +- src/lsi/mouse.c | 4 +++- src/lsi/mouse.h | 2 +- 10 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/lsi/Makefile b/src/lsi/Makefile index 531170e..9a82cc2 100644 --- a/src/lsi/Makefile +++ b/src/lsi/Makefile @@ -2,8 +2,8 @@ PREFIX?= /usr/local -OBJS= main.o vm.o dmx.o midi.o beatdetect.o fft.o map3d.o mouse.o -SRCS= main.c vm.c dmx.c midi.c beatdetect.c fft.c map3d.c mouse.c +OBJS= main.o vm.o plugins.o dmx.o midi.o beatdetect.o fft.o map3d.o mouse.o +SRCS= main.c vm.c plugins.c dmx.c midi.c beatdetect.c fft.c map3d.c mouse.c COMMONOBJS= mem.o hash.o COMMONDIR= ../common diff --git a/src/lsi/beatdetect.c b/src/lsi/beatdetect.c index 023a468..974bac3 100644 --- a/src/lsi/beatdetect.c +++ b/src/lsi/beatdetect.c @@ -114,7 +114,7 @@ double beatdetect_getconfidence(void) return confidence; } -void beatdetect_init(void) +int beatdetect_init(void) { audio_info_t info, oinfo; @@ -156,6 +156,8 @@ void beatdetect_init(void) /* vm_register_blah */ vm_register_signal_fd(audiofd, VM_BEATQ); + + return 1; } int beatdetect_read(void) diff --git a/src/lsi/beatdetect.h b/src/lsi/beatdetect.h index f0d258f..41684fe 100644 --- a/src/lsi/beatdetect.h +++ b/src/lsi/beatdetect.h @@ -1,6 +1,6 @@ /* beatdetect.h */ -void beatdetect_init(void); +int beatdetect_init(void); void beatdetect_close(void); double beatdetect_getphase(void); double beatdetect_getconfidence(void); diff --git a/src/lsi/dmx.c b/src/lsi/dmx.c index 4922ec5..49108ea 100644 --- a/src/lsi/dmx.c +++ b/src/lsi/dmx.c @@ -92,7 +92,7 @@ out: printf("%x %x %x %x %x %x %x %x %x %x %x %x\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); } -void dmx_init(void) +int dmx_init(void) { dmx_open(); dmxpacket[0] = 0x7e; @@ -104,6 +104,7 @@ void dmx_init(void) dmxpacket[DMX_UNIVERSESIZE+5] = 0xe7; dmxuniverse = dmxpacket+5; // dmx_dumpparams(); + return 1; } void dmx_setchannel(int channel, int value) diff --git a/src/lsi/dmx.h b/src/lsi/dmx.h index 9872af5..a25dc50 100644 --- a/src/lsi/dmx.h +++ b/src/lsi/dmx.h @@ -3,7 +3,7 @@ #define DMX_UNIVERSESIZE 512 void dmx_close(void); -void dmx_init(void); +int dmx_init(void); void dmx_setchannel(int channel, int value); void dmx_output(void); diff --git a/src/lsi/main.c b/src/lsi/main.c index be7fbf7..283705b 100644 --- a/src/lsi/main.c +++ b/src/lsi/main.c @@ -2,18 +2,25 @@ #include #include +#include #include #include "vm.h" -#include "dmx.h" -#include "midi.h" -#include "beatdetect.h" -#include "mouse.h" +#include "plugins.h" + +/* This macro exists purely to shorten subsequent lines for readability */ +#define PT(i) plugins_table[i] void finish(void) { - dmx_close(); - midi_close(); - beatdetect_close(); + int i; + + for (i = nplugins-1; i >= 0; i--) { + printf("Shutting down plugin '%s'\n", PT(i).pl_name); + (PT(i).pl_init)(); + printf("Plugin '%s' shut down\n", PT(i).pl_name); + PT(i).pl_active = 0; + } + exit(0); } @@ -24,6 +31,8 @@ void sigint_handler(int signal) int main(int argc, char *argv[]) { + int i; + argv++; argc--; if (argc != 1) @@ -31,10 +40,20 @@ int main(int argc, char *argv[]) vm_init(); vm_load(argv[0]); signal(SIGINT, sigint_handler); - midi_init(); - dmx_init(); - beatdetect_init(); - mouse_init(); + + /* Initialise plugins */ + for (i = 0; i < nplugins; i++) { + printf("Initialising plugin '%s'\n", PT(i).pl_name); + if ((PT(i).pl_init)()) { + printf("Plugin '%s' initialised\n", PT(i).pl_name); + PT(i).pl_active = 1; + } else { + printf("Plugin '%s' failed\n", PT(i).pl_name); + PT(i).pl_active = 0; + } + } + + /* Showtime */ vm_spawn("main"); vm_run(); finish(); diff --git a/src/lsi/midi.c b/src/lsi/midi.c index 517901d..e780562 100644 --- a/src/lsi/midi.c +++ b/src/lsi/midi.c @@ -16,7 +16,7 @@ int midi_bytes; seq_event_rec midi_buf; int midi_initialised = 0; -void midi_init(void) +int midi_init(void) { midi_fd = open(PORT, O_NONBLOCK | O_RDONLY, 0); if (midi_fd == -1) { @@ -25,6 +25,7 @@ void midi_init(void) midi_bytes = 0; midi_initialised = 1; vm_register_signal_fd(midi_fd, VM_MIDIQ); + return 1; } void midi_close(void) diff --git a/src/lsi/midi.h b/src/lsi/midi.h index 3ab3b6f..d690c94 100644 --- a/src/lsi/midi.h +++ b/src/lsi/midi.h @@ -1,6 +1,6 @@ /* midi.h */ -void midi_init(void); +int midi_init(void); void midi_close(void); int midi_filter(void); int midi_read(void); diff --git a/src/lsi/mouse.c b/src/lsi/mouse.c index 100086d..a502b24 100644 --- a/src/lsi/mouse.c +++ b/src/lsi/mouse.c @@ -50,7 +50,7 @@ struct wscons_event mouse_buf; int mouse_initialised = 0; int mouse_x, mouse_y, mouse_z; -void mouse_init(void) +int mouse_init(void) { mouse_fd = open(MOUSEDEVICE, O_RDONLY); if (mouse_fd < 0) @@ -61,6 +61,8 @@ void mouse_init(void) mouse_x = 0; mouse_y = 0; mouse_z = 0; + + return 1; } void mouse_close(void) diff --git a/src/lsi/mouse.h b/src/lsi/mouse.h index 681f153..1a18b0c 100644 --- a/src/lsi/mouse.h +++ b/src/lsi/mouse.h @@ -1,6 +1,6 @@ /* mouse.h */ -void mouse_init(void); +int mouse_init(void); void mouse_close(void); int mouse_filter(void); void mouse_getpos(int *, int *, int *);