From 11e9dec049e64989c5f3656415d00c6c69851e8e Mon Sep 17 00:00:00 2001 From: cbdev Date: Sun, 4 Jun 2017 18:13:29 +0200 Subject: Move instance allocator to backend object, introduce backend start function --- artnet.c | 15 ++++++++++++ artnet.h | 4 +++ backend.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend.h | 3 +++ config.c | 5 ++++ midi.c | 5 ++++ midi.h | 1 + midimonster.c | 69 ++++++--------------------------------------------- midimonster.h | 5 ++-- monster.cfg | 1 + 10 files changed, 123 insertions(+), 64 deletions(-) diff --git a/artnet.c b/artnet.c index a1f52d7..1fe8fb2 100644 --- a/artnet.c +++ b/artnet.c @@ -13,6 +13,7 @@ int artnet_init(){ .channel = artnet_channel, .handle = artnet_set, .process = artnet_handle, + .start = artnet_start, .shutdown = artnet_shutdown }; @@ -64,6 +65,15 @@ static int artnet_configure_instance(instance* instance, char* option, char* val data->uni = strtoul(value, NULL, 10); return 0; } + else if(!strcmp(option, "output")){ + if(!strcmp(value, "true")){ + data->mode |= MODE_OUTPUT; + } + else{ + data->mode &= ~MODE_OUTPUT; + } + return 0; + } fprintf(stderr, "Unknown ArtNet instance option %s\n", option); return 1; @@ -85,6 +95,11 @@ static int artnet_handle(size_t num, int* fd, void** data){ return 1; } +static int artnet_start(){ + //TODO + return 1; +} + static int artnet_shutdown(){ size_t n, p; instance** inst = NULL; diff --git a/artnet.h b/artnet.h index 03106bf..8e05153 100644 --- a/artnet.h +++ b/artnet.h @@ -7,9 +7,13 @@ static instance* artnet_instance(); static channel* artnet_channel(instance* instance, char* spec); static int artnet_set(size_t num, channel* c, channel_value* v); static int artnet_handle(size_t num, int* fd, void** data); +static int artnet_start(); static int artnet_shutdown(); +#define MODE_OUTPUT 1 + typedef struct /*_artnet_instance_model*/ { uint8_t net; uint8_t uni; + uint8_t mode; } artnet_instance_data; diff --git a/backend.c b/backend.c index 673fe9b..1d728d1 100644 --- a/backend.c +++ b/backend.c @@ -4,6 +4,66 @@ static size_t nbackends = 0; static backend* backends = NULL; +static size_t ninstances = 0; +static instance** instances = NULL; + +instance* mm_instance(){ + instance** new_inst = realloc(instances, (ninstances + 1) * sizeof(instance*)); + if(!new_inst){ + //TODO free + fprintf(stderr, "Failed to allocate memory\n"); + ninstances = 0; + return NULL; + } + instances = new_inst; + instances[ninstances] = calloc(1, sizeof(instance)); + if(!instances[ninstances]){ + fprintf(stderr, "Failed to allocate memory\n"); + return NULL; + } + + return instances[ninstances++]; +} + +int mm_backend_instances(char* name, size_t* ninst, instance*** inst){ + backend* b = backend_match(name); + size_t n = 0, u; + //count number of affected instances + for(u = 0; u < ninstances; u++){ + if(instances[u]->backend == b){ + n++; + } + } + + *ninst = n; + *inst = calloc(n, sizeof(instance*)); + if(!*inst){ + fprintf(stderr, "Failed to allocate memory\n"); + return 1; + } + + n = 0; + for(u = 0; u < ninstances; u++){ + if(instances[u]->backend == b){ + (*inst)[n] = instances[u]; + n++; + } + } + return 0; +} + +void instances_free(){ + size_t u; + for(u = 0; u < ninstances; u++){ + free(instances[u]->name); + instances[u]->name = NULL; + instances[u]->backend = NULL; + free(instances[u]); + instances[u] = NULL; + } + free(instances); + ninstances = 0; +} backend* backend_match(char* name){ size_t u; @@ -15,6 +75,16 @@ backend* backend_match(char* name){ return NULL; } +instance* instance_match(char* name){ + size_t u; + for(u = 0; u < ninstances; u++){ + if(!strcmp(instances[u]->name, name)){ + return instances[u]; + } + } + return NULL; +} + int mm_backend_register(backend b){ if(!backend_match(b.name)){ backends = realloc(backends, (nbackends + 1) * sizeof(backend)); @@ -32,6 +102,15 @@ int mm_backend_register(backend b){ return 1; } +int backends_start(){ + int rv = 0; + size_t u; + for(u = 0; u < nbackends; u++){ + rv |= backends[u].start(); + } + return rv; +} + int backends_stop(){ size_t u; for(u = 0; u < nbackends; u++){ diff --git a/backend.h b/backend.h index 5c844a7..65ca97e 100644 --- a/backend.h +++ b/backend.h @@ -1,2 +1,5 @@ backend* backend_match(char* name); +instance* instance_match(char* name); +int backends_start(); int backends_stop(); +void instances_free(); diff --git a/config.c b/config.c index b87394f..f425fe4 100644 --- a/config.c +++ b/config.c @@ -85,6 +85,11 @@ int config_read(char* cfg_file){ goto bail; } + if(instance_match(separator)){ + fprintf(stderr, "Duplicate instance name %s\n", separator); + goto bail; + } + current_instance = current_backend->create(); if(!current_instance){ fprintf(stderr, "Failed to instantiate backend %s\n", line); diff --git a/midi.c b/midi.c index a59ebeb..0d51d38 100644 --- a/midi.c +++ b/midi.c @@ -14,6 +14,7 @@ int midi_init(){ .channel = midi_channel, .handle = midi_set, .process = midi_handle, + .start = midi_start, .shutdown = midi_shutdown }; @@ -82,6 +83,10 @@ static int midi_handle(size_t num, int* fd, void** data){ return 1; } +static int midi_start(){ + return 1; +} + static int midi_shutdown(){ size_t n, p; instance** inst = NULL; diff --git a/midi.h b/midi.h index abad971..c83345b 100644 --- a/midi.h +++ b/midi.h @@ -7,4 +7,5 @@ static instance* midi_instance(); static channel* midi_channel(instance* instance, char* spec); static int midi_set(size_t num, channel* c, channel_value* v); static int midi_handle(size_t num, int* fd, void** data); +static int midi_start(); static int midi_shutdown(); diff --git a/midimonster.c b/midimonster.c index 765c1ce..e59b7f9 100644 --- a/midimonster.c +++ b/midimonster.c @@ -8,67 +8,6 @@ int artnet_init(); int midi_init(); int osc_init(); -static size_t ninstances = 0; -static instance** instances = NULL; - -instance* mm_instance(){ - instance** new_inst = realloc(instances, (ninstances + 1) * sizeof(instance*)); - if(!new_inst){ - //TODO free - fprintf(stderr, "Failed to allocate memory\n"); - ninstances = 0; - return NULL; - } - instances = new_inst; - instances[ninstances] = calloc(1, sizeof(instance)); - if(!instances[ninstances]){ - fprintf(stderr, "Failed to allocate memory\n"); - return NULL; - } - - return instances[ninstances++]; -} - -int mm_backend_instances(char* name, size_t* ninst, instance*** inst){ - backend* b = backend_match(name); - size_t n = 0, u; - //count number of affected instances - for(u = 0; u < ninstances; u++){ - if(instances[u]->backend == b){ - n++; - } - } - - *ninst = n; - *inst = calloc(n, sizeof(instance*)); - if(!*inst){ - fprintf(stderr, "Failed to allocate memory\n"); - return 1; - } - - n = 0; - for(u = 0; u < ninstances; u++){ - if(instances[u]->backend == b){ - (*inst)[n] = instances[u]; - n++; - } - } - return 0; -} - -static void instances_free(){ - size_t u; - for(u = 0; u < ninstances; u++){ - free(instances[u]->name); - instances[u]->name = NULL; - instances[u]->backend = NULL; - free(instances[u]); - instances[u] = NULL; - } - free(instances); - ninstances = 0; -} - int usage(char* fn){ fprintf(stderr, "MIDIMonster v0.1\n"); fprintf(stderr, "Usage:\n"); @@ -98,7 +37,13 @@ int main(int argc, char** argv){ return usage(argv[0]); } - //wait for events + //start backends + if(backends_start()){ + fprintf(stderr, "Failed to start backends\n"); + goto bail; + } + + //TODO wait for & translate events rv = EXIT_SUCCESS; bail: diff --git a/midimonster.h b/midimonster.h index f15d39c..9026222 100644 --- a/midimonster.h +++ b/midimonster.h @@ -34,19 +34,20 @@ typedef struct /*_mm_backend*/ { mmbackend_parse_channel channel; mmbackend_handle_event handle; mmbackend_process_fd process; + mmbackend_start start; mmbackend_shutdown shutdown; } backend; typedef struct _backend_instance { backend* backend; - size_t ident; + uint64_t ident; /*FIXME needed? identification provided by name*/ void* impl; char* name; } instance; typedef struct _backend_channel { instance* instance; - size_t ident; + uint64_t ident; void* impl; } channel; diff --git a/monster.cfg b/monster.cfg index ccb0d11..4bb7bd2 100644 --- a/monster.cfg +++ b/monster.cfg @@ -22,6 +22,7 @@ port = Translation Output 1 [artnet net1] net = 1 uni = 1 +output = true [artnet foo] net = 0 -- cgit v1.2.3