Brightness Control

Brightness Control

README

Brightness is an attribute of visual perception in which a source appears to be radiating or reflecting light. In other words, brightness is the perception elicited by the luminance of a visual target. This is a subjective attribute/property of an object being observed.

Screen brightness can often be tricky to control. On many machines, physical hardware switches are missing and software solutions may or may not work well. Make sure to find a working method for your hardware! Too bright screens can cause eye strain. There are many ways to adjust the screen backlight of a monitor, laptop or integrated panel (such as the iMac) using software, but depending on hardware and model, sometimes only some options are available.

There are many ways to control brightness, according to this discussion and this wiki page, the control method could be divided into these categories:

  • Brightness is controlled by vendor specified hotkey, and there is no interface for OS to adjust brightness.
  • Brightness is controlled by OS using ACPI or a Graphic driver.
  • Brightness is controlled by HW register throught setpci.

And basically all methods expose themselves to the user by /sys/class/backlight/ and /sys/class/brightness.

Code

It is often possible to adjust the backlight by ACPI. This controls the actual LEDs or cathodes of the screen. When this ACPI option is available, the illumination is controllable using a GUI slider in the Display/Screen system settings or by simple commands on the CLI. Different cards might manage this differently. Execute this command to find out: ls -l /sys/class/backlight.

In my case I found a symbolic link called acpi_video0 containing (among various files) these three ASCII text files: actual_brightness, brightness, max_brightness. If you open the file max_brightness you’ll find a number representing the maximum brightness level your driver support for your display, and the file actual_brightness has the actual brightness level configured. Then I decided to write an Script to let me control the brightness level through these files, the Script basically grant Root privileges to write in the file brightness and prompt to the user for the level to set using the Entry Zenity Widget.

#!/bin/sh
LEVEL=$(zenity --entry \
    --title='Set brightness level' \
    --text='Type a number in range 0-9 representing the\nbrightness level you want for the display'
)
FILEPATH="/sys/devices/pci0000:00/0000:00:01.0/backlight/acpi_video0/brightness"
FILEPATH="/sys/class/backlight/acpi_video0/brightness"
#
echo "Setting brightness to: ${LEVEL}"
if [ -w "${FILEPATH}" ]; then
    echo "The brightness file is writable"
else
    echo "Granting permissions to write on the brightness file to the current user"
    gksu chmod 777 "${FILEPATH}"
    if [ -w "${FILEPATH}" ]; then
        echo "Permission to write on the brightness file granted successfully"
    else
        echo "Error granting permission to write on the brightness file"
        zenity --error --title='Set brightness level' --text='Set brightness level\nThe brightness file is not writable.'
    fi
fi
if [ "${LEVEL}" != "" ]; then
    if [ $LEVEL -gt -1 -a $LEVEL -lt 10 ]; then
        # echo "gt -1 -a lt 10"
        echo "${LEVEL}" > "${FILEPATH}"
    else
        echo "Invalid value, use a number in range 0-9"
        zenity --error --title='Set brightness level' --text='Set brightness level\nInvalid value, use a number in range 0-9'
    fi
else
    echo "Invalid value, type a number as argument"
    zenity --error --title='Set brightness level' --text='Set brightness level\nInvalid value, type a number as argument'
fi

Differences

  • APCI, Once your brightness keys work, you can set the brightness by writing to the procfs echo LIGHT_LEVEL > /proc/acpi/video/VID1/LCD0/brightness or use the X-Server to native functionality xrandr --output LVDS --set BACKLIGHT_CONTROL native. An alternative can be using the command setpci -s 00:02.0 F4.B=LEVEL.

  • Using procfs

  echo 'level 3' > /proc/acpi/ibm/brightness
  echo 'up' > /proc/acpi/ibm/brightness
  echo 'down' > /proc/acpi/ibm/brightness
  • Using sysfs (sysfs-interface)
  cat /sys/class/backlight/thinkpad_screen/{max,actual}_brightness
  echo 3 > /sys/class/backlight/thinkpad_screen/brightness
  • Nvidia driver The Nvidia driver does not enable brightness control by default, the solution is to add to the file /etc/X11/xorg.conf a new configuration using the xorg.conf.d mechanism: Create a new file sudo nano /usr/share/X11/xorg.conf.d/20-nvidia.conf and insert the following lines (then save and exit):
  Section "Device"
    Identifier "NVIDIA"
    Driver "nvidia"
    Option "NoLogo" "True"
    Option "RegistryDwords" "EnableBrightnessControl=1"
  EndSection

Utilities

  • Xbacklight: Simple command-line utility to set the backlight level using the RandR 1.2 Backlight output property. The utility is provided by the xorg-xbacklight package allowing you to adjust the backlight through the xorg-server.
  • RedShift: This software uses randr to adjust the screen brightness depending on the time of day and your geographic position. It can also do RGB gamma corrections and set color temperatures. As with xcalib, this is very much a software solution and the look of the mouse cursor is unaffected. To execute a single quick adjustment of the brightness use this command redshift -o -l 0:0 -b 0.8 -t 6500:6500, if your longitude is west or your latitude is south, you should input it as negative gtk-redshift -l 37.8717:-122.2728.
  • ReLight: The package provides a service to automatically restore previous backlight settings during reboot along using the ACPI method. The package also contains a dialog-based menu for selecting and configuring backlights for different screens.
  • Calise: It basically computes ambient brightness, and set screen’s correct backlight, simply making captures from the webcam, for laptop without light sensor.
  • XCalib: This program can correct gamma, invert colors and reduce contrast; the program uses ICC technology to interact with X11 and while the screen is dimmed, you may find that the mouse cursor is just as bright as before, example: xcalib -co 40 -a.

Credits

References:

Do you have a project idea? Let's make it together!