Linux – Suspend linux from KDE Plasma 5 lockscreen

arch linuxkdeplasma5screen-lock

TLDR: How to suspend from KDE LockScreen?

I'm using KDE's default lockscreen which you can somewhat configure Kde lockscreen config
But I haven't found a way to suspend my PC from the lockscreen, so I had to enter my password and then suspend every time I needed it which is very inconvenient.

Currently I modified KDE plasma lockscreen files (LockScreenUi.qml in particular) to add Suspend button. But maybe the is a simpler way to do this?

Also yes, I have seen reddit post and a few posts on kde forums without any solutions.

My configuration:

uname -a
Linux neko 4.15.14-1-ARCH #1 SMP PREEMPT Wed Mar 28 17:34:29 UTC 2018 x86_64 GNU/Linux

kded5 --version
kded5 5.45.0

plasmashell --version
plasmashell 5.12.5

The patch itself, it basically adds Suspend button with default icon and uses PowerDevil KDE service to suspend (found this usage logout files)

USE AT YOUR OWN RISK:

--- /usr/share/plasma/look-and-feel/org.kde.breeze.desktop/contents/lockscreen/LockScreenUi.qml 2018-05-01 16:03:40.000000000 +0300
+++ backups/kde-plasma-lockscreen/LockScreenUi.qml  2018-05-05 19:56:59.764353585 +0300
@@ -31,6 +31,18 @@

     colorGroup: PlasmaCore.Theme.ComplementaryColorGroup

+    function performOperation(what) {
+        var service = dataEngine.serviceForSource("PowerDevil");
+        var operation = service.operationDescription(what);
+        service.startOperationCall(operation);
+    }
+
+    PlasmaCore.DataSource {
+      id: dataEngine
+      engine: "powermanagement"
+      connectedSources: ["PowerDevil"]
+    }
+
     Connections {
         target: authenticator
         onFailed: {
@@ -174,6 +186,11 @@
                         onClicked: mainStack.push(switchSessionPage)
                         // the current session isn't listed in the model, hence a check for greater than zero, not one
                         visible: (sessionsModel.count > 0 || sessionsModel.canStartNewSession) && sessionsModel.canSwitchUser
+                    },
+                    ActionButton {
+                      text: i18nd("plasma_lookandfeel_org.kde.lookandfeel", "Suspend")
+                      iconSource: "system-suspend"
+                      onClicked: performOperation("suspendToRam")
                     }
                 ]

EDIT1:

There is already a report for the feature on KDE bugtracker (thanks Lekensteyn for the link)

Best Answer

thanks for the patch, it works like a charm!

same to handle the physical power key:

      Keys.onPressed: {
+            if (event.key == 16777399) performOperation("suspendToRam")

confirmed to work on kded5 5.67.0 and plasmashell 5.17.5 (Gentoo).

UPDATE 2020-06: refreshed patch for kde-plasma/plasma-workspace-5.18.5

--- /usr/share/plasma/look-and-feel/org.kde.breeze.desktop/contents/lockscreen/LockScreenUi.qml.orig    2020-06-25 00:50:49.181771074 +0300
+++ /usr/share/plasma/look-and-feel/org.kde.breeze.desktop/contents/lockscreen/LockScreenUi.qml 2020-06-25 00:56:23.750323655 +0300
@@ -38,6 +38,18 @@
 
     colorGroup: PlasmaCore.Theme.ComplementaryColorGroup
 
+    function performOperation(what) {
+        var service = dataEngine.serviceForSource("PowerDevil");
+        var operation = service.operationDescription(what);
+        service.startOperationCall(operation);
+    }
+
+    PlasmaCore.DataSource {
+      id: dataEngine
+      engine: "powermanagement"
+      connectedSources: ["PowerDevil"]
+    }
+
     Connections {
         target: authenticator
         onFailed: {
@@ -125,6 +137,8 @@
             }
         }
         Keys.onPressed: {
+            if (event.key == 16908292) performOperation("suspendToRam")
+            if (event.key == 16777399) performOperation("suspendToRam")
             uiVisible = true;
             event.accepted = false;
         }
@@ -279,6 +293,11 @@
                         anchors{
                             verticalCenter: parent.top
                         }
+                    },
+                    ActionButton {
+                        text: i18nd("plasma_lookandfeel_org.kde.lookandfeel", "Suspend")
+                        iconSource: "system-suspend"
+                        onClicked: performOperation("suspendToRam")
                     }
                 ]
 
Related Question