kdeconnect-kde/plugins/pausemusic/pausemusicplugin.cpp
Weixuan Xiao f1843cb492 Improve D-Bus implementation on macOS
Better patch to replace !218.

- Auto and quick detection of previous D-Bus instance;
- Remove private D-Bus compile definition, only use it on macOS without an existing D-Bus instance;
- Safe reboot after crashes because the indicator is not relating on the kdeconnectd to run a D-Bus session;
- Safe exit after clicking on `Quit` in the systray.


More details in commit logs:

Only enable private D-Bus on macOS because the other platforms do not
need them.
The app should be able to easily detect the session bus from the env
DBUS_LAUNCHD_SESSION_BUS_SOCKET from launchd through launchctl.
Because https://gitlab.freedesktop.org/dbus/dbus/-/blob/master/dbus/dbus-sysdeps-unix.c#L4392
shows that it is the only probing method on macOS with launchd.

The D-Bus session bus can be easily found from launchd/launchctl
with DBUS_LAUNCHD_SESSION_BUS_SOCKET env. It can be an external one
(installed from HomeBrew) or an internal one (launched by a previous
instance followed by a crash).

The indicator helper on macOS can now automatically detect whether we can use a potentially
(with launchd/launchctl env set, or KDE Connect macOS
private_bus_address set) existed and usable session bus.
If previous bus is usable, just try to launch the kdeconnectd with us.
Otherwise, launch a private D-Bus daemon, export the launchd/launchctl
env, and run a kdeconnectd instance.

Everything works better and quicker now :)
2022-04-12 05:40:03 +00:00

115 lines
3.9 KiB
C++

/**
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "pausemusicplugin.h"
#include <KPluginFactory>
#include <PulseAudioQt/Context>
#include <PulseAudioQt/Sink>
#include <dbushelper.h>
#include "mprisplayer.h"
//In older Qt released, qAsConst isnt available
#include "qtcompat_p.h"
#include "plugin_pausemusic_debug.h"
K_PLUGIN_CLASS_WITH_JSON(PauseMusicPlugin, "kdeconnect_pausemusic.json")
PauseMusicPlugin::PauseMusicPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
, mutedSinks()
{}
bool PauseMusicPlugin::receivePacket(const NetworkPacket& np)
{
bool pauseOnlyWhenTalking = config()->getBool(QStringLiteral("conditionTalking"), false);
if (pauseOnlyWhenTalking) {
if (np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) {
return true;
}
} else { //Pause as soon as it rings
if (np.get<QString>(QStringLiteral("event")) != QLatin1String("ringing") && np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) {
return true;
}
}
bool pauseConditionFulfilled = !np.get<bool>(QStringLiteral("isCancel"));
bool pause = config()->getBool(QStringLiteral("actionPause"), true);
bool mute = config()->getBool(QStringLiteral("actionMute"), false);
const bool autoResume = config()->getBool(QStringLiteral("actionResume"), true);
if (pauseConditionFulfilled) {
if (mute) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muting system volume";
const auto sinks = PulseAudioQt::Context::instance()->sinks();
for (const auto sink : sinks) {
if (!sink->isMuted()) {
sink->setMuted(true);
mutedSinks.insert(sink->name());
}
}
}
if (pause) {
//Search for interfaces currently playing
const QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
for (const QString& iface : interfaces) {
if (iface.startsWith(QLatin1String("org.mpris.MediaPlayer2"))) {
OrgMprisMediaPlayer2PlayerInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QDBusConnection::sessionBus());
QString status = mprisInterface.playbackStatus();
if (status == QLatin1String("Playing")) {
if (!pausedSources.contains(iface)) {
pausedSources.insert(iface);
if (mprisInterface.canPause()) {
mprisInterface.Pause();
} else {
mprisInterface.Stop();
}
}
}
}
}
}
} else {
if (mute) {
qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuting system volume";
if (autoResume) {
const auto sinks = PulseAudioQt::Context::instance()->sinks();
for (const auto sink : sinks) {
if (mutedSinks.contains(sink->name())) {
sink->setMuted(false);
}
}
}
mutedSinks.clear();
}
if (pause && !pausedSources.empty()) {
if (autoResume) {
for (const QString& iface : qAsConst(pausedSources)) {
OrgMprisMediaPlayer2PlayerInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QDBusConnection::sessionBus());
mprisInterface.Play();
}
}
pausedSources.clear();
}
}
return true;
}
#include "pausemusicplugin.moc"