What this little tutorial shows is:
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.
I will show a specific example which consist in:
- When the lid closes -> play a "closing" sound and set my Pidgin status to away with a message saying "My laptop lid is closed..."
- When the lid is opened -> play an "opening" sound and set my Pidgin status to available with a message saying "I am here..."
- When I undock my laptop from the docking station -> play a "undock" sound
- When I dock my laptop into the docking station -> play a "docked" sound
NOTES:
- This was tested on a Lenovo Thinkpad T61 and a T61p both running Ubuntu 8.10. 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.
- 20/07/09: I just tested this on a T61p with Ubuntu 9.04 and it work fine, there is one minor thing to look, stated below.
- Make sure throughout the tutorial you change your_user with your actual user on the scripts and also feel free to save scripts on different paths updating the paths on the scripts of course.
- 27/02/10: If you want to implement the lid solution on a laptop with multiple users then look at posts 32, 33 and 40 from airtonix. I haven't tested these myself.
- 14/04/10: Tested this on a Thikpad W500 with Ubuntu 9.10. The script part works fine, the dock part needs a slight change, see post 48 for details.
- 15/07/10: 14/04/10: Tested this on a Thikpad W500 with Ubuntu 10.04. The above change is also necessary.
***I am not responsible if you mess things up, please be careful!***
It is important to understand that these events will be catched by processes owned by root. So we need to do a little hack so that root can run commands on our user's X environment. Many thanks to Earl Ruby for pointing me on how to do this http://earlruby.org/2008/08/update-p...us-using-cron/
In order to make the environment variables available for root:
Code:
gedit ~/export_x_info
and paste the following in:
Code:
#!/bin/bash
# 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:
Code:
chmod 700 ~/export_x_info
Now put it on your startup scripts. System -> Preferences -> Sessions and click Add.
Code:
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. There is a file /etc/acpi/lid.sh which runs every time your lid open or closes. So run:
Code:
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.
and paste the following into the file:
Code:
#!/bin/bash
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.
Code:
chmod +x ~/lid_event
And finally lets create the files open and close.
and paste the following into the file:
Code:
#!/bin/bash
#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:
Now the close file:
and paste the following into the file:
Code:
#!/bin/bash
#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:
Now you need to restart ACPI. Run:
Code:
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 gnome power management option for laptop lid closed is set to do nothing.
- The sounds used are from OpenOffice 3.0, the path changes for previous versions.
- Make sure you have libpurple-bin installed -- sudo apt-get install libpurple-bin
Now lets catch the docking event. Thanks to Steven King on pointing me how to go about this http://www.nabble.com/How-does-a-uev...html#a21898641
This is done by using a udev event.
Note: For Ubuntu 9.10 and 10.04 you need to do this slightly different as explained in post 20.
Code:
gksudo gedit /etc/udev/rules.d/80-thinkpad-T61.rules
and paste the following:
Code:
KERNEL=="dock.0", ATTR{docked}=="1", RUN+="/etc/thinkpad/dock.sh 1"
KERNEL=="dock.0", ATTR{docked}=="0", RUN+="/etc/thinkpad/dock.sh 0"
Save and close.
Code:
sudo mkdir /etc/thinkpad
gksudo gedit /etc/thinkpad/dock.sh
and paste the following:
Code:
#!/bin/sh
# 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:
Code:
sudo chmod -x /etc/thinkpad/dock.sh
NOTE: For some reason on Ubuntu 9.04 the above command will not work, the file will not be made executable. The workaround is to become root and execute the chmod command.
Now lets create the undocked file:
and paste the following:
Code:
#!/bin/bash
#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:
Code:
chmod +x ~/undocked
then the docked file:
and paste the following:
Code:
#!/bin/bash
#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:
Now finally we need to restart hal:
Code:
sudo /etc/init.d/hal restart
That's it, you should be able to dock and undock and hear a sound each time you do. From here the options for you are endless, you can basically do anything you want.
Bookmarks