Difference between revisions of "Acpid"

From ArchWiki
Jump to navigation Jump to search
(function getuser added)
m (Tips and tricks: Fix typo)
 
(218 intermediate revisions by 72 users not shown)
Line 1: Line 1:
[[Category:Power management (English)]]
+
{{Lowercase title}}
[[Category:Daemons and system services (English)]]
+
[[Category:Power management]]
[[Category:HOWTOs (English)]]
+
[[Category:Daemons]]
 
+
[[es:Acpid]]
= Summary =
+
[[it:Acpid]]
ACPID is a flexible and extensible daemon for delivering ACPI events. These events are triggered by certain actions, such as:
+
[[ja:Acpid]]
* Pressing the Power button
+
[[ru:Acpid]]
* Pressing the Sleep/Suspend button
+
[[zh-hans:Acpid]]
 +
{{Related articles start}}
 +
{{Related|ACPI modules}}
 +
{{Related|DSDT}}
 +
{{Related articles end}}
 +
[http://acpid2.sourceforge.net/ acpid2] is a flexible and extensible daemon for delivering [[ACPI modules|ACPI events]]. When an event occurs, it executes programs to handle the event. These events are triggered by certain actions, such as:
 +
* Pressing special keys, including the Power/Sleep/Suspend button
 
* Closing a notebook lid
 
* Closing a notebook lid
 
* (Un)Plugging an AC power adapter from a notebook
 
* (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.
+
* (Un)Plugging phone jack etc.
  
= Installation =
+
{{Note|[[Desktop environments]], such as [[GNOME]], [[Power management#ACPI events|systemd]] login manager and some [[Extra keyboard keys|extra key handling]] daemons may implement own event handling schemes, independent of acpid. Running more than one system at the same time may lead to unexpected behaviour, such as suspending two times in a row after one sleep button press. You should be aware of this and only activate desirable handlers.}}
The acpid package is available from the [http://www.archlinux.org/packages/search/?q=acpid Current] repository:
 
# pacman -S acpid
 
Edit <tt>/etc/rc.conf</tt> as root and add '''acpid''' to the DAEMONS array, for example:
 
DAEMONS=(syslog-ng '''''acpid''''' cpufreq dhcdbd networkmanager @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 =
+
== Installation ==
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''.  The following is a shortened example 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 state.
 
button/sleep)
 
case "$2" in
 
SLPB) echo -n mem >/sys/power/state ;;
 
*)    logger "ACPI action undefined: $2" ;;
 
esac
 
;;
 
Unfortunately, not every computer labels the 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:
+
[[Install]] the {{Pkg|acpid}} package. Then [[start]] and/or [[enable]] {{ic|acpid.service}}.
# 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 more sample commands.
+
== Configuration ==
  
= Tips & Tricks =
+
{{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}}).
== Sample Events ==
 
The following are samples of events that can be dropped into the existing <tt>/etc/acpi/handler.sh</tt> 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:
+
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:
button/lid)
 
        #echo "LID switched!">/dev/tty5
 
        '''''/usr/bin/xscreensaver-command -lock'''''
 
        ;;
 
  
Execute <tt>pm-suspend</tt> (from [[Pm-utils]]) when the Sleep button is pressed:
 
 
  button/sleep)
 
  button/sleep)
case "$2" in
+
    case "$2" in
SBTN)  
+
        SLPB) echo -n mem >/sys/power/state ;;
'''''#echo -n mem >/sys/power/state'''''
+
*)    logger "ACPI action undefined: $2" ;;
'''''/usr/sbin/pm-suspend'''''
+
    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 {{ic|Fn}} shortcuts are recognized, run the following command:
 +
 
 +
# journalctl -f
 +
 
 +
Now press the Power button and/or Sleep button (e.g. {{ic|Fn+Esc}}) 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
 +
 
 +
or with {{Pkg|openbsd-netcat}}:
 +
 
 +
$ netcat -U /var/run/acpid.socket
 +
 
 +
Then press the power button and you will see something like this:
 +
 
 +
button/power PBTN 00000000 00000b31
 +
 
 +
