From a466725d3a95591820ce8035cefadcba7baafcd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Tue, 6 Feb 2024 19:12:15 +0100 Subject: [PATCH] Fix lid actions Should fix all lid issues, will do some more testing. Fix #44 #34 --- src/powerkit_common.h | 1 + src/powerkit_manager.cpp | 83 +++++++++++++++++++++++++++++++--------- src/powerkit_manager.h | 5 +++ 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/powerkit_common.h b/src/powerkit_common.h index c4f448b..9a6af8c 100644 --- a/src/powerkit_common.h +++ b/src/powerkit_common.h @@ -114,6 +114,7 @@ namespace PowerKit #define DBUS_PROPERTIES "org.freedesktop.DBus.Properties" #define DBUS_DEVICE "Device" #define DBUS_CHANGED "Changed" +#define DBUS_PROPERTIES_CHANGED "PropertiesChanged" // config keys #define CONF_DIALOG_GEOMETRY "dialog_geometry" diff --git a/src/powerkit_manager.cpp b/src/powerkit_manager.cpp index 80bc241..2a6127e 100644 --- a/src/powerkit_manager.cpp +++ b/src/powerkit_manager.cpp @@ -265,7 +265,8 @@ void Manager::setup() DBUS_DEVICE_REMOVED, this, SLOT(deviceRemoved(QString))); - system.connect(UPOWER_SERVICE, + // not used anymore? + /*system.connect(UPOWER_SERVICE, UPOWER_PATH, UPOWER_SERVICE, DBUS_CHANGED, @@ -276,7 +277,13 @@ void Manager::setup() UPOWER_SERVICE, DBUS_DEVICE_CHANGED, this, - SLOT(deviceChanged())); + SLOT(deviceChanged()));*/ + system.connect(UPOWER_SERVICE, + UPOWER_PATH, + DBUS_PROPERTIES, + DBUS_PROPERTIES_CHANGED, + this, + SLOT(propertiesChanged())); system.connect(UPOWER_SERVICE, UPOWER_PATH, UPOWER_SERVICE, @@ -289,13 +296,6 @@ void Manager::setup() UPOWER_NOTIFY_SLEEP, this, SLOT(handleSuspend())); - // DONT WORK ANYMORE! WHY? - /*system.connect(LOGIND_SERVICE, - LOGIND_PATH, - LOGIND_MANAGER, - PK_PREPARE_FOR_SUSPEND, - this, - SLOT(handlePrepareForSuspend(bool)));*/ if (upower == NULL) { upower = new QDBusInterface(UPOWER_SERVICE, UPOWER_PATH, @@ -327,6 +327,7 @@ void Manager::setup() SLOT(handlePrepareForSuspend(bool))); } if (!suspendLock) { registerSuspendLock(); } + if (!lidLock) { registerLidLock(); } scan(); } else { qWarning() << "Failed to connect to system bus"; } } @@ -338,6 +339,7 @@ void Manager::check() return; } if (!suspendLock) { registerSuspendLock(); } + if (!lidLock) { registerLidLock(); } if (!upower->isValid()) { scan(); } } @@ -393,31 +395,48 @@ void Manager::deviceRemoved(const QString &path) void Manager::deviceChanged() { - if (wasLidClosed != LidIsClosed()) { - if (!wasLidClosed && LidIsClosed()) { + qDebug() << "a device changed, tell the world!"; + emit UpdatedDevices(); +} + +void Manager::propertiesChanged() +{ + bool isLidClosed = LidIsClosed(); + bool isOnBattery = OnBattery(); + + qDebug() << "properties changed:" + << "lid closed?" << wasLidClosed << isLidClosed + << "on battery?" << wasOnBattery << isOnBattery; + + if (wasLidClosed != isLidClosed) { + if (!wasLidClosed && isLidClosed) { + qDebug() << "lid changed status to closed"; emit LidClosed(); - } else if (wasLidClosed && !LidIsClosed()) { + } else if (wasLidClosed && !isLidClosed) { + qDebug() << "lid changed status to open"; emit LidOpened(); } } - wasLidClosed = LidIsClosed(); + wasLidClosed = isLidClosed; - if (wasOnBattery != OnBattery()) { - if (!wasOnBattery && OnBattery()) { + if (wasOnBattery != isOnBattery) { + if (!wasOnBattery && isOnBattery) { + qDebug() << "switched to battery power"; emit SwitchedToBattery(); - } else if (wasOnBattery && !OnBattery()) { + } else if (wasOnBattery && !isOnBattery) { + qDebug() << "switched to ac power"; emit SwitchedToAC(); } } wasOnBattery = OnBattery(); - emit UpdatedDevices(); + deviceChanged(); } void Manager::handleDeviceChanged(const QString &device) { + Q_UNUSED(device) qDebug() << "device changed" << device; - if (device.isEmpty()) { return; } deviceChanged(); } @@ -532,6 +551,28 @@ bool Manager::registerSuspendLock() return false; } +bool Manager::registerLidLock() +{ + if (lidLock) { return false; } + qDebug() << "register lid lock"; + QDBusReply reply; + if (HasLogind() && logind && logind->isValid()) { + reply = logind->call("Inhibit", + "handle-lid-switch", + "powerkit", + "Custom lid handler", + "block"); + } + if (reply.isValid()) { + lidLock.reset(new QDBusUnixFileDescriptor(reply.value())); + qDebug() << "lidLock" << lidLock->fileDescriptor(); + return true; + } else { + qWarning() << reply.error(); + } + return false; +} + void Manager::SetWakeAlarmFromSettings() { if (!CanHibernate()) { return; } @@ -861,6 +902,12 @@ void Manager::ReleaseSuspendLock() suspendLock.reset(nullptr); } +void Manager::ReleaseLidLock() +{ + qDebug() << "release lid lock"; + lidLock.reset(nullptr); +} + void Manager::SetSuspendWakeAlarmOnBattery(int value) { qDebug() << "set suspend wake alarm on battery" << value; diff --git a/src/powerkit_manager.h b/src/powerkit_manager.h index a633220..74c879f 100644 --- a/src/powerkit_manager.h +++ b/src/powerkit_manager.h @@ -77,6 +77,7 @@ namespace PowerKit QDateTime wakeAlarmDate; QScopedPointer suspendLock; + QScopedPointer lidLock; int suspendWakeupBattery; int suspendWakeupAC; @@ -113,6 +114,7 @@ namespace PowerKit void deviceRemoved(const QDBusObjectPath &obj); void deviceRemoved(const QString &path); void deviceChanged(); + void propertiesChanged(); void handleDeviceChanged(const QString &device); void handleResume(); void handleSuspend(); @@ -128,6 +130,8 @@ namespace PowerKit void handleDelInhibitPowerManagement(quint32 cookie); bool registerSuspendLock(); + bool registerLidLock(); + void SetWakeAlarmFromSettings(); public slots: @@ -168,6 +172,7 @@ namespace PowerKit QMap GetInhibitors(); const QDateTime GetWakeAlarm(); void ReleaseSuspendLock(); + void ReleaseLidLock(); void SetSuspendWakeAlarmOnBattery(int value); void SetSuspendWakeAlarmOnAC(int value); bool SetDisplayBacklight(QString const &device, int value);