Skip to content

Instantly share code, notes, and snippets.

@symmetryninja
Last active June 27, 2025 00:52
Show Gist options
  • Save symmetryninja/b7d56b937f3e8c583a62d702ddb3b562 to your computer and use it in GitHub Desktop.
Save symmetryninja/b7d56b937f3e8c583a62d702ddb3b562 to your computer and use it in GitHub Desktop.
Getting a backlight to work on a lenovo oled laptop
#! /usr/bin/env bash
##
# A little hack that forces the brightness up and down on a specific `backlight`
# device - this is largely redundant but helps when you have a lenovo laptop
# with an OLED and both an iGPU and a discrete GPU.
#
# This can be used in conjunction with keyboard shortcuts by creating a new
# file and assigning the shortcut to it.
#
# Example 'up' bind your shortcut to:
# /bin/bash -c "/home/scripts/bin/screen_brightness_up.sh up"
# Example 'down' bind your shortcut to:
# /bin/bash -c "/home/scripts/bin/screen_brightness_up.sh down"
#
# refs: https://wiki.archlinux.org/title/Backlight
##
# The device to force the brightness to
export BRIGHTNESS_DEVICE_NAME="intel_backlight"
# A temporary file to hold the brightness value to retain between rebuilds
export BRIGHTNESS_HACK_FILE="/tmp/.tmp.brightness_hack"
# Minvalue if you wanna play with it
export BRIGHTNESS_HACK_MIN=0
# Max value - this varies between screens, you can just override it
export BRIGHTNESS_HACK_MAX=`cat /sys/class/backlight/${BRIGHTNESS_DEVICE_NAME}/max_brightness`
# number of divisions
BRIGHTNESS_HACK_STEPS=10
# set the incrementor
export BRIGHTNESS_HACK_INC=$(( ($BRIGHTNESS_HACK_MAX - $BRIGHTNESS_HACK_MIN) / $BRIGHTNESS_HACK_STEPS ))
# if there's no tmp file, just make it now and at 50%
if [ ! -f "$BRIGHTNESS_HACK_FILE" ]; then
echo $(( $BRIGHTNESS_HACK_MAX / 2 )) > $BRIGHTNESS_HACK_FILE
fi
#### Functions
# Turn up the brightness by adding the increment to the current value
brightness_hack_up() {
BRIGHTNESS_HACK_VALUE=$(($BRIGHTNESS_HACK_VALUE + $BRIGHTNESS_HACK_INC))
brightness_hack_check
brightness_hack_write
}
# Turn down the brightness by subtracting the increment to the current value
brightness_hack_down() {
BRIGHTNESS_HACK_VALUE=$(($BRIGHTNESS_HACK_VALUE - $BRIGHTNESS_HACK_INC))
brightness_hack_check
brightness_hack_write
}
# Check for overflowed values
brightness_hack_check() {
if (( $BRIGHTNESS_HACK_VALUE > $BRIGHTNESS_HACK_MAX )); then
BRIGHTNESS_HACK_VALUE=$BRIGHTNESS_HACK_MAX;
fi
if (( $BRIGHTNESS_HACK_VALUE < $BRIGHTNESS_HACK_MIN )); then
BRIGHTNESS_HACK_VALUE=$BRIGHTNESS_HACK_MIN;
fi
}
# Reads the last brightness value from the tmp file
brightness_hack_read() {
export BRIGHTNESS_HACK_VALUE=`cat ${BRIGHTNESS_HACK_FILE}`
}
# Writes the last brightness value to the tmp file and forces the device to update
brightness_hack_write() {
echo "setting brightness to ${BRIGHTNESS_HACK_VALUE}"
echo $BRIGHTNESS_HACK_VALUE > ${BRIGHTNESS_HACK_FILE}
sudo /bin/bash -c "echo $BRIGHTNESS_HACK_VALUE > /sys/class/backlight/${BRIGHTNESS_DEVICE_NAME}/brightness"
}
# Read the file
brightness_hack_read
# see if there's input
if [[ "${1}" == "up" ]]; then
brightness_hack_up
fi
if [[ "${1}" == "down" ]]; then
brightness_hack_down
fi
# Write the file and Var
if [[ -z "${1}" ]]; then
brightness_hack_write
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment