759 lines
26 KiB
Diff
759 lines
26 KiB
Diff
|
|
From d627f399c25625fa3a0483b2bdcb25f2bf25fe6a Mon Sep 17 00:00:00 2001
|
|||
|
|
From: luoqing <luoqing@kylinsec.com.cn>
|
|||
|
|
Date: Thu, 29 Dec 2022 11:33:18 +0800
|
|||
|
|
Subject: [PATCH] fix(audio):Improve the disabling of volume plugin and tray
|
|||
|
|
MIME-Version: 1.0
|
|||
|
|
Content-Type: text/plain; charset=UTF-8
|
|||
|
|
Content-Transfer-Encoding: 8bit
|
|||
|
|
|
|||
|
|
- 完善音量插件和托盘显示禁用的情况
|
|||
|
|
---
|
|||
|
|
plugins/audio/src/plugin/input-page.cpp | 33 ++--
|
|||
|
|
plugins/audio/src/plugin/input-page.h | 4 +
|
|||
|
|
plugins/audio/src/plugin/output-page.cpp | 153 ++++++++++++------
|
|||
|
|
plugins/audio/src/plugin/output-page.h | 22 ++-
|
|||
|
|
.../src/system-tray/audio-system-tray.cpp | 14 ++
|
|||
|
|
.../audio/src/system-tray/audio-system-tray.h | 8 +-
|
|||
|
|
.../src/system-tray/volume-setting-page.cpp | 139 ++++++++++++----
|
|||
|
|
.../src/system-tray/volume-setting-page.h | 18 ++-
|
|||
|
|
8 files changed, 292 insertions(+), 99 deletions(-)
|
|||
|
|
|
|||
|
|
diff --git a/plugins/audio/src/plugin/input-page.cpp b/plugins/audio/src/plugin/input-page.cpp
|
|||
|
|
index 9dcd9bd..e9f5018 100644
|
|||
|
|
--- a/plugins/audio/src/plugin/input-page.cpp
|
|||
|
|
+++ b/plugins/audio/src/plugin/input-page.cpp
|
|||
|
|
@@ -167,6 +167,16 @@ qint64 AudioInfo::writeData(const char *data, qint64 len)
|
|||
|
|
InputPage::InputPage(QWidget *parent) : QWidget(parent), ui(new Ui::InputPage)
|
|||
|
|
{
|
|||
|
|
ui->setupUi(this);
|
|||
|
|
+ init();
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+InputPage::~InputPage()
|
|||
|
|
+{
|
|||
|
|
+ delete ui;
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+void InputPage::init()
|
|||
|
|
+{
|
|||
|
|
m_audioInterface = AudioInterface::instance();
|
|||
|
|
ui->inputVolume->setStyleSheet("color:#2eb3ff;");
|
|||
|
|
initInputDevice();
|
|||
|
|
@@ -176,16 +186,8 @@ InputPage::InputPage(QWidget *parent) : QWidget(parent), ui(new Ui::InputPage)
|
|||
|
|
initConnet();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-InputPage::~InputPage()
|
|||
|
|
-{
|
|||
|
|
- delete ui;
|
|||
|
|
-}
|
|||
|
|
-
|
|||
|
|
void InputPage::initInputDevice()
|
|||
|
|
{
|
|||
|
|
- QDBusPendingReply<QStringList> getSources = m_audioInterface->GetSources();
|
|||
|
|
- QStringList sourcesList = getSources.value();
|
|||
|
|
-
|
|||
|
|
QDBusPendingReply<QString> defaultSourcePath = m_audioInterface->GetDefaultSource();
|
|||
|
|
KLOG_DEBUG() << "defaultSourcePath" << defaultSourcePath;
|
|||
|
|
|
|||
|
|
@@ -236,11 +238,7 @@ void InputPage::initActivedPort()
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
KLOG_DEBUG() << "ports is null";
|
|||
|
|
- m_isValidPort = false;
|
|||
|
|
- ui->inputDevices->insertItem(0, tr("No input device detected"));
|
|||
|
|
- ui->inputDevices->setEnabled(false);
|
|||
|
|
- ui->volumeSetting->setValue(0);
|
|||
|
|
- ui->volumeSetting->setEnabled(false);
|
|||
|
|
+ disableSettings();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@@ -294,6 +292,15 @@ void InputPage::initConnet()
|
|||
|
|
connect(m_audioInterface, &AudioInterface::DefaultSourceChange, this, &InputPage::handleDefaultSourceChanged, Qt::QueuedConnection);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+void InputPage::disableSettings()
|
|||
|
|
+{
|
|||
|
|
+ m_isValidPort = false;
|
|||
|
|
+ ui->inputDevices->insertItem(0, tr("No input device detected"));
|
|||
|
|
+ ui->inputDevices->setEnabled(false);
|
|||
|
|
+ ui->volumeSetting->setValue(0);
|
|||
|
|
+ ui->volumeSetting->setEnabled(false);
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
void InputPage::handleActivePortChanged(const QString &value)
|
|||
|
|
{
|
|||
|
|
KLOG_DEBUG() << "handleActivePortChanged :" << value;
|
|||
|
|
diff --git a/plugins/audio/src/plugin/input-page.h b/plugins/audio/src/plugin/input-page.h
|
|||
|
|
index 95b8aa4..3b57daf 100644
|
|||
|
|
--- a/plugins/audio/src/plugin/input-page.h
|
|||
|
|
+++ b/plugins/audio/src/plugin/input-page.h
|
|||
|
|
@@ -62,10 +62,14 @@ public:
|
|||
|
|
explicit InputPage(QWidget *parent = nullptr);
|
|||
|
|
~InputPage() override;
|
|||
|
|
QSize sizeHint() const override;
|
|||
|
|
+
|
|||
|
|
+private:
|
|||
|
|
+ void init();
|
|||
|
|
void initInputDevice();
|
|||
|
|
void initInputSettins();
|
|||
|
|
void initActivedPort();
|
|||
|
|
void initConnet();
|
|||
|
|
+ void disableSettings();
|
|||
|
|
|
|||
|
|
void initVoulumeFeedBack();
|
|||
|
|
void initAudioFormat();
|
|||
|
|
diff --git a/plugins/audio/src/plugin/output-page.cpp b/plugins/audio/src/plugin/output-page.cpp
|
|||
|
|
index cc19e07..b28ba53 100644
|
|||
|
|
--- a/plugins/audio/src/plugin/output-page.cpp
|
|||
|
|
+++ b/plugins/audio/src/plugin/output-page.cpp
|
|||
|
|
@@ -21,32 +21,74 @@
|
|||
|
|
#include <qt5-log-i.h>
|
|||
|
|
#include <QComboBox>
|
|||
|
|
|
|||
|
|
-OutputPage::OutputPage(QWidget *parent) : QWidget(parent), ui(new Ui::OutputPage)
|
|||
|
|
+OutputPage::OutputPage(QWidget *parent) : QWidget(parent),
|
|||
|
|
+ ui(new Ui::OutputPage),
|
|||
|
|
+ m_audioInterface(nullptr),
|
|||
|
|
+ m_defaultSink(nullptr)
|
|||
|
|
{
|
|||
|
|
ui->setupUi(this);
|
|||
|
|
- ui->outputVolume->setStyleSheet("color:#2eb3ff;");
|
|||
|
|
- m_audioInterface = AudioInterface::instance();
|
|||
|
|
- initOutputDevice();
|
|||
|
|
- initOutputSettins();
|
|||
|
|
- initConnect();
|
|||
|
|
+ init();
|
|||
|
|
+
|
|||
|
|
+ m_dbusServiceWatcher = new QDBusServiceWatcher();
|
|||
|
|
+ m_dbusServiceWatcher->setConnection(QDBusConnection::sessionBus());
|
|||
|
|
+ m_dbusServiceWatcher->addWatchedService(AUDIO_DBUS_NAME);
|
|||
|
|
+
|
|||
|
|
+ m_dbusServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
|
|||
|
|
+ connect(m_dbusServiceWatcher, &QDBusServiceWatcher::serviceUnregistered,[this](const QString &service){
|
|||
|
|
+ KLOG_DEBUG() << "serviceUnregistered:" << service;
|
|||
|
|
+ disableSettings();
|
|||
|
|
+ });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
OutputPage::~OutputPage()
|
|||
|
|
{
|
|||
|
|
+ delete m_dbusServiceWatcher;
|
|||
|
|
delete ui;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-void OutputPage::initOutputDevice()
|
|||
|
|
+void OutputPage::init()
|
|||
|
|
{
|
|||
|
|
- QDBusPendingReply<QStringList> getSinks = m_audioInterface->GetSinks();
|
|||
|
|
- QStringList sinksList = getSinks.value();
|
|||
|
|
+ ui->outputVolume->setStyleSheet("color:#2eb3ff;");
|
|||
|
|
+ ui->volumeSetting->setRange(0, 100);
|
|||
|
|
+ ui->volumeSetting->setSingleStep(1);
|
|||
|
|
+ ui->volumeSetting->setPageStep(1);
|
|||
|
|
+
|
|||
|
|
+ ui->volumeBalance->setRange(-100, 100);
|
|||
|
|
+ ui->volumeBalance->setSingleStep(1);
|
|||
|
|
+ ui->volumeBalance->setPageStep(1);
|
|||
|
|
+
|
|||
|
|
+ m_audioInterface = AudioInterface::instance();
|
|||
|
|
+ initOutputDevice();
|
|||
|
|
+ initConnect();
|
|||
|
|
+}
|
|||
|
|
|
|||
|
|
+void OutputPage::initOutputDevice()
|
|||
|
|
+{
|
|||
|
|
QDBusPendingReply<QString> defaultSinkPath = m_audioInterface->GetDefaultSink();
|
|||
|
|
KLOG_DEBUG() << "defaultSink" << defaultSinkPath;
|
|||
|
|
-
|
|||
|
|
- m_defaultSink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus(), this);
|
|||
|
|
- m_defaultSinkIndex = m_defaultSink->index();
|
|||
|
|
- initActivedPort();
|
|||
|
|
+ KLOG_DEBUG() << "defaultSinkPath.isValid():" << defaultSinkPath.isValid();
|
|||
|
|
+
|
|||
|
|
+ if(defaultSinkPath.isValid())
|
|||
|
|
+ {
|
|||
|
|
+ QString defaultSinkPathString = defaultSinkPath.value();
|
|||
|
|
+ if(!defaultSinkPathString.isEmpty())
|
|||
|
|
+ {
|
|||
|
|
+ m_defaultSink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus(), this);
|
|||
|
|
+ initActivedPort();
|
|||
|
|
+
|
|||
|
|
+ connect(m_defaultSink, &AudioDeviceInterface::volumeChanged, this, &OutputPage::handleVolumeChanged);
|
|||
|
|
+ connect(m_defaultSink, &AudioDeviceInterface::balanceChanged, this, &OutputPage::handleBalanceChanged);
|
|||
|
|
+ connect(m_defaultSink, &AudioDeviceInterface::active_portChanged, this, &OutputPage::handleActivePortChanged);
|
|||
|
|
+ }
|
|||
|
|
+ else
|
|||
|
|
+ {
|
|||
|
|
+ disableSettings();
|
|||
|
|
+ }
|
|||
|
|
+ }
|
|||
|
|
+ else
|
|||
|
|
+ {
|
|||
|
|
+ KLOG_DEBUG() << "defaultSinkPath error:" <<defaultSinkPath.error();
|
|||
|
|
+ }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OutputPage::initActivedPort()
|
|||
|
|
@@ -79,53 +121,40 @@ void OutputPage::initActivedPort()
|
|||
|
|
if (m_defaultSink->active_port() == name)
|
|||
|
|
{
|
|||
|
|
m_defaultDeviceIndex = i;
|
|||
|
|
- KLOG_DEBUG() << "m_defaultDeviceIndex" << m_defaultDeviceIndex;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//默认选中已激活的端口
|
|||
|
|
ui->outputDevices->setCurrentIndex(m_defaultDeviceIndex);
|
|||
|
|
- ui->outputDevices->setEnabled(true);
|
|||
|
|
- ui->volumeSetting->setEnabled(true);
|
|||
|
|
- ui->volumeBalance->setEnabled(true);
|
|||
|
|
+ initOutputSettins();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//无激活端口则禁用音量设置和平衡
|
|||
|
|
KLOG_DEBUG() << "default sink ports is null";
|
|||
|
|
- ui->outputDevices->insertItem(0, tr("No output device detected"));
|
|||
|
|
- ui->outputDevices->setEnabled(false);
|
|||
|
|
- ui->volumeSetting->setValue(0);
|
|||
|
|
- ui->volumeSetting->setEnabled(false);
|
|||
|
|
- ui->volumeBalance->setValue(0);
|
|||
|
|
- ui->volumeBalance->setEnabled(false);
|
|||
|
|
+ disableSettings();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OutputPage::initOutputSettins()
|
|||
|
|
{
|
|||
|
|
- initVolumeSetting();
|
|||
|
|
- initBalanceSetting();
|
|||
|
|
+ ui->outputDevices->setEnabled(true);
|
|||
|
|
+ ui->volumeSetting->setEnabled(true);
|
|||
|
|
+ ui->volumeBalance->setEnabled(true);
|
|||
|
|
+ initVolumeValue();
|
|||
|
|
+ initBalanceValue();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-void OutputPage::initVolumeSetting()
|
|||
|
|
+void OutputPage::initVolumeValue()
|
|||
|
|
{
|
|||
|
|
- ui->volumeSetting->setRange(0, 100);
|
|||
|
|
- ui->volumeSetting->setSingleStep(1);
|
|||
|
|
- ui->volumeSetting->setPageStep(1);
|
|||
|
|
-
|
|||
|
|
double currentVolumeDouble = m_defaultSink->volume() * 100;
|
|||
|
|
int currentVolume = round(currentVolumeDouble);
|
|||
|
|
ui->volumeSetting->setValue(currentVolume);
|
|||
|
|
ui->outputVolume->setText(QString::number(currentVolume) + "%");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-void OutputPage::initBalanceSetting()
|
|||
|
|
+void OutputPage::initBalanceValue()
|
|||
|
|
{
|
|||
|
|
- ui->volumeBalance->setRange(-100, 100);
|
|||
|
|
- ui->volumeBalance->setSingleStep(1);
|
|||
|
|
- ui->volumeBalance->setPageStep(1);
|
|||
|
|
-
|
|||
|
|
KLOG_DEBUG() << "current balance:" << m_defaultSink->balance();
|
|||
|
|
double currentBalanceDouble = m_defaultSink->balance() * 100;
|
|||
|
|
int currentBalance = round(currentBalanceDouble);
|
|||
|
|
@@ -134,9 +163,6 @@ void OutputPage::initBalanceSetting()
|
|||
|
|
|
|||
|
|
void OutputPage::initConnect()
|
|||
|
|
{
|
|||
|
|
- connect(m_defaultSink, &AudioDeviceInterface::volumeChanged, this, &OutputPage::handleVolumeChanged);
|
|||
|
|
- connect(m_defaultSink, &AudioDeviceInterface::balanceChanged, this, &OutputPage::handleBalanceChanged);
|
|||
|
|
- connect(m_defaultSink, &AudioDeviceInterface::active_portChanged, this, &OutputPage::handleActivePortChanged);
|
|||
|
|
connect(m_audioInterface, &AudioInterface::SinkAdded, this, &OutputPage::handleSinkAdded);
|
|||
|
|
connect(m_audioInterface, &AudioInterface::SinkDelete, this, &OutputPage::handleSinkDelete);
|
|||
|
|
connect(m_audioInterface, &AudioInterface::DefaultSinkChange, this, &OutputPage::handleDefaultSinkChanged, Qt::QueuedConnection);
|
|||
|
|
@@ -204,31 +230,68 @@ void OutputPage::handleBalanceChanged(double value)
|
|||
|
|
ui->volumeBalance->blockSignals(false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
-void OutputPage::handleSinkAdded(int index)
|
|||
|
|
+void OutputPage::disableSettings()
|
|||
|
|
{
|
|||
|
|
- KLOG_DEBUG() << "SinkAdded";
|
|||
|
|
+ ui->outputDevices->insertItem(0, tr("No output device detected"));
|
|||
|
|
+ ui->outputDevices->setEnabled(false);
|
|||
|
|
+
|
|||
|
|
+ ui->volumeSetting->setValue(0);
|
|||
|
|
+ ui->outputVolume->setText(QString::number(0) + "%");
|
|||
|
|
+ ui->volumeSetting->setEnabled(false);
|
|||
|
|
+
|
|||
|
|
+ ui->volumeBalance->setValue(0);
|
|||
|
|
+ ui->volumeBalance->setEnabled(false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OutputPage::handleDefaultSinkChanged(int index)
|
|||
|
|
{
|
|||
|
|
// delete and restart init defaultSource
|
|||
|
|
- m_defaultSink->deleteLater();
|
|||
|
|
- m_defaultSink = nullptr;
|
|||
|
|
+ if(m_defaultSink != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ m_defaultSink->deleteLater();
|
|||
|
|
+ m_defaultSink = nullptr;
|
|||
|
|
+ }
|
|||
|
|
ui->outputDevices->blockSignals(true);
|
|||
|
|
ui->outputDevices->clear();
|
|||
|
|
ui->outputDevices->blockSignals(false);
|
|||
|
|
|
|||
|
|
initOutputDevice();
|
|||
|
|
initOutputSettins();
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
|
|||
|
|
- connect(m_defaultSink, &AudioDeviceInterface::volumeChanged, this, &OutputPage::handleVolumeChanged);
|
|||
|
|
- connect(m_defaultSink, &AudioDeviceInterface::balanceChanged, this, &OutputPage::handleBalanceChanged);
|
|||
|
|
- connect(m_defaultSink, &AudioDeviceInterface::active_portChanged, this, &OutputPage::handleActivePortChanged);
|
|||
|
|
+void OutputPage::handleSinkAdded(int index)
|
|||
|
|
+{
|
|||
|
|
+ KLOG_DEBUG() << "SinkAdded";
|
|||
|
|
+ //当已经存在defaultSink时,暂时不处理其他sink的添加
|
|||
|
|
+ if(m_defaultSink != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ //刷新界面
|
|||
|
|
+ initOutputSettins();
|
|||
|
|
+ }
|
|||
|
|
+ else
|
|||
|
|
+ {
|
|||
|
|
+ //defaultSink不存在,则重新初始化设备
|
|||
|
|
+ initOutputDevice();
|
|||
|
|
+ }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+//当pulseAudio被kill时,会发出SinkDelete和SourceDelete信号
|
|||
|
|
void OutputPage::handleSinkDelete(int index)
|
|||
|
|
{
|
|||
|
|
KLOG_DEBUG() << "SinkDelete";
|
|||
|
|
+ QDBusPendingReply<QStringList> getSinks = m_audioInterface->GetSinks();
|
|||
|
|
+ QStringList sinksList = getSinks.value();
|
|||
|
|
+
|
|||
|
|
+ //当前存在defaultSink
|
|||
|
|
+ if(m_defaultSink != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ //删除的是defaultSink则进行处理,删除其他sink暂时不处理
|
|||
|
|
+ if(m_defaultSink->index() == index)
|
|||
|
|
+ {
|
|||
|
|
+ disableSettings();
|
|||
|
|
+ }
|
|||
|
|
+ }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QSize OutputPage::sizeHint() const
|
|||
|
|
diff --git a/plugins/audio/src/plugin/output-page.h b/plugins/audio/src/plugin/output-page.h
|
|||
|
|
index 76ac458..abaf9a1 100644
|
|||
|
|
--- a/plugins/audio/src/plugin/output-page.h
|
|||
|
|
+++ b/plugins/audio/src/plugin/output-page.h
|
|||
|
|
@@ -16,6 +16,7 @@
|
|||
|
|
|
|||
|
|
#include <QMap>
|
|||
|
|
#include <QWidget>
|
|||
|
|
+#include <QDBusServiceWatcher>
|
|||
|
|
|
|||
|
|
QT_BEGIN_NAMESPACE
|
|||
|
|
namespace Ui
|
|||
|
|
@@ -35,12 +36,7 @@ public:
|
|||
|
|
explicit OutputPage(QWidget *parent = nullptr);
|
|||
|
|
~OutputPage() override;
|
|||
|
|
QSize sizeHint() const override;
|
|||
|
|
- void initOutputDevice();
|
|||
|
|
- void initOutputSettins();
|
|||
|
|
- void initConnect();
|
|||
|
|
- void initActivedPort();
|
|||
|
|
- void initVolumeSetting();
|
|||
|
|
- void initBalanceSetting();
|
|||
|
|
+
|
|||
|
|
public slots:
|
|||
|
|
void handleDefaultSinkChanged(int index);
|
|||
|
|
void handleSinkAdded(int index);
|
|||
|
|
@@ -49,13 +45,25 @@ public slots:
|
|||
|
|
void handleVolumeChanged(double value);
|
|||
|
|
void handleBalanceChanged(double value);
|
|||
|
|
|
|||
|
|
+private:
|
|||
|
|
+ void init();
|
|||
|
|
+ void initOutputDevice();
|
|||
|
|
+ void initOutputSettins();
|
|||
|
|
+ void initConnect();
|
|||
|
|
+ void initActivedPort();
|
|||
|
|
+ void initVolumeValue();
|
|||
|
|
+ void initBalanceValue();
|
|||
|
|
+
|
|||
|
|
+ void disableSettings();
|
|||
|
|
+
|
|||
|
|
private:
|
|||
|
|
Ui::OutputPage *ui;
|
|||
|
|
AudioInterface *m_audioInterface;
|
|||
|
|
QMap<int, AudioDeviceInterface *> m_outputDevicesMap;
|
|||
|
|
AudioDeviceInterface *m_defaultSink;
|
|||
|
|
- int m_defaultSinkIndex;
|
|||
|
|
int m_defaultDeviceIndex;
|
|||
|
|
+ QDBusServiceWatcher *m_dbusServiceWatcher;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
+
|
|||
|
|
#endif //KIRAN_CPANEL_AUDIO_OUTPUT_PAGE_H
|
|||
|
|
diff --git a/plugins/audio/src/system-tray/audio-system-tray.cpp b/plugins/audio/src/system-tray/audio-system-tray.cpp
|
|||
|
|
index 7fcb9c3..00e90f7 100644
|
|||
|
|
--- a/plugins/audio/src/system-tray/audio-system-tray.cpp
|
|||
|
|
+++ b/plugins/audio/src/system-tray/audio-system-tray.cpp
|
|||
|
|
@@ -47,8 +47,10 @@ AudioSystemTray::AudioSystemTray(QWidget *parent) : QWidget(parent)
|
|||
|
|
|
|||
|
|
initTrayIcon();
|
|||
|
|
initMenu();
|
|||
|
|
+ initDbusServiceWatcher();
|
|||
|
|
initConnect();
|
|||
|
|
}
|
|||
|
|
+
|
|||
|
|
AudioSystemTray::~AudioSystemTray()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
@@ -99,6 +101,18 @@ void AudioSystemTray::initMenu()
|
|||
|
|
connect(m_mixedSetting, &QAction::triggered, this, &AudioSystemTray::handleMixedSettingClicked);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+void AudioSystemTray::initDbusServiceWatcher()
|
|||
|
|
+{
|
|||
|
|
+ m_dbusServiceWatcher = new QDBusServiceWatcher();
|
|||
|
|
+ m_dbusServiceWatcher->setConnection(QDBusConnection::sessionBus());
|
|||
|
|
+ m_dbusServiceWatcher->addWatchedService(AUDIO_DBUS_NAME);
|
|||
|
|
+ m_dbusServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
|
|||
|
|
+ connect(m_dbusServiceWatcher, &QDBusServiceWatcher::serviceUnregistered, [this](const QString &service)
|
|||
|
|
+ {
|
|||
|
|
+ setTrayIcon(0);
|
|||
|
|
+ });
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
void AudioSystemTray::initConnect()
|
|||
|
|
{
|
|||
|
|
connect(m_systemTray, &QSystemTrayIcon::activated, this, &AudioSystemTray::handleAudioTrayClicked);
|
|||
|
|
diff --git a/plugins/audio/src/system-tray/audio-system-tray.h b/plugins/audio/src/system-tray/audio-system-tray.h
|
|||
|
|
index 623c602..16846e6 100644
|
|||
|
|
--- a/plugins/audio/src/system-tray/audio-system-tray.h
|
|||
|
|
+++ b/plugins/audio/src/system-tray/audio-system-tray.h
|
|||
|
|
@@ -17,8 +17,7 @@
|
|||
|
|
|
|||
|
|
#include <QSystemTrayIcon>
|
|||
|
|
#include <QWidget>
|
|||
|
|
-// #include <QVBoxLayout>
|
|||
|
|
-// #include <QScrollArea>
|
|||
|
|
+#include <QDBusServiceWatcher>
|
|||
|
|
|
|||
|
|
class VolumeSettingPage;
|
|||
|
|
class MixedSettingPage;
|
|||
|
|
@@ -38,12 +37,13 @@ public:
|
|||
|
|
void initTrayIcon();
|
|||
|
|
void initMenu();
|
|||
|
|
void initConnect();
|
|||
|
|
+ void initDbusServiceWatcher();
|
|||
|
|
void setVolumeSettingPos();
|
|||
|
|
void setMixedSettingPos();
|
|||
|
|
QPixmap trayIconColorSwitch(const QString& iconPath, const int iconSize = 16);
|
|||
|
|
void getTrayGeometry();
|
|||
|
|
-public slots:
|
|||
|
|
|
|||
|
|
+public slots:
|
|||
|
|
void handleAudioTrayClicked(QSystemTrayIcon::ActivationReason reason);
|
|||
|
|
void handleMixedSettingClicked();
|
|||
|
|
void handleVolumeSettingClicked();
|
|||
|
|
@@ -72,6 +72,8 @@ private:
|
|||
|
|
|
|||
|
|
QString m_colorTheme;
|
|||
|
|
int xTray, yTray, heightTray, widthTray;
|
|||
|
|
+
|
|||
|
|
+ QDBusServiceWatcher *m_dbusServiceWatcher;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif // KIRAN_CPANEL_AUDIO_AUDIOSYSTEMTRAY_H
|
|||
|
|
diff --git a/plugins/audio/src/system-tray/volume-setting-page.cpp b/plugins/audio/src/system-tray/volume-setting-page.cpp
|
|||
|
|
index dc43283..fac69a6 100644
|
|||
|
|
--- a/plugins/audio/src/system-tray/volume-setting-page.cpp
|
|||
|
|
+++ b/plugins/audio/src/system-tray/volume-setting-page.cpp
|
|||
|
|
@@ -26,26 +26,38 @@
|
|||
|
|
#include <QStyleOption>
|
|||
|
|
#include <QSvgRenderer>
|
|||
|
|
|
|||
|
|
-VolumeSettingPage::VolumeSettingPage(enum AudioNode audio, QString objectPath, QWidget *parent) : QWidget(parent), ui(new Ui::VolumeSettingPage)
|
|||
|
|
+VolumeSettingPage::VolumeSettingPage(enum AudioNode audio, const QString objectPath, QWidget *parent) : QWidget(parent), ui(new Ui::VolumeSettingPage)
|
|||
|
|
{
|
|||
|
|
ui->setupUi(this);
|
|||
|
|
ui->volume->setStyleSheet("color:#2eb3ff;");
|
|||
|
|
QDBusConnection session = QDBusConnection::sessionBus();
|
|||
|
|
m_audioInterface = AudioInterface::instance();
|
|||
|
|
- audioNode = audio;
|
|||
|
|
- if (audioNode == AUDIO_DEVICE)
|
|||
|
|
+ m_audioNode = audio;
|
|||
|
|
+
|
|||
|
|
+ if (m_audioNode == AUDIO_DEVICE)
|
|||
|
|
{
|
|||
|
|
- KLOG_DEBUG() << "AUDIO_DEVICE";
|
|||
|
|
- m_sink = new AudioDeviceInterface(AUDIO_DBUS_NAME, objectPath, session, this);
|
|||
|
|
+ QDBusPendingReply<QString> defaultSinkPath = m_audioInterface->GetDefaultSink();
|
|||
|
|
+ m_sink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus(), this);
|
|||
|
|
initAudioDevice();
|
|||
|
|
+
|
|||
|
|
+ connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
|
|||
|
|
+ connect(ui->volumeSetting, &QSlider::valueChanged, [this](int value)
|
|||
|
|
+ {
|
|||
|
|
+ double volumeValue = static_cast<double>(value) / static_cast<double>(100);
|
|||
|
|
+ m_sink->SetVolume(volumeValue); });
|
|||
|
|
+
|
|||
|
|
+ connect(m_audioInterface, &AudioInterface::SinkAdded, this, &VolumeSettingPage::handleSinkAdded);
|
|||
|
|
+ connect(m_audioInterface, &AudioInterface::SinkDelete, this, &VolumeSettingPage::handleSinkDelete);
|
|||
|
|
+ connect(m_audioInterface, &AudioInterface::DefaultSinkChange, this, &VolumeSettingPage::handleDefaultSinkChanged, Qt::QueuedConnection);
|
|||
|
|
}
|
|||
|
|
- else if (audioNode == AUDIO_STREAM)
|
|||
|
|
+ else if (m_audioNode == AUDIO_STREAM)
|
|||
|
|
{
|
|||
|
|
- KLOG_DEBUG() << "AUDIO_STREAM";
|
|||
|
|
m_sinkInput = new AudioStreamInterface(AUDIO_DBUS_NAME, objectPath, session, this);
|
|||
|
|
initAudioStream();
|
|||
|
|
}
|
|||
|
|
connect(ui->muteButton, &QPushButton::clicked, this, &VolumeSettingPage::handleMuteButtonClicked);
|
|||
|
|
+
|
|||
|
|
+ initDbusServiceWatcher();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
VolumeSettingPage::~VolumeSettingPage()
|
|||
|
|
@@ -53,22 +65,47 @@ VolumeSettingPage::~VolumeSettingPage()
|
|||
|
|
delete ui;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+void VolumeSettingPage::initDbusServiceWatcher()
|
|||
|
|
+{
|
|||
|
|
+ m_dbusServiceWatcher = new QDBusServiceWatcher();
|
|||
|
|
+ m_dbusServiceWatcher->setConnection(QDBusConnection::sessionBus());
|
|||
|
|
+ m_dbusServiceWatcher->addWatchedService(AUDIO_DBUS_NAME);
|
|||
|
|
+ m_dbusServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
|
|||
|
|
+ connect(m_dbusServiceWatcher, &QDBusServiceWatcher::serviceUnregistered, [this](const QString &service)
|
|||
|
|
+ {
|
|||
|
|
+ KLOG_DEBUG() << "serviceUnregistered:" << service;
|
|||
|
|
+ disableSettings(); });
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
void VolumeSettingPage::initAudioDevice()
|
|||
|
|
{
|
|||
|
|
- initSettings(m_sink);
|
|||
|
|
+ KLOG_DEBUG() << "AUDIO_DEVICE";
|
|||
|
|
+ QDBusPendingReply<QString> getPorts = m_sink->GetPorts();
|
|||
|
|
+ // 解析默认sink的端口信息
|
|||
|
|
+ QJsonParseError jsonParseError;
|
|||
|
|
+ QJsonDocument doc = QJsonDocument::fromJson(getPorts.value().toLatin1(), &jsonParseError);
|
|||
|
|
+ if (!doc.isNull() && jsonParseError.error == QJsonParseError::NoError)
|
|||
|
|
+ {
|
|||
|
|
+ initSettings(m_sink);
|
|||
|
|
+ }
|
|||
|
|
+ else
|
|||
|
|
+ {
|
|||
|
|
+ // 无激活端口则禁用音量设置
|
|||
|
|
+ disableSettings();
|
|||
|
|
+ }
|
|||
|
|
ui->volumeName->setText(tr("Volume"));
|
|||
|
|
- connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
|
|||
|
|
- connect(m_sink, &AudioDeviceInterface::muteChanged, [=](bool value)
|
|||
|
|
- { KLOG_DEBUG() << "m_sink muteChanged:" << value; });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void VolumeSettingPage::initAudioStream()
|
|||
|
|
{
|
|||
|
|
+ KLOG_DEBUG() << "AUDIO_STREAM";
|
|||
|
|
initSettings(m_sinkInput);
|
|||
|
|
ui->volumeName->setText(m_sinkInput->GetProperty("application.name"));
|
|||
|
|
connect(m_sinkInput, &AudioStreamInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
|
|||
|
|
- connect(m_sinkInput, &AudioStreamInterface::muteChanged, [=](bool value)
|
|||
|
|
- { KLOG_DEBUG() << "m_sinkInput muteChanged:" << value; });
|
|||
|
|
+ connect(ui->volumeSetting, &QSlider::valueChanged, [=](int value)
|
|||
|
|
+ {
|
|||
|
|
+ double volumeValue = static_cast<double>(value) / static_cast<double>(100);
|
|||
|
|
+ m_sinkInput->SetVolume(volumeValue); });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
template <class Audio>
|
|||
|
|
@@ -77,6 +114,7 @@ void VolumeSettingPage::initSettings(Audio *audio)
|
|||
|
|
ui->volumeSetting->setRange(0, 100);
|
|||
|
|
ui->volumeSetting->setSingleStep(1);
|
|||
|
|
ui->volumeSetting->setPageStep(1);
|
|||
|
|
+ ui->volumeSetting->setEnabled(true);
|
|||
|
|
|
|||
|
|
KLOG_DEBUG() << "current volume:" << audio->volume();
|
|||
|
|
double currentVolumeDouble = audio->volume() * 100;
|
|||
|
|
@@ -84,17 +122,12 @@ void VolumeSettingPage::initSettings(Audio *audio)
|
|||
|
|
setVolumeIcon(currentVolume);
|
|||
|
|
ui->volumeSetting->setValue(currentVolume);
|
|||
|
|
ui->volume->setText(QString::number(currentVolume) + "%");
|
|||
|
|
-
|
|||
|
|
- connect(ui->volumeSetting, &QSlider::valueChanged, [=](int value)
|
|||
|
|
- {
|
|||
|
|
- double volumeValue = static_cast<double>(value) / static_cast<double>(100);
|
|||
|
|
- audio->SetVolume(volumeValue); });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void VolumeSettingPage::handleVolumeChanged(double value)
|
|||
|
|
{
|
|||
|
|
- ui->volumeSetting->blockSignals(true); //为了避免拖动的同时设置位置会出现问题
|
|||
|
|
- int currentVolume = round(value * 100); //表示数值的时候向上取整
|
|||
|
|
+ ui->volumeSetting->blockSignals(true); // 为了避免拖动的同时设置位置会出现问题
|
|||
|
|
+ int currentVolume = round(value * 100); // 表示数值的时候向上取整
|
|||
|
|
ui->volume->setText(QString::number(currentVolume) + "%");
|
|||
|
|
setVolumeIcon(currentVolume);
|
|||
|
|
ui->volumeSetting->setValue(currentVolume);
|
|||
|
|
@@ -103,12 +136,56 @@ void VolumeSettingPage::handleVolumeChanged(double value)
|
|||
|
|
|
|||
|
|
void VolumeSettingPage::handleMuteButtonClicked()
|
|||
|
|
{
|
|||
|
|
- if (audioNode == AUDIO_DEVICE)
|
|||
|
|
+ if (m_audioNode == AUDIO_DEVICE)
|
|||
|
|
clickMuteButton(m_sink);
|
|||
|
|
else
|
|||
|
|
clickMuteButton(m_sinkInput);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+void VolumeSettingPage::handleDefaultSinkChanged(int index)
|
|||
|
|
+{
|
|||
|
|
+ // delete and restart init defaultSink
|
|||
|
|
+ if (m_sink != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ m_sink->deleteLater();
|
|||
|
|
+ m_sink = nullptr;
|
|||
|
|
+ }
|
|||
|
|
+
|
|||
|
|
+ QDBusPendingReply<QString> defaultSinkPath = m_audioInterface->GetDefaultSink();
|
|||
|
|
+ m_sink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus(), this);
|
|||
|
|
+ initAudioDevice();
|
|||
|
|
+ connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+void VolumeSettingPage::handleSinkAdded(int index)
|
|||
|
|
+{
|
|||
|
|
+ KLOG_DEBUG() << "SinkAdded";
|
|||
|
|
+ // 当已经存在defaultSink时,暂时不处理其他sink的添加
|
|||
|
|
+ if (m_sink != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ // 刷新界面
|
|||
|
|
+ initSettings(m_sink);
|
|||
|
|
+ }
|
|||
|
|
+ else
|
|||
|
|
+ {
|
|||
|
|
+ // defaultSink不存在,则重新初始化设备
|
|||
|
|
+ initAudioDevice();
|
|||
|
|
+ }
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
+void VolumeSettingPage::handleSinkDelete(int index)
|
|||
|
|
+{
|
|||
|
|
+ // 当前存在Sink
|
|||
|
|
+ if (m_sink != nullptr)
|
|||
|
|
+ {
|
|||
|
|
+ // 删除的是defaultSink则进行处理,删除其他sink暂时不处理
|
|||
|
|
+ if (m_sink->index() == index)
|
|||
|
|
+ {
|
|||
|
|
+ disableSettings();
|
|||
|
|
+ }
|
|||
|
|
+ }
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
template <class Audio>
|
|||
|
|
void VolumeSettingPage::clickMuteButton(Audio *audio)
|
|||
|
|
{
|
|||
|
|
@@ -120,19 +197,17 @@ void VolumeSettingPage::clickMuteButton(Audio *audio)
|
|||
|
|
KLOG_DEBUG() << "m_sink->mute() :" << audio->mute();
|
|||
|
|
if (!audio->mute())
|
|||
|
|
{
|
|||
|
|
- volumeBeforeMute = currentVolume;
|
|||
|
|
- KLOG_DEBUG() << "volumeBeforeMute:" << volumeBeforeMute;
|
|||
|
|
+ m_volumeBeforeMute = currentVolume;
|
|||
|
|
audio->SetMute(true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
- if (volumeBeforeMute != 0)
|
|||
|
|
+ if (m_volumeBeforeMute != 0)
|
|||
|
|
{
|
|||
|
|
- KLOG_DEBUG() << "SetVolume volumeBeforeMute:" << volumeBeforeMute;
|
|||
|
|
- audio->SetVolume(static_cast<double>(volumeBeforeMute) / static_cast<double>(100));
|
|||
|
|
- volumeBeforeMute = 0;
|
|||
|
|
- KLOG_DEBUG() << "volumeBeforeMute = 0";
|
|||
|
|
+ KLOG_DEBUG() << "SetVolume m_volumeBeforeMute:" << m_volumeBeforeMute;
|
|||
|
|
+ audio->SetVolume(static_cast<double>(m_volumeBeforeMute) / static_cast<double>(100));
|
|||
|
|
+ m_volumeBeforeMute = 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
@@ -172,6 +247,14 @@ QPixmap VolumeSettingPage::trayIconColorSwitch(const QString &iconPath)
|
|||
|
|
return pixmap;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
+void VolumeSettingPage::disableSettings()
|
|||
|
|
+{
|
|||
|
|
+ ui->volumeSetting->setValue(0);
|
|||
|
|
+ ui->volume->setText(QString::number(0) + "%");
|
|||
|
|
+ ui->volumeSetting->setEnabled(false);
|
|||
|
|
+ setVolumeIcon(0);
|
|||
|
|
+}
|
|||
|
|
+
|
|||
|
|
void VolumeSettingPage::hideLine()
|
|||
|
|
{
|
|||
|
|
ui->line->hide();
|
|||
|
|
diff --git a/plugins/audio/src/system-tray/volume-setting-page.h b/plugins/audio/src/system-tray/volume-setting-page.h
|
|||
|
|
index d78bd13..4719021 100644
|
|||
|
|
--- a/plugins/audio/src/system-tray/volume-setting-page.h
|
|||
|
|
+++ b/plugins/audio/src/system-tray/volume-setting-page.h
|
|||
|
|
@@ -17,6 +17,7 @@
|
|||
|
|
|
|||
|
|
#include <QWidget>
|
|||
|
|
#include "common/audio-node.h"
|
|||
|
|
+#include <QDBusServiceWatcher>
|
|||
|
|
|
|||
|
|
QT_BEGIN_NAMESPACE
|
|||
|
|
namespace Ui
|
|||
|
|
@@ -36,17 +37,26 @@ public:
|
|||
|
|
explicit VolumeSettingPage(enum AudioNode audio, const QString objectPath, QWidget *parent = nullptr);
|
|||
|
|
~VolumeSettingPage() override;
|
|||
|
|
|
|||
|
|
+ QPixmap trayIconColorSwitch(const QString &iconPath);
|
|||
|
|
+ void disableSettings();
|
|||
|
|
+
|
|||
|
|
+private:
|
|||
|
|
+ void initDbusServiceWatcher();
|
|||
|
|
void initAudioDevice();
|
|||
|
|
void initAudioStream();
|
|||
|
|
template <class Audio>
|
|||
|
|
void initSettings(Audio *audio);
|
|||
|
|
template <class Audio>
|
|||
|
|
void clickMuteButton(Audio *audio);
|
|||
|
|
- QPixmap trayIconColorSwitch(const QString &iconPath);
|
|||
|
|
+
|
|||
|
|
|
|||
|
|
public slots:
|
|||
|
|
void handleVolumeChanged(double value);
|
|||
|
|
void handleMuteButtonClicked();
|
|||
|
|
+ void handleDefaultSinkChanged(int index);
|
|||
|
|
+ void handleSinkAdded(int index);
|
|||
|
|
+ void handleSinkDelete(int index);
|
|||
|
|
+
|
|||
|
|
void setVolumeIcon(int value);
|
|||
|
|
void hideLine();
|
|||
|
|
|
|||
|
|
@@ -56,8 +66,10 @@ private:
|
|||
|
|
AudioDeviceInterface *m_sink;
|
|||
|
|
AudioStreamInterface *m_sinkInput;
|
|||
|
|
|
|||
|
|
- int volumeBeforeMute;
|
|||
|
|
- AudioNode audioNode;
|
|||
|
|
+ int m_volumeBeforeMute;
|
|||
|
|
+ AudioNode m_audioNode;
|
|||
|
|
+
|
|||
|
|
+ QDBusServiceWatcher *m_dbusServiceWatcher;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif // KIRAN_CPANEL_AUDIO_VOLUME_SETTING_PAGE_H
|
|||
|
|
--
|
|||
|
|
2.33.0
|
|||
|
|
|