Handles a single message, then segfaults

This commit is contained in:
Neale Pickett 2013-08-07 22:11:55 -06:00
parent fbc59832be
commit 98c870b09b
4 changed files with 39 additions and 20 deletions

18
alsa.c
View File

@ -2,11 +2,13 @@
#include <poll.h>
#include <sys/select.h>
#include <alsa/asoundlib.h>
#include "alsa.h"
#include "dump.h"
static snd_seq_t *snd_handle;
static int seq_port;
static snd_midi_event_t *midi_event_parser;
static snd_seq_event_t *ev;
int
alsa_setup(const char *name)
@ -22,11 +24,8 @@ alsa_setup(const char *name)
SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_READ |
SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
if (snd_seq_event_input(handle, &ev) < 0) {
return -1;
}
if (snd_midi_event_new(256, &midi_event_parser) < 0) {
perror("snd_midi_event_new");
return -1;
}
@ -63,15 +62,12 @@ alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds)
void
alsa_read_ready()
{
snd_seq_event_t *ev;
int ret = 1;
for (;;) {
char buf[256];
unsigned char buf[256];
long converted;
int i;
if (snd_seq_event_input(handle, &ev) < 0) {
if (snd_seq_event_input(snd_handle, &ev) < 0) {
break;
}
if (!midi_event_parser) {
@ -94,9 +90,9 @@ alsa_check_fds(fd_set *rfds, fd_set *wfds)
int i;
for (i = 0; i < npfd; i += 1) {
int fd = pfds[i]->fd;
int fd = pfd[i].fd;
if (FD_ISSET(fd, &rfds) || FD_ISSET(fd, &wfds)) {
if (FD_ISSET(fd, rfds) || FD_ISSET(fd, wfds)) {
alsa_read_ready();
return;
}

2
alsa.h
View File

@ -6,5 +6,5 @@
int alsa_setup(const char *name);
void alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds);
void alsa_read_ready();
void alsa_check_fds(fd_set *rfds, fd_set *wfds);
#endif

5
hdjd.c
View File

@ -44,8 +44,11 @@ main(int argc, char *argv[])
if (-1 == ret) {
DUMP();
}
alsa_check_fds(&rfds, &wfds);
usb_check_fds(&rfds, &wfds);
DUMP();
//DUMP();
}
return 0;

34
usb.c
View File

@ -2,7 +2,9 @@
#include <stdio.h>
#include <poll.h>
#include <stdint.h>
#include <string.h>
#include <sys/select.h>
#include "usb.h"
#include "dump.h"
static struct libusb_device_handle *usb_dev;
@ -48,12 +50,17 @@ usb_xfer_done(struct libusb_transfer *transfer)
}
int
usb_setup(char *name, size_t namelen)
usb_setup(const char *name, size_t namelen)
{
if (libusb_init(NULL) < 0) {
return -1;
}
if (libusb_pollfds_handle_timeouts(NULL) == 0) {
printf("I'm too dumb to handle events on such an old system.\n");
return -1;
}
for (d = devices; d->product_id; d += 1) {
usb_dev = libusb_open_device_with_vid_pid(NULL, 0x6f8, d->product_id);
if (usb_dev) {
@ -69,12 +76,16 @@ usb_setup(char *name, size_t namelen)
{
int ret;
struct libusb_device_descriptor ddesc;
char *p = name;
char name_[200];
char *p = name_;
strncpy(name_, name, sizeof(name_) - 1);
name_[sizeof(name_) - 1] = 0;
libusb_get_device_descriptor(libusb_get_device(usb_dev), &ddesc);
ret = libusb_get_string_descriptor_ascii(usb_dev, ddesc.iManufacturer, (unsigned char *)name, namelen);
if (ret > 0) {
p = name + ret;
p = name_ + ret;
*p = ' ';
p += 1;
ret = libusb_get_string_descriptor_ascii(usb_dev, ddesc.iProduct, (unsigned char *)p, namelen - ret - 1);
@ -109,13 +120,22 @@ usb_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds)
FD_SET(ufd->fd, wfds);
}
}
usb_initiate_transfer();
}
void
usb_read_ready()
void
usb_check_fds(fd_set *rfds, fd_set *wfds)
{
libusb_handle_events(NULL);
const struct libusb_pollfd **usb_fds = libusb_get_pollfds(NULL);
int i;
for (i = 0; usb_fds[i]; i += 1) {
int fd = usb_fds[i]->fd;
if (FD_ISSET(fd, rfds) || FD_ISSET(fd, wfds)) {
libusb_handle_events(NULL);
return;
}
}
}