backlightctl/backlightctl

42 lines
961 B
Bash
Executable File

#!/bin/sh
set -euf
BACKLIGHT_PATH="/sys/class/backlight/amdgpu_bl0"
errecho() {
>&2 printf '%s\n' "$*"
}
usage() {
errecho "\
Usage: ${0##*/} [-hq] [-s VALUE] [-c VALUE]
No arguments: Print current brightness and exit
If -s and -c are specified, -c takes priority
-h show this help
-q print current brightness to stdout and exit
-s <VALUE> set brightness to VALUE
-c <VALUE> change brightness by VALUE"
exit 0
}
nb=
mb="$(cat "$BACKLIGHT_PATH/max_brightness")"
cb="$(cat "$BACKLIGHT_PATH/brightness")"
while getopts :hqs:c: OPT; do
case $OPT in
h) usage ;;
q) echo "$cb" && exit 0 ;;
s) nb=$OPTARG ;;
c) nb=$((cb + OPTARG)) ;;
*) exit 1 ;;
esac
done
errecho "Current brightness: $cb"
[ -z "$nb" ] && errecho "Brightness not changed" && exit 0
[ "$nb" -gt "$mb" ] && nb=$mb
[ "$nb" -lt 0 ] && nb=0
printf '%s' "$nb" > "$BACKLIGHT_PATH/brightness" && errecho "New brightness: $nb" || errecho "Error setting brightness!"