Skip to content

Instantly share code, notes, and snippets.

@ktec
Last active May 16, 2018 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktec/6efc41613a772f1e2807a14782478342 to your computer and use it in GitHub Desktop.
Save ktec/6efc41613a772f1e2807a14782478342 to your computer and use it in GitHub Desktop.
Script to control keyboard led lights for Macbook Pro on Arch Linux
Controlling the keyboard led lights on a Macbook Pro running linux is harder than it should be.
For ...reasons the bundled `kbdlight` 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:
$ kbdlight -set 10
$ kbdlight -set +10
$ kbdlight -set 10%
$ kbdlight -set +10%
$ kbdlight -inc 10
$ kbdlight -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=="kbdlight", KERNEL=="smc::kbd_backlight", RUN+="/bin/chgrp video /sys/class/leds/%k/brightness"
ACTION=="add", SUBSYSTEM=="kbdlight", KERNEL=="smc::kbd_backlight", RUN+="/bin/chmod g+w /sys/class/leds/%k/brightness"
To add a user to the `video` group:
sudo gpasswd -a <USER> video
#!/bin/sh
#
# Keyboard Backlight 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 "$max/100 * ${1} + ${current}" | bc -l)
set_brightness ${value%.*}
}
set_decrement_by_percentage() {
max=$(get_max_brightness)
current=$(get_brightness)
value=$(echo "$max/100 * ${1} - ${current}" | bc -l)
set_brightness ${value%.*}
}
set_brightness() {
tee /sys/class/leds/smc::kbd_backlight/brightness <<< ${1} &>/dev/null
}
get_brightness() {
cat /sys/class/leds/smc::kbd_backlight/brightness
}
get_max_brightness() {
cat /sys/class/leds/smc::kbd_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