Difference between revisions of "How to configure acpid"

From ThinkWiki
Jump to: navigation, search
Line 1: Line 1:
With the introduction of ACPI there are more things under programmatic
+
Basically, acpid just executes scripts residing in <tt>/etc/acpi/actions</tt>. Which script to launch at which event is configured in several files in <tt>/etc/acpi/events</tt>. {{cmd|man acpid}} holds detailed information on how to configure acpid.
control. Sleeping is one of those things. To sleep the {{T30}} manually is
 
simple. You just do:
 
  
echo -n "mem" /sys/power/state
+
The [[ibm-acpi]] packages includes example scripts in the <tt>config</tt> folder inside the tarball. They are a good starting point to adjust them to your needs. You also might want to have a look at the [[Configs#ACPI | ACPI section of the Configs page]] and you can find information about the event strings ibm-acpi generates for certain keys at the [[How to get special keys to work#ibm-acpi_events | Special Keys HOWTO]].
  
However there are two problems with this. It doesn't happen automatically
+
==Example: go to sleep on lid close==
when you close the lid and it does not turn off the LCD backlight. The
 
acpid daemon is provided to solve the first problem. By adding an event
 
handler you can run a script in response to both closing the lid and
 
pressing Fn-F4 (suspend on the T30). You just need to create a file in
 
/etc/acpi/events like the following:
 
  
$ cat /etc/acpi/events/sleep
+
To make the ThinkPad go to sleep when you close the lid, you need to add
  event=button/(sleep|lid)
+
an event handler for the lid event and an action script that takes care
  action=/etc/acpi/actions/sleep %e
+
of going to sleep and resuming.
 +
 
 +
The event script needs to be created within <tt>/etc/acpi/events</tt> and can have any name you like.
 +
In this case we call it lid because it will trigger the lid event. Do {{cmdroot|vi /etc/acpi/events/lid}} and make it look like this:
 +
  event=button/lid
 +
  action=/etc/acpi/actions/sleep.sh %e
  
 
The "event" line is a regular expression specifying the events we're
 
The "event" line is a regular expression specifying the events we're
interested in. I determined what the event strings are from looking at
+
interested in. You can determine what the event strings are from looking at
/var/log/acpid after trying to suspend, close the lid, etc. What I found
+
<tt>/var/log/acpid</tt> after trying to suspend, close the lid, etc. .
was that when the Fn-F4 key sequence is selected a "button/sleep" event is
+
You can find information about the event strings [[ibm-acpi]] generates for certain keys at the [[How to get special keys to work#ibm-acpi_events | Special Keys HOWTO]].
dispatched. Closing and opening the lid both yield "button/lid" events.
 
Note that if you add or modify one of these events files you must kill
 
-SUGHUP <pidofacpid>.
 
  
 
The "action" line is the command to be executed when these events are
 
The "action" line is the command to be executed when these events are
dispatched. In this example I call a script and pass the event description
+
dispatched. In this example we call the <tt>sleep.sh</tt> script residing in <tt>/etc/acpi/actions</tt> and pass the event description text using the %e placeholder.
text using the builtin %e macro. The /etc/acpi/actions/sleep script is as
+
 
follows:
+
{{NOTE|To make your changes take effect after adding or modifying the events files you must do a <tt>'''kill -SIGHUP `pidof acpid`'''</tt>}}.
 +
 
 +
Our example <tt>/etc/acpi/actions/sleep.sh</tt> script looks as follows:
  
 
  #!/bin/sh
 
  #!/bin/sh
 
   
 
   
  if [ "$1" = "button/sleep" ] || \
+
  # if launched through a lid event and lid is open, do nothing
        grep -q closed /proc/acpi/button/lid/${2}/state
+
echo "$1" | grep "button/lid" && grep -q open /proc/acpi/button/lid/LID/state && exit 0
  then
+
   
    chvt 6;
+
# sync filesystem and clock
    /usr/sbin/radeontool light off;
+
sync
    echo -n "mem" > /sys/power/state;
+
/sbin/hwclock --systohc
    chvt 7;
+
  fi
+
# switch to console
 +
FGCONSOLE=`fgconsole`
 +
chvt 6
 +
/usr/sbin/radeontool light off
 +
 +
# go to sleep
 +
echo -n "mem" > /sys/power/state
 +
 +
# readjust the clock (it might be off a bit after suspend)
 +
/sbin/hwclock --adjust
 +
/sbin/hwclock --hctosys
 +
 +
# turn on the backlight and switch back to X
 +
radeontool light on
 +
  chvt $FGCONSOLE
  
Translated this reads -- if the event is a sleep event (meaning Fn-F4 was
+
The lid generates an event for both opening and closing thus requiring
triggered) or the state of the lid is closed, then switch to a virtual
+
that we check it's state and only act if it's closed.
console, turn off the backlight, and goto sleep. When we wake up, switch
 
back to X.
 
  
Note that the echo line does not return until we are revived. So there is
+
Note that the <code>echo -n "mem" > /sys/power/state
 +
</code> line does not return until we are revived. So there is
 
only one event generated and there is no need to check the state of
 
only one event generated and there is no need to check the state of
anything. This is unlike the lid which generates an event for both opening
+
anything.
and closing thus requiring that we check it's state and only act if it's
 
closed.
 
 
 
Unfortunately this will not work for a wide range of machines. For some
 
machines it may be sufficient to turn off the backlight using 'xset dpms
 
force off' (for this to even do anything for me with FC3 I had to use 'su
 
miallen -c "xset -display :0 dpms force off"'). But for whatever reason,
 
when the T30 starts to sleep, it switches to console mode which causes the
 
backlight to come back on. So my solution above is to preemptively switch
 
to console mode and turn off the backlight so that doesn't happen. However
 
now, we cannot use xset because it's an X program that has no bearing on
 
the console. Fortunately I happenstanced across [[radeontool]] which
 
communicates directly with the T30 radeon mobility and therefore works
 
from the console.
 
  
Altogether these two files and [[radeontool]] make sleeping the T30 when you
+
The console switching code in this script is a special solution for
close the lid or hit Fn-F4 work as expected.
+
[[Problem with LCD backlight remaining on during ACPI sleep|a problem where the backlight doesn't switch off]]
 +
on the T30 and some other models. Before going to sleep, these models switch to console mode which causes the
 +
backlight to come back on. So we preemptively switch to console mode and turn off the backlight using [[radeontool]]
 +
before going to sleep.

Revision as of 12:31, 14 May 2005

Basically, acpid just executes scripts residing in /etc/acpi/actions. Which script to launch at which event is configured in several files in /etc/acpi/events. {{{2}}} man acpid holds detailed information on how to configure acpid.

The ibm-acpi packages includes example scripts in the config folder inside the tarball. They are a good starting point to adjust them to your needs. You also might want to have a look at the ACPI section of the Configs page and you can find information about the event strings ibm-acpi generates for certain keys at the Special Keys HOWTO.

Example: go to sleep on lid close

To make the ThinkPad go to sleep when you close the lid, you need to add an event handler for the lid event and an action script that takes care of going to sleep and resuming.

The event script needs to be created within /etc/acpi/events and can have any name you like. In this case we call it lid because it will trigger the lid event. Do # vi /etc/acpi/events/lid and make it look like this:

event=button/lid
action=/etc/acpi/actions/sleep.sh %e

The "event" line is a regular expression specifying the events we're interested in. You can determine what the event strings are from looking at /var/log/acpid after trying to suspend, close the lid, etc. . You can find information about the event strings ibm-acpi generates for certain keys at the Special Keys HOWTO.

The "action" line is the command to be executed when these events are dispatched. In this example we call the sleep.sh script residing in /etc/acpi/actions and pass the event description text using the %e placeholder.

NOTE!
To make your changes take effect after adding or modifying the events files you must do a kill -SIGHUP `pidof acpid`

.

Our example /etc/acpi/actions/sleep.sh script looks as follows:

#!/bin/sh

# if launched through a lid event and lid is open, do nothing
echo "$1" | grep "button/lid" && grep -q open /proc/acpi/button/lid/LID/state && exit 0

# sync filesystem and clock
sync
/sbin/hwclock --systohc

# switch to console
FGCONSOLE=`fgconsole`
chvt 6
/usr/sbin/radeontool light off

# go to sleep
echo -n "mem" > /sys/power/state

# readjust the clock (it might be off a bit after suspend)
/sbin/hwclock --adjust
/sbin/hwclock --hctosys

# turn on the backlight and switch back to X
radeontool light on
chvt $FGCONSOLE

The lid generates an event for both opening and closing thus requiring that we check it's state and only act if it's closed.

Note that the echo -n "mem" > /sys/power/state line does not return until we are revived. So there is only one event generated and there is no need to check the state of anything.

The console switching code in this script is a special solution for a problem where the backlight doesn't switch off on the T30 and some other models. Before going to sleep, these models switch to console mode which causes the backlight to come back on. So we preemptively switch to console mode and turn off the backlight using radeontool before going to sleep.