Mandriva 2009.1: Lock KDE 4 on Suspend

Published by cybso on
This is a post from my original site, which was hosted by the former blog service of the University of Osnabrück. I have moved it to the new site for archiving. Pages linked in this article may no longer work today, and the blog comments under the article no longer exist. Opinions expressed in this article reflect the point of view of the time of publication and do not necessarily reflect my opinion today.

In my installation, KDE failes to lock the screen on suspend / hibernate, even if the checkbox "lock screen" in the "Energieverwaltung" (how is this labeled in english? Power configuration? Don't know...) is enabled. So I've adapted this patch to Mandriva.

Create the file /etc/pm/sleep.d/50-lock with the following content:

#!/bin/sh

lockX() {
    for x in /tmp/.X11-unix/*; do
        displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
        user=`w -hs | awk '{ if ($3 == ":'$displaynum'" || $2 == ":'$displaynum'" ) { print $1; exit; } }'`
        export DISPLAY=":$displaynum"
        su $user -c "dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call --print-reply /ScreenSaver org.freedesktop.ScreenSaver.Lock"
    done
}

case "$1" in
    hibernate|suspend)
        lockX
        ;;
    thaw|resume)
        ;;
    *) exit $NA
        ;;
esac

Make this file executable (chmod 755 /etc/pm/sleep.d/50-lock). That's it. Pretty simple, isn't it?

Update 2009-05-30:

Here is another version of this script (from Ubuntu Bug #283315 ).