I use the wonderful i3 window manager. Since I run it on my laptop, I need some sort of power management: suspend when lid is closed, adjust screen brightness and CPU performance when running on battery power, etc. My first thought was to use
gnome-power-manager, but that seems to have disappeared in Fedora 17. I then tried
yum install xfce4-power-manager, then start by running
xfce4-power-manager and then
xfce4-power-manager-settings to configure), which mostly of worked: I could get it to suspend and adjust brightness, but suspend-and-LOCK wouldn’t work for whatever reason.
So I decided to do it a different, and more customizable way. (Most of the following is probably a waste of time if you’re using something like Unity, Gnome, KDE, Xfce, etc., which all take care of these things for you.)
ACPI events for suspend
Let’s start with suspending and locking. First, install
acpid if you don’t already have it and start it (I’m using Fedora here, but the instructions should be pretty much the same for Ubuntu):
Now we can listen to APCI events and react to them. To check that it works, run
acpi_listen and try things like adjusting screen brightness and closing/opening the lid. This should generate events like this:
$ acpi_listen video/brightnessdown BRTDN 00000087 00000000 video/brightnessup BRTUP 00000086 00000000 video/brightnessup BRTUP 00000086 00000000 button/lid LID close button/lid LID open
/etc/acpi/events, I created the file
lidbtn (you can call it anything you want) with the following content:
event=button[ /]lid action=/etc/acpi/lid.sh
The above simply specifies that on any button/lid event,
/etc/acpi/lid.sh should be executed. In
/etc/acpi/lid.sh, I have the following:
If the lid is closed,
pm-suspend will be executed. physlock is a neat, lightweight locking tool, written because
vlock had issues with suspend/hibernate.
Performance settings depending on AC/battery power
To adjust things depending on whether the laptop is running on AC or battery, I created
/etc/pm/power.d/performance with the following. Note that for Ubuntu, at least in 12.04.1, change
cpupower -c $cpu frequency-set to
cpufreq-set -c $cpu—and don’t forget to install
Don’t copy the above code blindly (unless you happen to have an X220, and the same preferences…)! And don’t forget to make it executable:
chmod +x /etc/pm/power.d/performance.
Next step was getting the laptop’s hotkeys for adjusting brightness to work. I could use ACPI events (
video/brightnessup) to trigger something, or just bind the X keycodes. I opted for the latter.
You should be able to set brightness manually, with e.g.
echo 10 > /sys/class/backlight/acpi_video0/brightness. To make life easier I installed
xbacklight, which lets you set or increase/decrease by a specificed amount.
Next, I used
xev to grab the keycodes for the brightness up/down buttons and put the following in my
bindcode 232 exec xbacklight -dec 10 bindcode 233 exec xbacklight -inc 10
To get the hotkeys for sound to work, I again used
xev to get the keycodes, and then bound them to
bindcode 121 exec amixer set Master toggle -q bindcode 122 exec amixer set Master 1%- unmute -q bindcode 123 exec amixer set Master 1%+ unmute -q