From dd26c2292ec7ca5385c99738f390ee7015451c4a Mon Sep 17 00:00:00 2001 From: clsr Date: Sun, 19 Jul 2020 22:31:12 +0200 Subject: Support leds and absolute paths in $BACKLIGHT, add --list --- bl | 92 +++++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 30 deletions(-) diff --git a/bl b/bl index 7db821e..c928519 100755 --- a/bl +++ b/bl @@ -12,33 +12,58 @@ set -eu sys_backlight="/sys/class/backlight" +sys_leds="/sys/class/leds" + +find_backlight() { + for f in "$sys_backlight"/*; do + if [ "$f" = "$sys_backlight/*" ]; then + printf "cannot find a backlight in %s\n" "$sys_backlight" >&2 + exit 1 + elif [ -n "$bldir" ]; then + printf "multiple backlights found, choose one with \$BACKLIGHT\n" >&2 + exit 1 + fi + bldir="$f" + done +} + +is_backlight() { + [ -d "$1" ] && [ -f "$1/brightness" ] && [ -f "$1/max_brightness" ] +} + +list_backlights() { + for f in "$sys_backlight"/* "$sys_leds"/*; do + if is_backlight "$f"; then + printf "%s\n" "${f##*/}" + fi + done +} get_bldir() { bldir= - if [ -n "${BACKLIGHT:-}" ]; then - bldir="$sys_backlight/$BACKLIGHT" - else - for f in "$sys_backlight"/*; do - if [ "$f" = "$sys_backlight/*" ]; then - printf "cannot find a backlight in %s\n" "$sys_backlight" >&2 - exit 1 - elif [ -n "$bldir" ]; then - printf "multiple backlights found, choose one with \$BACKLIGHT\n" >&2 + case "${BACKLIGHT:-}" in + /*) bldir="$BACKLIGHT" ;; + "") find_backlight ;; + *) + if is_backlight "$sys_backlight/$BACKLIGHT"; then + bldir="$sys_backlight/$BACKLIGHT" + elif is_backlight "$sys_leds/$BACKLIGHT"; then + bldir="$sys_leds/$BACKLIGHT" + else + printf "no such backlight found: '%s'\n" "$BACKLIGHT" >&2 exit 1 fi - bldir="$f" - BACKLIGHT="${f##*/}" - done - fi + ;; + esac + blname="${bldir##*/}" } read_num() { - read -r data <"$1" - if ! is_num "$data"; then - printf "expected a number reading '%s', but got '%s'\n" "$1" "$data" >&2 + read -r num <"$1" + if ! is_num "$num"; then + printf "expected a number reading '%s', but got '%s'\n" "$1" "$num" >&2 exit 1 fi - printf %d "$data" } is_num() { @@ -57,14 +82,16 @@ is_num() { } get_brightness() { - raw_max="$(read_num "$bldir/max_brightness")" - raw_curr="$(read_num "$bldir/brightness")" + read_num "$bldir/max_brightness" + raw_max="$num" + read_num "$bldir/brightness" + raw_curr="$num" max=100 curr=$(( (raw_curr + (raw_max / 2 / max)) * max / raw_max )) # brightness 0 tends to completely turn off the backlight raw_min=1 - case "$BACKLIGHT" in + case "$blname" in nvidia_[0-9]*) raw_min=0 ;; # nvidia_0 at 0 is still not off esac } @@ -115,18 +142,19 @@ usage() { printf "%s: usage: %s OPERATOR\n" "$prog" "$prog" printf "\n" printf "Operators:\n" - printf " (none) print current brightness in percentages\n" - printf " PERC set brightness to PERC percentages of max\n" - printf " +PERC increase brightness by PERC percentage points\n" - printf " -PERC decrease brightness by PERC percentage points\n" - printf " = print current brightness raw value\n" - printf " VAL set brightness to VAL raw value\n" - printf " =+VAL increase brightness by VAL raw value\n" - printf " =-VAL decrease brightness by VAL raw value\n" + printf " (none) print current brightness in percentages\n" + printf " PERC set brightness to PERC percentages of max\n" + printf " +PERC increase brightness by PERC percentage points\n" + printf " -PERC decrease brightness by PERC percentage points\n" + printf " = print current brightness raw value\n" + printf " VAL set brightness to VAL raw value\n" + printf " =+VAL increase brightness by VAL raw value\n" + printf " =-VAL decrease brightness by VAL raw value\n" + printf " -L, --list list all found backlights\n" printf "\n" printf "Environment variables:\n" - printf " BACKLIGHT the backlight in %s/ to use\n" "$sys_backlight" - printf " (unnecessary if only one exists)\n" + printf " BACKLIGHT the backlight to use (absolute path or a folder\n" + printf " in %s or %s)\n" "$sys_backlight" "$sys_leds" } main() { @@ -138,6 +166,10 @@ main() { usage exit fi + if [ "${1:-}" = "-L" ] || [ "${1:-}" = "--list" ]; then + list_backlights + exit + fi get_bldir get_brightness op="${1:-}" -- cgit