PulseAudio Qt Bindings

server.cpp
1/*
2 SPDX-FileCopyrightText: 2016 David Rosca <nowrep@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "server.h"
8#include "server_p.h"
9
10#include "context.h"
11#include "context_p.h"
12#include "debug.h"
13#include "sink.h"
14#include "source.h"
15
16namespace PulseAudioQt
17{
18Server::Server(Context *context)
19 : QObject(context)
20 , d(new ServerPrivate(this))
21{
22 Q_ASSERT(context);
23
24 connect(&context->d->m_sinks, &MapBaseQObject::added, this, &Server::updateDefaultDevices);
25 connect(&context->d->m_sinks, &MapBaseQObject::removed, this, &Server::updateDefaultDevices);
26 connect(&context->d->m_sources, &MapBaseQObject::added, this, &Server::updateDefaultDevices);
27 connect(&context->d->m_sources, &MapBaseQObject::removed, this, &Server::updateDefaultDevices);
28}
29
30Server::~Server()
31{
32 delete d;
33}
34
35ServerPrivate::ServerPrivate(Server *q)
36 : q(q)
37 , m_defaultSink(nullptr)
38 , m_defaultSource(nullptr)
39{
40}
41
42ServerPrivate::~ServerPrivate()
43{
44}
45
46Sink *Server::defaultSink() const
47{
48 return d->m_defaultSink;
49}
50
51void Server::setDefaultSink(Sink *sink)
52{
53 Q_ASSERT(sink);
54 Context::instance()->setDefaultSink(sink->name());
55}
56
57Source *Server::defaultSource() const
58{
59 return d->m_defaultSource;
60}
61
62void Server::setDefaultSource(Source *source)
63{
64 Q_ASSERT(source);
65 Context::instance()->setDefaultSource(source->name());
66}
67
68void Server::reset()
69{
70 if (d->m_defaultSink) {
71 d->m_defaultSink = nullptr;
72 Q_EMIT defaultSinkChanged(d->m_defaultSink);
73 }
74
75 if (d->m_defaultSource) {
76 d->m_defaultSource = nullptr;
77 Q_EMIT defaultSourceChanged(d->m_defaultSource);
78 }
79}
80
81void ServerPrivate::update(const pa_server_info *info)
82{
83 m_defaultSinkName = QString::fromUtf8(info->default_sink_name);
84 m_defaultSourceName = QString::fromUtf8(info->default_source_name);
85
86 const bool isPw = QString::fromUtf8(info->server_name).contains("PipeWire");
87
88 if (isPw != m_isPipeWire) {
89 m_isPipeWire = isPw;
90 Q_EMIT q->isPipeWireChanged();
91 }
92
93 q->updateDefaultDevices();
94
95 Q_EMIT q->updated();
96}
97
98/** @private */
99template<typename Type, typename Vector>
100static Type *findByName(const Vector &vector, const QString &name)
101{
102 Type *out = nullptr;
103 if (name.isEmpty()) {
104 return out;
105 }
106 for (Type *t : vector) {
107 out = t;
108 if (out->name() == name) {
109 return out;
110 }
111 }
112 qCWarning(PULSEAUDIOQT) << "No object for name" << name;
113 return out;
114}
115
116void Server::updateDefaultDevices()
117{
118 Sink *sink = findByName<Sink>(Context::instance()->d->m_sinks.data(), d->m_defaultSinkName);
119 Source *source = findByName<Source>(Context::instance()->d->m_sources.data(), d->m_defaultSourceName);
120
121 if (d->m_defaultSink != sink) {
122 qCDebug(PULSEAUDIOQT) << "Default sink changed" << sink;
123 d->m_defaultSink = sink;
124 Q_EMIT defaultSinkChanged(d->m_defaultSink);
125 }
126
127 if (d->m_defaultSource != source) {
128 qCDebug(PULSEAUDIOQT) << "Default source changed" << source;
129 d->m_defaultSource = source;
130 Q_EMIT defaultSourceChanged(d->m_defaultSource);
131 }
132}
133
134bool Server::isPipeWire() const
135{
136 return d->m_isPipeWire;
137}
138
139} // PulseAudioQt
QString name(StandardShortcut id)
StandardShortcut findByName(const QString &name)
The primary namespace of PulseAudioQt.
Definition card.cpp:17
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.