Example #1
0
void MidiEnumeratorPrivate::rescan()
{
    qDebug() << Q_FUNC_INFO;

    if (m_alsa == NULL)
        return;

    bool changed = false;
    QList <MidiOutputDevice*> destroyOutputs(m_outputDevices);
    QList <MidiInputDevice*> destroyInputs(m_inputDevices);

    snd_seq_client_info_t* clientInfo = NULL;
    snd_seq_client_info_alloca(&clientInfo);

    snd_seq_port_info_t* portInfo = NULL;
    snd_seq_port_info_alloca(&portInfo);

    snd_seq_client_info_set_client(clientInfo, 0);
    while (snd_seq_query_next_client(m_alsa, clientInfo) == 0)
    {
        /* Get the client ID */
        int client = snd_seq_client_info_get_client(clientInfo);

        /* Ignore our own client */
        if (m_address->client == client)
            continue;

        /* Go thru all available ports in the client */
        snd_seq_port_info_set_client(portInfo, client);
        snd_seq_port_info_set_port(portInfo, -1);
        while (snd_seq_query_next_port(m_alsa, portInfo) == 0)
        {
            const snd_seq_addr_t* address = snd_seq_port_info_get_addr(portInfo);
            if (address == NULL)
                continue;

            uint caps = snd_seq_port_info_get_capability(portInfo);
            if (caps & SND_SEQ_PORT_CAP_READ)
            {
                // Don't expose own ports
                QString name = AlsaMidiUtil::extractName(m_alsa, address);
                if (name.contains("__QLC__") == true)
                    continue;

                QVariant uid = AlsaMidiUtil::addressToVariant(address);
                MidiInputDevice* dev = inputDevice(uid);
                if (dev == NULL)
                {
                    AlsaMidiInputDevice* dev = new AlsaMidiInputDevice(
                            uid, name, address, m_alsa, m_inputThread, this);
                    m_inputDevices << dev;
                    changed = true;
                }
                else
                {
                    destroyInputs.removeAll(dev);
                }
            }

            if (caps & SND_SEQ_PORT_CAP_WRITE)
            {
                // Don't expose own ports
                QString name = AlsaMidiUtil::extractName(m_alsa, address);
                if (name.contains("__QLC__") == true)
                    continue;

                QVariant uid = AlsaMidiUtil::addressToVariant(address);
                MidiOutputDevice* dev = outputDevice(uid);
                if (dev == NULL)
                {
                    AlsaMidiOutputDevice* dev = new AlsaMidiOutputDevice(
                                    uid, name, address, m_alsa, m_address, this);
                    m_outputDevices << dev;
                    changed = true;
                }
                else
                {
                    destroyOutputs.removeAll(dev);
                }
            }
        }
    }

    foreach (MidiOutputDevice* dev, destroyOutputs)
    {
        m_outputDevices.removeAll(dev);
        delete dev;
        changed = true;
    }
void MidiEnumeratorPrivate::rescan()
{
    qDebug() << Q_FUNC_INFO;

    bool changed = false;
    QList <MidiOutputDevice*> destroyOutputs(m_outputDevices);
    QList <MidiInputDevice*> destroyInputs(m_inputDevices);

    /* Find out which devices are still present */
    ItemCount numDevices = MIDIGetNumberOfDevices();
    for (ItemCount devIndex = 0; devIndex < numDevices; devIndex++)
    {
        MIDIDeviceRef dev = MIDIGetDevice(devIndex);
        ItemCount numEntities = MIDIDeviceGetNumberOfEntities(dev);
        for (ItemCount entIndex = 0; entIndex < numEntities; entIndex++)
        {
            MIDIEntityRef entity = MIDIDeviceGetEntity(dev, entIndex);

            /* Get the entity's UID */
            QVariant uid = extractUID(entity);
            if (uid.isValid() == false)
                continue;

            QString name = extractName(entity);
            qDebug() << Q_FUNC_INFO << "Found device:" << name << "UID:" << uid.toString();

            ItemCount destCount = MIDIEntityGetNumberOfDestinations(entity);
            if (destCount > 0)
            {
                MidiOutputDevice* dev = outputDevice(uid);
                if (dev == NULL)
                {
                    CoreMidiOutputDevice* dev = new CoreMidiOutputDevice(
                        uid, name, entity, m_client, this);
                    m_outputDevices << dev;
                    changed = true;
                }
                else
                {
                    destroyOutputs.removeAll(dev);
                }
            }

            ItemCount srcCount = MIDIEntityGetNumberOfSources(entity);
            if (srcCount > 0)
            {
                MidiInputDevice* dev = inputDevice(uid);
                if (dev == NULL)
                {
                    CoreMidiInputDevice* dev = new CoreMidiInputDevice(
                        uid, name, entity, m_client, this);
                    m_inputDevices << dev;
                    changed = true;
                }
                else
                {
                    destroyInputs.removeAll(dev);
                }
            }
        }
    }

    foreach (MidiOutputDevice* dev, destroyOutputs)
    {
        m_outputDevices.removeAll(dev);
        delete dev;
        changed = true;
    }