Browse Source

Get MIDI ports in an array

Do not use a callback function to iterate through the available
MIDI ports.  Instead, get a list of all the available ports as
an automatically allocated string array, then iterate on the
array members.
develop
Damien Goutte-Gattat 8 years ago
parent
commit
8b08869dd7
  1. 57
      src/kmxtool.c
  2. 66
      src/midi.c
  3. 6
      src/midi.h

57
src/kmxtool.c

@ -184,21 +184,30 @@ do_list_programs(midi_io_t *midi)
free(reply);
}
int
check_microx_on_port(const char *name, void *user)
static int
find_microx_on_port(const char *port,
struct kmx_microx_version *version,
int quiet)
{
struct kmx_microx_version *version;
version = (struct kmx_microx_version *)user;
if ( (midi = midi_open(name)) != NULL ) {
if ( kmx_microx_identify(midi, version) == 0 )
return 1;
midi_close(midi);
midi = NULL;
int ret, e;
ret = -1;
if ( (midi = midi_open(port)) != NULL ) {
if ( (e = kmx_microx_identify(midi, version)) == 0 )
ret = 0;
else {
midi_close(midi);
midi = NULL;
if ( ! quiet )
errx(EXIT_FAILURE, "cannot identify microX device: %s",
kmx_microx_error(midi, e));
}
}
else if ( ! quiet )
errx(EXIT_FAILURE, "cannot open MIDI port %s", port);
return 0;
return ret;
}
@ -271,16 +280,24 @@ main(int argc, char **argv)
}
}
if ( port ) {
if ( ! (midi = midi_open(port)) )
errx(EXIT_FAILURE, "cannot open MIDI port %s", port);
if ( port )
find_microx_on_port(port, &version, 0);
else {
char **ports, **p;
int found;
if ( (e = kmx_microx_identify(midi, &version)) < 0 )
errx(EXIT_FAILURE, "cannot identify microX device: %s",
kmx_microx_error(midi, e));
ports = midi_get_ports();
for ( p = ports, found = 0; *p != NULL; p++ ) {
if ( ! found )
found = find_microx_on_port(*p, &version, 1) != -1;
free(*p);
}
free(ports);
if ( ! found )
errx(EXIT_FAILURE, "no microX device found");
}
else if ( ! midi_list_ports(check_microx_on_port, &version) )
errx(EXIT_FAILURE, "no microX device found");
if ( mode == MODE_STATUS ) {
printf("KORG microX synthesizer, version %02d.%02d.%02d.%02d\n",

66
src/midi.c

@ -147,14 +147,18 @@ midi_error(midi_io_t *midi)
return snd_strerror(midi->error);
}
int
midi_list_ports(midi_list_port_cb cb, void *user)
char **
midi_get_ports(void)
{
int card, ret;
char **ports;
size_t n, max;
int card;
ret = 0;
ports = NULL;
n = max = 0;
card = -1;
while ( ret == 0 && snd_card_next(&card) >= 0 && card != -1 ) {
while ( snd_card_next(&card) >= 0 && card != -1 ) {
snd_ctl_t *ctl;
char name[32];
@ -163,16 +167,27 @@ midi_list_ports(midi_list_port_cb cb, void *user)
int device;
device = -1;
while ( ret == 0 && snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
while ( snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
sprintf(name, "hw:%d,%d", card, device);
ret = cb(name, user);
if ( n >= max ) {
max += 10;
ports = xrealloc(ports, max);
}
ports[n++] = xstrdup(name);
}
snd_ctl_close(ctl);
}
}
return ret;
if ( n >= max ) {
max += 1;
ports = xrealloc(ports, max);
}
ports[n++] = NULL;
return ports;
}
#elif USE_OSS_MIDI_API
@ -309,28 +324,39 @@ is_midi_device(const struct dirent *entry)
return 0;
}
int
midi_list_ports(midi_list_port_cb cb, void *user)
char **
midi_get_ports(void)
{
int n, ret;
char **ports;
size_t max, n, k;
struct dirent **namelist;
char path[32];
ret = 0;
if ( (n = scandir("/dev", &namelist, is_midi_device, alphasort)) != -1 ) {
while ( n-- ) {
if ( ret == 0 ) {
snprintf(path, sizeof(path), "/dev/%s", namelist[n]->d_name);
ret = cb(path, user);
ports = NULL;
max = n = 0;
if ( (k = scandir("/dev", &namelist, is_midi_device, alphasort)) != -1 ) {
while ( k-- ) {
snprintf(path, sizeof(path), "/dev/%s", namelist[k]->d_name);
free(namelist[k]);
if ( n >= max ) {
max += 10;
ports = xrealloc(ports, max);
}
free(namelist[n]);
ports[n++] = xstrdup(path);
}
free(namelist);
}
return ret;
}
if ( n >= max ) {
max += 1;
ports = xrealloc(ports, max);
}
ports[n++] = NULL;
return ports;
}
#else
#error "No MIDI API available"

6
src/midi.h

@ -56,10 +56,8 @@ midi_change_program(midi_io_t *,
unsigned short,
unsigned char);
typedef int (*midi_list_port_cb)(const char *, void *);
int
midi_list_ports(midi_list_port_cb, void *);
char **
midi_get_ports(void);
#ifdef __cplusplus
}

Loading…
Cancel
Save