summaryrefslogtreecommitdiffstats
path: root/src/PulseAudioQt/device_p.h
blob: 462862efc5b8efbf875a6a668d127ecfa573e3c3 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
    SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#ifndef DEVICE_P_H
#define DEVICE_P_H

#include <pulse/proplist.h>

#include <QHash>
#include <QVector>

#include "device.h"
#include "port.h"
#include "port_p.h"
#include "volumeobject_p.h"

namespace PulseAudioQt
{
class DevicePrivate
{
public:
    explicit DevicePrivate(Device *q);

    Device *q;

    QString m_description;
    QString m_formFactor;
    quint32 m_cardIndex = -1;
    QList<Port *> m_ports;
    quint32 m_activePortIndex = -1;
    Device::State m_state = Device::UnknownState;

    Device::State stateFromPaState(int value) const;

    template<typename PAInfo>
    void updateDevice(const PAInfo *info)
    {
        q->VolumeObject::d->updateVolumeObject(info);

        if (m_description != info->description) {
            m_description = info->description;
            Q_EMIT q->descriptionChanged();
        }
        const char *form_factor = pa_proplist_gets(info->proplist, PA_PROP_DEVICE_FORM_FACTOR);
        if (form_factor) {
            QString formFactor = QString::fromUtf8(form_factor);
            if (m_formFactor != formFactor) {
                m_formFactor = formFactor;
                Q_EMIT q->formFactorChanged();
            }
        }

        m_cardIndex = info->card;
        Q_EMIT q->cardIndexChanged();

        QStringList newPorts;
        QStringList existingPorts;

        // Build list of existing ports
        for (const Port *port : qAsConst(m_ports)) {
            existingPorts << port->name();
        }

        // Add new ports from the updated port list and re/set port info
        for (auto **it = info->ports; it && *it != nullptr; ++it) {
            const QString name = QString::fromUtf8((*it)->name);
            newPorts << name;

            Port *port = nullptr;

            if (existingPorts.contains(name)) {
                port = m_ports[existingPorts.indexOf(name)];
            } else {
                port = new Port(q);
                m_ports << port;
            }

            port->d->setInfo(*it);
        }

        // Remove ports that are not in the updated port list
        for (Port *port : qAsConst(m_ports)) {
            if (!newPorts.contains(port->name())) {
                m_ports.removeOne(port);
                delete port;
            }
        }

        // Set active port
        for (Port *port : qAsConst(m_ports)) {
            if (info->active_port->name == port->name()) {
                m_activePortIndex = m_ports.indexOf(port);
            }
        }

        Q_EMIT q->portsChanged();
        Q_EMIT q->activePortIndexChanged();

        Device::State infoState = stateFromPaState(info->state);
        if (infoState != m_state) {
            m_state = infoState;
            Q_EMIT q->stateChanged();
        }
    }
};

} // namespace PulseAudioQt

#endif