https://wiki.archlinux.org/api.php?action=feedcontributions&user=Crorvick&feedformat=atomArchWiki - User contributions [en]2024-03-28T22:35:03ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Acpid&diff=414694Acpid2016-01-08T04:29:00Z<p>Crorvick: Fix power button event name</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Power management]]<br />
[[es:Acpid]]<br />
[[it:Acpid]]<br />
[[ja:Acpid]]<br />
[[ru:Acpid]]<br />
[[zh-cn:Acpid]]<br />
{{Related articles start}}<br />
{{Related|ACPI modules}}<br />
{{Related|DSDT}}<br />
{{Related articles end}}<br />
[http://acpid2.sourceforge.net/ acpid2] is a flexible and extensible daemon for delivering [[ACPI modules|ACPI events]]. When an event occurs, executes programs to handle the event. These events are triggered by certain actions, such as:<br />
* Pressing special keys, including the Power/Sleep/Suspend button<br />
* Closing a notebook lid<br />
* (Un)Plugging an AC power adapter from a notebook<br />
* (Un)Plugging phone jack etc.<br />
<br />
{{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.}}<br />
<br />
== Installation ==<br />
<br />
[[Install]] the {{Pkg|acpid}} package.<br />
<br />
To have ''acpid'' started on boot, [[enable]] {{ic|acpid.service}}.<br />
<br />
== Configuration ==<br />
<br />
{{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}}).<br />
<br />
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:<br />
<br />
button/sleep)<br />
case "$2" in<br />
SLPB) echo -n mem >/sys/power/state ;;<br />
*) logger "ACPI action undefined: $2" ;;<br />
esac<br />
;;<br />
<br />
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''. <br />
<br />
To determine how your buttons or {{ic|Fn}} shortcuts are recognized, run the following command:<br />
<br />
# journalctl -f<br />
<br />
Now press the Power button and/or Sleep button (e.g. {{ic|Fn+Esc}}) on your machine. The result should look something this:<br />
<br />
logger: ACPI action undefined: PBTN<br />
logger: ACPI action undefined: SBTN<br />
<br />
If that does not work, run:<br />
<br />
# acpi_listen<br />
<br />
or with {{Pkg|openbsd-netcat}}:<br />
<br />
$ netcat -U /var/run/acpid.socket<br />
<br />
Then press the power button and you will see something like this:<br />
<br />
button/power PBTN 00000000 00000b31<br />
<br />
The output of {{ic|acpi_listen}} is sent to {{ic|/etc/acpi/handler.sh}} as $1, $2 , $3 & $4 parameters.<br />
Example:<br />
<br />
$1 button/power<br />
$2 PBTN<br />
$3 00000000<br />
$4 00000b31<br />
<br />
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)''.<br />
<br />
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 & Tricks|Tips & Tricks]] section below for other commonly used commands.<br />
<br />
=== Alternative configuration ===<br />
<br />
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}}:<br />
<br />
# Pass all events to our one handler script<br />
event=.*<br />
action=/etc/acpi/handler.sh %e<br />
<br />
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:<br />
<br />
As root, create the following file:<br />
<br />
{{hc|/etc/acpi/events/sleep-button|2=<br />
event=button sleep.*<br />
action=/etc/acpi/actions/sleep-button.sh %e<br />
}}<br />
<br />
Now create the following file:<br />
<br />
{{hc|/etc/acpi/actions/sleep-button.sh|2=<br />
#!/bin/sh<br />
case "$3" in<br />
SLPB) echo -n mem >/sys/power/state ;;<br />
*) logger "ACPI action undefined: $3" ;;<br />
esac<br />
}}<br />
<br />
Finally, make the script executable:<br />
<br />
# chmod +x /etc/acpi/actions/sleep-button.sh<br />
<br />
Using this method, it is easy to create any number of individual event/action scripts.<br />
<br />
== Tips and tricks ==<br />
<br />
{{Note|Some of 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.}}<br />
<br />
=== Example events ===<br />
<br />
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}}.<br />
<br />
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}}):<br />
<br />
ac_adapter)<br />
case "$2" in<br />
AC*|AD*)<br />
case "$4" in<br />
00000000)<br />
echo -n 50 > /sys/class/backlight/acpi_video0/brightness<br />
;;<br />
00000001)<br />
echo -n 100 > /sys/class/backlight/acpi_video0/brightness<br />
;;<br />
esac<br />
<br />
=== Enabling volume control ===<br />
<br />
Find out the acpi identity of the volume buttons (see above) and susbtitute it for the acpi events in the files below.<br />
<br />
{{hc|1=/etc/acpi/events/vol-d|2=<br />
event=button/volumedown<br />
action=amixer set Master 5-<br />
}}<br />
<br />
{{hc|1=/etc/acpi/events/vol-m|2=<br />
event=button/mute<br />
action=amixer set Master toggle<br />
}}<br />
<br />
{{hc|1=/etc/acpi/events/vol-u|2=<br />
event=button/volumeup<br />
action=amixer set Master 5+<br />
}}<br />
<br />
{{Tip|Disable the volume buttons in Xorg to prevent conflicts with other applications. See [[Xmodmap]] for details.}}<br />
<br />
See also [http://blog.lastlog.de/posts/fixing_volume_change_in_linux/ Fixing volume change in Linux].<br />
<br />
=== Enabling backlight control ===<br />
<br />
Similar to volume control, acpid also enables you to control screen backlight. To achieve this you write some handler, like this:<br />
<br />
{{hc|/etc/acpi/handlers/bl|2=<br />
#!/bin/sh<br />
bl_dev=/sys/class/backlight/acpi_video0<br />
step=1<br />
<br />
case $1 in<br />
-) echo $(($(< $bl_dev/brightness) - $step)) >$bl_dev/brightness;;<br />
+) echo $(($(< $bl_dev/brightness) + $step)) >$bl_dev/brightness;;<br />
esac<br />
}}<br />
<br />
and again, connect keys to ACPI events:<br />
<br />
{{hc|/etc/acpi/events/bl_d|2=<br />
event=video/brightnessdown<br />
action=/etc/acpi/handlers/bl -<br />
}}<br />
{{hc|/etc/acpi/events/bl_u|2=<br />
event=video/brightnessup<br />
action=/etc/acpi/handlers/bl +<br />
}}<br />
<br />
=== Enabling Wi-Fi toggle ===<br />
<br />
You can also create a simple wireless-power switch by pressing the WLAN button. Example of event:<br />
<br />
{{hc|/etc/acpi/events/wlan|2=<br />
event=button/wlan<br />
action=/etc/acpi/handlers/wlan<br />
}}<br />
<br />
and its handler:<br />
<br />
{{hc|/etc/acpi/handlers/wlan|2=<br />
#!/bin/sh<br />
rf=/sys/class/rfkill/rfkill0<br />
<br />
case $(< $rf/state) in<br />
0) echo 1 >$rf/state;;<br />
1) echo 0 >$rf/state;;<br />
esac<br />
}}<br />
<br />
=== Getting user name of the current display ===<br />
<br />
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 {{ic|man xauth}}). <br />
<br />
See [https://gist.githubusercontent.com/anonymous/a1f8537740aec26ebd8f/raw/a055bf052d58c8d711b89221638cf0bd00a0508f/acpid] for an example function when using [[xinitrc]].<br />
<br />
{{Note|<br />
* 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#DPMS settings]].<br />
* When using ''who'' or ''w'', make sure {{ic|/run/utmp}} is created at boot-time. See {{ic|man utmp}} for details.<br />
}}<br />
<br />
==== Connect to acpid socket ====<br />
<br />
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.<br />
<br />
{{bc|<br />
#!/bin/bash<br />
coproc acpi_listen<br />
while read -u ${COPROC[0]} -a event; do<br />
''handler.sh'' ${event[@]}<br />
done<br />
}}<br />
<br />
Where ''handler.sh'' can be a script similar to {{ic|/etc/acpi/handler.sh}}.<br />
<br />
== See also ==<br />
<br />
* [http://acpid.sourceforge.net/ acpid homepage]<br />
* [http://www.gentoo-wiki.info/ACPI/Configuration Gentoo wiki]</div>Crorvick