LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   lock screen before suspend with power button (https://www.linuxquestions.org/questions/slackware-14/lock-screen-before-suspend-with-power-button-601154/)

BCarey 11-20-2007 10:32 AM

lock screen before suspend with power button
 
I am using Slackware 12.0 with KDE on a HP zv5000 laptop and a 2.6.22 kernel. I use kpowersave for suspending and hibernating. Everything works fine, and I have it configured to lock the screen before either action. I would like to be able to suspend (to RAM) when I press the power button, so I edited "/etc/acpi/acpi_handler.sh" to suspend instead of shutdown the system. This also works fine, but I do not know how to get it to lock the screen first. I have tried adding "/usr/bin/kdesktop_lock --forcelock" to acpi_handler.sh before I call suspend, but it doesn't work (the command does work on its own, it just doesn't seem to have any effect in the script.

Does anyone know how I can do this?

Thanks,
Brian

jtshaw 11-27-2007 08:04 AM

Moved to Slackware at posters request.

zhoun 11-27-2007 08:28 AM

Code:

USER="guo"
LOCKAPP="/usr/bin/kdesktop_lock --forcelock"
su ${USER} -c "${LOCKAPP}"

change the USER to your login name and try it.

BCarey 11-27-2007 09:59 AM

zhoun,

Thanks for the suggestion. Unfortunately it does not help and the behavior is the same.

Brian

Alien_Hominid 11-27-2007 02:51 PM

Just a wild guess: maybe that kdesktop_lock isn't quick enough to lock the screen before actual suspend to RAM happens.

BCarey 11-27-2007 04:17 PM

Quote:

Originally Posted by Alien_Hominid (Post 2972501)
Just a wild guess: maybe that kdesktop_lock isn't quick enough to lock the screen before actual suspend to RAM happens.

I put a "sleep 5" in the script before the suspend command, and it appears that kdesktop_lock is not working at all in the script, even though it works fine if I just run it from the command line. Hmmm. At least this gives me a path to pursue. Thanks.

Brian

BCarey 11-27-2007 04:19 PM

BTW, This languished for a week in another LQ forum with no response. I get it moved to the Slackware forum this morning and I've got two responses already. Thanks, Slackers for a great forum.

Brian

rg3 11-27-2007 06:52 PM

I bet kdesktop_lock needs to find the environment variables of a corresponding running KDE session for it to work, and they won't be available when it's called from acpid, which is launched at boot. In my system I suspend to ram by closing the laptop, so I have created a shell script that stops a couple of things, calls s2ram and then restores the things I stopped, and I have associated the LID button event to call that script. It's similar to what you do. To lock the screen before suspending, I do it by hand. It's very easy and not annoying if you set up a keyboard shortcut to do so, like I did with Ctrl+Alt+L. So when I want to suspend to RAM I press Ctrl+Alt+L and close the laptop. Piece of cake.

zhoun 11-27-2007 07:23 PM

Please try this again.

Code:

USER="guo"       
export XAUTHORITY="/home/${USER}/.Xauthority"       
LOCKAPP="/usr/bin/kdesktop_lock --forcelock"
su ${USER} -c "${LOCKAPP}"

Remember to change your login name.
I copy the code from my acpi scripts.
It worked fine for me with all thinkpad Fn functions.

BCarey 11-27-2007 07:50 PM

zhoun,

I added the additional line, still no dice.

rg3,

Which environment variables were you referring to?

Brian

zhoun 11-27-2007 08:15 PM

My /etc/acpi/acpi_handler.sh, just for reference.
I have no idea why it not work for you.

Code:

#!/bin/sh
#
# ACPI handler script default.sh
# Optimized for Thinkpad R52
#
# Copyright (c) Phillip Berndt, 2007
# Modified by Forever Steel, 2007-11-19, add Fn+Home, Fn+End function (Brightness Controll)

## Configuration #####################################################
# X11 access for osd_cat and xset dpms
USER="guo"                                        # Main user of this PC
export XAUTHORITY="/home/guo/.Xauthority"        # X authority file
export DISPLAY="auto"                                # Default display

# Screen saver
LOCKAPP="export LANG=zh_CN.gbk;/usr/bin/kdesktop_lock --forcelock"

# WLAN device driver
WLAN_MODULE="ipw3945"
WLAN_PARAMS=""
# or: WLAN_PARAMS="rtap_iface=1"

# Which application to run on "zoom" key
# In my case, start a network autoconfiguration tool AS ROOT!!
# You might want to use "su ${USER} -c <foo>" here
zoom_do() {
        xterm -title "Network autoconfiguration" -e /usr/local/lib/net/network &
}

# File for OSD disabling
OSD_DISABLE_FILE="/var/lib/misc/acpid_osd_disable"

## Help ##############################################################
#
# Installation:
#  Copy to /etc/acpi/default.sh for acpid and symlink to tpb.sh to use it
#  as a callback script for tpb.
#  Run
#    echo enable,0xffff >/proc/acpi/ibm/hotkey
#    /usr/bin/tpb -d --callback="/etc/acpi/tpb/tpb.sh" -m on -x off
#  on startup.
#
# This script has the following configuration:
#  * F1: Change between performance and powersave govenor (CPU)
#  * F2: dpms force off (Blank screen)
#  * F3: Lock screen
#  * F4: Suspend to ram
#  * F5: Bluetooth on/off
#  * F7: VGA out on/off
#  * F8: LCD on/off
#  * F11: Deactivate OSD
#  * F12: Suspend to disk
#  * Change to performance on AC-plug-in
#  * Change to powersave on AC-unplug
#  * Hibernate on battery power < 5%
#  * Spindown HDD on lid close
#  * Poweroff after pressing the power button TWO times
#  * Load and unload wlan driver on "Access IBM" button
#
# You'll need the following tools to use all features:
#
#  * /usr/sbin/hibernate        Hibernate script
#  * /usr/sbin/radeontool        Radeon graphics settings
#  * /usr/bin/osd_cat                cat'like X11 tool
#  * /usr/bin/xset                Xset (Not installed by default with modular Xorg)
######################################################################

export PATH=/bin/mem:/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin

if [ "$(basename $0)" == "tpb.sh" ]; then
        ACTION="$1"
        PARAMETER="$2"
        logger "Got TPB Event: Action ${ACTION}, Parameter: ${PARAMETER}"
else
        GROUP="${1/\/*/}"
        ACTION="${1/*\//}"
        PARAMETER2="$2"
        PARAMETER3="$3"
        PARAMETER="$4"
        logger "Got ACPI Event: In Group ${GROUP} action ${ACTION}, Parameter: ${PARAMTER2} ${PARAMTER3} ${PARAMETER}"
fi

## Functions #########################################################
# OSD cat
if [ "${DISPLAY}" == "auto" ]; then
        X="$(ls /tmp/.X11-unix/X* | head -n1)";
        [ "${X}" != "" ] && export DISPLAY=":${X/\/tmp\/.X11-unix\/X/}"
fi

if [ "${DISPLAY}" != "auto" ] && which osd_cat &>/dev/null; then
        osd() {
                [ -f ${OSD_DISABLE_FILE} ] && return
                pkill osd_cat
                echo $@ | osd_cat -p bottom -A center -c green -O 1 -u black -f "-adobe-helvetica-bold-r-normal-*-*-320-*-*-p-*-iso8859-1" &
        }
else
        osd() {
                logger "OSD cat: $@"
        }
fi

# Suspend to ram
suspram() {
                # Prepare suspend (Power saving hacks)
                echo -n eject > /proc/acpi/ibm/bay
                ethtool -s eth0 wol d
                su ${USER} -c "${LOCKAPP}"
               
                # Suspend
                sleep 1
                echo mem > /sys/power/state
                /etc/rc.d/rc.smartctl
}

## Handlers ##########################################################
if [ "${ACTION}" == "thinkpad" ]; then
        # Load wlan driver
        if [ -d /sys/module/${WLAN_MODULE} ]; then
                modprobe -r ${WLAN_MODULE}
                osd "${WLAN_MODULE} unloaded"
        else
                modprobe ${WLAN_MODULE} ${WLAN_PARAMS}
                osd "${WLAN_MODULE} loaded"
        fi
fi

if [ "${ACTION}" == "zoom" ]; then
        zoom_do
fi

# Closing the laptop (Or opening it ;))
if [ "${ACTION}" == "lid" ]; then
        if [ "$(cat /proc/acpi/button/lid/LID/state | grep open)" != "" ]; then
                # Enable the LCD
                xset dpms force on
                /etc/rc.d/rc.smartctl
        else
                # Immerdiately spindown the harddrive to prevent loosing data
                # due to drive problems. I'd prefer to power it down, but it
                # needs a very long time to power on again.
                xset dpms force off
                su ${USER} -c "${LOCKAPP}" &
                sync
                hdparm -y /dev/sda
                # when lid up, no event send, so add this
                if [ "$(cat /proc/acpi/button/lid/LID/state | grep open)" != "" ]; then
                        # Enable the LCD
                        xset dpms force on
                        /etc/rc.d/rc.smartctl
                fi
        fi
fi

# Special IBM hotkeys
if [ "${ACTION}" == "hotkey" ]; then
        # Change speed
        if [ "${PARAMETER}" == "00001001" ]; then
                DO="performance"
                grep "performance" "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" &>/dev/null && DO="powersave"
                echo ${DO} > "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
                osd "Governor is now ${DO}"
        fi

        # Blank screen
        if [ "${PARAMETER}" == "00001002" ]; then
                xset dpms force off
        fi

        # Blank screen buton (Sleep instead)
        if [ "${PARAMETER}" == "00001003" ]; then
                #chvt 6
                xset dpms force off
                su ${USER} -c "${LOCKAPP}" &
        fi

        # Suspend-To-Ram on sleep
        if [ "${PARAMETER}" == "00001004" ]; then
                suspram
        fi

        # Bluetooth
        if [ "${PARAMETER}" == "00001005" ]; then
                DO="disable"
                grep -q "disabled" "/proc/acpi/ibm/bluetooth" && DO="enable"
                echo ${DO} > "/proc/acpi/ibm/bluetooth"
                osd "Bluetooth ${DO}d"
        fi

        # Display (External)
        if [ "${PARAMETER}" == "00001007" ]; then
                if /usr/local/sbin/radeontool dac | grep -q "looks on"; then
                        /usr/local/sbin/radeontool dac off
                        echo "crt_disable" > /proc/acpi/ibm/video
                        osd "Video switch (off)"
                else
                        /usr/local/sbin/radeontool dac on
                        echo "crt_enable" > /proc/acpi/ibm/video
                        osd "Video switch (on)"
                fi
        fi

        # Display (Internal)
        if [ "${PARAMETER}" == "00001008" ]; then
                if /usr/local/sbin/radeontool light | grep -q "looks on"; then
                        /usr/local/sbin/radeontool light off
                        osd "Internal video switch (off)"
                else
                        /usr/local/sbin/radeontool light on
                        osd "Internal video switch (on)"
                fi

        fi

        # (De)activate OSD
        if [ "${PARAMETER}" == "0000100b" ]; then
                if [ -f ${OSD_DISABLE_FILE} ]; then
                        rm ${OSD_DISABLE_FILE}
                        osd "OSD activated"
                else
                        osd "OSD deactivated"
                        touch ${OSD_DISABLE_FILE}
                fi
        fi

        # Hibernate button
        if [ "${PARAMETER}" == "0000100c" ]; then
                su ${USER} -c "${LOCKAPP}" &
                /usr/local/sbin/hibernate
                /etc/rc.d/rc.smartctl
        fi

        # Brightness up
        #if [ "${PARAMETER}" == "00001010" ]; then
        #        echo up > /proc/acpi/ibm/brightness
        #        xbacklight -inc 7
        #        level=$(cat /proc/acpi/ibm/brightness | grep level:)
        #        osd "${level}"
        #fi
        # Brightness down
        #if [ "${PARAMETER}" == "00001011" ]; then
        #        echo down > /proc/acpi/ibm/brightness
        #        xbacklight -dec 3
        #        level=$(cat /proc/acpi/ibm/brightness | grep level:)
        #        osd "${level}"
        #fi
fi


# Battery actions
if [ "${GROUP}" == "battery" ]; then
        # $ACTION is the battery i.e. BAT0
        # Get remaining capacity
        capacityPercent=$(/usr/bin/acpitool -b | grep \#$[${ACTION/*[A-Z]/} + 1] | egrep -o [0-9.]+% | egrep -o ^[0-9]+)

        # OSD info
        osd "Battery Event: ${capacityPercent}%"

        # Hibernate on low battery
        if [ "${capacityPercent}" -lt 5 ]; then
                /usr/local/sbin/hibernate -f
        fi
fi

# Brightness button
if [ "${ACTION}" == "video" ]; then
        if [ "${PARAMETER3}" == "00000086" ]; then
                echo up > /proc/acpi/ibm/brightness
                xbacklight -inc 7
                level=$(cat /proc/acpi/ibm/brightness | grep level:)
                osd "${level}"
        fi
        if [ "${PARAMETER3}" == "00000087" ]; then
                echo down > /proc/acpi/ibm/brightness
                xbacklight -dec 3
                level=$(cat /proc/acpi/ibm/brightness | grep level:)
                osd "${level}"
        fi
fi

# Power button
if [ "${ACTION}" == "power" ]; then
        if [ ! -e /var/run/poweroff ]; then
                date "+%s" > /var/run/poweroff
        else
                DIFF=$[ $(date "+%s") - $(cat "/var/run/poweroff") ]
                date "+%s" > /var/run/poweroff

                if [ ${DIFF} -lt 5 ]; then
                        /sbin/poweroff
                fi
        fi
fi

# AC Control
if [ "${ACTION}" == "ac_adapter" ]; then
        if [ "`cat /proc/acpi/ac_adapter/AC/state | grep on-line`" != "" ]; then
                echo -n "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
                osd "Powering from AC"
        else
                echo -n "powersave" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
                osd "Powering from Battery"
        fi
fi


zhoun 11-27-2007 08:27 PM

Or you can monitor the acpi log file:
/var/log/acpid

If error happens, it will be logged in this file.

BCarey 11-27-2007 10:46 PM

Thank you all, especially zhoun. I have a solution. This is the relevant section of my acpi_handler.sh:

Code:

export DISPLAY=":0"
/usr/bin/kdesktop_lock --forcelock &
sleep 3
/usr/lib/hal/scripts/hal-system-power-suspend

Comments:

1. I do not need the "export XAUTHORITY" line because I do "xhost +localhost". If I didn't, then the "export XAUTHORITY" line is necessary

2. The ampersand after the kdesktop_lock command is necessary or it will not suspend until I unlocked the display

3. The sleep 3 line is necessary or it will not lock until after the system is resumed, allowing a brief glimpse of the screen display

4. If you do not have the root password, then the "su..." line from zhoun's post is necessary.

Thanks again for all the help.

Brian

zhoun 11-27-2007 11:04 PM

You got it! :)

Your comments is clear and will help other people.


All times are GMT -5. The time now is 10:36 PM.