Modifying screen brightness on AC state changes

The acpid daemon listens to the /proc/acpi/event socket. When the Linux kernel any ACPI-related event, like an AC state change, it sends a message to that socket.

When the Linux kernel detects AC power loss, it sends a message to the socket. acpid then runs the /etc/acpi/power.sh script which, in turn, invokes all the scripts under /etc/acpi/battery.d directory, orderly.

When the Linux detects that AC power has been restored, it sends a message to the socket. acpid then runs /etc/acpi/power.sh which, in turn, invokes all the scripts under /etc/acpi/ac.d directory, orderly.

To make the LCD brightness dim when AC power is lost and return the LCD brightness to its previous value once AC power has been restored, we can create two new scripts:

  • /etc/acpi/ac.d/01-brightness.sh
  • /etc/acpi/battery.d/01-brightness.sh

Both of these scripts use a configuration file, located at /etc/default/brightness. A sample configuration file is shown below:

/etc/default/brightness

# Whether or not enable LCD brightness control depending
# on AC power state.
BRIGHTNESS_CONTROL="yes"

# Binary used to control LCD brightness.
BRIGHTNESS_PROGRAM="/usr/local/bin/bl1"

# Defines the brightness level when running on batteries.
BRIGHTNESS_LEVEL_ON_BATTERY="10"

# Defines the default brightness level for the system when
# running on AC power. This value is superseded by the value
# stored in the ${BRIGHTNESS_LAST_LEVEL_FILE} file.
BRIGHTNESS_LEVEL_ON_AC="15"

# Defines where to store the brightness level used the last
# time the system was running on AC power.
BRIGHTNESS_LAST_LEVEL_FILE="/var/tmp/brightness"

For MacBook Pro, these scripts depend use bl1, which can be found at Basic backlight support for MacBook Pro, for the BRIGHTNESS_PROGRAM, used to control the LCD brightness.

/etc/acpi/ac.d/01-brightness.sh

The following shell script restores the brightness level in use before losing AC power:

#!/bin/bash # Load configuration parameters . /etc/default/brightness exec 2> /dev/null if [ "${BRIGHTNESS_CONTROL}" = "yes" ]; then # Check the value stored in ${BRIGHTNESS_LAST_LEVEL_FILE} # holds a numeric value. OLD=$(cat ${BRIGHTNESS_LAST_LEVEL_FILE}) let NUM=${OLD}+1 let NUM=${NUM}-1 if [ "${OLD}" -eq "${NUM}" ] 2>/dev/null; then # If previously stored value is a number, set the brightness # level to that. VALUE=${OLD} else # If previously stored value is not a number, or undefined # restore the brightness to its default value for AC power. VALUE=${BRIGHTNESS_LEVEL_ON_AC} fi # Sets the new brightness and stores its value /usr/local/bin/bl1 ${VALUE} | sed 's/new value: //g' > ${BRIGHTNESS_LAST_LEVEL_FILE} fi

/etc/acpi/battery.d/01-brightness.sh

The following shell script stores the current brightness level in order to restore it once AC power is restored and, since AC power has just been lost, dims the LCD brightness:

#!/bin/bash # Load configuration parameters . /etc/default/brightness if [ "${BRIGHTNESS_CONTROL}" = "yes" ]; then # Gets current brightness level and stores it ${BRIGHTNESS_PROGRAM} | sed 's/Current value : //g' > ${BRIGHTNESS_LAST_LEVEL_FILE} # Sets the brightness level used when running on batteries ${BRIGHTNESS_PROGRAM} ${BRIGHTNESS_LEVEL_ON_BATTERY} fi
Advertisements

2 thoughts on “Modifying screen brightness on AC state changes

  1. Easily, the article is actually the greatest on this deserving topic. I agree with your conclusions and will eagerly look forward to your coming updates. Saying thanks will not just be sufficient, for the wonderful lucidity in your writing. I will immediately grab your rss feed to stay informed of any updates. Authentic work and much success in your business efforts!

  2. Hi my friend! I would like to say that this post is awesome, wonderful written including almost all the important information and facts. I have to see more posts similar to this.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s