Difference between revisions of "How to configure acpid"

From ThinkWiki
Jump to: navigation, search
 
m (Make the Microphone Mute button work)
 
(42 intermediate revisions by 24 users not shown)
Line 1: Line 1:
With the introduction of ACPI there are more things under programmatic
+
__TOC__
control. Sleeping is one of those things. To sleep the T30 manually is
+
==Introduction==
simple. You just do:
+
Basically, [[acpid]] just executes scripts residing in {{path|/etc/acpi/actions}}. Which script to launch at which event is configured in several files in {{path|/etc/acpi/events}}. All actions are documented in {{path|/var/log/acpid}} in older versions of acpid and to {{path|/var/log/messages}}, and {{path|/var/log/syslog}} via the syslog interface in newer versions >~1.0.6.
  
echo -n "mem" /sys/power/state
+
{{NOTE|Modern distributions no longer use acpid for power manager, and instead rely on HAL and pm-utils (but you can still use acpid for other hotkeys)}}
  
However there are two problems with this. It doesn't happen automatically
+
{{NOTE|Unfortunately, debian and ubuntu had to break logging in acpid, *you will see nothing in syslog or /var/log/acpid* }}.  
when you close the lid and it does not turn off the LCD backlight. The
+
See [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=447200 Debian Bug 447200] for why logging was broken. The "fix" is to run {{path|bash -c "acpid -d &"}}
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
+
==Sources of Information==
  event=button/(sleep|lid)
+
*{{cmduser|man acpid}} holds detailed information on how to configure acpid.
  action=/etc/acpi/actions/sleep %e
