summaryrefslogtreecommitdiff
path: root/apps/midi.c
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-10-10 07:20:31 +0200
committerChristian Pointner <equinox@spreadspace.org>2015-10-10 07:20:31 +0200
commite1e94206e5de0a1b4e03593ffeb9d5cb6ce81abb (patch)
tree1801606ee6f5b8385905e7db133e597566a270d7 /apps/midi.c
parentimproved error handling (diff)
midi is back from threded to async io
Diffstat (limited to 'apps/midi.c')
-rw-r--r--apps/midi.c96
1 files changed, 31 insertions, 65 deletions
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;
}