diff options
author | Christian Pointner <equinox@spreadspace.org> | 2015-10-10 07:20:31 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2015-10-10 07:20:31 +0200 |
commit | e1e94206e5de0a1b4e03593ffeb9d5cb6ce81abb (patch) | |
tree | 1801606ee6f5b8385905e7db133e597566a270d7 | |
parent | improved error handling (diff) |
midi is back from threded to async io
-rwxr-xr-x | apps/configure | 2 | ||||
-rw-r--r-- | apps/dolmetschctl.c | 45 | ||||
-rw-r--r-- | apps/midi.c | 96 | ||||
-rw-r--r-- | apps/midi.h | 9 |
4 files changed, 76 insertions, 76 deletions
diff --git a/apps/configure b/apps/configure index f96a329..9c5109f 100755 --- a/apps/configure +++ b/apps/configure @@ -88,7 +88,7 @@ rm -f config.h rm -f include.mk case $TARGET in Linux) - LDFLAGS=$LD_FLAGS' -lasound -llo -lpthread' + LDFLAGS=$LD_FLAGS' -lasound -llo' ;; *) echo "platform not supported" diff --git a/apps/dolmetschctl.c b/apps/dolmetschctl.c index cdf95bb..26b8e83 100644 --- a/apps/dolmetschctl.c +++ b/apps/dolmetschctl.c @@ -23,7 +23,7 @@ #include "config.h" #include <stdio.h> -#include <unistd.h> +#include <error.h> #include "osc.h" #include "midi.h" @@ -41,6 +41,42 @@ void print_version() #endif } +int main_loop(osc_t* o, midi_t* m) +{ + int ret = 0; + + printf("main_loop just started\n"); + + int npfds; + struct pollfd *pfds; + + npfds = midi_get_poll_desc_count(m); + pfds = alloca(npfds * sizeof(struct pollfd)); + midi_get_poll_desc(m, pfds, npfds); + + printf("main_loop running...\n"); + for (;;) { + int err = poll(pfds, npfds, 200); + if(err < 0) { + if(errno == EINTR) + continue; + + error(0, errno, "poll failed"); + break; + } + if(err == 0) { + // timeout is reached + continue; + } + + ret = midi_handle_revents(m, pfds, npfds); + if(ret) + break; + } + + return ret; +} + int main(int argc, char* argv[]) { osc_t o; @@ -51,13 +87,12 @@ int main(int argc, char* argv[]) if(midi_init(&m, "hw:2,0,0")) return -1; - if(osc_start(&o) || midi_start(&m)) + if(osc_start(&o)) return -1; - sleep(10); + int ret = main_loop(&o, &m); - midi_stop(&m); osc_stop(&o); - return 0; + return ret; } diff --git a/apps/midi.c b/apps/midi.c index b923316..261e12f 100644 --- a/apps/midi.c +++ b/apps/midi.c @@ -34,7 +34,6 @@ int midi_init(midi_t* m, const char* device) { assert(m != NULL); - m->stop_ = 0; int ret = snd_rawmidi_open(&(m->input_), NULL, device, SND_RAWMIDI_NONBLOCK); if(ret < 0) { error(0, 0, "MIDI: cannot open port '%s': %s", device, snd_strerror(ret)); @@ -44,77 +43,44 @@ int midi_init(midi_t* m, const char* device) return 0; } -static void* midi_run(void* p) +int midi_get_poll_desc_count(midi_t* m) { - assert(p != NULL); - - midi_t* m = (midi_t*)p; - - int npfds; - struct pollfd *pfds; - - npfds = snd_rawmidi_poll_descriptors_count(m->input_); - pfds = alloca(npfds * sizeof(struct pollfd)); - snd_rawmidi_poll_descriptors(m->input_, pfds, npfds); - for (;;) { - int err = poll(pfds, npfds, 200); - if(err < 0 && errno == EINTR) - break; - if(err < 0) { - error(0, errno, "MIDI: poll failed"); - break; - } - if(err == 0) { - if(m->stop_) - break; - } - - unsigned short revents; - if((err = snd_rawmidi_poll_descriptors_revents(m->input_, pfds, npfds, &revents)) < 0) { - error(0, 0, "MIDI: cannot get poll events: %s", snd_strerror(errno)); - break; - } - if(revents & (POLLERR | POLLHUP)) - break; - if(!(revents & POLLIN)) - continue; - - u_int8_t buf[256]; - int ret = snd_rawmidi_read(m->input_, buf, sizeof(buf)); - if (ret == -EAGAIN) - continue; - if(ret < 0) { - error(0, 0, "MIDI: cannot read from midi port: %s", snd_strerror(ret)); - break; - } - - int i; - printf("MIDI:%s", ret ? " ": " no data?\n"); - for (i = 0; i < ret; ++i) - printf("%02X%c", buf[i], (i >= (ret-1)) ? '\n' : ' '); - } - return NULL; + return snd_rawmidi_poll_descriptors_count(m->input_); } -int midi_start(midi_t* m) +int midi_get_poll_desc(midi_t* m, struct pollfd *pfds, int npfds) { - assert(m != NULL); - - int ret = pthread_create(&(m->thread_), NULL, midi_run, (void*)m); - if(ret) - error(0, ret, "faild to create midi thread"); - - return ret; + return snd_rawmidi_poll_descriptors(m->input_, pfds, npfds); } -int midi_stop(midi_t* m) +int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds) { - assert(m != NULL); + int err; + unsigned short revents; + if((err = snd_rawmidi_poll_descriptors_revents(m->input_, pfds, npfds, &revents)) < 0) { + error(0, 0, "MIDI: cannot get poll events: %s", snd_strerror(errno)); + return -1; + } + if(revents & (POLLERR | POLLHUP)) { + error(0, 0, "MIDI: got POLLERR or POLLHUP"); + return -1; + } + if(!(revents & POLLIN)) + return 0; - m->stop_ = 1; - int ret = pthread_join(m->thread_, NULL); - if(ret) - error(0, ret, "faild to join midi thread"); + u_int8_t buf[256]; + int ret = snd_rawmidi_read(m->input_, buf, sizeof(buf)); + if (ret == -EAGAIN) + return 0; + if(ret < 0) { + error(0, 0, "MIDI: cannot read from midi port: %s", snd_strerror(ret)); + return -1; + } - return ret; + int i; + printf("MIDI:%s", ret ? " ": " no data?\n"); + for (i = 0; i < ret; ++i) + printf("%02X%c", buf[i], (i >= (ret-1)) ? '\n' : ' '); + + return 0; } diff --git a/apps/midi.h b/apps/midi.h index 2c0153b..f2aa119 100644 --- a/apps/midi.h +++ b/apps/midi.h @@ -24,16 +24,15 @@ #define DOLMETSCHCTL_midi_h_INCLUDED #include <alsa/asoundlib.h> -#include <pthread.h> +#include <poll.h> typedef struct { - pthread_t thread_; snd_rawmidi_t* input_; - u_int8_t stop_; } midi_t; int midi_init(midi_t* m, const char* device); -int midi_start(midi_t* m); -int midi_stop(midi_t* m); +int midi_get_poll_desc_count(midi_t* m); +int midi_get_poll_desc(midi_t* m, struct pollfd *pfds, int npfds); +int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds); #endif |