diff options
-rw-r--r-- | client/dolmetschctl-client.c | 2 | ||||
-rw-r--r-- | client/midi.c | 93 | ||||
-rw-r--r-- | client/midi.h | 17 | ||||
-rw-r--r-- | client/osc.c | 37 | ||||
-rw-r--r-- | client/osc.h | 15 |
5 files changed, 72 insertions, 92 deletions
diff --git a/client/dolmetschctl-client.c b/client/dolmetschctl-client.c index 4dbaf5a..b67f238 100644 --- a/client/dolmetschctl-client.c +++ b/client/dolmetschctl-client.c @@ -183,7 +183,7 @@ int main(int argc, char* argv[]) return -1; osc_t o; - if(osc_init(&o, host, port)) + if(osc_init(&o, &m, host, port)) return -1; int ret = main_loop(&m, &o); diff --git a/client/midi.c b/client/midi.c index 25ec551..7075432 100644 --- a/client/midi.c +++ b/client/midi.c @@ -31,8 +31,16 @@ #define NOTE_EN 0x00 #define NOTE_DE 0x01 -u_int8_t done_data_en[] = { 0xB0, NOTE_EN, 0x01, 0xB0, NOTE_DE, 0x00 }; -u_int8_t done_data_de[] = { 0xB0, NOTE_EN, 0x00, 0xB0, NOTE_DE, 0x01 }; +u_int8_t led_cmd_en[] = { 0xB0, NOTE_EN, 0x01, 0xB0, NOTE_DE, 0x00 }; +u_int8_t led_cmd_de[] = { 0xB0, NOTE_EN, 0x00, 0xB0, NOTE_DE, 0x01 }; + +static void free_cmd_entry(void* ptr) +{ + cmd_t* c = ptr; + assert(c); + free(c->buf_); + free(c); +} int midi_init(midi_t* m, const char* device) { @@ -42,8 +50,7 @@ int midi_init(midi_t* m, const char* device) m->output_ = NULL; memset(m->buf_, 0, sizeof(m->buf_)); m->read_idx_ = 0; - - slist_init(&(m->done_data_), free); + slist_init(&(m->cmds_), free_cmd_entry); int ret = snd_rawmidi_open(&(m->input_), &(m->output_), device, SND_RAWMIDI_NONBLOCK); if(ret < 0) { @@ -54,6 +61,35 @@ int midi_init(midi_t* m, const char* device) return 0; } +int midi_enqueue_cmd(midi_t* m, const char* lang) +{ + assert(m && lang); + + int len; + const u_int8_t* src; + if(!strcmp(lang, "en")) { + len = sizeof(led_cmd_en); + src = led_cmd_en; + } + else if(!strcmp(lang, "de")) { + len = sizeof(led_cmd_de); + src = led_cmd_de; + } + else + return 0; + + cmd_t* cmd = malloc(sizeof(cmd_t)); + assert(cmd); + cmd->len_ = len; + assert((cmd->buf_ = malloc(cmd->len_))); + memcpy(cmd->buf_, src, cmd->len_); + cmd->write_idx_ = 0; + + slist_add(&(m->cmds_), cmd); + + return 0; +} + int midi_get_poll_fd_count(midi_t* m) { assert(m); @@ -73,13 +109,7 @@ int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds) snd_rawmidi_poll_descriptors(m->input_, pfds, m->in_pfds_cnt_); snd_rawmidi_poll_descriptors(m->output_, &(pfds[m->in_pfds_cnt_]), npfds - m->in_pfds_cnt_); - int pending_data = 0; - if(m->done_data_.first_) { - midi_done_data_t* d = (midi_done_data_t*)(m->done_data_.first_->data_); - if(d->active_) - pending_data = 1; - } - if(!pending_data) { + if(!slist_length(&(m->cmds_))) { int i; for(i = m->in_pfds_cnt_; i < npfds; ++i) pfds[i].events = 0; @@ -87,33 +117,17 @@ int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds) return (m->in_pfds_cnt_ + m->out_pfds_cnt_); } -void midi_lang_switch_done(void* data) -{ - assert(data); - midi_done_data_t* d = data; - d->active_ = 1; -} - -static int midi_enqueue_lang_switch(midi_t* m, osc_t* o, const char* lang, const u_int8_t* buf, int len) +static int midi_enqueue_lang_switch(midi_t* m, osc_t* o, const char* lang) { - midi_done_data_t* done_data = malloc(sizeof(midi_done_data_t)); - assert(done_data); - done_data->self_ = m; - done_data->active_ = 0; - done_data->buf_ = buf; - done_data->len_ = len; - done_data->write_idx_ = 0; - assert(slist_add(&(m->done_data_), done_data)); - - return osc_switch_lang(o, lang, &midi_lang_switch_done, done_data); + return osc_switch_lang(o, lang); } static int midi_handle_note_on(midi_t* m, osc_t* o) { int ret = 0; switch(m->buf_[1]) { - case NOTE_EN: ret = midi_enqueue_lang_switch(m, o,"en", done_data_en, sizeof(done_data_en)); break; - case NOTE_DE: ret = midi_enqueue_lang_switch(m, o, "de", done_data_de, sizeof(done_data_de)); break; + case NOTE_EN: ret = midi_enqueue_lang_switch(m, o,"en"); break; + case NOTE_DE: ret = midi_enqueue_lang_switch(m, o, "de"); break; default: printf("ignoring unknown note\n"); break; } return ret; @@ -189,26 +203,25 @@ static int midi_handle_out_revents(midi_t* m, struct pollfd *pfds, int npfds) if(!(revents & POLLOUT)) return 0; - assert(m->done_data_.first_); - midi_done_data_t* done_data = (midi_done_data_t*)(m->done_data_.first_->data_); - assert(done_data); - assert(done_data->active_); - assert(done_data->buf_); + assert(m->cmds_.first_); + cmd_t* cmd = (cmd_t*)(m->cmds_.first_->data_); + assert(cmd); + assert(cmd->buf_); - int ret = snd_rawmidi_write(m->output_, &(done_data->buf_[done_data->write_idx_]), done_data->len_ - done_data->write_idx_); + int ret = snd_rawmidi_write(m->output_, &(cmd->buf_[cmd->write_idx_]), cmd->len_ - cmd->write_idx_); if(ret == -EAGAIN) return 0; if(ret < 0) { error(0, 0, "MIDI: cannot write to port: %s", snd_strerror(ret)); return -1; } - done_data->write_idx_ += ret; - if(done_data->write_idx_ >= done_data->len_) { + cmd->write_idx_ += ret; + if(cmd->write_idx_ >= cmd->len_) { if((err = snd_rawmidi_drain(m->output_)) < 0) { error(0, 0, "MIDI: cannot drain output: %s", snd_strerror(err)); return -1; } - slist_remove(&(m->done_data_), done_data); + slist_remove(&(m->cmds_), cmd); } return 0; diff --git a/client/midi.h b/client/midi.h index b6e51fc..1a051ae 100644 --- a/client/midi.h +++ b/client/midi.h @@ -30,24 +30,23 @@ #include "osc.h" typedef struct { + u_int8_t* buf_; + int len_; + int write_idx_; +} cmd_t; + +typedef struct { snd_rawmidi_t* input_; int in_pfds_cnt_; snd_rawmidi_t* output_; int out_pfds_cnt_; u_int8_t buf_[3]; int read_idx_; - slist_t done_data_; + slist_t cmds_; } midi_t; -typedef struct { - midi_t* self_; - int active_; - const u_int8_t* buf_; - int len_; - int write_idx_; -} midi_done_data_t; - int midi_init(midi_t* m, const char* device); +int midi_enqueue_cmd(midi_t* m, const char* lang); int midi_get_poll_fd_count(midi_t* m); int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds); int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o); diff --git a/client/osc.c b/client/osc.c index 79bfd76..79ee76e 100644 --- a/client/osc.c +++ b/client/osc.c @@ -29,6 +29,7 @@ #include <assert.h> #include "osc.h" +#include "midi.h" static void print_error(int num, const char *msg, const char *path) @@ -45,33 +46,19 @@ static int lang_handler(const char *path, const char *types, lo_arg ** argv, if(argc != 1 || !lo_is_string_type((lo_type)types[0])) return 1; - printf("got ack for: lang '%s'\n", &(argv[0]->s)); - - /* search for right task ... */ - /* if(task->done_cb_) */ - /* task->done_cb_(task->done_data_); */ - - /* slist_remove(&(o->tasks_), task); */ +// printf("got ack for: lang '%s'\n", &(argv[0]->s)); + midi_enqueue_cmd((midi_t*)o->m_, &(argv[0]->s)); return 0; } -void free_osc_task(void* ptr) -{ - task_t* task = (task_t*)ptr; - assert(task); - - free(task->lang_); - free(task); -} - -int osc_init(osc_t* o, const char* host, const char* port) +int osc_init(osc_t* o, void* m, const char* host, const char* port) { assert(o != NULL); o->server_ = NULL; o->target_ = lo_address_new(host, port); - slist_init(&(o->tasks_), free_osc_task); + o->m_ = m; if(!port) return 0; @@ -86,19 +73,9 @@ int osc_init(osc_t* o, const char* host, const char* port) return 0; } -int osc_switch_lang(osc_t* o, const char* lang, void (*done_cb)(void*), void* done_data) +int osc_switch_lang(osc_t* o, const char* lang) { - task_t* task = malloc(sizeof(task_t)); - assert(task); - task->state_ = 0; - assert((task->lang_ = strdup(lang))); - task->done_cb_ = done_cb; - task->done_data_ = done_data; - assert(slist_add(&(o->tasks_), task)); - - if(lo_send_from(o->target_, o->server_, LO_TT_IMMEDIATE, "/lang/switch", "s", task->lang_) > 0) - task->state_ = 1; - + lo_send_from(o->target_, o->server_, LO_TT_IMMEDIATE, "/lang/switch", "s", lang); return 0; } diff --git a/client/osc.h b/client/osc.h index a5b6454..9d1ad94 100644 --- a/client/osc.h +++ b/client/osc.h @@ -26,23 +26,14 @@ #include "lo/lo.h" #include <poll.h> -#include "slist.h" - typedef struct { lo_server server_; lo_address target_; - slist_t tasks_; + void* m_; // forward declarations of typedes are not possible -- this is a HACK! FIXME!!! } osc_t; -typedef struct { - int state_; - char* lang_; - void (*done_cb_)(void*); - void* done_data_; -} task_t; - -int osc_init(osc_t* o, const char* host, const char* port); -int osc_switch_lang(osc_t* o, const char* lang, void (*done_cb)(void*), void* done_data); +int osc_init(osc_t* o, void* m, const char* host, const char* port); +int osc_switch_lang(osc_t* o, const char* lang); int osc_get_poll_fd_count(osc_t* o); int osc_get_poll_fds(osc_t* o, struct pollfd *pfds, int npfds); int osc_handle_revents(osc_t* o, struct pollfd *pfds, int npfds); |