getDevices static method

List<AlsaMidiDevice> getDevices()

Implementation

static List<AlsaMidiDevice> getDevices() {
  StreamController<MidiMessage> rxStreamController = StreamController<MidiMessage>.broadcast();
  int status;
  var card = calloc<Int>();
  card.elementAt(0).value = -1;
  Pointer<Pointer<Char>> shortname = calloc<Pointer<Char>>();

  List<AlsaMidiDevice> devices = [];

  if ((status = alsa.snd_card_next(card)) < 0) {
    print('error: cannot determine card number $card ${stringFromNative(alsa.snd_strerror(status))}');
    return [];
  }
  // print('status $status');
  if (card.value < 0) {
    print('error: no sound cards found');
    return [];
  }

  while (card.value >= 0) {
    Pointer<Char> name = 'hw:${card.value}'.toNativeUtf8().cast<Char>();
    Pointer<Pointer<a.snd_ctl_>> ctl = calloc<Pointer<a.snd_ctl_>>();
    Pointer<Int> device = calloc<Int>();
    device.elementAt(0).value = -1;

    // print("card ${card.value}");
    if ((status = alsa.snd_card_get_name(card.value, shortname)) < 0) {
      print('error: cannot determine card shortname $card ${stringFromNative(alsa.snd_strerror(status))}');
      continue;
    }
    print('name: ${stringFromNative(shortname.value)}');
    status = alsa.snd_ctl_open(ctl, name, 0);
    // print("status after ctl_open $status ctl $ctl ctl.value ${ctl.value}");
    if (status < 0) {
      print('error: cannot open control for card number $card ${stringFromNative(alsa.snd_strerror(status))}');
      continue;
    }

    do {
      status = alsa.snd_ctl_rawmidi_next_device(ctl.value, device);
      // print("status $status device.value ${device.value}");
      if (status < 0) {
        print('error: cannot determine device number ${device.value} ${stringFromNative(alsa.snd_strerror(status))}');
        break;
      }

      if (device.value >= 0) {
        var deviceId = hardwareId(card.value, device.value);
        if (!_connectedDevices.containsKey(deviceId)) {
          // print('add unconnected device with id $deviceId');
          devices.add(AlsaMidiDevice(
              ctl.value, card.value, device.value, stringFromNative(shortname.value), 'native', rxStreamController));
        }
      }
    } while (device.value > 0);

    if ((status = alsa.snd_card_next(card)) < 0) {
      print('error: cannot determine card number $card ${stringFromNative(alsa.snd_strerror(status))}');
      break;
    }

    calloc.free(name);
    calloc.free(ctl);
    calloc.free(device);
  }

  // Add all connected devices
  devices.addAll(_connectedDevices.values);

  return devices;
}