Browse Source

Use backend-dependent functions to list MIDI ports

Move the code to list MIDI ports into a separate function for
each backend.
develop
Damien Goutte-Gattat 8 years ago
parent
commit
02cc92f664
  1. 97
      src/midi.c

97
src/midi.c

@ -147,6 +147,36 @@ alsa_midi_open(const char *name)
return midi;
}
static void
alsa_midi_get_ports(char ***ports, size_t *n, size_t *max)
{
char name[32];
int card;
card = -1;
while ( snd_card_next(&card) >= 0 && card != -1 ) {
snd_ctl_t *ctl;
snprintf(name, sizeof(name), "hw:%d", card);
if ( snd_ctl_open(&ctl, name, 0) >= 0 ) {
int device;
device = -1;
while ( snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
snprintf(name, sizeof(name), "hw:%d,%d", card, device);
if ( *n >= *max ) {
*max += 10;
*ports = xrealloc(*ports, *max);
}
*ports[(*n)++] = xstrdup(name);
}
snd_ctl_close(ctl);
}
}
}
#endif /* HAVE_ALSA */
#ifdef HAVE_OSS
@ -228,6 +258,27 @@ oss_midi_device(const struct dirent *entry)
return strncmp(entry->d_name, "midi", 4) == 0;
}
static void
oss_midi_get_ports(char ***ports, size_t *n, size_t *max)
{
char name[32];
size_t k;
struct dirent **namelist;
if ( (k = scandir("/dev", &namelist, oss_midi_device, alphasort)) != -1 ) {
while ( k-- ) {
snprintf(name, sizeof(name), "/dev/%s", namelist[k]->d_name);
free(namelist[k]);
if ( *n >= *max ) {
*max += 10;
*ports = xrealloc(*ports, *max);
}
ports[(*n)++] = xstrdup(name);
}
}
}
#endif /* HAVE_OSS */
@ -295,52 +346,10 @@ midi_get_ports(void)
n = max = 0;
#ifdef HAVE_ALSA
{
int card = -1;
while ( snd_card_next(&card) >= 0 && card != -1 ) {
snd_ctl_t *ctl;
snprintf(name, sizeof(name), "hw:%d", card);
if ( snd_ctl_open(&ctl, name, 0) >= 0 ) {
int device;
device = -1;
while ( snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
snprintf(name, sizeof(name), "hw:%d,%d", card, device);
if ( n >= max ) {
max += 10;
ports = xrealloc(ports, max);
}
ports[n++] = xstrdup(name);
}
snd_ctl_close(ctl);
}
}
}
alsa_midi_get_ports(&ports, &n, &max);
#endif
#ifdef HAVE_OSS
{
size_t k;
struct dirent **namelist;
if ( (k = scandir("/dev", &namelist, oss_midi_device, alphasort)) != -1 ) {
while ( k-- ) {
snprintf(name, sizeof(name), "/dev/%s", namelist[k]->d_name);
free(namelist[k]);
if ( n >= max ) {
max += 10;
ports = xrealloc(ports, max);
}
ports[n++] = xstrdup(name);
}
free(namelist);
}
}
oss_midi_get_ports(&ports, &n, &max);
#endif
if ( n >= max ) {

Loading…
Cancel
Save