summaryrefslogtreecommitdiffstats
path: root/src/helper.cc
blob: fac1bb9e42e8e2ea3ff7de55419af4c08239baa6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "helper.h"

#include "mainwindow.h"
#include <QDebug>

void populatePorts(const pa_card_info &info, std::map<QByteArray, PortInfo> &ports)
{
    ports.clear();
    for (uint32_t i = 0; i < info.n_ports; ++i) {
        PortInfo p;

        p.name = info.ports[i]->name;
        p.description = info.ports[i]->description;
        p.priority = info.ports[i]->priority;
        p.available = info.ports[i]->available;
        p.direction = info.ports[i]->direction;
        p.latency_offset = info.ports[i]->latency_offset;
        for (pa_card_profile_info2 ** p_profile = info.ports[i]->profiles2; p_profile && *p_profile != nullptr; ++p_profile)
            p.profiles.push_back((*p_profile)->name);

        ports[p.name] = p;
    }
}

void groupProfiles(const std::set<pa_card_profile_info2 *, profile_prio_compare> &profile_priorities,
                   const std::map<QByteArray, PortInfo> &ports,
                   QMap<QString, ProfileGroup> &profiles)
{
    profiles.clear();
    for (auto p_profile : profile_priorities) {
        bool hasNo = false, hasOther = false;
        std::map<QByteArray, PortInfo>::const_iterator portIt;
        QByteArray desc = p_profile->description;

        for (portIt = ports.begin(); portIt != ports.end(); portIt++) {
            PortInfo port = portIt->second;

            if (std::find(port.profiles.begin(), port.profiles.end(), p_profile->name) == port.profiles.end())
                continue;

            if (port.available == PA_PORT_AVAILABLE_NO)
                hasNo = true;
            else {
                hasOther = true;
                break;
            }
        }
        if (hasNo && !hasOther)
            desc += MainWindow::tr(" (unplugged)").toUtf8().constData();

        if (!p_profile->available)
            desc += MainWindow::tr(" (unavailable)").toUtf8().constData();

        QString parseId = QString::fromUtf8(p_profile->name);
        int plus = parseId.indexOf(QLatin1Char('+'));
        if (plus != -1) {
            parseId = parseId.left(plus);
        }
        auto list = parseId.split(QRegularExpression(QLatin1String("[:\\-]")));
        parseId.clear();
        for (auto p : list) {
            if (p == QLatin1String("input") || p == QLatin1String("output"))
                continue;
            if (parseId.isEmpty() || p.startsWith(QLatin1String("extra"))) {
                parseId.append(p);
            }
        }
        //qDebug() << "ParseID:" << parseId;
        ProfileGroup &group = profiles[parseId];
        if (p_profile->available) {
            group.available = true;
        }
        group.addEntry(p_profile->name, desc.constData());
    }
}