Laptop Lid and Dock Scripts

The purpose of this tutorial is to show:

  1. How to catch the lid open and close events.
  2. How to catch the laptop dock and undock events.
  3. How to run a script for each of those events.

Specific examples are portrayed here:

  • - When the lid closes play a "closing" sound and set Pidgin status to away with a message saying "My laptop lid is closed..."
    - When the lid is opened play an "opening" sound and set Pidgin status to available with a message saying "I am here..."
    - When the laptop is undocked from the docking station play a "undock" sound
    - When the laptop is docked in the docking station play a "docked" sound

NOTES: - The material here was tested in Ubuntu 10.04. The computers used were a Lenovo Thinkpad T61 and a T61p. Things might slightly change depending on the laptop or Linux distribution. But the main idea will be the same. This tutorial is just to point people towards the right solution.

Laptop Lid Open/Close Scripts

It is important to understand that these events will be caught by processes owned by root. A fix is needed to allow root to run processes in the normal user's X environment. In order to make the environment variables available for root:

gedit ~/export_x_info

and paste the following in:

# Export the dbus session address on startup so it can be used by any other environment
sleep 5
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus

Save and close. Then make it executable:

chmod 700 ~/export_x_info

Now set it to run at startup. In Gnome 2, System> Preferences> Sessions and click Add. In Ubuntu Unity, click the gear icon in the upper right, select Startup Programs, and click Add.

Name: X Environment Variables
Command: /home/your_user/export_x_info

Click Add and close.

This will execute every time you start your computer and the call to source ~/.Xdbus loads the DBUS_SESSION_BUS_ADDRESS and XAUTHORITY environment variables before executing the purple-remote command for Pidgin.

Ubuntu makes it easy to catch the laptop lid open and close events.

Firstly, you need to create (if it's not there, that's the case on Ubuntu 14.04) the /etc/acpi/events/lm_lid file and put:

event=button/lid.*
action=/etc/acpi/lid.sh

After that, there is a file /etc/acpi/lid.sh which runs every time your lid open or closes. So run:

gksudo gedit /etc/acpi/lid.sh

and right after line #!/bin/bash paste /home/your_user/lid_event. Save and close.

Now create the file that will call different scripts according to open or close events.

gedit ~/lid_event

and paste the following into the file:

grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
        /home/your_user/close;
else
        /home/your_user/open;
fi

Save and close and make it executable.

chmod +x ~/lid_event

And finally, create the files open and close.

gedit ~/open

Paste the following into the file:

#This runs so that root can run the following command under the user's environment
source /home/your_user/.Xdbus
#play a open sound
DISPLAY=:0.0 su your_user -c "aplay /usr/lib/openoffice/basis3.0/share/gallery/sounds/sparcle.wav"
#change Pidgin status
DISPLAY=:0.0 su your_user -c "purple-remote 'setstatus?status=available&message=I am here...'"

Save and close. Make it executable:

chmod +x ~/open

Now create the close file:

gedit ~/close

and paste the following into the file:

#This runs so that root can run the following command under the user's environment
source /home/your_user/.Xdbus
#play a close sound
DISPLAY=:0.0 su your_user -c "aplay /usr/lib/openoffice/basis3.0/share/gallery/sounds/falling.wav"
#change Pidgin status
DISPLAY=:0.0 su your_user -c "purple-remote 'setstatus?status=away&message=My laptop lid is closed...'"

Save and close. Make it executable:

chmod +x ~/close

Next, restart ACPI. Run:

sudo /etc/init.d/acpid restart

Now you can test that it works by opening and closing your laptop lid.

  • Notes: - Make sure that the Power Management option for laptop lid closed is set to do nothing.
    - The sounds used are from OpenOffice 3.0, the path changes for other versions.
    - Make sure you have libpurple-bin installed -- sudo apt-get install libpurple-bin.

Dock and Undock Scripts

Many laptops can be docked into a powered, cooled docking station. These scripts are meant to capture the dock/undock event and, in this case, play a sound.

This is done by creating and using a udev event.

gksudo gedit /lib/udev/rules.d/85-thinkpad-T61.rules

And paste the following:

KERNEL=="dock.0", ACTION=="change", RUN+="/etc/thinkpad/dock.sh"

Create the docked script:

sudo mkdir /etc/thinkpad
gksudo gedit /etc/thinkpad/dock.sh

and paste the following:

# wait for the dock state to change
sleep 1
DOCKED=$(cat /sys/devices/platform/dock.0/docked)
case "$DOCKED" in
        "0")
        #undocked event
        /home/your_user/undocked
        ;;
        "1")
        #docked event
        /home/your_user/docked
        ;;
esac
exit 0

Save and close. Make it executable:

sudo chmod +x /etc/thinkpad/dock.sh

Next, create the undocked file:

gedit ~/undocked

and paste the following:

#This runs so that root can run the following command under the user's environment
source /home/your_user/.Xdbus
#play a sound
DISPLAY=:0.0 su your_user -c "aplay /usr/lib/openoffice/basis3.0/share/gallery/sounds/falling.wav"

Save and close. Make it executable:

chmod +x ~/undocked

Next, the docked file:

gedit ~/docked

Paste the following:

#This runs so that root can run the following command under the user's environment
source /home/your_user/.Xdbus
#play a sound
DISPLAY=:0.0 su your_user -c "aplay /usr/lib/openoffice/basis3.0/share/gallery/sounds/sparcle.wav"

Save and close. Make it executable:

chmod +x ~/docked

Now finally we need to restart hal:

sudo /etc/init.d/hal restart

You now should be able to dock and undock and hear a sound each time you do. From here the options for you are endless.


Originally posted The Ubuntu Forums (ubuntuforums.org)

LaptopLidAndDockScripts (last edited 2015-09-06 20:57:35 by silentvoice37)