From ed91dfb1ecae11610a0b4568dc80d43022209a36 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Thu, 20 Aug 2020 00:47:29 +0200 Subject: [PATCH] [plugins/lockdevice] Port to use logind interface KScreenLocker does not allow unlocking the screen via the screensaver interface by design (https://bugs.kde.org/show_bug.cgi?id=425616). However it allows to do it via the logind interface, so let's use that here. While at it also refactor the code and properly track and send the state, so the other device can show an appropriate label. --- plugins/lockdevice/CMakeLists.txt | 3 +- plugins/lockdevice/lockdeviceplugin.cpp | 64 ++++++++++++++----- plugins/lockdevice/lockdeviceplugin.h | 14 ++-- .../org.freedesktop.DBus.Properties.xml | 26 ++++++++ plugins/lockdevice/org.freedesktop.login1.xml | 15 +++++ 5 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 plugins/lockdevice/org.freedesktop.DBus.Properties.xml create mode 100644 plugins/lockdevice/org.freedesktop.login1.xml diff --git a/plugins/lockdevice/CMakeLists.txt b/plugins/lockdevice/CMakeLists.txt index a6c25076..61ac67e2 100644 --- a/plugins/lockdevice/CMakeLists.txt +++ b/plugins/lockdevice/CMakeLists.txt @@ -1,4 +1,5 @@ -qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.ScreenSaver.xml screensaverdbusinterface) +qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.login1.xml login1dbusinterface) +qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.DBus.Properties.xml propertiesdbusinterface) set(debug_file_SRCS) ecm_qt_declare_logging_category( diff --git a/plugins/lockdevice/lockdeviceplugin.cpp b/plugins/lockdevice/lockdeviceplugin.cpp index aa9d0b4b..d24a9e39 100644 --- a/plugins/lockdevice/lockdeviceplugin.cpp +++ b/plugins/lockdevice/lockdeviceplugin.cpp @@ -10,7 +10,6 @@ #include #include -#include "screensaverdbusinterface.h" #include "plugin_lock_debug.h" #include @@ -21,19 +20,48 @@ K_PLUGIN_CLASS_WITH_JSON(LockDevicePlugin, "kdeconnect_lockdevice.json") LockDevicePlugin::LockDevicePlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) , m_remoteLocked(false) - , m_iface(nullptr) + , m_login1Interface(QStringLiteral("org.freedesktop.login1"), QStringLiteral("/org/freedesktop/login1/session/auto"), QDBusConnection::systemBus()) + // Connect on all paths since the PropertiesChanged signal is only emitted + // from /org/freedesktop/login1/session/ and not /org/freedesktop/login1/session/auto + , m_propertiesInterface(QStringLiteral("org.freedesktop.login1"), QString(), QDBusConnection::systemBus()) { + + if (!m_login1Interface.isValid()) { + qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Could not connect to logind interface" << m_login1Interface.lastError(); + } + + if (!m_propertiesInterface.isValid()) { + qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Could not connect to logind properties interface" << m_propertiesInterface.lastError(); + } + + connect(&m_propertiesInterface, &OrgFreedesktopDBusPropertiesInterface::PropertiesChanged, this, [this](const QString& interface, const QVariantMap& properties, QStringList invalidatedProperties ) { + + Q_UNUSED(invalidatedProperties); + + if (interface != QLatin1String("org.freedesktop.login1.Session")) { + return; + } + + if (!properties.contains(QStringLiteral("LockedHint"))) { + return; + } + + m_localLocked = properties.value(QStringLiteral("LockedHint")).toBool(); + sendState(); + }); + + m_localLocked = m_login1Interface.lockedHint(); } LockDevicePlugin::~LockDevicePlugin() { - delete m_iface; } bool LockDevicePlugin::isLocked() const { return m_remoteLocked; } + void LockDevicePlugin::setLocked(bool locked) { NetworkPacket np(PACKET_TYPE_LOCK_REQUEST, {{QStringLiteral("setLocked"), locked}}); @@ -50,27 +78,29 @@ bool LockDevicePlugin::receivePacket(const NetworkPacket & np) } } - bool sendState = np.has(QStringLiteral("requestLocked")); - if (np.has(QStringLiteral("setLocked"))) { - iface()->SetActive(np.get(QStringLiteral("setLocked"))); - sendState = true; + if (np.has(QStringLiteral("requestLocked"))) { + sendState(); } - if (sendState) { - NetworkPacket np(PACKET_TYPE_LOCK, QVariantMap {{QStringLiteral("isLocked"), QVariant::fromValue(iface()->GetActive())}}); - sendPacket(np); + + if (np.has(QStringLiteral("setLocked"))) { + const bool lock = np.get(QStringLiteral("setLocked")); + + if (lock) { + m_login1Interface.Lock(); + } else { + m_login1Interface.Unlock(); + } + + sendState(); } return true; } -OrgFreedesktopScreenSaverInterface* LockDevicePlugin::iface() +void LockDevicePlugin::sendState() { - if (!m_iface) { - m_iface = new OrgFreedesktopScreenSaverInterface(QStringLiteral("org.freedesktop.ScreenSaver"), QStringLiteral("/org/freedesktop/ScreenSaver"), DBusHelper::sessionBus()); - if(!m_iface->isValid()) - qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Couldn't connect to the ScreenSaver interface"; - } - return m_iface; + NetworkPacket np(PACKET_TYPE_LOCK, {{QStringLiteral("isLocked"), m_localLocked}}); + sendPacket(np); } void LockDevicePlugin::connected() diff --git a/plugins/lockdevice/lockdeviceplugin.h b/plugins/lockdevice/lockdeviceplugin.h index 46fb799f..d3b502e2 100644 --- a/plugins/lockdevice/lockdeviceplugin.h +++ b/plugins/lockdevice/lockdeviceplugin.h @@ -11,7 +11,8 @@ #include -class OrgFreedesktopScreenSaverInterface; +#include "login1dbusinterface.h" +#include "propertiesdbusinterface.h" #define PACKET_TYPE_LOCK QStringLiteral("kdeconnect.lock") #define PACKET_TYPE_LOCK_REQUEST QStringLiteral("kdeconnect.lock.request") @@ -35,14 +36,17 @@ public: bool receivePacket(const NetworkPacket & np) override; Q_SIGNALS: - void lockedChanged(bool locked); + Q_SCRIPTABLE void lockedChanged(bool locked); private: - bool m_remoteLocked; - OrgFreedesktopScreenSaverInterface* iface(); + void sendState(); + + bool m_remoteLocked; + bool m_localLocked = false; - OrgFreedesktopScreenSaverInterface* m_iface; + OrgFreedesktopLogin1SessionInterface m_login1Interface; + OrgFreedesktopDBusPropertiesInterface m_propertiesInterface; }; #endif diff --git a/plugins/lockdevice/org.freedesktop.DBus.Properties.xml b/plugins/lockdevice/org.freedesktop.DBus.Properties.xml new file mode 100644 index 00000000..684cdc07 --- /dev/null +++ b/plugins/lockdevice/org.freedesktop.DBus.Properties.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/lockdevice/org.freedesktop.login1.xml b/plugins/lockdevice/org.freedesktop.login1.xml new file mode 100644 index 00000000..a74eab7f --- /dev/null +++ b/plugins/lockdevice/org.freedesktop.login1.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + -- GitLab