Udev rules to manage brightness & desktop notifications


udev is a replacement for the Device File System (DevFS) starting with the Linux 2.6 kernel series. It allows you to identify devices based on their properties, like vendor ID and device ID, dynamically. udev runs in userspace (as opposed to devfs which was executed in kernel space).

udev rules written by the administrator go in /etc/udev/rules.d/, their file name has to end with .rules. The udev rules shipped with various packages are found in /usr/lib/udev/rules.d/. If there are two files by the same name under /usr/lib and /etc, the ones in /etc take precedence.

This is a very trivial example of how you can manage brightness and send desktop notifications when your computer is connected/disconnected to an external power source. Hopefully, this will give you a good understanding on how to write new udev rules to match your criteria.

udev handles all userspace events raised while hardware devices are added into the system or removed from it, including firmware loading as required by certain devices. First, let’s look at a sample udev rule. This is lifted straight out of the ArchWiki. There were bits and pieces of it that I could not connect together on my first read and hence the reason for this article. Your custom rules go into the /etc//udev/rules.d/ directory. Typically you create the rule with a high number like 99-, so that it can supersede and override all rules similar to it. Let’s jump right in and take a look at a sample rule I called 99-backlight_notification.rules

#Play a notification sound and send a desktop notification when screen brightness is changed according to power state on a laptop (a second ''udev'' rule actually changes the screen brightness)

#Rule for when switching to battery
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/aaron/.Xauthority" RUN+="/usr/bin/su aaron -c /usr/local/bin/brightness_notificationdown.sh"

#Rule for when switching to AC
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/aaron/.Xauthority" RUN+="/usr/bin/su aaron -c /usr/local/bin/brightness_notificationup.sh"

Now for the interesting bit, how to make sense out of this rule. let’s take the first one.

ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/aaron/.Xauthority" RUN+="/usr/bin/su aaron -c /usr/local/bin/brightness_notificationdown.sh"

The rules have Attributes and Environment variables that you can narrow down and focus on the UDEV event that will trigger your rule. The rule of the thumb is to probe for this variables using the command

udevadm info  -p  /sys/class/power_supply/BAT0

udevadm info -p /sys/class/power_supply/ac

You should see something like this:

P: /devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply/battery
E: DEVPATH=/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply/battery
E: POWER_SUPPLY_CAPACITY=100
E: POWER_SUPPLY_CURRENT_NOW=0
E: POWER_SUPPLY_ENERGY_FULL_DESIGN=3200
E: POWER_SUPPLY_HEALTH=Good
E: POWER_SUPPLY_MODEL_NAME=battery
E: POWER_SUPPLY_NAME=battery
E: POWER_SUPPLY_ONLINE=0
E: POWER_SUPPLY_PRESENT=0
E: POWER_SUPPLY_STATUS=Full
E: POWER_SUPPLY_TECHNOLOGY=Li-ion
E: POWER_SUPPLY_TEMP=300
E: POWER_SUPPLY_VOLTAGE_MAX_DESIGN=4200000
E: POWER_SUPPLY_VOLTAGE_MIN_DESIGN=3300
E: POWER_SUPPLY_VOLTAGE_NOW=0
E: SUBSYSTEM=power_supply

The E: lines are environment attributes you can use to write a udev rule. You can switch between attributes and environment variables. For Ex. you can replace the attribute ATTR{type}==”Mains” with ENV{POWER_SUPPLY_ONLINE}==”0″. You will have to experiment and play on this to get your rules written correctly.

Here are some references for you

https://unix.stackexchange.com/questions/227918/system-event-on-ac-adapter-insert-or-battery-unplugged

https://wiki.archlinux.org/index.php/udev

https://hackaday.com/2009/09/18/how-to-write-udev-rules/ 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s