Difference between revisions of "Acpid (简体中文)"

From ArchWiki
Jump to navigation Jump to search
m (Fix category.)
Line 4: Line 4:
[http://acpid.sourceforge.net/ acpid] is a flexible and extensible daemon for delivering ACPI events. It listens on {{ic|/proc/acpi/event}} and when an event occurs, executes programs to handle the event. These events are triggered by certain actions, such as:
[http://acpid.sourceforge.net/ acpid]是用于处理电源相关事件的守护进程,它非常灵活且易于扩展。它侦听{{ic|/proc/acpi/event}},当某个事件发生时,执行相关程序来处理该事件。这些事件是由某些动作触发的,比如:
* 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 [[CPU Frequency Scaling|cpufrequtils]] to provide a more complete power management solution.
acpid可以单独自己运行也可以配合一个更强健的系统(比如[[pm-utils]][[CPU Frequency Scaling|cpufrequtils]])来提供一个更完善的电源管理方案。
{{Warning|Please note that [[Desktop Environment|desktop environments]] such as [[GNOME]] tend to have their own means of handling ACPI events, independent of acpid. Running both systems (acpid and your desktop environment's ACPI handling system) at the same time may lead to unexpected behaviour, such as suspending and shutting down at the same time, when the power button is pressed or suspending two times in a row after one sleep button press. You should be aware of this and disable duplicate event handlers in one or the other configuration system, when using acpid and such desktop environments simultaneously.}}
{{警告|请注意[[Desktop Environment|desktop environments]]比如[[GNOME]]倾向于使用自己的电源事件管理方法(独立于acpid)。同时运行这两套系统(acpid和桌面环境自带电源管理方法)可能产生意想不到的结果,比如,当按下电源键时电脑同时执行挂起和关机;或者当按下睡眠按钮时电脑执行了两次挂起操作。所以,在同时使用桌面环境和acpid守护进程时,你应当在配置文件中禁用其中的一个电源管理方法以避免重复电脑重复执行命令。}}
{{Box GREEN|Note for KDE SC and GNOME users:|Since the script provided by acpid, '''/etc/acpi/handler.sh''', will override your desktop environment's power button functionality, you most likely want to disable acpid's power off routine to avoid shutting down the system suddenly when you press the power button (which can be very frustrating and lead to data loss since it will shutdown your system without asking to saving any current work). To power off with the shutdown menu of your preferred DE, just edit the '''/etc/acpi/handler.sh''' script and comment out with a hash '''#''' line fifteen (15), ''poweroff''.}}
{{Box GREEN|KDE SC and GNOME用户请注意:|由于acpid提供的脚本'''/etc/acpi/handler.sh'''会覆盖掉桌面环境的电源按钮的功能, 为了避免当按下电源按钮时你的电脑突然关机(这是很烦人的而且可能会造成资料损失因为它在关机前不会等待你先保存你目前的工作),你最好禁用掉acpid对电源按钮的管理。要禁用的话,只需编辑'''/etc/acpi/handler.sh''',用"#"注释掉第15行,''poweroff''.}}
== Installation ==
== 安装 ==
[[pacman|Install]] the {{Pkg|acpid}} package, available in the [[Official Repositories|official repositories]].
[[pacman|Install]] the {{Pkg|acpid}} package, available in the [[Official Repositories|official repositories]].

Revision as of 14:18, 27 September 2012


  • 按下电源按钮
  • 按下睡眠/挂起按钮
  • 合上笔记本盖子
  • (拔下)插上笔记本外接电源


警告: 请注意desktop environments比如GNOME倾向于使用自己的电源事件管理方法(独立于acpid)。同时运行这两套系统(acpid和桌面环境自带电源管理方法)可能产生意想不到的结果,比如,当按下电源键时电脑同时执行挂起和关机;或者当按下睡眠按钮时电脑执行了两次挂起操作。所以,在同时使用桌面环境和acpid守护进程时,你应当在配置文件中禁用其中的一个电源管理方法以避免重复电脑重复执行命令。
KDE SC and GNOME用户请注意: 由于acpid提供的脚本/etc/acpi/handler.sh会覆盖掉桌面环境的电源按钮的功能, 为了避免当按下电源按钮时你的电脑突然关机(这是很烦人的而且可能会造成资料损失因为它在关机前不会等待你先保存你目前的工作),你最好禁用掉acpid对电源按钮的管理。要禁用的话,只需编辑/etc/acpi/handler.sh,用"#"注释掉第15行,poweroff.


Install the acpid package, available in the official repositories.

Edit /etc/rc.conf as root and add acpid to the DAEMONS array to have it start on boot.


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:

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

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 Template:Keypress 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. Template:Keypress) on your machine. The result should look something this:

logger: ACPI action undefined: PBTN
logger: ACPI action undefined: SBTN

If that does not work, run:

# acpi_listen

Then press the power button and you will see something like this:

power/button PBTN 00000000 00000b31

The output of acpi_listen is sent to /etc/acpi/handler.sh as $1, $2 , $3 & $4 parameters. Example:

$1 power/button
$3 00000000
$4 00000b31

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 /etc/acpi/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
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 the following file:

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

Now create the following file:

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

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 and 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.

Example Events

The following are examples of events that can be used in the /etc/acpi/handler.sh script. These examples should be modified so that they apply your specific environment e.g. changing the event variable names interpreted by acpi_listen.

To lock the screen with xscreensaver when closing the laptop lid:

    case $3 in
            # The lock command need to be run as the user who owns the xscreensaver process and not as root.
            # See: man xscreensaver-command. $xs will have the value of the user owning the process, if any.

            xs=$(ps -C xscreensaver -o user=)
            if test $xs; then su $xs -c "xscreensaver-command -lock"; fi

To suspend the system and lock the screen using slimlock when the lid is closed:

    case $3 in
            #echo "LID switched!">/dev/tty5
	     /usr/sbin/pm-suspend &
	     DISPLAY=:0.0 su -c - username /usr/bin/slimlock

Execute pm-suspend when the sleep button is pressed:

    case "$2" in
	     #echo -n mem >/sys/power/state

To set the laptop screen brightness when plugged in power or not (the numbers might need to be adjusted, see /sys/class/backlight/acpi_video0/max_brightness):

    case "$2" in
            case "$4" in
                    echo -n 50 > /sys/class/backlight/acpi_video0/brightness
                    echo -n 100 > /sys/class/backlight/acpi_video0/brightness

Enabling volume control

Find out the acpi identity of the volume buttons (see above) and susbtitute it for the acpi events in the files below. We create a pair of scripts to control the volume (assuming an ALSA sound card):

  /usr/bin/amixer set Master 5%+
  /usr/bin/amixer set Master 5%-

and connect these to new acpi events:

  event=button[ /]volumeup
  event=button[ /]volumedown

as well as another event to toggle the mute setting:

  event=button[ /]volumemute
  action=/usr/bin/amixer set Master toggle

Laptop Monitor Power Off

Adapted from the Gentoo Wiki comes this little gem. Add this to the bottom of /etc/acpi/actions/lm_lid.sh or to the button/lid section /etc/acpi/handler.sh. This will turn off the LCD back-light when the lid is closed, and restart when the lid is opened.

case $(cat /proc/acpi/button/lid/LID0/state | awk '{print $2}') in
    closed) XAUTHORITY=$(ps -C xinit -f --no-header | sed -n 's/.*-auth //; s/ -[^ ].*//; p') xset -display :0 dpms force off ;;
    open)   XAUTHORITY=$(ps -C xinit -f --no-header | sed -n 's/.*-auth //; s/ -[^ ].*//; p') xset -display :0 dpms force on  ;;

If you would like to increase/decrease brightness or anything dependent on X, you should specify the X display as well as the MIT magic cookie file (via XAUTHORITY). The last is a security credential providing read and write access to the X server, display, and any input devices.

Here is another script not using XAUTHORITY but sudo:

case $(cat /proc/acpi/button/lid/LID0/state | awk '{print $2}') in
    closed) sudo -u `ps -o ruser= -C xinit` xset -display :0 dpms force off ;;
    open)   sudo -u `ps -o ruser= -C xinit` xset -display :0 dpms force on  ;;

With certain combinations of Xorg and stubborn hardware, xset dpms force off only blanks the display leaving the backlight turned on. This can be fixed using vbetool from the official repositories. Change the LCD section to:

case $(cat /proc/acpi/button/lid/LID0/state | awk '{print $2}') in
    closed) vbetool dpms off ;;
    open)   vbetool dpms on  ;;

If the monitor appears to shut off only briefly before being re-powered, very possibly the power management shipped with xscreensaver conflicts with any manual dpms settings.

Getting user name of the current display

You can use the function getuser to discover 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:

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

See also