Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 105 additions & 19 deletions panels/notification/osd/audio/audioapplet.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "audioapplet.h"

#include "pluginfactory.h"

Check warning on line 7 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "pluginfactory.h" not found.

#include <QDBusConnection>

Check warning on line 9 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusConnection> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusMessage>

Check warning on line 10 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusMessage> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusPendingCall>

Check warning on line 11 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusPendingCall> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusReply>

Check warning on line 12 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <DDBusSender>

namespace osd {

const QString audioService = QStringLiteral("org.deepin.dde.Audio1");
const QString audioPath = QStringLiteral("/org/deepin/dde/Audio1");
const QString audioSinkInterface = QStringLiteral("org.deepin.dde.Audio1.Sink");

static DDBusSender audioInter()
{
return DDBusSender().service("org.deepin.dde.Audio1")
.path("/org/deepin/dde/Audio1")
.interface("org.deepin.dde.Audio1");
return DDBusSender().service(audioService)
.path(audioPath)
.interface(audioService);
}

static DDBusSender audioSinkInter()
static DDBusSender audioSinkInter(const QString &sinkPath)
{
return DDBusSender().service(audioService)
.path(sinkPath)
.interface(audioSinkInterface);
}

static std::optional<QString> fetchDefaultSinkPath()
{
QDBusReply<QVariant> defaultSink = audioInter().property("DefaultSink").get();
if (!defaultSink.isValid()) {
qWarning() << "Failed to fetch DefaultSink" << defaultSink.error();
return DDBusSender();
return std::nullopt;
}
const auto path = qdbus_cast<QDBusObjectPath>(defaultSink);
return DDBusSender().service("org.deepin.dde.Audio1")
.path(path.path())
.interface("org.deepin.dde.Audio1.Sink");
return qdbus_cast<QDBusObjectPath>(defaultSink).path();
}

AudioApplet::AudioApplet(QObject *parent)
: DApplet(parent)
{
}

bool AudioApplet::load()
{
if (!DApplet::load())
return false;

// Subscribe before reading the initial state so changes during sync are delivered as well.
QDBusConnection::sessionBus().connect(
audioService, QString(),
QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("PropertiesChanged"),
this, SLOT(onAudioPropertiesChanged(QString,QVariantMap,QStringList,QDBusMessage)));

return true;
}

bool AudioApplet::increaseVolume() const
Expand All @@ -55,21 +77,85 @@

void AudioApplet::sync()
{
setVolumeValue(fetchVolume());
if (const auto sinkPath = fetchDefaultSinkPath())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · OSD shows stale output state

When the DefaultSink read fails during an output switch or while an audio OSD request is being synced, AudioApplet::sync() leaves m_sinkPath and the displayed state unchanged; after an output switch, notifications from the new sink are ignored because their path does not match m_sinkPath, so the OSD shows stale output data.

Handle a failed DefaultSink read so the cached sink and displayed state cannot remain stale.

Also at panels/notification/osd/audio/audioapplet.cpp:125.

Prompt for AI agents
In `panels/notification/osd/audio/audioapplet.cpp` at line 80:

**OSD shows stale output state**

When the DefaultSink read fails during an output switch or while an audio OSD request is being synced, `AudioApplet::sync()` leaves `m_sinkPath` and the displayed state unchanged; after an output switch, notifications from the new sink are ignored because their path does not match `m_sinkPath`, so the OSD shows stale output data.

Handle a failed `DefaultSink` read so the cached sink and displayed state cannot remain stale.

Also at `panels/notification/osd/audio/audioapplet.cpp:125`.

updateDefaultSink(*sinkPath);
}

void AudioApplet::updateDefaultSink(const QString &sinkPath)
{
m_sinkPath = sinkPath;
readSinkState(true, true);
setIncreaseVolume(fetchIncreaseVolume());
setIconName(fetchIconName());
}

void AudioApplet::readSinkState(bool readVolume, bool readMute)
{
if (!readVolume && !readMute)
return;

if (m_sinkPath.isEmpty() || m_sinkPath == QStringLiteral("/")) {
m_muted.reset();
setVolumeValue(0.0);
return;
}

auto sink = audioSinkInter(m_sinkPath);
if (readMute) {
const QDBusReply<QVariant> reply = sink.property("Mute").get();
if (reply.isValid()) {
m_muted = qdbus_cast<bool>(reply);
} else {
qWarning() << "Failed to fetch Mute" << reply.error();
m_muted.reset();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Transient reads corrupt audio OSD

When an invalidated Mute or Volume property is re-read while the audio service returns a D-Bus error, readSinkState clears m_muted or stores fetchVolume’s 0.0 fallback instead of retaining the last valid value, so the visible OSD loses its icon or reports zero volume.

Keep the cached property value when its refresh fails, and update the OSD only with successfully read values.

Also at panels/notification/osd/audio/audioapplet.cpp:114.

Prompt for AI agents
In `panels/notification/osd/audio/audioapplet.cpp` at line 110:

**Transient reads corrupt audio OSD**

When an invalidated Mute or Volume property is re-read while the audio service returns a D-Bus error, `readSinkState` clears `m_muted` or stores `fetchVolume`’s `0.0` fallback instead of retaining the last valid value, so the visible OSD loses its icon or reports zero volume.

Keep the cached property value when its refresh fails, and update the OSD only with successfully read values.

Also at `panels/notification/osd/audio/audioapplet.cpp:114`.

}
}
if (readVolume)
setVolumeValue(fetchVolume(sink));
}

void AudioApplet::onAudioPropertiesChanged(const QString &interface, const QVariantMap &changed,
const QStringList &invalidated, const QDBusMessage &message)
{
if (interface == audioService && message.path() == audioPath) {
const auto defaultSink = changed.constFind(QStringLiteral("DefaultSink"));
if (defaultSink != changed.cend()) {
updateDefaultSink(qdbus_cast<QDBusObjectPath>(*defaultSink).path());
} else if (invalidated.contains(QStringLiteral("DefaultSink"))) {
sync();
}
return;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Progress scale stays outdated

When the audio service changes IncreaseVolume while the default sink stays the same, onAudioPropertiesChanged() returns without refreshing m_increaseVolume, so the visible OSD uses the old progress-bar scale and boost marks.

Handle IncreaseVolume changes in onAudioPropertiesChanged() even when the default sink does not change.

Also at panels/notification/osd/audio/audioapplet.cpp:127.

Prompt for AI agents
In `panels/notification/osd/audio/audioapplet.cpp` at line 128:

**Progress scale stays outdated**

When the audio service changes `IncreaseVolume` while the default sink stays the same, `onAudioPropertiesChanged()` returns without refreshing `m_increaseVolume`, so the visible OSD uses the old progress-bar scale and boost marks.

Handle `IncreaseVolume` changes in `onAudioPropertiesChanged()` even when the default sink does not change.

Also at `panels/notification/osd/audio/audioapplet.cpp:127`.


if (interface != audioSinkInterface || message.path() != m_sinkPath)
return;

const auto volume = changed.constFind(QStringLiteral("Volume"));
const auto mute = changed.constFind(QStringLiteral("Mute"));
const bool hasVolume = volume != changed.cend();
const bool hasMute = mute != changed.cend();
const bool readVolume = !hasVolume && invalidated.contains(QStringLiteral("Volume"));
const bool readMute = !hasMute && invalidated.contains(QStringLiteral("Mute"));
if (!hasVolume && !hasMute && !readVolume && !readMute)
return;

if (hasMute)
m_muted = mute->toBool();

if (hasVolume)
setVolumeValue(volume->toDouble());

readSinkState(readVolume, readMute);
setIconName(fetchIconName());
}

QString AudioApplet::fetchIconName() const
{
QDBusReply<QVariant> muteReply = audioSinkInter().property("Mute").get();
if (!muteReply.isValid()) {
qWarning() << "Failed to fetch Mute" << muteReply.error();
return QString();
if (!m_muted.has_value()) {
return {};
}
if (qdbus_cast<bool>(muteReply)) {

if (*m_muted) {
return "osd_volume_mute";
}

Expand All @@ -88,11 +174,11 @@
return QString("osd_volume_%1").arg(level);
}

double AudioApplet::fetchVolume() const
double AudioApplet::fetchVolume(DDBusSender &sink) const
{
QDBusReply<QVariant> volumeReply = audioSinkInter().property("Volume").get();
QDBusReply<QVariant> volumeReply = sink.property("Volume").get();
if (!volumeReply.isValid()) {
qWarning() << "Failed to Volume Volume" << volumeReply.error();
qWarning() << "Failed to fetch Volume" << volumeReply.error();
return 0.0;
}

Expand Down
19 changes: 17 additions & 2 deletions panels/notification/osd/audio/audioapplet.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include "applet.h"

Check warning on line 7 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "applet.h" not found.

#include <QVariantMap>

Check warning on line 9 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QVariantMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <optional>

Check warning on line 10 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <optional> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class QDBusMessage;
class DDBusSender;

namespace osd {

class AudioApplet : public DS_NAMESPACE::DApplet
Expand All @@ -16,6 +22,7 @@
Q_PROPERTY(bool increaseVolume READ increaseVolume WRITE setIncreaseVolume NOTIFY increaseVolumeChanged FINAL)
public:
explicit AudioApplet(QObject *parent = nullptr);
bool load() override;

double volumeValue() const;
void setVolumeValue(double newVolumeValue);
Expand All @@ -37,13 +44,21 @@
void volumeRatioChanged();
void increaseVolumeChanged();

private Q_SLOTS:

Check warning on line 47 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void onAudioPropertiesChanged(const QString &interface, const QVariantMap &changed,
const QStringList &invalidated, const QDBusMessage &message);

private:
void updateDefaultSink(const QString &sinkPath);
void readSinkState(bool readVolume, bool readMute);
QString fetchIconName() const;
double fetchVolume() const;
double fetchVolume(DDBusSender &sink) const;
bool fetchIncreaseVolume() const;
private:
double m_increaseVolume = 1.0;
double m_volumeValue = 1.0;
QString m_sinkPath;
std::optional<bool> m_muted;
QString m_iconName;
double m_volumeRatio;
};
Expand Down
28 changes: 17 additions & 11 deletions panels/notification/osd/displaymode/displaymodeapplet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,25 @@ QQmlListProperty<DPItem> DisPlayModeApplet::planItems()

void DisPlayModeApplet::sync()
{
if (!m_actionTimer->isActive()) {
fetchPlanItems();
auto current = fetchCurrentPlanItem();
setCurrentPlanItem(current);
Q_EMIT planItemsChanged();
}
auto state = fetchState();
if (!m_actionTimer->isActive())
refreshDisplayState();

setState(fetchState());
}

setState(state);
void DisPlayModeApplet::refreshDisplayState()
{
m_planItemsValid = fetchPlanItems();
setCurrentPlanItem(m_planItemsValid ? fetchCurrentPlanItem() : nullptr);
Q_EMIT planItemsChanged();
}

void DisPlayModeApplet::next()
{
if (m_planItems.isEmpty())
if (!m_planItemsValid)
refreshDisplayState();

if (!m_planItemsValid || m_planItems.isEmpty())
return;

const auto nextIndex = (m_planItems.indexOf(m_currentPlanItem) + 1) % m_planItems.size();
Expand Down Expand Up @@ -159,7 +164,7 @@ void DisPlayModeApplet::setCurrentPlanItem(DPItem *newCurrentPlanItem)
emit currentPlanItemChanged();
}

void DisPlayModeApplet::fetchPlanItems()
bool DisPlayModeApplet::fetchPlanItems()
{
setCurrentPlanItem(nullptr);
qDeleteAll(m_planItems);
Expand All @@ -168,7 +173,7 @@ void DisPlayModeApplet::fetchPlanItems()
QDBusReply<QStringList> listOutputNames = displayInter().method("ListOutputNames").call();
if (!listOutputNames.isValid()) {
qCWarning(osdDPLog) << "Failed to fetch ListOutputNames" << listOutputNames.error();
return;
return false;
}
auto outputNames = listOutputNames.value();

Expand All @@ -184,6 +189,7 @@ void DisPlayModeApplet::fetchPlanItems()
m_planItems << new DPItem(tr("Only on %1").arg(item), item, iconName, DPItem::Single, this);
}
}
return true;
}

DPItem *DisPlayModeApplet::fetchCurrentPlanItem() const
Expand Down
4 changes: 3 additions & 1 deletion panels/notification/osd/displaymode/displaymodeapplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ class DisPlayModeApplet : public DS_NAMESPACE::DApplet
private:
void setState(int newState);
void setCurrentPlanItem(DPItem *newCurrentPlanItem);
void fetchPlanItems();
void refreshDisplayState();
bool fetchPlanItems();
DPItem *fetchCurrentPlanItem() const;
int fetchState() const;

private:
QList<DPItem *> m_planItems;
DPItem *m_currentPlanItem = nullptr;
bool m_planItemsValid = false;
int m_state;
QTimer *m_actionTimer = nullptr;
Dtk::Core::DConfig *m_osdConfig = nullptr;
Expand Down
23 changes: 10 additions & 13 deletions panels/notification/osd/displaymode/package/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ AppletItem {

function update(osdType)
{
Applet.sync()
if (match(osdType)) {

if (osdType === "DirectSwitchLayout") {
if (Applet.state !== 2) {
Applet.doAction()
}
} else if (osdType === "SwitchMonitors") {
if (Panel.lastOsdType() === osdType) {
Applet.next()
}
}
if (!match(osdType))
return false

if (osdType === "SwitchMonitors" && Panel.lastOsdType() === osdType) {
Applet.next()
return true
}
return false

Applet.sync()
if (osdType === "DirectSwitchLayout" && Applet.state !== 2)
Applet.doAction()

return true
}
function match(osdType)
{
Expand Down
Loading
Loading