#!/bin/sh
-if [ -f /proc/acpi/button/lid/LID/state ]; then
- if grep -q open /proc/acpi/button/lid/LID/state; then
- # Do not act on lid opened event. Only on lid closed.
- exit 0
- fi
+case "$1" in
+ button-sleep)
+ logger "Sleep button pressed"
+ ;;
+ button-lid)
+ if [ -f /proc/acpi/button/lid/LID/state ]; then
+ if grep -q open /proc/acpi/button/lid/LID/state; then
+ logger "Lid opened detected, do nothing"
+ # Do not act on lid opened event. Only on lid closed.
+ exit 0
+ fi
+
+ logger "Lid closed detected"
+ fi
+ ;;
+ *)
+ logger "Unknown event: ${1}"
+ ;;
+esac
+
+logger "Going to sleep"
+
+XSCREENSAVER_ACTIVE="$(pidof /usr/bin/xscreensaver)"
+
+# Check if xscreensaver is running. if not, just skip on.
+if [ "x${XSCREENSAVER_ACTIVE}" != "x" ]; then
+ # Run the lock command as the user who owns xscreensaver process,
+ # and not as root, which won't work.
+ su "$(ps aux | grep xscreensaver | grep -v grep | grep ${XSCREENSAVER_ACTIVE} | awk '{print $1}' )" \
+ -c "/usr/bin/xscreensaver-command -lock" &
+ sleep 1
fi
# discover video card's ID