December 3

the backlight script..

     In my experience, some GNU/Linux installations on laptops don’t quite handle the display backlight adjustment well enough. Sometimes the adjustment lags (which is strange as key-strokes are hardware interrupts, should prempt) with an annoying delay, and other times it simply doesn’t work at all. I often find myself having to reconfigure the hotkeys. This procedure may be unsafe, as it changes the ownership of a file in /sys to that of my user. Use at your own risk!

     To do this, we will use xbindkeys-config (XBindKeys_Config 0.1.3 by Laurent Vuibert), a gui utility that helps setup script execution by means of key combinatons or mouse clicks, using the xbindkeys utility (xbindkeys 1.8.6 by Philippe Brochard).

     On ubuntu, you can get it via the repositories issuing the following commands:

sudo apt-get update && sudo apt-get install xbindkeys xbindkeys-config

     If there are more files being installed, don’t worry, chances are you need to update some of your packages. Next, we need to make sure we can acces the brightness files. In my case, I look in the directory /sys/class/backlight/.

     Now in my case, I have 2 directories in there, acpi_video0/, and intel_backlight/. While both have the brightness file I want to adjust, they both have different maximum brightness vaulues found in the max_brightness file. This doens’t mean that each will have a different maxmum display brightness, it just means that the highest max_brightness number will provide a wider range of brightness adjustment. e.g.:

  • /sys/cass/backlight/acpi_video0/max_brightnesss contains 100, so we have at most 100 different brightness levels, 0 being screen off.
  • /sys/cass/backlight/intel_backlight/max_brightnesss contains 937, so we have at most 937 different brightness levels, 0 being screen off.

     I can get these values simply using cat, e.g.:

cat /sys/cass/backlight/intel_backlight/max_brightnesss

     Now the obvious remark to make here is that we will hardly see any difference between 50 and 51 in the case of acpi_video0/brightness, let alone will we see any difference between 468 and 469 in the case of intel_backlight/brightness. However, we’ll just pick one and go along with it. Let’s pick the intel_baklight/brightness one.

     So now that we have identified the location of the file, and we know that the maximum brightness is 937, we can start by testing if changing the values in the file changes our display brightness:

sudo echo "450" > /sys/class/backlight/intel_backlight/brightness

     The above command should bring the backlight brightness to about half.

sudo echo "750" > /sys/class/backlight/intel_backlight/brightness

     The above command should bring the backlight brighness back up some more. If this worked, then we’re good to proceed, if not, we might try getting the value in the acpi_video0/max_brightness file and repeating the command using:

sudo echo "50" > /sys/class/backlight/acpi_video0/brightness

instead and then bringing the value back up. Remeber that we have to respect the value in the max_brightness file,and adjust the brightness according to that value.
     We can now move on with the BASH script. It can be stored in any directory of your choice really, but for this example, we’ll create a .script/ directory in our home directory where we will store the script file.

mkdir ~/.script && cd ~/.script

     Now, we can create a script file, let’s call it brightness.sh:

touch brightness.sh

     Once we’re done creating this file, it is a good idea to set this file as an executable:

chmod +x brightness.sh

     We can now use an editor of our choice to edit the contents the script file below. I used vim, and entered the following in the script file:

##  Script to adjust backlight brightness
#!/bin/bash
FILE="/sys/class/backlight/intel_backlight/brightness"
MBFILE="/sys/class/backlight/intel_backlight/max_brightness"
CUR=`cat $FILE`
MAX=`cat $MBFILE`
MIN=10
STEP=20
echo $CUR
case $1 in
        show)
                echo $CUR
                ;;
        up)
                if [ $(($CUR + $STEP)) -le $MAX ]; then
                        echo $(( $CUR + $STEP )) > $FILE
                fi;;
        down)
                if [ $(($CUR - $STEP)) -ge $MIN ]; then
                        echo $(( $CUR - $STEP )) > $FILE
                fi;;
        min)
                if [ $CUR -gt $MIN ]; then
                        echo $(( $MIN )) > $FILE
                fi;;
        max)
                if [ $CUR -lt $MAX ]; then
                        echo $(( $MAX )) > $FILE
                fi;;
esac
CUR=`cat $FILE`
echo $CUR

     As you might have noticed, I use steps of 20, and set the minimum brightness value to be 10. Also, this script takes an argument in the form of: show, up, down, min, max. Now, we’ll use xbindkeys to proceed. First, we’ll create a default configuration file:

xbindkeys--defaults ~/.xbindkeysrc

     This creates a default onfigration file and place it in the user’s home directory. Next, we can launch the gui utility from a terminal as follows:

xbindkeys-config

     The following window pops open:
xbindkeys-config
     To create an entry, we simply click new at the bottom of the window. This will create a new entry that we can edit. We can then edit the name which gives a meanigfull description of what we’re doing, and the action which in our case is to run a script with a certain argument apssed to it. As for the key to use, it is up to you to select which key combinations you would like to use to run the scrit with the differnt arguments.

     To select a new key combinatin, select the new entry you have created, and then click on Get Key button, and perform the key combination of you choice on the keyboard. When you have released all kes, the key field will be automatically filled for you. When you have setup the different entries with the different key combintions of your choice, simply click on the Save and Apply and Exit button.

     The last step is to modify the ownership of the brightness file so that we can write to it with our user. We do this in two steps. In the case of Ubuntu, the first is to add a line at the end of the /etc/init.d/rc.local file, and add the following lines at the very end of the file:

# backlight brightness
sudo chown user:user6 /sys/class/backlight/intel_backlight/brightness

where you can substitue user:user for your actual user’s name, like bob:bob for instance. This will ensure that the file is always writable by your user during startup. Beware, this may not be a safe solution, so use it at your own risk.

     The last and final step would be to run xbindkeys, and to do so, we can launch it from the terminal by issuing the command:

xbindkeys -f ~/.xbindkeysrc &

     You can now test the results.

Tags: , , ,

Posted December 3, 2014 by wm in category scripts, tweaks