Skip to content

Instantly share code, notes, and snippets.

@ktec
Last active August 21, 2021 05:00
Show Gist options
  • Save ktec/155d4599a79dea985d3bdefde6f87903 to your computer and use it in GitHub Desktop.
Save ktec/155d4599a79dea985d3bdefde6f87903 to your computer and use it in GitHub Desktop.
Script to control the screen brightness (Backlight) for Linux
Controlling the screen brightness on a Macbook Pro running linux is harder than it should be.
For ...reasons the bundled `xbacklight` doesn't work, so we're left with no option but to roll
up our sleaves and get our hands dirty. Fortunately various bash commands and `bc` makes this
task pretty trivial so here is a script to control the brightness.
This script supports the following api:
$ brightness -set 10
$ brightness -set +10
$ brightness -set 10%
$ brightness -set +10%
$ brightness -inc 10
$ brightness -dec 10
As with all things linux, its not as easy as save this to `/usr/local/bin` and you're ready
to go.
By default, only `root` can change the brightness by this method. To allow users in the `video`
group to change the brightness, a `udev` rule such as the following can be used:
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="gmux_backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="gmux_backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="gmux_backlight", RUN+="setpci -v -H1 -s 00:01.00 BRIDGE_CONTROL=0"
To add a user to the `video` group:
sudo gpasswd -a <USER> video
#!/bin/sh
#
# Brightness control for linux.
#
# Author: ktec
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
usage() {
cat <<EOF
Usage: ${0} [options]
-set value
-set value%
-set +value%
-set -value%
-get
-inc value
-inc value%
-dec value
-dec value%
EOF
}
parse_args() {
case ${1} in
\+[0-9]*%) set_increment_by_percentage ${1//[!0-9]/} ;;
\-[0-9]*%) set_decrement_by_percentage ${1//[!0-9]/} ;;
\+[0-9]*) set_increment ${1//[!0-9]/} ;;
\-[0-9]*) set_decrement ${1//[!0-9]/} ;;
[0-9]*%) set_percentage ${1//[!0-9]/} ;;
*) set_brightness ${1} ;;
esac
}
set_increment() {
current=$(get_brightness)
value=$(echo $current + ${1} | bc)
set_brightness $value
}
set_decrement() {
current=$(get_brightness)
value=$(echo $current - ${1} | bc)
set_brightness $value
}
set_percentage() {
max=$(get_max_brightness)
value=$(echo "$max/100 * ${1}" | bc -l)
set_brightness ${value%.*}
}
set_increment_by_percentage() {
max=$(get_max_brightness)
current=$(get_brightness)
value=$(echo "${current} + ($max/100 * ${1})" | bc -l)
set_brightness ${value%.*}
}
set_decrement_by_percentage() {
max=$(get_max_brightness)
current=$(get_brightness)
value=$(echo "${current} - ($max/100 * ${1})" | bc -l)
set_brightness ${value%.*}
}
set_brightness() {
tee /sys/class/backlight/*/brightness <<< ${1} &>/dev/null
}
get_brightness() {
cat /sys/class/backlight/*/brightness
}
get_max_brightness() {
cat /sys/class/backlight/*/max_brightness
}
if [ "$#" -eq "0" ]; then
usage
exit 2
fi
case "$1" in
"-set") parse_args $2;;
"-get") get_brightness;;
"-inc") parse_args "+$2";;
"-dec") parse_args "-$2";;
*) usage;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment