summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Uwe Broulik <[email protected]>2017-05-10 13:23:46 +0200
committerKai Uwe Broulik <[email protected]>2017-05-10 13:23:46 +0200
commit9ac78b69ddca15c7ba3f86791eb03eee3318db89 (patch)
tree991af0151ea6c7e20a5b3726f6740e15d990333e
parentc9139df5bca7c9c5d86124b9617a812eb331c371 (diff)
Pause media players on suspend
When suspending the machine pause any media players to avoid embarrassing situation where the machine wakes up with loud music blaring out of its speakers. Differential Revision: https://phabricator.kde.org/D4960
-rw-r--r--PowerDevilSettings.kcfg3
-rw-r--r--daemon/powerdevilcore.cpp36
-rw-r--r--daemon/powerdevilcore.h1
-rw-r--r--kcmodule/global/GeneralPage.cpp6
-rw-r--r--kcmodule/global/generalPage.ui20
5 files changed, 63 insertions, 3 deletions
diff --git a/PowerDevilSettings.kcfg b/PowerDevilSettings.kcfg
index b35bfb0..0b82211 100644
--- a/PowerDevilSettings.kcfg
+++ b/PowerDevilSettings.kcfg
@@ -10,6 +10,9 @@
<entry name="doNotInhibitOnLidClose" type="Bool">
<default>true</default>
</entry>
+ <entry name="pausePlayersOnSuspend" type="Bool">
+ <default>true</default>
+ </entry>
</group>
<group name="Brightness">
diff --git a/daemon/powerdevilcore.cpp b/daemon/powerdevilcore.cpp
index e9e91bc..a69eee5 100644
--- a/daemon/powerdevilcore.cpp
+++ b/daemon/powerdevilcore.cpp
@@ -137,6 +137,8 @@ void Core::onBackendReady()
this, SLOT(onBatteryRemainingTimeChanged(qulonglong)));
connect(m_backend, SIGNAL(lidClosedChanged(bool)),
this, SLOT(onLidClosedChanged(bool)));
+ connect(m_backend, &BackendInterface::aboutToSuspend,
+ this, &Core::onAboutToSuspend);
connect(KIdleTime::instance(), SIGNAL(timeoutReached(int,int)),
this, SLOT(onKIdleTimeoutReached(int,int)));
connect(KIdleTime::instance(), SIGNAL(resumingFromIdle()),
@@ -757,6 +759,40 @@ void Core::onLidClosedChanged(bool closed)
Q_EMIT lidClosedChanged(closed);
}
+void Core::onAboutToSuspend()
+{
+ if (PowerDevilSettings::pausePlayersOnSuspend()) {
+ qCDebug(POWERDEVIL) << "Pausing all media players before suspending";
+
+ QDBusPendingCall listNamesCall = QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames"));
+ QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(listNamesCall, this);
+ connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
+ QDBusPendingReply<QStringList> reply = *watcher;
+ watcher->deleteLater();
+
+ if (reply.isError()) {
+ qCWarning(POWERDEVIL) << "Failed to fetch list of DBus service names for pausing players on suspend" << reply.error().message();
+ return;
+ }
+
+ const QStringList &services = reply.value();
+ for (const QString &serviceName : services) {
+ if (!serviceName.startsWith(QLatin1String("org.mpris.MediaPlayer2."))) {
+ continue;
+ }
+
+ qCDebug(POWERDEVIL) << "Pausing media player with service name" << serviceName;
+
+ QDBusMessage pauseMsg = QDBusMessage::createMethodCall(serviceName,
+ QStringLiteral("/org/mpris/MediaPlayer2"),
+ QStringLiteral("org.mpris.MediaPlayer2.Player"),
+ QStringLiteral("Pause"));
+ QDBusConnection::sessionBus().asyncCall(pauseMsg);
+ }
+ });
+ }
+}
+
void Core::registerActionTimeout(Action* action, int timeout)
{
// Register the timeout with KIdleTime
diff --git a/daemon/powerdevilcore.h b/daemon/powerdevilcore.h
index cbe04ac..2c1f9d0 100644
--- a/daemon/powerdevilcore.h
+++ b/daemon/powerdevilcore.h
@@ -154,6 +154,7 @@ private Q_SLOTS:
void onNotificationTimeout();
void onServiceRegistered(const QString &service);
void onLidClosedChanged(bool closed);
+ void onAboutToSuspend();
};
}
diff --git a/kcmodule/global/GeneralPage.cpp b/kcmodule/global/GeneralPage.cpp
index 78a6fe2..565e01f 100644
--- a/kcmodule/global/GeneralPage.cpp
+++ b/kcmodule/global/GeneralPage.cpp
@@ -124,6 +124,8 @@ void GeneralPage::fillUi()
connect(BatteryCriticalCombo, SIGNAL(currentIndexChanged(int)), SLOT(changed()));
+ connect(pausePlayersCheckBox, &QCheckBox::stateChanged, this, static_cast<void(KCModule::*)()>(&KCModule::changed));
+
if (!hasPowerSupplyBattery) {
BatteryCriticalLabel->hide();
BatteryCriticalCombo->hide();
@@ -150,6 +152,8 @@ void GeneralPage::load()
lowPeripheralSpin->setValue(PowerDevilSettings::peripheralBatteryLowLevel());
BatteryCriticalCombo->setCurrentIndex(BatteryCriticalCombo->findData(PowerDevilSettings::batteryCriticalAction()));
+
+ pausePlayersCheckBox->setChecked(PowerDevilSettings::pausePlayersOnSuspend());
}
void GeneralPage::configureNotifications()
@@ -165,6 +169,8 @@ void GeneralPage::save()
PowerDevilSettings::setBatteryCriticalAction(BatteryCriticalCombo->itemData(BatteryCriticalCombo->currentIndex()).toInt());
+ PowerDevilSettings::setPausePlayersOnSuspend(pausePlayersCheckBox->checkState() == Qt::Checked);
+
PowerDevilSettings::self()->save();
// Notify Daemon
diff --git a/kcmodule/global/generalPage.ui b/kcmodule/global/generalPage.ui
index 52985af..8d6b466 100644
--- a/kcmodule/global/generalPage.ui
+++ b/kcmodule/global/generalPage.ui
@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
- <width>494</width>
+ <width>510</width>
<height>276</height>
</rect>
</property>
@@ -113,7 +113,7 @@
</property>
</widget>
</item>
- <item row="5" column="0">
+ <item row="6" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
@@ -126,7 +126,7 @@
</property>
</spacer>
</item>
- <item row="6" column="1">
+ <item row="9" column="1">
<widget class="QPushButton" name="notificationsButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Maximum">
@@ -162,6 +162,20 @@
</property>
</widget>
</item>
+ <item row="8" column="1">
+ <widget class="QCheckBox" name="pausePlayersCheckBox">
+ <property name="text">
+ <string>Enabled</string>
+ </property>
+ </widget>
+ </item>
+ <item row="8" column="0">
+ <widget class="QLabel" name="pausePlayersLabel">
+ <property name="text">
+ <string>Pause media players when suspending:</string>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
<item>