Showing posts with label dbus. Show all posts
Showing posts with label dbus. Show all posts

Standby/Suspend to Ram with Dbus and HAL in KDE 4/GNOME/XFCE

I was trying to work out how to issue the standby command over dbus so that I can send my computer to standby with just a single click from a desktop icon. It turns out at least ksmserver can't receive such a command as there isn't even a type for it in kworkspace:

http://api.kde.org/4.x-api/kdebase-workspace-apidocs/libs/kworkspace/html/kworkspace_8h-source.html

00049 enum ShutdownType {
00053 ShutdownTypeDefault = -1,
00057 ShutdownTypeNone = 0,
00061 ShutdownTypeReboot = 1,
00065 ShutdownTypeHalt = 2
00066 };

So the best I could come up with was this to shut down the system(If anyone has seen suspend anywhere in KDE dbus, please comment!):

dbus-send --session --dest=org.kde.ksmserver --type=method_call \
--print-reply /KSMServer org.kde.KSMServerInterface.logout int32:-1 int32:2 int32:2


But then I found out you can use hal directly and finally I had my script working for immediate suspend:

dbus-send --system --dest=org.freedesktop.Hal --type=method_call \
--print-reply /org/freedesktop/Hal/devices/computer \ org.freedesktop.Hal.Device.SystemPowerManagement.Suspend int32:0

This should work in any desktop environment. You can replace the 0 after "int32:" with the number of seconds to delay the Suspend. And you can also use it for hibernation:

dbus-send --system --dest=org.freedesktop.Hal --type=method_call \
--print-reply /org/freedesktop/Hal/devices/computer \ org.freedesktop.Hal.Device.SystemPowerManagement.Hibernate

Now we can enjoy suspend with a single click. Just put it into a shell script and link that to your desktop.

Locking your Screen with dbus from the command line in Gnome, KDE4 and others.

dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call \
--print-reply /ScreenSaver org.freedesktop.ScreenSaver.Lock


This will work with KDE4 and should as well work with newer Gnome versions. Tested with KDE 4.1 beta1. It needs the display variable to be set and be run as the appropriate user.
e.g. su $user -c "DISPLAY=:0 dbus-send... "

In Ubuntu 8.04 you can change your /etc/acpi/resume.d/90-xscreensaver.sh to lock the screen on resume like this:

#!/bin/sh

# now, we should poke xscreensaver so you get a dialog
#if pidof xscreensaver > /dev/null; then
for x in /tmp/.X11-unix/*; do
displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
getXuser;
if [ x"$XAUTHORITY" != x"" ]; then
export DISPLAY=":$displaynum"
su $user -c "(xscreensaver-command -deactivate)"
su $user -c "dbus-send --session --dest=org.freedesktop.ScreenSaver \
--type=method_call --print-reply /ScreenSaver \
org.freedesktop.ScreenSaver.Lock"
fi
done
#fi

Finding dbus targets made easy

I have been looking for a tool to display dbus targets for a while...

It's called qdbusviewer and comes with the package qt4-dev-tools. I found it in this howto save your current session in kde4.