The output of {{ic|acpi_listen}} is sent to {{ic|/etc/acpi/handler.sh}} as $1, $2 , $3 & $4 parameters.
 +
Example:
 +
 
 +
$1 button/power
 +
$2 PBTN
 +
$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 {{ic|/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 {{ic|/etc/acpi/handler.sh}} file to execute a variety of commands depending on which event is triggered.  See the [[#Tips and tricks]] section below for other commonly used commands.
 +
 
 +
=== 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}}:
 +
 
 +
# 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:
 +
 
 +
{{hc|/etc/acpi/events/sleep-button|2=
 +
event=button sleep.*
 +
action=/etc/acpi/actions/sleep-button.sh %e
 +
}}
 +
 
 +
Now create the following file:
 +
 
 +
{{hc|/etc/acpi/actions/sleep-button.sh|2=
 +
#!/bin/sh
 +
case "$3" in
 +
    SLPB) echo -n mem >/sys/power/state ;;
 +
    *)    logger "ACPI action undefined: $3" ;;
 +
esac
 +
}}
 +
 
 +
Make the script executable:
 +
 +
# chmod +x /etc/acpi/actions/sleep-button.sh
 +
 
 +
Finally, [[reload]] the {{ic|acpid.service}} to get acpid to recognize the changes to these files.
 +
 
 +
Using this method, it is easy to create any number of individual event/action scripts.
 +
 
 +
== Tips and tricks ==
 +
 
 +
{{Note|Some of the actions described here, such as Wi-Fi toggle and backlight control, may already be managed directly by driver. You should consult documentation of corresponding kernel modules, when this is the case.}}
 +
 
 +
=== 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}}.
 +
 
 +
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}}):
 +
 
 +
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 substitute it for the acpi events in the files below.
 +
 
 +
{{hc|1=/etc/acpi/events/vol-d|2=
 +
event=button/volumedown
 +
action=amixer set Master 5-
 +
}}
 +
 
 +
{{hc|1=/etc/acpi/events/vol-m|2=
 +
event=button/mute
 +
action=amixer set Master toggle
 +
}}
 +
 
 +
{{hc|1=/etc/acpi/events/vol-u|2=
 +
event=button/volumeup
 +
action=amixer set Master 5+
 +
}}
 +
 
 +
{{Note|1=These commands may not work as expected with PulseAudio. [https://lists.freedesktop.org/archives/pulseaudio-discuss/2015-December/025062.html] For full functionality, run commands as the current user while specifying the {{ic|XDG_RUNTIME_DIR}} [[environment variable]], for example with {{ic|1=sudo -u ''user'' XDG_RUNTIME_DIR=/run/user/''1000'' pactl}}.}}
 +
 
 +
{{Tip|Disable or bind the volume buttons in Xorg to prevent conflicts with other applications. See [[Xmodmap]] for details.}}
 +
 
 +
See also [http://web.archive.org/web/20150711044207/http://blog.lastlog.de/posts/fixing_volume_change_in_linux].
 +
 
 +
=== Enabling backlight control ===
 +
 
 +
Similar to volume control, acpid also enables you to control screen backlight. To achieve this you write some handler, like this:
 +
 
 +
{{hc|/etc/acpi/handlers/bl|2=
 +
#!/bin/sh
 +
bl_dev=/sys/class/backlight/acpi_video0
 +
step=1
 +
 
 +
case $1 in
 +
  -) echo $(($(< $bl_dev/brightness) - $step)) >$bl_dev/brightness;;
 +
  +) echo $(($(< $bl_dev/brightness) + $step)) >$bl_dev/brightness;;
 +
esac
 +
}}
 +
 
 +
and again, connect keys to ACPI events:
 +
 
 +
{{hc|/etc/acpi/events/bl_d|2=
 +
event=video/brightnessdown
 +
action=/etc/acpi/handlers/bl -
 +
}}
 +
{{hc|/etc/acpi/events/bl_u|2=
 +
event=video/brightnessup
 +
action=/etc/acpi/handlers/bl +
 +
}}
 +
 
 +
=== Enabling Wi-Fi toggle ===
 +
 
 +
You can also create a simple wireless-power switch by pressing the WLAN button. Example of event:
 +
 
 +
{{hc|/etc/acpi/events/wlan|2=
 +
event=button/wlan
 +
action=/etc/acpi/handlers/wlan
 +
}}
 +
 
 +
and its handler:
 +
 
 +
{{hc|/etc/acpi/handlers/wlan|2=
 +
#!/bin/sh
 +
rf=/sys/class/rfkill/rfkill0
 +
 
 +
case $(< $rf/state) in
 +
  0) echo 1 >$rf/state;;
 +
  1) echo 0 >$rf/state;;
 +
esac
 +
}}
 +
 
 +
