Acpid

From ArchWiki

Jump to: navigation, search

Contents

Summary

acpid is a flexible and extensible daemon for delivering ACPI events. These events are triggered by certain actions, such as:

  • Pressing the Power button
  • Pressing the Sleep/Suspend button
  • Closing a notebook lid
  • (Un)Plugging an AC power adapter from a notebook

acpid can be used by itself, or combined with a more robust system such as Pm-utils and Cpufrequtils to provide a more complete power management solution.

Installation

The acpid package is available from the official repositories:

# pacman -S acpid

Edit /etc/rc.conf as root and add acpid to the DAEMONS array, for example:

DAEMONS=(syslog-ng @acpid @alsa @crond ... )

Note: If you already have hal specified in your DAEMONS, there is no need to add acpid. HAL will automatically detect and load the acpid daemon.

Configuration

acpid comes with a number of predefined actions for triggered events, such as what should happen when you press the Power button on your machine. By default, these actions are defined in /etc/acpi/handler.sh, which is executed after any ACPI events are detected (as determined by /etc/acpi/events/anything).

The following is a brief example of one such action. In this case, when the Sleep Button is pressed, acpid runs the command echo -n mem >/sys/power/state which should place the computer into a sleep (suspend) state:

button/sleep)
    case "$2" in
        SLPB) echo -n mem >/sys/power/state ;;
	 *)    logger "ACPI action undefined: $2" ;;
    esac
    ;;

Unfortunately, not every computer labels ACPI events in the same way. For example, the Sleep button may be identified on one machine as SLPB and on another as SBTN.

To determine how your buttons or Fn shortcuts are recognized, run the following command from a terminal as root:

# tail -f /var/log/messages.log

Now press the Power button and/or Sleep button (e.g. Fn+Esc) on your machine. The result should look something this:

Aug 29 17:18:31 dublin logger: ACPI action undefined: PBTN
Aug 29 17:18:33 dublin logger: ACPI action undefined: SBTN

As you might have noticed, the Sleep button in the sample output is actually recognized as SBTN, rather than the SLPB label specified in the default /etc/acpi/handler.sh. In order for Sleep function to work properly on this machine, we would need to replace SLPB) with SBTN).

Using this information as a base, you can easily customize the handler.sh file to execute a variety of commands depending on which event is triggered. See the Tips & Tricks section below for other commonly used commands.

Alternative configuration

By default, all ACPI events are passed through the /etc/acpi/handler.sh script. This is due to the ruleset outlined in /etc/acpi/events/anything:

# Pass all events to our one handler script
event=.*
action=/etc/acpi/handler.sh %e

While this works just fine as it is, some users may prefer to define event rules and actions in their own self-contained scripts. The following is an example of how to use an individual event file and corresponding action script:

As root, create and edit the file: /etc/acpi/events/sleep-button. Add the following:

event=button sleep.*
action=/etc/acpi/actions/sleep-button.sh "%e"

Now create and edit the file: /etc/acpi/actions/sleep-button.sh. Add:

#!/bin/sh
case "$2" in
    SLPB) echo -n mem >/sys/power/state ;;
    *)    logger "ACPI action undefined: $2" ;;
esac

Finally, make the script executable:

# chmod +x /etc/acpi/actions/sleep-button.sh

Using this method, it is easy to create any number of individual event/action scripts.

Tips & Tricks

Extending acpid with pm-utils

Although acpid can provide basic suspend2ram out-of-the-box, a more robust system may be desired. Pm-utils provides a very flexible framework for suspend2ram (suspend) and suspend2disk (hibernate) operations, including common fixes for stubborn hardware and drivers (e.g. fglrx module). pm-utils provides two scripts, pm-suspend and pm-hibernate, both of which can be inserted as events into acpid. For more information, check the Pm-utils wiki.

Sample Events

The following are samples of events that can be dropped into the existing /etc/acpi/handler.sh script. Bolded text indicates modifications or additions to the default script. As noted above, your event labels may differ so some tweaking may be necessary.

Activate Xscreensaver upon lid closure:

button/lid)
    #echo "LID switched!">/dev/tty5
        /usr/bin/xscreensaver-command -lock
        ;;

Execute pm-suspend (from Pm-utils) when the Sleep button is pressed:

button/sleep)
    case "$2" in
        SBTN) 
	     #echo -n mem >/sys/power/state
            /usr/sbin/pm-suspend
            ;;

Laptop Monitor Power Off

From "AM088" on the gentoo wiki comes this little gem. Add this to the bottom of your /etc/acpi/actions/lm_lid.sh file (requires laptop-mode-tools package). This will do nothing but turn your monitor off when you close the lid:

 
if grep -q open /proc/acpi/button/lid/LID/state
then
    XAUTHORITY=/home/YOURUSERNAME/.Xauthority /usr/bin/xset -display :0.0 dpms force on
else
    XAUTHORITY=/home/YOURUSERNAME/.Xauthority /usr/bin/xset -display :0.0 dpms force off
fi

From shrimants: With the latest versions of xorg, dpms force off only blanks the display, leaving the backlight turned on. This can be fixed using vbetool from the 'extra' repository. change the XAUTHORITY lines to:

 
if grep -q open /proc/acpi/button/lid/LID/state
then
    vbetool dpms on
else
    vbetool dpms off
fi

Getting user name of the current display

You can use the function getuser to acquire the user of the current display:

getuser ()
    {
     export DISPLAY=`echo $DISPLAY | cut -c -2`
     user=`who | grep " $DISPLAY" | awk '{print $1}' | tail -n1`
     export XAUTHORITY=/home/$user/.Xauthority
     eval $1=$user
}

This function can be used for example, when you press the power button and want to shutdown KDE properly:

button/power)
    case "$2" in
        PBTN)
            getuser "$user"
            echo $user > /dev/tty5
            su $user -c "dcop ksmserver ksmserver logout 0 2 0"
            ;;
          *) logger "ACPI action undefined $2" ;;
    esac
    ;;

More Resources

Personal tools