How to automate KDE using D-Bus

Today I was playing with D-Bus message bus and KDE so I will describe here couple of basic actions that can be quickly automated by using shell commands. It shouldn't matter much but I need to mention that I am using KDE 4.9.5.

How to read session idle time using D-Bus?

At first you need to configure screensaver as I always got zero session idle time before I configured screensaver.

Then you can read session idle time using dbus-send command:

$ sleep 10; dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call --print-reply --reply-timeout=1000 /ScreenSaver org.freedesktop.ScreenSaver.GetSessionIdleTime
method return sender=:1.38 -> dest=:1.515 reply_serial=2
   uint32 9

Alternatively you can use QT-based D-Bus utility:

$ sleep 10 && qdbus org.kde.screensaver /ScreenSaver GetSessionIdleTime
9

I strongly recommend to read manual pages of above mentioned utilities.

How to check if lid is closed using D-Bus and proc pseudo-file system?

To check if lid is closed use the following command:

$ qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement isLidClosed
false

You are not forced to utilize D-Bus here as alternatively you can use proc pseudo-file system or udev. Udev is out of scope of this article as it would be too long (for udev example see How to automatically set up external monitor) so I will just mention proc pseudo-file system:

proc pseudo-file system - opened lid:

$ cat /proc/acpi/button/lid/LID0/state 
state:      open

proc pseudo-file system - closed lid:

$ sleep 5 && cat /proc/acpi/button/lid/LID0/state 
state:      closed

How to activate screensaver using D-Bus?

To activate screensaver execute command:

$ qdbus org.kde.screensaver /ScreenSaver SetActive true

How to check if screensaver is active?

To check if screensaver is active execute command:

$ qdbus org.kde.screensaver /ScreenSaver GetActive
true

How long is screensaver active?

If screensaver is active then you can use the following command to check how long it is active.

$ qdbus org.kde.screensaver /ScreenSaver GetActiveTime
10

How to activate lock screen using D-Bus?

To lock screen execute command:

$ qdbus org.kde.screensaver /ScreenSaver Lock

How to activate suspend using D-Bus?

Check out if computer can be suspended before entering into this state:

$ qdbus org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement CanSuspend
true
$ qdbus org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement Suspend

How to activate hibernate using D-Bus?

Check out if computer can be hibernated before entering into mentioned state:

$ qdbus org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement CanHibernate
true
$ qdbus org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement Hibernate

How to change brightness using D-Bus?

To read brightness value execute command:

$ qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement brightness
68

To set new brightness value use command:

$ qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement setBrightness 56

How to change sound volume?

To read current volume level execute command:

$ qdbus org.kde.kmix /Mixers/0/alsa_output_pci_0000_00_1b_0_analog_stereo volume
40

To change volume execute command:

$ qdbus org.kde.kmix /Mixers/0/alsa_output_pci_0000_00_1b_0_analog_stereo volume 60

To check out if device is muted execute command:

$ qdbus org.kde.kmix /Mixers/0/alsa_output_pci_0000_00_1b_0_analog_stereo mute
false

To mute the volume execute command:

$ qdbus org.kde.kmix /Mixers/0/alsa_output_pci_0000_00_1b_0_analog_stereo mute true

How to switch current desktop?

To read current desktop execute command:

$ qdbus org.kde.kwin /KWin currentDesktop
1

To change current desktop execute command:

$ qdbus org.kde.kwin /KWin setCurrentDesktop 2

How to simulate user action?

To simulate user action and reset session idle time execute command:

$ qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity

Notes

You can use any action mentioned here to perform specific actions like:

  • activate suspend/hibernate mode
  • change CPU governor
  • turn off computer
  • suppress screensaver if specific application is running
  • whatever comes into your mind

I will soon publish couple of sample scripts to show you how to create semi-intelligent screensaver - Semi-intelligent screensaver

You can use qdbusviewer (available in qt4-dev-tools package) to explore D-Bus in graphical environment.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu