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

From ArchWiki
Jump to navigation Jump to search
(Fix order. Add translateme.)
Line 24: Line 24:
 
== 配置 ==
 
== 配置 ==
  
{{Pkg|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 {{ic|/etc/acpi/handler.sh}}, which is executed after any ACPI events are detected (as determined by {{ic|/etc/acpi/events/anything}}).
+
{{Pkg|acpid}}预置了许多事件触发行为,比如它定义了当你按下电源按钮时应当发生什么。这些触发行为默认在{{ic|/etc/acpi/handler.sh}}中定义。在{{ic|/etc/acpi/events/anything}})规定:任何侦测到的电源事件都会按照{{ic|/etc/acpi/handler.sh}}中定义的触发行为执行相关动作。
  
The following is a brief example of one such action.  In this case, when the Sleep button is pressed, acpid runs the command {{ic|echo -n mem >/sys/power/state}} which ''should'' place the computer into a sleep (suspend) state:
+
下面是一个定义触发行为的简单例子。在这个例子中,当按下睡眠按钮时acpid运行命令{{ic|echo -n mem >/sys/power/state}},这将会使你的电脑挂起:
  
 
  button/sleep)
 
  button/sleep)
Line 35: Line 35:
 
     ;;
 
     ;;
  
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''.
+
不幸的是计算机上的这些电源事件标识并不统一,比如,在一些计算机上睡眠按钮可能被标识为''SLPB'',而在另一些计算机上为''SBTN''
  
To determine how your buttons or {{Keypress|Fn}} shortcuts are recognized, run the following command from a terminal as root:
+
要想确定各种按钮或{{Keypress|Fn}}快捷键在你的计算机上是如何定义的,以root身份在终端运行:
 
  # tail -f /var/log/messages.log
 
  # tail -f /var/log/messages.log
  
Now press the Power button and/or Sleep button (e.g. {{Keypress|Fn+Esc}}) on your machine.  The result should look something this:
+
现在在你计算机上按下电源按钮或睡眠按钮(比如{{Keypress|Fn+Esc}}),你会得到类似以下的结果:
 
  logger: ACPI action undefined: PBTN
 
  logger: ACPI action undefined: PBTN
 
  logger: ACPI action undefined: SBTN
 
  logger: ACPI action undefined: SBTN
  
If that does not work, run:
+
如果这不起作用的话,运行:
 
  # acpi_listen
 
  # acpi_listen
  
Then press the power button and you will see something like this:
+
然后按下电源按钮,你会看到类似以下输出:
 
  power/button PBTN 00000000 00000b31
 
  power/button PBTN 00000000 00000b31
  
The output of {{ic|acpi_listen}} is sent to {{ic|/etc/acpi/handler.sh}} as $1, $2 , $3 & $4 parameters.
+
{{ic|acpi_listen}}的输出会被作为$1, $2 , $3 & $4参数发送给{{ic|/etc/acpi/handler.sh}}
Example:
+
举例:
 
  $1 power/button
 
  $1 power/button
 
  $2 PBTN
 
  $2 PBTN
Line 57: Line 57:
 
  $4 00000b31
 
  $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 {{ic|/etc/acpi/handler.sh}}. In order for Sleep function to work properly on this machine, we would need to replace ''SLPB)'' with ''SBTN)''.
+
像你看到的那样,在这个例子中睡眠按钮被识别为''SBTN'',而不是''SLPB''(这是{{ic|/etc/acpi/handler.sh}}中默认定义的标识符)。所以,要想让你的睡眠按钮正常工作的话,你需要编辑{{ic|/etc/acpi/handler.sh}}把''SLPB)''替换为''SBTN)''
  
