Using acpid to control your PC buttons

1. Mount acpid.tcz

2. Create files /usr/local/etc/acpi/events/all and /opt/.acpi/gen.sh along with the containing directories. Change the permission of the gen.sh file to be executable. (I use rox - right-click → permissions.)

3. In /usr/local/etc/acpi/events/all, write:

event=.*
action=/home/tc/.acpi/gen.sh "%e"

This code catches all events generated by acpid and passes them to gen.sh.

4. In gen.sh, write:

#!/bin/sh
case $1 in
    button/power*)
       exitcheck.sh shutdown;; #power button code, with backup
    button/sleep*)
        echo -n "mem" > /sys/power/state;; # sleep button code
    "hotkey ATKD 0000001a"*) # special key #1 extra assigned sleep button
	 echo -n "mem" > /sys/power/state;;
    "hotkey ATKD 0000001d"*)
         exitcheck.sh reboot;; # special key #4 reboot with backup
    # *)
    # popup $1;;
esac

The 4 keys are computer specific and only given as examples.

5. To discover your PC's key codes, uncomment the last 2 lines of code just before the esac statement. These lines will take each generated event code and popup a message with the key code. Write enough characters into the gen.sh script file from each key code to uniquely identify each key (don't take all as the last parts may change with each button press). The * wild card will accept the rest of the key code. If there are spaces in the key code, enclose it with double quote marks as shown.

6. Assign actions for each key event as shown by the examples in the file.

7. Backup the file /usr/local/etc/acpi/events/all in filetool, and, if you do not have a persistent home, backup the /.acpi home directory as well.

8. In /home/tc/.X.d, create a blank file called for example myacpi, and write the following line in it:

sudo /usr/local/etc/init.d/acpid start

This line starts acpid and loads the event file. The advantage of having the actions file gen.sh in /home/tc/.acpi/ is you can edit and experiment all you want without having to re-initialise acpid each time you edit something.

9. Reboot the pc to activate acpid.

Once you have everything you want, simply comment out the 2 lines of code before esac again.

For more sleep command options, see the suspend-utils.tcz extension and also the thread about it in tce 2.x news.

The above method is just one way to control buttons with acpid; there are many other ways of doing it; this way is convenient due to the ability to edit the actions file without having to re-initialise acpid each time.

Here is a full actions file that is used on a EeePC 1000HE:

#!/bin/sh
case $1 in
    button/power*)
        exitcheck.sh shutdown;;
    button/sleep*)
        echo -n "mem" > /sys/power/state;;
    "hotkey ATKD 0000001a"*) # special key #1
        echo -n "mem" > /sys/power/state;;
    "hotkey ATKD 0000001d"*)
         exitcheck.sh reboot;; # special key #4
    "hotkey ATKD 00000030"*)
    	xrandr --output VGA --auto --output LVDS --off;;
    "hotkey ATKD 00000031"*)
    	xrandr --output VGA --off --output LVDS --auto;;
    "hotkey ATKD 00000032"*)
    	xrandr --output VGA --auto --output LVDS --auto;;
    "hotkey ATKD 0000002"*) ;; # LVDS brightness - do nothing, bios controlled
    "hotkey ATKD 00000013"*) # mute/un-mute sound
    	if [[ -z `amixer cget numid=1 | grep "on,on"` ]]
    	then
    		amixer cset numid=1 on
    	else
    		amixer cset numid=1 off
    	fi
    	aplay /home/tc/.sounds/blip.wav;;
    "hotkey ATKD 00000014"*)
|    	VOL=`amixer cget numid=2 | grep ": values=" | cut -d= -f2 | cut -d, -f1`|
      	[[ $VOL -gt 3 ]] && amixer cset numid=2 `expr $VOL - 4`;;
    "hotkey ATKD 00000015"*)
|    	VOL=`amixer cget numid=2 | grep ": values=" | cut -d= -f2 | cut -d, -f1`|
      	[[ $VOL -lt 61 ]] && amixer cset numid=2 `expr $VOL + 4`;;
    "hotkey ATKD 00000016"*)
    	xset dpms force off;;
    "hotkey ATKD 00000039"*)
    	/home/tc/.mywbar/chromium.sh;;
    *)
      	popup $1 &
      	sleep 1
      	kill `pidof popup` &;;
esac