Skip to content

Instantly share code, notes, and snippets.

@lalten
Forked from thomasleveil/README.md
Last active December 26, 2022 22:53
Show Gist options
  • Save lalten/ce5ac0b1cc4b24a24cc44926255b871d to your computer and use it in GitHub Desktop.
Save lalten/ce5ac0b1cc4b24a24cc44926255b871d to your computer and use it in GitHub Desktop.
Workaround for oled backlight issues

Works for Thinkpad X1 Yoga 2G (20JDCTO1WW) (and most probably others) with OLED display

First install inotify-tools. Then create a script around inotify that will be launched upon each boot or through autostart.

sudo apt install inotify-tools
cat <<-EO1 | sudo tee /usr/local/bin/xbacklightmon.sh#!/bin/bash
    # https://wiki.archlinux.org/index.php/backlight#sysfs_modified_but_no_brightness_change
    # https://wiki.archlinux.org/index.php/Alienware_13#OLED_screen_brightness
    # https://gist.github.com/lalten/ce5ac0b1cc4b24a24cc44926255b871d
    # apt install inotify-tools 

    path=/sys/class/backlight/intel_backlight

    luminance() {
        read -r level < "$path"/actual_brightness
        new_brightness="$(bc -l <<< "scale = 2; $level / $max")"
        LC_NUMERIC="en_US.UTF-8" printf '%f\n' $new_brightness
    }

    read -r max < "$path"/max_brightness

    xrandr --output eDP-1 --brightness "$(luminance)"

    inotifywait -me modify --format '' "$path"/actual_brightness | while read; do
        xrandr --output eDP-1 --brightness "$(luminance)"
    done

EO1
sudo chmod +x /usr/local/bin/xbacklightmon.sh

cat <<-EO2 >~/.config/autostart/xbacklightmon.desktop
    [Desktop Entry]
    Type=Application
    Exec=/usr/local/bin/xbacklightmon.sh
    Hidden=false
    X-GNOME-Autostart-enabled=true
    Name=xbacklightmon
EO2
#!/bin/bash
# Make Oled brightness work for Thinkpad X1 Yoga
# apt install inotify-tools
# https://wiki.archlinux.org/index.php/backlight#sysfs_modified_but_no_brightness_change
# https://wiki.archlinux.org/index.php/Alienware_13#OLED_screen_brightness
# https://gist.github.com/lalten/ce5ac0b1cc4b24a24cc44926255b871d
path=/sys/class/backlight/intel_backlight
luminance() {
read -r level < "$path"/actual_brightness
new_brightness="$(bc -l <<< "scale = 2; $level / $max")"
LC_NUMERIC="en_US.UTF-8" printf '%f\n' $new_brightness
}
read -r max < "$path"/max_brightness
xrandr --output eDP-1 --brightness "$(luminance)"
inotifywait -me modify --format '' "$path"/actual_brightness | while read; do
xrandr --output eDP-1 --brightness "$(luminance)"
done
@lalten
Copy link
Author

lalten commented Sep 27, 2019

Note that this doesn't work well together with Gnome's Night Light.
Here's a function that reads the current gamma settings from xrandr, so they can be reapplied when setting brightness:

gamma() {
    # read and invert gamma values, see https://gitlab.freedesktop.org/xorg/app/xrandr/issues/33
    gammas_inverted=$(xrandr --verbose | awk '/Gamma/ {print $2; exit}')
    IFS=':' read -r r g b <<< "$gammas_inverted"
    r=$(bc -l <<< "1/$r")
    g=$(bc -l <<< "1/$g")
    b=$(bc -l <<< "1/$b")
    LC_NUMERIC="en_US.UTF-8" printf '%f:%f:%f\n' $r $g $b
}

xrandr --output eDP-1 --brightness "$(luminance)" --gamma "$(gamma)"

Unfortunately, Night Light seems to do more than just adjust gamma so reapplying these values will look really bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment