[ltp] ACPI dock/undock events?

Stephan Groß linux-thinkpad@linux-thinkpad.org
Mon, 25 Feb 2008 11:00:04 +0100


On Montag, 25. Februar 2008, Henrique de Moraes Holschuh wrote:

Hello David,

> On Sun, 24 Feb 2008, David Abrahams wrote:
> > on Sun Feb 24 2008, Henrique de Moraes Holschuh <hmh-AT-hmh.eng.br> wrote:
> > > On Sun, 24 Feb 2008, David Abrahams wrote:
> > >> support, I've been unable to find an ACPI event that will tell me that
> > >> I've docked or undocked.  Is there such an event hiding somewhere? 
> > >> I'd
> > >
> > > Yes.  Look at uevents, using udev.
> >
> > Hey, thanks.  I just barely got the hang of ACPI -- can you give any
> > more details?

I am running two ThinkPads (X61T and X41T) with Fedore 8. To support my 
docking station I added a config file 98-docking.rules to /etc/udev/rules.d:

---------------------------------8<--------------------------------
# /etc/udev/rules.d/98-docking.rules
#
# Handle docking events
#
KERNEL=="dock.0", RUN+="docking.sh"
---------------------------------8<--------------------------------


Furthermore, I placed my handler script docking.sh in /lib/udev:


---------------------------------8<--------------------------------
#!/bin/bash
#
# /lib/udev/docking
#
# Tweaking your laptop when (un)docking


# Set default config to save values
EXT_MONITOR="VGA"
INT_MONITOR="LVDS"
UNDOCK_PWRMODE="mem"
XLOCK="true"

# Read config
if [ -f /etc/sysconfig/docking ]; then
        . /etc/sysconfig/docking
fi


# Verify that Xorg is running and xrandr can be used to switch resolutions
check_xorg()
{
        XORG=`/sbin/pidof Xorg`
        if [ ! -x /usr/bin/xrandr ] || [ -z $XORG ]; then
                return 1
        fi

        return 0
}

# Enable KDE or Gnome screen lock
lock_screen()
{
        # find out about running desktop environment
        if [ "`/usr/bin/w -s | /bin/grep -c 'startkde$'`" -gt "0" ]; then
                DESKTOP_SESSION=kde
        fi
        if [ "`/usr/bin/w -s | /bin/grep -c 'gnome-session$'`" -gt "0" ]; then
                DESKTOP_SESSION=gnome
        fi

        # enable screen lock depending on actual running desktop environment
        case "$DESKTOP_SESSION" in
                kde)
                        /usr/bin/dcop --all-users kdesktop KScreensaverIface 
lock
                        ;;
                gnome)
                        
XUSER=`/usr/bin/w | /bin/grep 'gnome-session$' | /usr/bin/awk '{ print $1; 
exit; }'`
                        /bin/su 
$XUSER -c "/usr/bin/gnome-screensaver; /usr/bin/gnome-screensaver-command -l"
                        ;;
        esac
}

# Check wether output is connected or not
check_output()
{
        OUTPUT="$1"
        if [ -e "$1" ]; then
                return 0
        fi

        STATUS=`/usr/bin/xrandr -q | /bin/grep $OUTPUT | /usr/bin/awk '{ print 
$2; exit; }'`

        if [ "$STATUS" == "connected" ]; then
                return 1
        else
                return 0
        fi
}

# Actions taken when docking
docking()
{
        check_output "$EXT_MONITOR"
        OUTPUT_CHK=$?
        if [ "$OUTPUT_CHK" == "1" ]; then
                /usr/bin/xrandr --output $INT_MONITOR --off --output 
$EXT_MONITOR --auto
        fi
}

# Actions taken when undocking
undocking()
{
        /usr/bin/xrandr --output $EXT_MONITOR --off --output 
$INT_MONITOR --auto

        # Switch to configured suspend mode.
        # Check for uptime first to prevent suspend-resume-cycle when booting.
        UPTIME=`/bin/cat /proc/uptime | /usr/bin/awk '{ print $1; 
exit; }' | /usr/bin/awk -F\. '{ print $1; exit; }'`
        if [ "$UPTIME" -gt "60" ]; then
                if [ "$XLOCK" == "1" ] || [ "$XLOCK" == "true" ]; then
                        lock_screen
                fi
                case "$UNDOCK_PWRMODE" in
                        mem)
                                /usr/sbin/pm-suspend
                                ;;
                        disk)
                                /usr/sbin/pm-hibernate
                                ;;
                esac
        fi
}


check_xorg
XORG_CHK=$?

if [ "$XORG_CHK" == "0" ]; then
        exit
fi

# Take care of X authentication and display settings
export DISPLAY=":0.0"
export XAUTHORITY=/var/gdm/\:0.Xauth

# Give the kernel some time to adjust the actual dock status
sleep 2

# Get current dock status
ACTION=`/bin/cat /sys/devices/platform/dock.0/docked`

case "$ACTION" in
   0)
        logger "$0: Undocking"
        undocking
        ;;
   1)
        logger "$0: Docking"
        docking
        ;;
   *)
        logger "$0: Error accessing dock status"
        ;;
esac
---------------------------------8<--------------------------------

Hope this helps,

Stephan.