=== Getting user name of the current display ===
 +
 
 +
To run commands dependening on [[Xorg]], defining the X display as well as the MIT magic cookie file (via XAUTHORITY) is required. Latter is a security credential providing read and write access to the X server, display, and any input devices (see {{man|1|xauth}}).
 +
 
 +
See [https://gist.githubusercontent.com/AladW/de1c5676d93d05a5a0e1/raw/16e010ecda9f2328e1e22d4e02ac814ed27717b4/gistfile1.txt] for an example function when using [[xinitrc]].
 +
 
 +
{{Note|
 +
* If the LCD backlight is not turned off when the lid is closed, you may do so manually by running {{ic|getXuser xset dpms force off}} and {{ic|getXuser xset dpms force on}} respectively on lid close and lid open events. Should the display be blanked, but the backlight left on, instead use {{Pkg|vbetool}} with {{ic|vbetool dpms off}} and {{ic|vbetool dpms on}}. See also [[XScreenSaver#Configuration]].
 +
* When using ''who'' or ''w'', make sure {{ic|/run/utmp}} is created at boot-time. See {{man|5|utmp}} for details.
 +
}}
 +
 
 +
==== Connect to acpid socket ====
 +
 
 +
In addition to rule files, acpid accepts connections on a UNIX domain socket, by default {{ic|/var/run/acpid.socket}}. User applications may connect to this socket.
  
== Extending acpid with pm-utils ==
+
{{bc|
Although <tt>acpid</tt> 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 fixes to stubborn hardware and drivers (e.g. fglrx video).  pm-utils primarily consists of 2 scripts, <tt>pm-suspend</tt> and <tt>pm-hibernate</tt>, both of which can be inserted as events into acpid.  For more information, check the [[Pm-utils]] wiki.
+
#!/bin/bash
 +
coproc acpi_listen
 +
trap 'kill $COPROC_PID' EXIT
  
== Getting user name at the current display ==
+
while read -u "${COPROC[0]}" -a event; do
You can use function getuser
+
    ''handler.sh'' "${event[@]}"
 +
done
 +
}}
  
getuser ()
+
Where ''handler.sh'' can be a script similar to {{ic|/etc/acpi/handler.sh}}.
{
 
export DISPLAY=`echo $DISPLAY | cut -c -2`
 
user=`who | grep " $DISPLAY" | awk '{print $1}'`
 
export XAUTHORITY=/home/$user/.Xauthority
 
eval $1=$user
 
}
 
  
to achieve user at the current display. For example when you press the power button and want to shutdown KDE properly you can use this function:
+
== See also ==
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 =
+
* [http://acpid.sourceforge.net/ acpid homepage]
*http://acpid.sourceforge.net/ - acpid homepage
+
* [https://wiki.gentoo.org/wiki/ACPI#Configuration Gentoo wiki]
*http://www.capaman.8m.com/acpid.html - acpid mini-howto
 

Latest revision as of 21:51, 26 January 2019

acpid2 is a flexible and extensible daemon for delivering ACPI events. When an event occurs, it executes programs to handle the event. These events are triggered by certain actions, such as:

  • Pressing special keys, including the Power/Sleep/Suspend button
  • Closing a notebook lid
  • (Un)Plugging an AC power adapter from a notebook
  • (Un)Plugging phone jack etc.
Note: Desktop environments, such as GNOME, systemd login manager and some extra key handling daemons may implement own event handling schemes, independent of acpid. Running more than one system at the same time may lead to unexpected behaviour, such as suspending two times in a row after one sleep button press. You should be aware of this and only activate desirable handlers.

Installation

Install the acpid package. Then start and/or enable acpid.service.

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:

# journalctl -f

Now press the Power button and/or Sleep button (e.g. Fn+Esc) 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

or with openbsd-netcat:

$ netcat -U /var/run/acpid.socket

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

button/power PBTN 00000000 00000b31

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

$1 button/power
$2 PBTN
$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 and 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 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 "$3" in
    SLPB) echo -n mem >/sys/power/state ;;
    *)    logger "ACPI action undefined: $3" ;;
esac

Make the script executable:

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

Finally, reload the acpid.service to get acpid to recognize the changes to these files.

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

Tips and tricks

Note: Some of the actions described here, such as Wi-Fi toggle and backlight control, may already be managed directly by driver. You should consult documentation of corresponding kernel modules, when this is the case.

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 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 substitute it for the acpi events in the files below.

/etc/acpi/events/vol-d
event=button/volumedown
action=amixer set Master 5-
/etc/acpi/events/vol-m
event=button/mute
action=amixer set Master toggle
/etc/acpi/events/vol-u
event=button/volumeup
action=amixer set Master 5+
Note: These commands may not work as expected with PulseAudio. [1] For full functionality, run commands as the current user while specifying the XDG_RUNTIME_DIR environment variable, for example with sudo -u user XDG_RUNTIME_DIR=/run/user/1000 pactl.
Tip: Disable or bind the volume buttons in Xorg to prevent conflicts with other applications. See Xmodmap for details.

See also [2].

Enabling backlight control

Similar to volume control, acpid also enables you to control screen backlight. To achieve this you write some handler, like this:

/etc/acpi/handlers/bl
#!/bin/sh
bl_dev=/sys/class/backlight/acpi_video0
step=1

case $1 in
  -) echo $(($(< $bl_dev/brightness) - $step)) >$bl_dev/brightness;;
  +) echo $(($(< $bl_dev/brightness) + $step)) >$bl_dev/brightness;;
esac

and again, connect keys to ACPI events:

/etc/acpi/events/bl_d
event=video/brightnessdown
action=/etc/acpi/handlers/bl -
/etc/acpi/events/bl_u
event=video/brightnessup
action=/etc/acpi/handlers/bl +

Enabling Wi-Fi toggle

You can also create a simple wireless-power switch by pressing the WLAN button. Example of event:

/etc/acpi/events/wlan
event=button/wlan
action=/etc/acpi/handlers/wlan

and its handler:

/etc/acpi/handlers/wlan
#!/bin/sh
rf=/sys/class/rfkill/rfkill0

case $(< $rf/state) in
  0) echo 1 >$rf/state;;
  1) echo 0 >$rf/state;;
esac

Getting user name of the current display

To run commands dependening on Xorg, defining the X display as well as the MIT magic cookie file (via XAUTHORITY) is required. Latter is a security credential providing read and write access to the X server, display, and any input devices (see xauth(1)).

See [3] for an example function when using xinitrc.

Note:
  • If the LCD backlight is not turned off when the lid is closed, you may do so manually by running getXuser xset dpms force off and getXuser xset dpms force on respectively on lid close and lid open events. Should the display be blanked, but the backlight left on, instead use vbetool with vbetool dpms off and vbetool dpms on. See also XScreenSaver#Configuration.
  • When using who or w, make sure /run/utmp is created at boot-time. See utmp(5) for details.

Connect to acpid socket

In addition to rule files, acpid accepts connections on a UNIX domain socket, by default /var/run/acpid.socket. User applications may connect to this socket.

#!/bin/bash
coproc acpi_listen
trap 'kill $COPROC_PID' EXIT

while read -u "${COPROC[0]}" -a event; do
    handler.sh "${event[@]}"
done

Where handler.sh can be a script similar to /etc/acpi/handler.sh.

See also