Changeset View
Changeset View
Standalone View
Standalone View
plugins/systemvolume/device.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2014-2015 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef PULSE_DEVICE_H | ||||
22 | #define PULSE_DEVICE_H | ||||
23 | | ||||
24 | #include <QString> | ||||
25 | | ||||
26 | #include <pulse/volume.h> | ||||
27 | | ||||
28 | #include "volumeobject.h" | ||||
29 | #include "port.h" | ||||
30 | #include "pulseobject.h" | ||||
31 | | ||||
32 | namespace QPulseAudio | ||||
33 | { | ||||
34 | | ||||
35 | class Device : public VolumeObject | ||||
36 | { | ||||
37 | Q_OBJECT | ||||
38 | Q_PROPERTY(State state READ state NOTIFY stateChanged) | ||||
39 | Q_PROPERTY(QString name READ name NOTIFY nameChanged) | ||||
40 | Q_PROPERTY(QString description READ description NOTIFY descriptionChanged) | ||||
41 | Q_PROPERTY(QString formFactor READ formFactor NOTIFY formFactorChanged) | ||||
42 | Q_PROPERTY(quint32 cardIndex READ cardIndex NOTIFY cardIndexChanged) | ||||
43 | Q_PROPERTY(QList<QObject *> ports READ ports NOTIFY portsChanged) | ||||
44 | Q_PROPERTY(quint32 activePortIndex READ activePortIndex WRITE setActivePortIndex NOTIFY activePortIndexChanged) | ||||
45 | Q_PROPERTY(bool default READ isDefault WRITE setDefault NOTIFY defaultChanged) | ||||
46 | public: | ||||
47 | enum State { | ||||
48 | InvalidState = 0, | ||||
49 | RunningState, | ||||
50 | IdleState, | ||||
51 | SuspendedState, | ||||
52 | UnknownState | ||||
53 | }; | ||||
54 | Q_ENUMS(State); | ||||
55 | | ||||
56 | virtual ~Device() {} | ||||
57 | | ||||
58 | template <typename PAInfo> | ||||
59 | void updateDevice(const PAInfo *info) | ||||
60 | { | ||||
61 | updateVolumeObject(info); | ||||
62 | | ||||
63 | if (m_name != info->name) { | ||||
64 | m_name = info->name; | ||||
65 | emit nameChanged(); | ||||
66 | } | ||||
67 | if (m_description != info->description) { | ||||
68 | m_description = info->description; | ||||
69 | emit descriptionChanged(); | ||||
70 | } | ||||
71 | const char *form_factor = pa_proplist_gets(info->proplist, PA_PROP_DEVICE_FORM_FACTOR); | ||||
72 | if (form_factor) { | ||||
73 | QString formFactor = QString::fromUtf8(form_factor); | ||||
74 | if (m_formFactor != formFactor) { | ||||
75 | m_formFactor = formFactor; | ||||
76 | emit formFactorChanged(); | ||||
77 | } | ||||
78 | } | ||||
79 | | ||||
80 | m_cardIndex = info->card; | ||||
81 | emit cardIndexChanged(); | ||||
82 | | ||||
83 | // TODO: this rebuilds the entire port list on every update. would be | ||||
84 | // nicer if it actually removed what needs removing updates what needs | ||||
85 | // updating and adds what needs adding. Alas, this is a tad more | ||||
86 | // involved. | ||||
87 | qDeleteAll(m_ports); | ||||
88 | m_ports.clear(); | ||||
89 | for (auto **ports = info->ports; ports && *ports != nullptr; ++ports) { | ||||
90 | Port *port = new Port(this); | ||||
91 | port->setInfo(*ports); | ||||
92 | m_ports.append(port); | ||||
93 | if (info->active_port == *ports) { | ||||
94 | m_activePortIndex = m_ports.length() - 1; | ||||
95 | } | ||||
96 | } | ||||
97 | emit portsChanged(); | ||||
98 | emit activePortIndexChanged(); | ||||
99 | | ||||
100 | State infoState = stateFromPaState(info->state); | ||||
101 | if (infoState != m_state) { | ||||
102 | m_state = infoState; | ||||
103 | emit stateChanged(); | ||||
104 | } | ||||
105 | } | ||||
106 | | ||||
107 | State state() const; | ||||
108 | QString name() const; | ||||
109 | QString description() const; | ||||
110 | QString formFactor() const; | ||||
111 | quint32 cardIndex() const; | ||||
112 | QList<QObject *> ports() const; | ||||
113 | quint32 activePortIndex() const; | ||||
114 | virtual void setActivePortIndex(quint32 port_index) = 0; | ||||
115 | virtual bool isDefault() const = 0; | ||||
116 | virtual void setDefault(bool enable) = 0; | ||||
117 | | ||||
118 | signals: | ||||
119 | void stateChanged(); | ||||
120 | void nameChanged(); | ||||
121 | void descriptionChanged(); | ||||
122 | void formFactorChanged(); | ||||
123 | void cardIndexChanged(); | ||||
124 | void portsChanged(); | ||||
125 | void activePortIndexChanged(); | ||||
126 | void defaultChanged(); | ||||
127 | | ||||
128 | protected: | ||||
129 | Device(QObject *parent); | ||||
130 | | ||||
131 | private: | ||||
132 | State stateFromPaState(int value) const; | ||||
133 | | ||||
134 | QString m_name; | ||||
135 | QString m_description; | ||||
136 | QString m_formFactor; | ||||
137 | quint32 m_cardIndex = -1; | ||||
138 | QList<QObject *> m_ports; | ||||
139 | quint32 m_activePortIndex = -1; | ||||
140 | State m_state = UnknownState; | ||||
141 | }; | ||||
142 | | ||||
143 | } // QPulseAudio | ||||
144 | | ||||
145 | #endif // PULSE_DEVICE_H |