Controlling brightness, the easy way

<<Back home

I report here a simple procedure to set the brightness of your screen under GNU/Linux that does not rely on GUI applications, and does not rely on X11/Wayland being available on your system.

The proposed solution is based on using two keys of your choice (in the following we will use Fn+F5 and Fn+F6) to decrease/increase the current brightness of your screen. This is achieved by binding those keys through acpid to a shell script that does the dirty job.

Pre-requisites

I assume that you are able to:

Preliminary checks

The solution is based on setting brightness values through the /sys/class/backlight/* interface. So the first thing to check is that you actually have something there. In my system I have only the folder "intel_backlight":
    $ ls -l /sys/class/backlight/
    intel_backlight
    $
which in turns contains:
    $ ls /sys/class/backlight/intel_backlight
    actual_brightness  bl_power  brightness  device  
    max_brightness     power     subsystem   type    uevent
    $
Now, the exact pool of files you have in that folder depends on your hardware. The baseline is that there should be a file called brightness and another file called max_brightness. We don't need much more than that. So let's get started.

Install required stuff

You need to install acpid&friends. If you are root, just run:
    # apt-get install acpid acpi-support
in a terminal. If you are a luser with sudo privileges, run instead:
    $ sudo apt-get install acpid acpi-support
Before you continue, check that acpid is running:
    $ ps ax | grep /usr/sbin/acpid
     2084 ?        Ss     5:50 /usr/sbin/acpid
    $
You should also have some stuff in the folder /etc/acpi:
    $ ls /etc/acpi
    always-mute.sh              ejectbtn.sh       lenovo-bright.sh  
    asus-keyboard-backlight.sh  events            lid.sh           
    asus-wireless.sh            ibm-wireless.sh   mutebtn.sh        
    powerbtn-acpi-support.sh    sleep_suspend.sh  undock.sh
    power.sh                    sonybright.sh     voldownbtn.sh
    sleep_suspendbtn.sh         tosh-wireless.sh  volupbtn.sh
    $
This comes with acpi_support, usually.

Put the script in place

The script reported below, named set-bright.sh, is responsible for increasing (decreasing) the brightness when called with the parameter up (down). Just copy-paste the script in the following in the file /etc/acpi/set-bright.sh:
#!/bin/sh


if [ ! $# -le 1 ]; then 
        echo "Usage: $0 {up|down}"
        exit 1;
fi

INCR=80
BRIGHT_FILE=/sys/class/backlight/intel_backlight/brightness

min_br=0
max_br=`cat /sys/class/backlight/intel_backlight/max_brightness`
act_br=`cat /sys/class/backlight/intel_backlight/actual_brightness`


case $1 in 
        up)
        if [ ${act_br} -le ${max_br} ]; then 
                new_br=$((${act_br} + $INCR));
                if [ ${new_br} -gt ${max_br} ]; then 
                        new_br=${max_br};
                fi
                echo ${new_br} >> ${BRIGHT_FILE}
        fi 
        ;;
        down)
        if [ ${act_br} -gt ${min_br} ]; then 
                new_br=$((${act_br} - $INCR));
                if [ ${new_br} -lt ${min_br} ]; then 
                        new_br=${min_br};
                fi
                echo ${new_br} >> ${BRIGHT_FILE}
        fi 
        ;;
        *)
        echo "Usage: $0 {up|down}"
        exit 1;
esac

IMPORTANT Make sure that the file /etc/acpi/set-bright.sh is executable:

    # chmod 755 /etc/acpi/set-bright.sh

N.B.: you might need to edit the variables BRIGHT_FILE, INCR, max_br, and act_br to reflect your hardware configuration and/or your personal preferences.

Choose which buttons to use

We assume that you want to use the buttons Fn+F5 (press-function-and-F5-together) and Fn+F6 to decrease and increase the brightness, respectively. When those buttons are pressed, they trigger an acpi event. We need to know the name of such events. So we run:
    $ acpi_listen

This command prints on output the name of the acpi event associated to each button you press, until you hit Ctrl+C to terminate it. For instance, if I type Fn+F5 followed by Fn+F6 in my system I see:
    $ acpi_listen
    video/brightnessdown BRTDN 00000087 00000000
    video/brightnessup BRTUP 00000086 00000000

Note down the names of the events associated to Fn+F5 and Fn+F6 (you'll need them in a moment) and hit Ctrl+C to terminate acpi_listen.

Now as user root open the file /etc/acpi/events/brightup:

    # editor /etc/acpi/events/brightup
and be sure that it contains (at least) something like:
event=video[ /]brightnessup
action=/etc/acpi/set-bright.sh up
Notice that the first line (event=video...) is referring to the event video/brightnessup, that is the event generated by Fn+F6. The associated action is to call the script /etc/acpi/lenovo-bright.sh up

Now repeat the process described above and make sure that the file /etc/acpi/events/brightdown contains something like:

event=video[ /]brightnessdown
action=/etc/acpi/set-bright.sh down 

After having saved those two files, just restart acpid:

    # /etc/init.d/acpid restart
and adjust your brightness as you like.

HTH

KatolaZ


DISCLAIMER: ALL THE INFORMATION AND SOFTWARE AVAILABLE FOR DOWNLOAD ON THIS PAGE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Last update: 2018-02-02