Using this information as a base, you can easily customize the {{ic|/etc/acpi/handler.sh}} file to execute a variety of commands depending on which event is triggered.  See the [[Acpid#Tips & Tricks | Tips & Tricks]] section below for other commonly used commands.
+
参照上面的例子,你应该可以很容易的通过定制{{ic|/etc/acpi/handler.sh}}来根据侦测到的电源时间来执行不同的命令。更多例子可参考下面的[[Acpid#Tips & Tricks | Tips & Tricks]]部分。
  
=== Alternative configuration ===
+
=== 其它配置方案 ===
  
By default, all ACPI events are passed through the {{ic|/etc/acpi/handler.sh}} script. This is due to the ruleset outlined in {{ic|/etc/acpi/events/anything}}:
+
默认所有的电源事件都是交由{{ic|/etc/acpi/handler.sh}}处理的。这是由{{ic|/etc/acpi/events/anything}}规定的:
 
  # Pass all events to our one handler script
 
  # Pass all events to our one handler script
 
  event=.*
 
  event=.*

Revision as of 02:14, 28 September 2012

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.
翻译状态: 本文是英文页面 Acpid翻译,最后翻译时间:2012-09-28,点击这里可以查看翻译后英文页面的改动。

acpid是用于处理电源相关事件的守护进程,它非常灵活且易于扩展。它侦听/proc/acpi/event,当某个事件发生时,执行相关程序来处理该事件。这些事件是由某些动作触发的,比如:

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

acpid可以单独自己运行也可以配合一个更强健的系统(比如pm-utilscpufrequtils)来提供一个更完善的电源管理方案。

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

安装

acpid官方软件仓库中,你可以使用Pacman来安装它。

你可以用root身份编辑/etc/rc.conf把{ic|acpid}}加入DAEMONS array中来让它开机自动运行。

配置

acpid预置了许多事件触发行为,比如它定义了当你按下电源按钮时应当发生什么。这些触发行为默认在/etc/acpi/handler.sh中定义。在/etc/acpi/events/anything)规定:任何侦测到的电源事件都会按照/etc/acpi/handler.sh中定义的触发行为执行相关动作。

下面是一个定义触发行为的简单例子。在这个例子中,当按下睡眠按钮时acpid运行命令echo -n mem >/sys/power/state,这将会使你的电脑挂起:

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

不幸的是计算机上的这些电源事件标识并不统一,比如,在一些计算机上睡眠按钮可能被标识为SLPB,而在另一些计算机上为SBTN

要想确定各种按钮或Template:Keypress快捷键在你的计算机上是如何定义的,以root身份在终端运行:

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

现在在你计算机上按下电源按钮或睡眠按钮(比如Template:Keypress),你会得到类似以下的结果:

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

如果这不起作用的话,运行:

# acpi_listen

然后按下电源按钮,你会看到类似以下输出:

power/button PBTN 00000000 00000b31

acpi_listen的输出会被作为$1, $2 , $3 & $4参数发送给/etc/acpi/handler.sh。 举例:

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

像你看到的那样,在这个例子中睡眠按钮被识别为SBTN,而不是SLPB(这是/etc/acpi/handler.sh中默认定义的标识符)。所以,要想让你的睡眠按钮正常工作的话,你需要编辑/etc/acpi/handler.shSLPB)替换为SBTN)

参照上面的例子,你应该可以很容易的通过定制/etc/acpi/handler.sh来根据侦测到的电源时间来执行不同的命令。更多例子可参考下面的 Tips & Tricks部分。

其它配置方案

默认所有的电源事件都是交由/etc/acpi/handler.sh处理的。这是由/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 the following file:

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

Now create the following file:

/etc/acpi/actions/sleep-button.sh
#!/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 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:

button/lid)
    case $3 in
        close)
            # 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:

button/lid)
    case $3 in
        close)
            #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:

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

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):

ac_adapter)
    case "$2" in
        AC*|AD*)
            case "$4" in
                00000000)
                    echo -n 50 > /sys/class/backlight/acpi_video0/brightness
                    ;;
                00000001)
                    echo -n 100 > /sys/class/backlight/acpi_video0/brightness
                    ;;
            esac

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):

/etc/acpi/actions/volume_up.sh
  #!/bin/bash
  /usr/bin/amixer set Master 5%+
/etc/acpi/actions/volume_down.sh
  #!/bin/bash
  /usr/bin/amixer set Master 5%-

and connect these to new acpi events:

/etc/acpi/events/volume_up
  event=button[ /]volumeup
  action=/etc/acpi/actions/volume_up.sh
/etc/acpi/events/volume_down
  event=button[ /]volumedown
  action=/etc/acpi/actions/volume_down.sh

as well as another event to toggle the mute setting:

/etc/acpi/events/volume_mute
  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  ;;
esac

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  ;;
esac

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  ;;
esac

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:

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
    ;;

See also