Ubuntu – How to lock xscreensaver on suspend

lock-screenscreensaversuspend

Hy!

I replaced gnome-screensaver with xscreensaver, it works great so far, but I can't get it to lock my session when I suspend my laptop.

Any ideas how to do that?

Ubuntu 10.10
Dell Vostro 1310

Thanks!

Best Answer

Finally I found the right way of doing this combining the parts I found in /etc/acpi/sleep.sh (that seems left unused there), and Scaine pointed in /usr/lib/pm-utils...

So the final script that works for me is: /usr/lib/pm-utils/sleep.d/00xscreensaver

#!/bin/sh

# Lock xscreensaver on resume from a suspend.

# getXuser gets the X user belonging to the display in $displaynum.
# If you want the foreground X user, use getXconsole!
getXuser() {
        user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
        if [ x"$user" = x"" ]; then
                startx=`pgrep -n startx`
                if [ x"$startx" != x"" ]; then
                        user=`ps -o user --no-headers $startx`
                fi
        fi
        if [ x"$user" != x"" ]; then
                userhome=`getent passwd $user | cut -d: -f6`
                export XAUTHORITY=$userhome/.Xauthority
        else
                export XAUTHORITY=""
        fi
        export XUSER=$user
}

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"
            case "$1" in
                resume|thaw)
                    sudo -u $XUSER xscreensaver-command -unthrottle
                ;;
                suspend|hibernate)
                    sudo -u $XUSER xscreensaver-command -throttle
                    sudo -u $XUSER xscreensaver-command -lock
                ;;
            esac
        fi
    done
fi

The actual xscreensaver commands can also invoked trough su (instead of sudo) like

su $XUSER -c "(xscreensaver-command -lock)"

but that won't works for me because I use pam_mount to mount my encrypted home during login, and pam asks for my password for pam_mount in the script when I use su...

Related Question