Changeset View
Changeset View
Standalone View
Standalone View
plugins/systemvolume/server.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2016 David Rosca <nowrep@gmail.com> | ||||
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 | #include "server.h" | ||||
22 | #include "context.h" | ||||
23 | #include "sink.h" | ||||
24 | #include "source.h" | ||||
25 | #include "debug.h" | ||||
26 | | ||||
27 | namespace QPulseAudio | ||||
28 | { | ||||
29 | | ||||
30 | Server::Server(Context *context) | ||||
31 | : QObject(context) | ||||
32 | , m_defaultSink(nullptr) | ||||
33 | , m_defaultSource(nullptr) | ||||
34 | { | ||||
35 | Q_ASSERT(context); | ||||
36 | | ||||
37 | connect(&context->sinks(), &MapBaseQObject::added, this, &Server::updateDefaultDevices); | ||||
38 | connect(&context->sinks(), &MapBaseQObject::removed, this, &Server::updateDefaultDevices); | ||||
39 | connect(&context->sources(), &MapBaseQObject::added, this, &Server::updateDefaultDevices); | ||||
40 | connect(&context->sources(), &MapBaseQObject::removed, this, &Server::updateDefaultDevices); | ||||
41 | } | ||||
42 | | ||||
43 | Sink *Server::defaultSink() const | ||||
44 | { | ||||
45 | return m_defaultSink; | ||||
46 | } | ||||
47 | | ||||
48 | void Server::setDefaultSink(Sink *sink) | ||||
49 | { | ||||
50 | Q_ASSERT(sink); | ||||
51 | Context::instance()->setDefaultSink(sink->name()); | ||||
52 | } | ||||
53 | | ||||
54 | Source *Server::defaultSource() const | ||||
55 | { | ||||
56 | return m_defaultSource; | ||||
57 | } | ||||
58 | | ||||
59 | void Server::setDefaultSource(Source *source) | ||||
60 | { | ||||
61 | Q_ASSERT(source); | ||||
62 | Context::instance()->setDefaultSource(source->name()); | ||||
63 | } | ||||
64 | | ||||
65 | void Server::reset() | ||||
66 | { | ||||
67 | if (m_defaultSink) { | ||||
68 | m_defaultSink = nullptr; | ||||
69 | emit defaultSinkChanged(m_defaultSink); | ||||
70 | } | ||||
71 | | ||||
72 | if (m_defaultSource) { | ||||
73 | m_defaultSource = nullptr; | ||||
74 | emit defaultSourceChanged(m_defaultSource); | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | void Server::update(const pa_server_info *info) | ||||
79 | { | ||||
80 | m_defaultSinkName = QString::fromUtf8(info->default_sink_name); | ||||
81 | m_defaultSourceName = QString::fromUtf8(info->default_source_name); | ||||
82 | | ||||
83 | updateDefaultDevices(); | ||||
84 | } | ||||
85 | | ||||
86 | template <typename Type, typename Map> | ||||
87 | static Type *findByName(const Map &map, const QString &name) | ||||
88 | { | ||||
89 | Type *out = nullptr; | ||||
90 | if (name.isEmpty()) { | ||||
91 | return out; | ||||
92 | } | ||||
93 | QMapIterator<quint32, Type *> it(map); | ||||
94 | while (it.hasNext()) { | ||||
95 | it.next(); | ||||
96 | out = it.value(); | ||||
97 | if (out->name() == name) { | ||||
98 | return out; | ||||
99 | } | ||||
100 | } | ||||
101 | qCWarning(PLASMAPA) << "No object for name" << name; | ||||
102 | return out; | ||||
103 | } | ||||
104 | | ||||
105 | void Server::updateDefaultDevices() | ||||
106 | { | ||||
107 | Sink *sink = findByName<Sink>(Context::instance()->sinks().data(), m_defaultSinkName); | ||||
108 | Source *source = findByName<Source>(Context::instance()->sources().data(), m_defaultSourceName); | ||||
109 | | ||||
110 | if (m_defaultSink != sink) { | ||||
111 | qCDebug(PLASMAPA) << "Default sink changed" << sink; | ||||
112 | m_defaultSink = sink; | ||||
113 | emit defaultSinkChanged(m_defaultSink); | ||||
114 | } | ||||
115 | | ||||
116 | if (m_defaultSource != source) { | ||||
117 | qCDebug(PLASMAPA) << "Default source changed" << source; | ||||
118 | m_defaultSource = source; | ||||
119 | emit defaultSourceChanged(m_defaultSource); | ||||
120 | } | ||||
121 | } | ||||
122 | | ||||
123 | } // QPulseAudio |