+
*The [[ibm-acpi]] package includes example scripts in the {{path|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 [[Configs#ACPI | ACPI section of the Configs page]] or the [[:Category:Scripts|Scripts]] repository.
 +
*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]].
 +
*You can use acpi_listen to show acpi events
 +
 
 +
==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.
 +
 
 +
===Event Script===
 +
The event script needs to be created within {{path|/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 {{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
+
{{path|/var/log/acpid}} 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 {{path|sleep.sh}} script residing in {{path|/etc/acpi/actions}} 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:
+
For the script you can use the <tt>hibernate</tt> script of [http://www.tuxonice.net/ Tux On Ice] (it is independent of the [[ www.suspend2.net | suspend-to-disk]] functionality), or any of many examples available on the web, such as the one below.
 +
 
 +
{{NOTE|To make your changes take effect after adding or modifying the events files you must do a <tt>kill -SIGHUP `pidof acpid`</tt>}}
 +
 
 +
===Action Script===
 +
Our example {{path|/etc/acpi/actions/sleep.sh}} 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
 +
 +
# remove USB 1.1 driver
 +
rmmod uhci_hcd
 +
 
 +
# sync filesystem and clock
 +
sync
 +
/sbin/hwclock --systohc
 +
 +
# switch to console
 +
FGCONSOLE=`fgconsole`
 +
chvt 6
 +
/usr/sbin/radeontool light off
 +
 +
# go to sleep
 +
sleep 5 && echo -n "mem" > /sys/power/state
 +
 +
# readjust the clock (it might be off a bit after suspend)
 +
/sbin/hwclock --adjust
 +
/sbin/hwclock --hctosys
 +
 +
# reload USB 1.1 driver
 +
modprobe uhci_hcd
 +
 +
# turn on the backlight and switch back to X
 +
radeontool light on
 +
chvt $FGCONSOLE
 +
 
 +
{{NOTE|This parts of this script are now part of some distributions, like Debian Etch and you probably don't need to add this script. Look into /etc/acpi/lid.sh, for example.}}
 +
 
 +
====Explanations====
 +
*The lid generates an event for both opening and closing thus requiring that we check its state and only act if it's closed.
 +
*There have been problems encountered with the USB devices not working properly after a resume from suspend. To circumvent those we remove the USB driver prior to suspend and reload it afterwards.
 +
*Note that the {{cmdroot|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 [[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.
 +
 
 +
==Screen blanking script==
 +
 
 +
You might want to be able to switch of the screen using Fn-f3.
 +
This is not completely straightforward.
 +
You will first  need to give permission to the script to muck around with your screen
 +
 
 +
xhost +local:root
 +
 
 +
/etc/acpi/events/sleepbtn
 +
contains
 +
event=(button[ /]sleep|ibm/hotkey HKEY 00000080 00001004)
 +
action=/etc/acpi/sleepbtn.sh
 +
 
 +
And /etc/acpi/sleepbtn.sh is
 +
 
 +
#!/bin/bash
 +
 +
# simple script to turn the display on or off      #
 +
# By Mahram Z.Foadi                                                          #
 +
# Oct 22 2005                                                                #
 +
# thank you linuxquestions.org                                                #
 +
#                                                                            #
 +
# the following lines must be present in your "/etc/acpi/events/" files. Some #
 +
# systems already have a file called sample.conf in the mentioned directory  #
 +
# rename it to something more meaningful (i.e. acpid.conf) and these lines to #
 +
# the end of it  (you don't HAVE to rename it, you can even create a new file #
 +
# and call it /etc/acpi/events/lid.conf with the 2 lines in it. If you intend #
 +
# to put this file anywhere other than /sbin/lidevent, make sure you make the #
 +
# proper changes in the "action" line below. For example if you are going to  #
 +
# save this script as /usr/bin/mylid.sh then the action line should be:      #
 +
# action=/usr/bin/mylid.sh                                                    #
 +
# be sure both files are executable, writable, and owned by root ONLY because #
 +
# the acpid daemon is most likely running as root in your system              #
 +
#                                                                            #
 +
#    ---- insert the two lines below in /etc/acpi/events/acpid.conf ----    #
 +
# event=button/lid.*                                                          #
 +
# action=/sbin/lidevent                                                      #
 +
#-----------------------------------------------------------------------------#
 +
 +
# default display on current host
 +
DISPLAY=:0.0
 +
 +
# find out if DPMS is enabled
 +
STATUS=`xset -display $DISPLAY -q | grep -e 'DPMS is'`
 +
 +
# enable DPMS if disabled
 +
if [ "$STATUS" == "  DPMS is Disabled" ]
 +
then
 +
echo "Enabling DPMS ..."
 +
xset -display $DISPLAY +dpms
 +
fi
 +
 +
  # find out if monitor is on
 +
STATUS=`xset -display $DISPLAY -q | grep 'Monitor'`
 +
 +
if [ "$STATUS" == "  Monitor is On" ]
 
  then
 
  then
    chvt 6;
+
echo "[`date`] Turning display OFF"
    /usr/sbin/radeontool light off;
+
xset -display $DISPLAY dpms force off
    echo -n "mem" > /sys/power/state;
+
else
    chvt 7;
+
echo "[`date`] Turning display ON" # shows up in log
 +
xset -display $DISPLAY dpms force on # turn monitor on
 +
  xset -display $DISPLAY s activate # un-blank monitor
 
  fi
 
  fi
 +
 +
#clean up
 +
unset STATUS
 +
 +
# comment this line out if you're manually running this script from a shell (put a # in front of it)
 +
unset DISPLAY
 +
 +
exit 0
 +
 +
An alternative is to call radeontool (if you have a radeon videocard only of course)
 +
 +
 +
==Switch external display==
 +
Again make sure that
 +
xhost +local:root 
 +
 +
With the newer versions of X, display switching can be done with xrandr.
 +
Create the script /etc/acpi/switchdisplay.sh
 +
#!/bin/csh
 +
/usr/bin/xrandr --output VGA-0 --auto
 +
 +
And /etc/acpi/events/displaybtn
 +
event=ibm/hotkey HKEY 00000080 00001007
 +
action=/etc/acpi/switchdisplay.sh
 +
 +
==Make the Microphone Mute button work==
 +
There is ATM no special key with this functionality defined in X or HAL.
 +
The easiest way I have found is to do it via amixer.
 +
 +
Create /etc/acpi/actions/micmute.sh
 +
  /usr/bin/amixer -q sset Capture toggle
 +
 +
And /etc/acpi/events/micmute
 +
event=ibm/hotkey HKEY 00000080 0000101b
 +
action=/etc/acpi/actions/micmute.sh
 +
 +
Or use a similar command in whatever script you use to handle ACPI HKEY events.
  
Translated this reads -- if the event is a sleep event (meaning Fn-F4 was
+
==Troubleshooting==
triggered) or the state of the lid is closed, then switch to a virtual
+
*If something doesn't work, your first action should be a {{cmdroot|tail /var/log/acpid}}. It will tell you a lot about what is going on. If it has "Permission denied" errors, check the permissions of your {{path|/etc/acpi/actions}} scripts (especially make sure that the executable bit is set). Also check the permissions for other involved files like i.e. device nodes.
console, turn off the backlight, and goto sleep. When we wake up, switch
+
*For further problems look at the [[Problems with ACPI suspend-to-ram|Problems with ACPI suspend-to-ram page]].
back to X.
 
  
Note that the echo 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. This is unlike the lid which generates an event for both opening
 
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
+
[[Category:770X]] [[Category:770Z]] [[Category:A20m]] [[Category:A20p]] [[Category:A20m]] [[Category:A20p]] [[Category:A21e]] [[Category:A21m]] [[Category:A21p]] [[Category:A22e]] [[Category:A22m]] [[Category:A22p]] [[Category:G40]] [[Category:G41]] [[Category:R30]] [[Category:R31]] [[Category:R32]] [[Category:R40]] [[Category:R40e]] [[Category:R50]] [[Category:R50p]] [[Category:R51]] [[Category:R52]] [[Category:T20]] [[Category:T21]] [[Category:T22]] [[Category:T23]] [[Category:T30]] [[Category:T40]] [[Category:T40p]] [[Category:T41]] [[Category:T41p]] [[Category:T42]] [[Category:T42p]] [[Category:T43]] [[Category:T43p]] [[Category:X20]] [[Category:X21]] [[Category:X22]] [[Category:X23]] [[Category:X24]] [[Category:X30]] [[Category:X31]] [[Category:X32]] [[Category:X40]] [[Category:X41]] [[Category:X41 Tablet]]  [[Category:Z60t]] [[Category:Z60m]] [[Category:TransNote]]
close the lid or hit Fn-F4 work as expected.
 

Latest revision as of 15:49, 19 September 2010

Introduction

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. All actions are documented in /var/log/acpid in older versions of acpid and to /var/log/messages, and /var/log/syslog via the syslog interface in newer versions >~1.0.6.

NOTE!
Modern distributions no longer use acpid for power manager, and instead rely on HAL and pm-utils (but you can still use acpid for other hotkeys)
NOTE!
Unfortunately, debian and ubuntu had to break logging in acpid, *you will see nothing in syslog or /var/log/acpid*

.

See Debian Bug 447200 for why logging was broken. The "fix" is to run bash -c "acpid -d &"

Sources of Information

  • $ man acpid holds detailed information on how to configure acpid.
  • The ibm-acpi package 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 or the Scripts repository.
  • And you can find information about the event strings ibm-acpi generates for certain keys at the Special Keys HOWTO.
  • You can use acpi_listen to show acpi events

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.

Event Script

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.

For the script you can use the hibernate script of Tux On Ice (it is independent of the suspend-to-disk functionality), or any of many examples available on the web, such as the one below.

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

Action Script

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

# remove USB 1.1 driver
rmmod uhci_hcd
 
# sync filesystem and clock
sync
/sbin/hwclock --systohc

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

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

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

# reload USB 1.1 driver
modprobe uhci_hcd

# turn on the backlight and switch back to X
radeontool light on
chvt $FGCONSOLE
NOTE!
This parts of this script are now part of some distributions, like Debian Etch and you probably don't need to add this script. Look into /etc/acpi/lid.sh, for example.

Explanations

  • The lid generates an event for both opening and closing thus requiring that we check its state and only act if it's closed.
  • There have been problems encountered with the USB devices not working properly after a resume from suspend. To circumvent those we remove the USB driver prior to suspend and reload it afterwards.
  • 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.

Screen blanking script

You might want to be able to switch of the screen using Fn-f3. This is not completely straightforward. You will first need to give permission to the script to muck around with your screen

xhost +local:root 

/etc/acpi/events/sleepbtn contains

event=(button[ /]sleep|ibm/hotkey HKEY 00000080 00001004)
action=/etc/acpi/sleepbtn.sh

And /etc/acpi/sleepbtn.sh is

#!/bin/bash

# simple script to turn the display on or off      #
# By Mahram Z.Foadi                                                           #
# Oct 22 2005                                                                 # 
# thank you linuxquestions.org                                                #
#                                                                             # 
# the following lines must be present in your "/etc/acpi/events/" files. Some #
# systems already have a file called sample.conf in the mentioned directory   #
# rename it to something more meaningful (i.e. acpid.conf) and these lines to #
# the end of it  (you don't HAVE to rename it, you can even create a new file #
# and call it /etc/acpi/events/lid.conf with the 2 lines in it. If you intend #
# to put this file anywhere other than /sbin/lidevent, make sure you make the #
# proper changes in the "action" line below. For example if you are going to  #
# save this script as /usr/bin/mylid.sh then the action line should be:       #
# action=/usr/bin/mylid.sh                                                    #
# be sure both files are executable, writable, and owned by root ONLY because #
# the acpid daemon is most likely running as root in your system              #
#                                                                             #
#     ---- insert the two lines below in /etc/acpi/events/acpid.conf ----     #
# event=button/lid.*                                                          #
# action=/sbin/lidevent                                                       #
#-----------------------------------------------------------------------------#

# default display on current host
DISPLAY=:0.0

# find out if DPMS is enabled
STATUS=`xset -display $DISPLAY -q | grep -e 'DPMS is'`

# enable DPMS if disabled
if [ "$STATUS" == "  DPMS is Disabled" ]
then
	echo "Enabling DPMS ..."
	xset -display $DISPLAY +dpms
fi

 # find out if monitor is on
STATUS=`xset -display $DISPLAY -q | grep 'Monitor'`

if [ "$STATUS" == "  Monitor is On" ]
then
	echo "[`date`] Turning display OFF"
	xset -display $DISPLAY dpms force off
else
	echo "[`date`] Turning display ON"		# shows up in log
	xset -display $DISPLAY dpms force on		# turn monitor on
 	xset -display $DISPLAY s activate		# un-blank monitor
fi

#clean up
unset STATUS

# comment this line out if you're manually running this script from a shell (put a # in front of it)
unset DISPLAY

exit 0

An alternative is to call radeontool (if you have a radeon videocard only of course)


Switch external display

Again make sure that

xhost +local:root  

With the newer versions of X, display switching can be done with xrandr. Create the script /etc/acpi/switchdisplay.sh

#!/bin/csh
/usr/bin/xrandr --output VGA-0 --auto

And /etc/acpi/events/displaybtn

event=ibm/hotkey HKEY 00000080 00001007
action=/etc/acpi/switchdisplay.sh

Make the Microphone Mute button work

There is ATM no special key with this functionality defined in X or HAL. The easiest way I have found is to do it via amixer.

Create /etc/acpi/actions/micmute.sh

 /usr/bin/amixer -q sset Capture toggle

And /etc/acpi/events/micmute

event=ibm/hotkey HKEY 00000080 0000101b
action=/etc/acpi/actions/micmute.sh

Or use a similar command in whatever script you use to handle ACPI HKEY events.

Troubleshooting

  • If something doesn't work, your first action should be a # tail /var/log/acpid. It will tell you a lot about what is going on. If it has "Permission denied" errors, check the permissions of your /etc/acpi/actions scripts (especially make sure that the executable bit is set). Also check the permissions for other involved files like i.e. device nodes.
  • For further problems look at the Problems with ACPI suspend-to-ram page.