PulseAudio Qt Bindings

server.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 David Rosca <[email protected]>
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 
16 namespace PulseAudioQt
17 {
18 Server::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 
30 Server::~Server()
31 {
32 }
33 
34 ServerPrivate::ServerPrivate(Server *q)
35  : q(q)
36  , m_defaultSink(nullptr)
37  , m_defaultSource(nullptr)
38 {
39 }
40 
41 ServerPrivate::~ServerPrivate()
42 {
43 }
44 
45 Sink *Server::defaultSink() const
46 {
47  return d->m_defaultSink;
48 }
49 
50 void Server::setDefaultSink(Sink *sink)
51 {
52  Q_ASSERT(sink);
53  Context::instance()->setDefaultSink(sink->name());
54 }
55 
56 Source *Server::defaultSource() const
57 {
58  return d->m_defaultSource;
59 }
60 
61 void Server::setDefaultSource(Source *source)
62 {
63  Q_ASSERT(source);
64  Context::instance()->setDefaultSource(source->name());
65 }
66 
67 void Server::reset()
68 {
69  if (d->m_defaultSink) {
70  d->m_defaultSink = nullptr;
71  Q_EMIT defaultSinkChanged(d->m_defaultSink);
72  }
73 
74  if (d->m_defaultSource) {
75  d->m_defaultSource = nullptr;
76  Q_EMIT defaultSourceChanged(d->m_defaultSource);
77  }
78 }
79 
80 void ServerPrivate::update(const pa_server_info *info)
81 {
82  m_defaultSinkName = QString::fromUtf8(info->default_sink_name);
83  m_defaultSourceName = QString::fromUtf8(info->default_source_name);
84 
85  const bool isPw = QString::fromUtf8(info->server_name).contains("PipeWire");
86 
87  if (isPw != m_isPipeWire) {
88  m_isPipeWire = isPw;
89  Q_EMIT q->isPipeWireChanged();
90  }
91 
92  q->updateDefaultDevices();
93 }
94 
95 /** @private */
96 template<typename Type, typename Vector>
97 static Type *findByName(const Vector &vector, const QString &name)
98 {
99  Type *out = nullptr;
100  if (name.isEmpty()) {
101  return out;
102  }
103  for (Type *t : vector) {
104  out = t;
105  if (out->name() == name) {
106  return out;
107  }
108  }
109  qCWarning(PULSEAUDIOQT) << "No object for name" << name;
110  return out;
111 }
112 
113 void Server::updateDefaultDevices()
114 {
115  Sink *sink = findByName<Sink>(Context::instance()->d->m_sinks.data(), d->m_defaultSinkName);
116  Source *source = findByName<Source>(Context::instance()->d->m_sources.data(), d->m_defaultSourceName);
117 
118  if (d->m_defaultSink != sink) {
119  qCDebug(PULSEAUDIOQT) << "Default sink changed" << sink;
120  d->m_defaultSink = sink;
121  Q_EMIT defaultSinkChanged(d->m_defaultSink);
122  }
123 
124  if (d->m_defaultSource != source) {
125  qCDebug(PULSEAUDIOQT) << "Default source changed" << source;
126  d->m_defaultSource = source;
127  Q_EMIT defaultSourceChanged(d->m_defaultSource);
128  }
129 }
130 
131 bool Server::isPipeWire() const
132 {
133  return d->m_isPipeWire;
134 }
135 
136 
137 } // PulseAudioQt
QString fromUtf8(const char *str, int size)
StandardShortcut findByName(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The primary namespace of PulseAudioQt.
Definition: card.cpp:16
bool isEmpty() const const
QString name(StandardShortcut id)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:12:40 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.