05.12.2010

Screen brightness

Changing the screen brightness can extend the battery life by a considerable amount, so this a quite important thing concerning laptops. Unfortunately, that functionality is broken. On my laptop with Ubuntu, I am unable to change the display brightness by the usual means. The buttons do create a reaction, but that reactions merely consists of the GNOME Power Manager displaying a notification with a bar that changes. Nothing happens to my display.
Additionally, every time I press one of the keys, that bar increases or decreases by two steps instead of one. So when I keep the button pressed for about 4-5 seconds, that notification keeps blinking for about 10-15 seconds before finally disappearing.
And now the good part: I found a solution :)
I managed to get the brightness keys working. I am able to change the brightness by an arbitrary amount upon a key press, yet so far I was unable to make that annoying notification disappear.
Thanks to some post in some forum (unfortunately I remember neither the author nor the forum), I got to know the location of a node on the /sys filesystem where the brightness can be changed by echoing numbers into it.
(Note: You need root access to write files in /etc/acpi/).
  1. Create a script to be executed upon pressing one of the brightness keys. Mine is named brightness.sh and is placed in /etc/acpi/.
    Code:
    #! /bin/bash

    # pressing the buttons generates 2 events, so we need to ignore one
    if [ "$2" != "LCD" ]
    then
        exit 0
    fi
    # get brightness
    CURRENT_BRIGHTNESS="$(sudo setpci -s 00:02.0 F4.B | tr "[a-z]" "[A-Z]")"
    # to upper case
    CURRENT_BRIGHTNESS="$(echo "$CURRENT_BRIGHTNESS" | tr "[a-z]" "[A-Z]")"
    # add/substract 16
    case "$3" in
        "00000086")
            OP="+"
            ;;
        "00000087")
            OP="-"
            ;;
        *)
            exit 0
    esac
    NEW_BRIGHTNESS="$(echo "obase=16;ibase=16;$CURRENT_BRIGHTNESS $OP 10" | bc)"
    # check boundaries
    if [ "$(echo "ibase=16; $NEW_BRIGHTNESS < F" | bc)" -eq 1 ] then     NEW_BRIGHTNESS="F" elif [ "$(echo "ibase=16; $NEW_BRIGHTNESS > FF" | bc)" -eq 1 ]
    then
        NEW_BRIGHTNESS="FF"
    fi
    # set brightness
    setpci -s 00:02.0 F4.B=$NEW_BRIGHTNESS
    The script changes the brightness in steps of 10. You can change this amount by changing the number in line 23 (NEW_BRIGHTNESS=...). The brightness is a number between 0 and 255 and is read and saved as a hexadecimal number (0 to FF).
  2. Make the script executable.
    Code:
    $ chmod +x /etc/acpi/brightness.sh
  3. Create an ACPI event handler to execute the script upon pressing one of the brightness keys. I called it brightness and placed it in /etc/acpi/events/.
    Code:
    # /etc/acpi/events/brightness
    # Calls /etc/acpi/brightness.sh to change screen brightness upon key press
    # Author: Frederik Möllers

    event=video
    action=/etc/acpi/brightness.sh %e
  4. Make acpid reload its configuration for the changes to take effect.
    Code:
    $ reload acpid

When pressing Your brightness keys, You display brightness should change now. The bar will still appear and display something wrong and will probably annoy You, but I haven't found out yet how to kill that thing. If it's not working, You can try to check wether the ACPI events are the correct ones (the script reacts on video LCD 00000086 00000000 and video LCD 00000087 00000000 - acpi_listen can tell You what events are generated by Your keys) and wether the call to setpci does the operation on the right card (lspci is Your friend, Your intel onBoard graphics card should have ID 00:02.0). If Your system has different values, just change them in the script and feel free to tell me so I can generalize the script and put a note on that here.

Warning: Never set Your brightness to 0! This is not only useless (You can't see anything because a value of 0 really means to completely turn off the light panel), but it will also kill Your system, forcing You to do a hard reset. You will be unable to change the brightness back to a positive number, even if You enter the correct command blindly. My script will not do this, but if You want to tinker around for Yourself, always keep this in mind!

That's it! You're done!

1 Kommentar: