diff options
Diffstat (limited to 'client/midi.c')
-rw-r--r-- | client/midi.c | 93 |
1 files changed, 53 insertions, 40 deletions
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; |