[HOWTO] Run scripts for laptop lid open/close and dock/undock events
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.
:D
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
Hey, thanks for the guide, honestly i think this is what i was looking for, it just didnt work for me.
A brief outline of my problem...
i have a thinkpad x61 tablet who's screen i would like to rotate when i rotate my lid. I have a button on my laptop screen that executes a program I wrote to rotate the screen. If i rotate the screen using this method, all is fine... its just inconvenient... I'd like acpi to do it for me....
I used acpi_listen to find the rotate event, created a file in /etc/acpi/events/ called "ibm-clockwise.sh". Then I made this file executable and had it call a script in /etc/acpi called "ibm-clockwise". This file is also executable. If i just put "rotate" (the name of my program) in "ibm-clockwise", compiz crashes on rotation.
So I did some testing and found that when I execute rotate as root, the same behavior is observed, i assume this is the problem, since the scripts in /etc/acpi are executed as root.
I screwed around with "su nick -c rotate" for a while, both while logged in as root, using "sudo su", and placing this command in the /etc/acpi/ibm-clockwise file. That was fun, all time wasting aside.
Everytime compiz crashes, I lose half of my workspaces... All of my special keys are unbound, and obviously desktop effects are gone... I have to go to the fusion-icon and do "reload window manager" ...
it seems this problem could be fixed by having acpi execute rotate as my user (nick). This brings me to your guide.
I followed parts of it, but i didnt really want my home directory cluttered with a bunch of scripts.
I created ~/export_x_info and pasted your info in directly. Then i made it executable. I also added it to my startup using your method, but I didnt feel like restarting for it to take effect, so i executed it with a ./export_x_info ...
I skipped all the linking and just went directly to pasting the source ~/.Xdbus stuff into the action script since i only need it to do one thing... my /etc/acpi/ibm-clockwise file looks like
#!/bin/bash
source /home/nick/.Xdbus
DISPLAY=:0.0 su nick -c "rotate"
Though im not even sure if the "DISPLAY=:0.0" is necessary, i tried removing it and things still didnt work.
i restarted acpi and tested, and compiz still crashes, i assume because the file is still not being executed as my user (nick).
Any help would be so great, i appreciate it...
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
Quote:
Originally Posted by
e64462
Any help would be so great, i appreciate it...
Right... let's see what we can do.
First, for troubleshooting purposes I recommend running stuff yourself as root. This way you will determine when, where and why it is not working. The idea is something like:
"su -" and:
Code:
env | grep DBUS_SESSION_BUS_ADDRESS
env | grep XAUTHORITY
Probably this didn't returned anything. That's fine. If you run your "rotate" script now it will most likely crash as you described. Try and see. You probably will see where it is failing.
Now:
Code:
source /home/daniel/.Xdbus
And again:
Code:
env | grep DBUS_SESSION_BUS_ADDRESS
env | grep XAUTHORITY
This time you should get your session's values. So now you should be able to start running stuff from root as your user on your current session.
What I suspect is happening is that there is a lot of stuff inside your rotate script that is still just been run as root. See, what happens is that when you do "source /home/nick/.Xdbus" the env variables are been imported just to that environment, if you run a script from there you are creating a new bash environment just for that script.
You should call "source /home/nick/.Xdbus" again within your rotate script and stuff that you do there that uses your session's env variables should be run as DISPLAY=:0.0 su nick -c "etc....."
Do you see my point?
I hope this helps.... let me know how you get on.
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
hey, thanks for the reply!
so im not sure i fully understand what's happening... do you want me to run
env | grep DBUS_SESSION_BUS_ADDRESS
env | grep XAUTHORITY
as root? like.. type "sudo su" and then enter those?
When I do that, i get
nick@notebook:~$ sudo su
root@notebook:/home/nick# cd
root@notebook:~# env | grep DBUS_SESSION_BUS_ADDRESS
root@notebook:~# env | grep XAUTHORITY
XAUTHORITY=/home/nick/.Xauthority
Then i source /home/nick/.Xdbus and rerun those env commands
root@notebook:~# source /home/nick/.Xdbus
root@notebook:~# env | grep DBUS_SESSION_BUS_ADDRESS
root@notebook:~# env | grep XAUTHORITY
XAUTHORITY=/home/nick/.Xauthority
the output is the same both times, the first returns nothing, the second returns ~/.Xauthority
I tried rotating the screen before and after both of those blocks... compiz crashed all 4 times... the output of my rotate program was pretty much the same in each case... the only instruction that really returns anything is the
system("compiz --replace");
which returns
nick@notebook:~$ sudo rotate
Checking for Xgl: not present.
Detected PCI ID for VGA:
Checking for texture_from_pixmap: not present.
Trying again with indirect rendering:
Checking for texture_from_pixmap: present.
Checking for non power of two support: present.
Checking for Composite extension: present.
Comparing resolution (1024x768) to maximum 3D texture size (2048): Passed.
Checking for Software Rasterizer: Not present.
Checking for nVidia: not present.
Checking for FBConfig: present.
Checking for Xgl: not present.
/usr/bin/compiz.real (video) - Warn: No 8 bit GLX pixmap format, disabling YV12 image format
I dont see too much of interest there...
In regards to your final comment about adding
source /home/nick/.Xdbus
to my rotate program and then executing anything that uses the env variables using su nick -c <do-stuff> ... I don't know which statements pertain to the env variables... how do i identify them (im lost needless to say)
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
hmm... perhaps you should mostly ignore my last post. I did some searching in hopes of gaining a better understanding of what your scripts were doing.
Playing with the sample export_x_info file you provided, i realized that it wasn't placing the output of
Code:
env | grep DBUS_SESSION_BUS_ADDRESS
in /home/nick/.Xdbus
is the line in your sample file supposed to read:
Code:
env | grep DBUS_SESSION_BUS_ADDRESS >> $HOME/.Xdbus
i made that correction, then did
Code:
nick@notebook:~$ rm -f .Xdbus
nick@notebook:~$ ./export_x_info
nick@notebook:~$ cat .Xdbus
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-YX3A7vUW9N,guid=b59d1b4c84bf031c1e65c1a549aecaa7
export DBUS_SESSION_BUS_ADDRESS
XAUTHORITY=/home/nick/.Xauthority
export XAUTHORITY
then logged in as root using "sudo su"
Code:
root@notebook:~# env | grep DBUS_SESSION_BUS_ADDRESS
root@notebook:~# env | grep XAUTHORITY
XAUTHORITY=/home/nick/.Xauthority
DBUS_SESSION_BUS_ADDRESS is still empty anyways... so i:
Code:
root@notebook:~# source /home/nick/.Xdbus
root@notebook:~# env | grep DBUS_SESSION_BUS_ADDRESS
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-YX3A7vUW9N,guid=b59d1b4c84bf031c1e65c1a549aecaa7
root@notebook:~# env | grep XAUTHORITY
XAUTHORITY=/home/nick/.Xauthority
which i think is what you expected...
at this point i think I've narrowed down the segment of code in my program that is causing all the problems. when i comment out this line, the screen rotates just fine, but the keys that i need reassigned after rotation aren't recognized (which is why it's there). But this is the troublesome line.
system(compiz --replace);
So i need to somehow give root the ability to restart compiz... playing with this idea, i started issuing compiz --replace every odd way i could think of (after assigning the environment variables) which didnt work =(
any ideas?
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
Quote:
Originally Posted by
e64462
then logged in as root using "sudo su"
Well, first thing is just to say that "sudo su" it's not the same as "su -". They both make you root alright but with the first you are still on your user's home directory, whereas the second makes you root and places you on root's home directory. That's why you are getting something for your xauthority value but not for your bdus session value.
When running scripts as root it is on root's home directory. So if you just call your "rotate" script root is not even able to find it. Probably the first problem you have in the line. You should use the full path of your script.
Quote:
Originally Posted by
e64462
But this is the troublesome line.
system(compiz --replace);
any ideas?
It sure is a troublesome line. I just tried
Code:
DISPLAY=:0.0 su user -c "compiz --replace"
And it crashed....
Compiz is a very complicated piece... you may want to ask on their forums to see what you need to do to restart it as root but for your users session. Sorry I can't be of more help.
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
its all good... stupid compiz... thanks for the help though! atleast i learned something!
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
Quote:
Originally Posted by
e64462
stupid compiz...
I agree! But we can't live without it can we? It's so beautiful...
Anyway, I hope you sort this out, post back if you do.
Cheers!
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
Good howto, i will try this in an hour ;)
Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events
Firstly, let me say a big thankyou for this! It makes my life far easier at work when moving between my desk and our test floor.
I was wondering if there's some way to link this into emesene (or amsn) to change my status? emesene has a dbus plugin so i thought it should be ok!