From 60d990e4ea791932bb6183f9bbbf440476035008 Mon Sep 17 00:00:00 2001 From: Harry van Haaren Date: Sat, 3 Dec 2016 16:33:26 +0000 Subject: [PATCH] ctlrscript: refactored API and events working --- src/controller/ctlrscript.cxx | 68 +++++++++++-------- src/controller/ctlrscript.hxx | 6 +- .../{ctlrscript_api.h => luppp_script_api.h} | 16 +++-- 3 files changed, 58 insertions(+), 32 deletions(-) rename src/controller/{ctlrscript_api.h => luppp_script_api.h} (83%) diff --git a/src/controller/ctlrscript.cxx b/src/controller/ctlrscript.cxx index b686b80..99960b4 100644 --- a/src/controller/ctlrscript.cxx +++ b/src/controller/ctlrscript.cxx @@ -18,15 +18,17 @@ #include "ctlrscript.hxx" -#include #include +#include +#include #include "../jack.hxx" #include "../event.hxx" +#include "../logic.hxx" #include "../gridlogic.hxx" #include "../eventhandler.hxx" -#include +#include "luppp_script_api.h" #include "libtcc.h" @@ -42,11 +44,27 @@ static void error(const char *msg) printf("%s\n", msg); } -#warning TODO: Externalize this to a header which the controllers should\ -include to understand the event types, and functions for sending -struct event_t { - uint32_t type; -}; +void luppp_do(enum EVENT_ID id, void* e) +{ + printf("%s : event: %d, %p\n", __func__, id, e); + switch(id) { + case EVENT_TRACK_SEND_ACTIVE: { + struct event_track_send_active *ev = + (struct event_track_send_active *)e; + jack->getLogic()->trackSendActive(ev->track, ev->send, + ev->active); + break; + } + case EVENT_TRACK_SEND: { + struct event_track_send *ev = + (struct event_track_send *)e; + jack->getLogic()->trackSend(ev->track, ev->send, + ev->value); + break; + } + default: break; + } +} static int file_modify_time(const char *path, time_t *new_time) { @@ -67,6 +85,7 @@ int CtlrScript::compile() if(program) { free(program); } + program_ok = 0; TCCState *s; s = tcc_new(); @@ -83,6 +102,12 @@ int CtlrScript::compile() return -EINVAL; } + tcc_add_symbol(s, "luppp_do", (void *)luppp_do); + if(ret < 0) { + error("failed to insert luppp_do() symbol\n"); + return -EINVAL; + } + program = malloc(tcc_relocate(s, NULL)); if(!program) error("failed to alloc mem for program\n"); @@ -107,6 +132,7 @@ int CtlrScript::compile() /* Store the time of compiling */ file_modify_time(filename.c_str(), &script_load_time); + program_ok = 1; } @@ -163,6 +189,9 @@ void CtlrScript::midi(unsigned char* midi) { script_reload(); + if(!program_ok) + return; + int status = midi[0]; int data = midi[1]; float value = midi[2] / 127.f; @@ -171,26 +200,8 @@ void CtlrScript::midi(unsigned char* midi) int ret = poll(midi); } -struct event_bpm_t { - struct event_t event; - int bpm; -}; - -void CtlrScript::bpm(int bpm) -{ - printf("%s : %d\n", __func__, bpm); - //struct event_t ev = { 2, 1 }; - - struct event_bpm_t ev = { - .event = { .type = 2 }, - .bpm = bpm, - }; - - handle(&ev); -} - struct event_trac_send_active_t { - struct event_t event; + int type; int track; int send; int active; @@ -198,9 +209,12 @@ struct event_trac_send_active_t { void CtlrScript::trackSendActive(int t, int send, bool a) { + if(!program_ok) + return; + printf("%s : %d : %d\n", __func__, send, a); struct event_trac_send_active_t ev = { - .event = { .type = 3 }, + .type = 3, .track = t, .send = send, .active = 0, diff --git a/src/controller/ctlrscript.hxx b/src/controller/ctlrscript.hxx index 644e4db..e705dc2 100644 --- a/src/controller/ctlrscript.hxx +++ b/src/controller/ctlrscript.hxx @@ -58,7 +58,6 @@ public: return "CtlrScript"; } - void bpm(int bpm); void trackSendActive(int t, int send, bool a); void midi(unsigned char* midi); @@ -66,6 +65,11 @@ public: Controller::STATUS status(); private: + /* a flag stating if the program linked OK. If not, we do not act + * on the script, and do not call *any* function pointers related + * to the script, as they *will* cause a segfault */ + int program_ok; + void *program; ctlr_handle_midi poll; ctlr_handle_event handle; diff --git a/src/controller/ctlrscript_api.h b/src/controller/luppp_script_api.h similarity index 83% rename from src/controller/ctlrscript_api.h rename to src/controller/luppp_script_api.h index c15610b..f6a4568 100644 --- a/src/controller/ctlrscript_api.h +++ b/src/controller/luppp_script_api.h @@ -17,8 +17,8 @@ */ -#ifndef LUPPP_CONTROLLER_SCRIPT_H -#define LUPPP_CONTROLLER_SCRIPT_H +#ifndef LUPPP_SCRIPT_API_H +#define LUPPP_SCRIPT_API_H /** * This file is the API for the Controller Scripts, allowing them to @@ -29,6 +29,7 @@ enum EVENT_ID { EVENT_NOP = 0, + EVENT_TRACK_SEND, EVENT_TRACK_SEND_ACTIVE, }; @@ -38,7 +39,14 @@ struct event_track_send_active { int active; }; -static void luppp_do(enum EVENT_ID id, void* event_struct); +struct event_track_send { + int track; + int send; + float value; +}; -#endif /* LUPPP_CONTROLLER_SCRIPT_H */ + +void luppp_do(enum EVENT_ID id, void* event_struct); + +#endif /* LUPPP_SCRIPT_API_H */