forked from linuxdeepin/dde-tray-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoundcontroller.cpp
More file actions
129 lines (108 loc) · 4.65 KB
/
Copy pathsoundcontroller.cpp
File metadata and controls
129 lines (108 loc) · 4.65 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "soundcontroller.h"
#include "soundmodel.h"
SoundController::SoundController(QObject *parent)
: QObject(parent)
, m_audioInter(new DBusAudio("org.deepin.dde.Audio1", "/org/deepin/dde/Audio1", QDBusConnection::sessionBus(), this))
, m_defaultSinkInter(nullptr)
, m_dconfig(DConfig::create("org.deepin.dde.tray-loader", "org.deepin.dde.dock.plugin.sound", QString(), this))
{
connect(m_audioInter, &DBusAudio::DefaultSinkChanged, this, &SoundController::onDefaultSinkChanged);
connect(m_audioInter, &DBusAudio::CardsWithoutUnavailableChanged, &SoundModel::ref(), &SoundModel::setCardsInfo);
connect(m_audioInter, &DBusAudio::MaxUIVolumeChanged, &SoundModel::ref(), &SoundModel::setMaxVolumeUI);
connect(m_audioInter, &DBusAudio::IncreaseVolumeChanged, this, [this] {
SoundModel::ref().setMaxVolumeUI(m_audioInter->maxUIVolume());
if (m_defaultSinkInter) {
SoundModel::ref().setVolume(m_defaultSinkInter->volume());
}
});
SoundModel::ref().setCardsInfo(m_audioInter->cardsWithoutUnavailable());
SoundModel::ref().setMaxVolumeUI(m_audioInter->maxUIVolume());
onDefaultSinkChanged(m_audioInter->defaultSink());
}
SoundController::~SoundController()
{
}
bool SoundController::mute()
{
if (!m_defaultSinkInter)
return false;
return m_defaultSinkInter->mute();
}
double SoundController::volume()
{
if (!m_defaultSinkInter)
return 0.0;
return m_defaultSinkInter->volume();
}
void SoundController::SetVolume(double in0, bool in1)
{
if (!m_defaultSinkInter)
return;
m_defaultSinkInter->SetVolume(in0, in1);
}
void SoundController::SetMuteQueued(bool in0)
{
if (!m_defaultSinkInter)
return;
m_defaultSinkInter->SetMuteQueued(in0);
}
void SoundController::SetMute(bool in0)
{
if (!m_defaultSinkInter)
return;
m_defaultSinkInter->SetMute(in0);
}
void SoundController::onDefaultSinkChanged(const QDBusObjectPath &path)
{
if (m_defaultSinkInter)
m_defaultSinkInter->disconnect(this);
// 防止手动切换设备,与后端交互时,获取到多个信号,设备切换多次,造成混乱
if (m_defaultSinkInter)
m_defaultSinkInter->deleteLater();
if (path.path() == "/" || path.path().isEmpty()) {
qWarning() << "default sink path is invalid, skip setting default sink interface.";
return;
}
m_defaultSinkInter = new DBusSink("org.deepin.dde.Audio1", path.path(), QDBusConnection::sessionBus(), this);
// 某些机型,如flmx,add和active先后时序有问题,active触发时,设备add还没执行,导致找不到需要active的设备项
// 这里手动刷新一下设备列表,再执行active操作
SoundModel::ref().setCardsInfo(m_audioInter->cardsWithoutUnavailable());
SoundModel::ref().setActivePort(m_defaultSinkInter->card(), m_defaultSinkInter->activePort().name);
SoundModel::ref().setMute(m_defaultSinkInter->mute());
SoundModel::ref().setVolume(existActiveOutputDevice() ? m_defaultSinkInter->volume() : 0);
// 音量和静音状态变化时手动获取下另外一个的状态,有时候收不到 changed 信号
connect(m_defaultSinkInter, &DBusSink::MuteChanged, &SoundModel::ref(), [this] (bool value) {
Q_UNUSED(value)
SoundModel::ref().setMute(m_defaultSinkInter->mute());
SoundModel::ref().setVolume(m_defaultSinkInter->volume());
});
connect(m_defaultSinkInter, &DBusSink::VolumeChanged, &SoundModel::ref(), [] (double value) {
SoundModel::ref().setVolume(value);
});
connect(m_defaultSinkInter, &DBusSink::ActivePortChanged, this, [this](AudioPort port) {
SoundModel::ref().setActivePort(m_defaultSinkInter->card(), port.name);
});
Q_EMIT defaultSinkChanged(m_defaultSinkInter);
}
/** 判断是否存在未禁用的声音输出设备
* @brief existActiveOutputDevice
* @return 存在返回true,否则返回false
*/
bool SoundController::existActiveOutputDevice() const
{
if (SoundModel::ref().existActiveOutputDevice()) {
return true;
}
if (m_dconfig.data()->isValid()) {
bool enableAdjustVolumeNoCard = m_dconfig.data()->value("enableAdjustVolumeNoCard", false).toBool();
if (enableAdjustVolumeNoCard) {
return m_defaultSinkInter;
}
}
// 兼容云平台无端口的情况
const QString &sinkName = m_defaultSinkInter ? m_defaultSinkInter->name() : QString();
return SoundModel::ref().ports().isEmpty() && !sinkName.isEmpty() && !sinkName.startsWith("auto_null") && !sinkName.contains("bluez");
}