Blog |Follow Nick on Twitter| About
 

Hardware stuff, especially laptops is usually window\$/manufacturer specifc so getting some of it work work can be a bit of a pain. In FC1 low batteries & standby is handled by APM, in FC2 upwards APM is replaced with ACPI & this is a quick text on getting going.

There are two folders & a service :

  • /etc/acpi/actions
  • /etc/acpi/events
  • /etc/init.d/acpid

The service , I think runs in the background monitoring the hardware, you can then put events into /etc/acpi/events and get things to happen when the deamon spots them .

I originally tested this on an HP Omnbook 6000, but now I'm using a compaq nc6000 . To replace apm I need to

  1. Monitor Battery / Power Status
  2. React when power button / standby pressed
  3. Do Something when the lid is closed

Documentation :
[

http://acpi.sourceforge.net/documentation/index.html](http://acpi.sourceforge.net/documentation/index.html)

To get started I had a look at the documentation ;) also there is an example in FC3 ( /etc/acpi/events/sample.conf )

Sample.conf

 # This is a sample ACPID configuration
event=button/power.*
action=/sbin/shutdown -h now

the Documentation explains that all acpi "real time" info is stored in /proc/acpi .

If you look /proc/acpi there is a directory "button" and in there "power" , so sample.conf seems to suggest if anything under "power" changes do action (in this case shutdown the machine).

Armed with this I could "test" with something a little quicker than switching on/off the machine; like opening / closing the lid....

So, in /proc/acpi/button/lid/C139 (the C139 is laptop specific) there is a file called state, and when it will tell you if the lid is open or closed (just in case you didn't know ;) ), now it is this "lid" directory that acpi will be looking for, so what you do is create a file called /etc/acpi/events/lid and put in it

/etc/acpi/events/lid

\# This is a sample ACPID configuration - what to do when a laptop lid is closed. event=button/lid.\* action=/etc/acpi/actions/lid "%e"

You'll notice that the action is set to /etc/acpi/actions/lid "%e" this will be a script, but you could point it to any script/executable you like. In /etc/acpi/actions/lid I have put

/etc/acpi/actions/lid

\#!/bin/bash

\# Make sure we recieve something ! if [ "\$1" = "" ] then echo "Script Failed, NO Input !!!" exit fi \# what we got from the acpi deamon acpi\_in="\$1" \# what the lid code was... lidstatus=\`echo "\$acpi\_in" | awk '{print \$4}'\` \# the lid code in dec dec\_lidstatus=\`printf "%d\\n " 0x\$lidstatus\` \#prepare for the maths x=\$dec\_lidstatus y=2 \# was the lid code odd or even ? mod=\$((\$x % \$y)) \# get the remainder of x / y and assign it to variable mod \# If %mod is 0 then the number is even, if it is 1 then the number is odd ! if [ \$mod = 0 ] then \# even numbers mean the lid is open !!! \# wall lid open exit else \# odd means closed !!! \# wall lid closed \# so lets lock the desktop ! su - nick -c "xscreensaver-command -lock" fi

Now, this needs a little explaining.....

There a couple of things you need to know about acpi

  1. acpi runs as root, so any event get's executed with root priveleges :D
  2. %e is an incremental number, odd numbers for lid closed & even for lid open
  3. To get these changes to work you need to restart the acpi service

So as you can see my lid open/close script gets a little complicated. To play if you remove the comments in from of the "wall" commands, save an open an empty shell, when you open & close the lid you get broadcast messages from root ! :cool: or if you just leave it as it is it'll try and run "nick"'s screen saver :D

Now my old laptop had both a standby or sleep button and a power button, but getting events to happen is exactly the same, simply create a script doing what you want it to do, create a file ine /etc/acpi/events and set the event to event=button/power.* (or event=button/sleep.*) and voila !

Now finally, monitoring battery status, thankfully gnome already has a nice applet

![](http://www.gnome.org/start/2.8/notes/figures/figure-battstat-applet.png)

or if you want to check it your self /proc/acpi/battery/C138/state (again C138 is laptop specific) this file will tell you !

I hope you've found this of some use !

rgds,

Nick

 
Nick Bettison ©