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

10 comments:

  1. I needed this. Thanks a lot! :)

    ReplyDelete
  2. I'm glad it helped. Often takes some time to figure it out, always nice to help others save time.

    ReplyDelete
  3. under kde 3.5, it was
    $ dcop kdesktop KScreensaverIface lock

    ReplyDelete
  4. How I can unlock the screen via dbus from screensaver without entering password? I try to setup BlueProximity working with KDE4 screensaver.
    I can lock via command:
    $ qdbus org.kde.krunner /ScreenSaver Lock
    but don't know how to unlock...

    ReplyDelete
  5. Hmmm... may I ask why you would want to circumvent entering the password?

    ReplyDelete
  6. I want to lock computer when my mobile phone with bluetooth go away, and unlock when I with my phone goes back to computer.

    Program BlueProximity do this, but work only with gnome screensaver:

    Lock:
    gnome-screensaver-command -l

    Unlock without password:
    gnome-screensaver-command -d

    And I want to do this function in KDE.

    ReplyDelete
  7. Ah... ok. Well in my experience it usually works quite well to simply killall (screensaver) (kscreenlock...).
    Just look at the processes active when it's locked. Good luck! And maybe you want to post the script. It would be a nice feature to include this automatically. :)

    ReplyDelete
  8. I have no script, I using the program BlueProximity with thouse settings:
    Lock:
    qdbus org.kde.krunner /ScreenSaver Lock

    Unlock without password:
    qdbus org.kde.krunner /ScreenSaver SetActive false

    second string I get from kbluetooth 0.4 sources.

    ReplyDelete
  9. I have found a working variant!
    qdbus didn't send the values, dbus-send do this.
    Working commands:
    Lock:
    dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.Lock

    Unlock without password:
    dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.SetActive boolean:false

    ReplyDelete
  10. Here is what i am doing :)

    #!/bin/sh

    for x in /tmp/.X11-unix/*; do display=`echo $x | sed s#/tmp/.X11-unix/X##`; done
    for x in /tmp/kde-*; do user=`echo $x | sed s#/tmp/kde-##`; done
    export DISPLAY=":$display"
    su $user -c "qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock"

    ReplyDelete

I appreciate comments. And I do read them.