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

From ArchWiki
Jump to navigation Jump to search
Line 68: Line 68:
 
  action=/etc/acpi/handler.sh %e
 
  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:
+
作为root,创建以下文件:
 
{{hc|/etc/acpi/events/sleep-button|<nowiki>
 
{{hc|/etc/acpi/events/sleep-button|<nowiki>
 
event=button sleep.*
 
event=button sleep.*
Line 76: Line 76:
 
</nowiki>}}
 
</nowiki>}}
  
Now create the following file:
+
然后建立以下文件:
 
{{hc|/etc/acpi/actions/sleep-button.sh|<nowiki>
 
{{hc|/etc/acpi/actions/sleep-button.sh|<nowiki>
 
#!/bin/sh
 
#!/bin/sh
Line 85: Line 85:
 
</nowiki>}}
 
</nowiki>}}
  
Finally, make the script executable:
+
最后,给脚本加上可执行权限:
 
  # chmod +x /etc/acpi/actions/sleep-button.sh
 
  # 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===
+
===用pm-utils扩展acpid===
  
Although {{Pkg|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, {{ic|pm-suspend}} and {{ic|pm-hibernate}}, both of which can be inserted as events into acpid.  For more information, check the [[pm-utils]] wiki.
+
虽然{{Pkg|acpid}}可以提供基本的挂起功能,你可能需要一个更强健的挂起方案。[[pm-utils]]为挂起和休眠提供了一个非常灵活的框架,能够修复挂起或休眠引起的一些顽固的硬件或驱动方面的问题。pm-utils提供了两个脚本,{{ic|pm-suspend}}{{ic|pm-hibernate}}。这两个脚本都可以在acpid中被调用,更多信息请参考[[pm-utils]]
  
===Example Events===
+
===脚本示例===
  
The following are examples of events that can be used in the {{ic|/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 {{ic|acpi_listen}}.
+
下面给出的例子可以用在你的{{ic|/etc/acpi/handler.sh}}脚本中,只是在使用的时候记得将事件变量更改为{{ic|acpi_listen}}侦测到的那个。
  
To lock the screen with {{ic|xscreensaver}} when closing the laptop lid:
+
实现合上笔记本盖子时使用{{ic|xscreensaver}}锁屏:
  
 
  button/lid)
 
  button/lid)
Line 112: Line 112:
 
             ;;
 
             ;;
  
To suspend the system and lock the screen using {{ic|slimlock}} when the lid is closed:
+
实现合上笔记本盖子时挂起系统并用{{ic|slimlock}}锁屏:
  
 
  button/lid)
 
  button/lid)
Line 122: Line 122:
 
             ;;
 
             ;;
  
Execute {{ic|pm-suspend}} when the sleep button is pressed:
+
实现当按下睡眠按钮时执行{{ic|pm-suspend}}
  
 
  button/sleep)
 
  button/sleep)
Line 131: Line 131:
 
             ;;
 
             ;;
  
To set the laptop screen brightness when plugged in power or not (the numbers might need to be adjusted, see {{ic|/sys/class/backlight/acpi_video0/max_brightness}}):
+
实现插拔外接电源时自动设置屏幕亮度(数值可能需要调整,参考{{ic|/sys/class/backlight/acpi_video0/max_brightness}}):
  
 
  ac_adapter)
 
  ac_adapter)
Line 145: Line 145:
 
             esac
 
             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):
 
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):

Revision as of 02:50, 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

尽管这样配置工作起来没有任何问题,但一些用户可能更喜欢使用各自独立的脚本来定义不同的电源事件。下面举例说明了如何使用不同的事件定义文件和行为定义文件:

作为root,创建以下文件:

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

然后建立以下文件:

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

最后,给脚本加上可执行权限:

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

用这种方法,你可以任意的独立的事件/行为脚本。

小技巧

用pm-utils扩展acpid

虽然acpid可以提供基本的挂起功能,你可能需要一个更强健的挂起方案。pm-utils为挂起和休眠提供了一个非常灵活的框架,能够修复挂起或休眠引起的一些顽固的硬件或驱动方面的问题。pm-utils提供了两个脚本,pm-suspendpm-hibernate。这两个脚本都可以在acpid中被调用,更多信息请参考pm-utils

脚本示例

下面给出的例子可以用在你的/etc/acpi/handler.sh脚本中,只是在使用的时候记得将事件变量更改为acpi_listen侦测到的那个。

实现合上笔记本盖子时使用xscreensaver锁屏:

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

实现合上笔记本盖子时挂起系统并用slimlock锁屏:

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

实现当按下睡眠按钮时执行pm-suspend

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

实现插拔外接电源时自动设置屏幕亮度(数值可能需要调整,参考/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

音量调整

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