From 3649b955c0a91494c391278f63e218dea19d12d1 Mon Sep 17 00:00:00 2001 From: clsr Date: Sat, 23 Apr 2022 19:11:53 +0000 Subject: Add -c option to modify percent to raw mapping curve --- bl | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/bl b/bl index a88eebe..4ebff00 100755 --- a/bl +++ b/bl @@ -1,4 +1,6 @@ -#!/bin/sh +#!/bin/zsh +# vim: set ft=sh: +#shellcheck shell=sh # This software is released into the public domain. # It is provided "as is", without warranties or conditions of any kind. @@ -6,8 +8,9 @@ # backlight brightness configuration utility # -# Requirements: POSIX sh, backlight in /sys/class/backlight/*, -# write permission for $BACKLIGHT/brightness +# Requirements: POSIX sh or zsh, backlight in /sys/class/backlight/*, +# write permission for $BACKLIGHT/brightness, +# one of zsh, bc or qalc (only for -c) set -eu @@ -81,13 +84,57 @@ is_num() { return 1 } +raw_to_num() { + case "$math" in + "") + : $(( num = (raw + (raw_max / 2 / max)) * max / raw_max )) + ;; + zsh) + : $(( num = int(0.5 + (100 - ((1.0 * raw * max / raw_max - 100) / 10) ** 2)) )) + ;; + zshcmd) + num="$(zsh -c "zmodload zsh/mathfunc && echo \$(( int(0.5 + (100 - ((1.0 * $raw * $max / $raw_max - 100) / 10) ** 2)) )) || :")" + ;; + bc) + num="$(printf "%s\n" "100.5 - ((($raw * $max / $raw_max) - 100) / 10) ^ 2" | bc -l)" + num="${num%.*}" + ;; + qalc) + num="$(qalc -t "round((100 - ((($raw * $max / $raw_max) - 100) / 10) ** 2))")" + ;; + esac +} + +num_to_raw() { + case "$math" in + "") + : $(( raw = num * raw_max / max )) + ;; + zsh) + : $(( raw = int(0.5 + ((100 - 10 * sqrt(100 - 1.0 * num)) * raw_max / max)) )) + ;; + zshcmd) + raw="$(zsh -c "zmodload zsh/mathfunc && echo \$(( int(0.5 + ((100 - 10 * sqrt(100 - 1.0 * $num)) * $raw_max / $max)) )) || :")" + ;; + bc) + raw="$(printf "%s\n" "0.5 + (100 - 10 * sqrt(100 - $num)) * $raw_max / $max" | bc -l)" + raw="${raw%.*}" + ;; + qalc) + raw="$(qalc -t "round((100 - 10 * sqrt(100 - $num)) * $raw_max / $max)")" + ;; + esac +} + get_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 )) + raw="$raw_curr" + raw_to_num + curr="$num" # brightness 0 tends to completely turn off the backlight raw_min=1 @@ -126,7 +173,9 @@ get_raw_new() { ;; esac if [ -n "$new" ]; then - : $(( raw_new = new * raw_max / max )) + num="$new" + num_to_raw + raw_new="$raw" if [ "$new" -lt "$curr" ] && [ "$raw_new" -ge "$raw_curr" ]; then : $(( raw_new = raw_curr - 1 )) elif [ "$new" -gt "$curr" ] && [ "$raw_new" -le "$raw_curr" ]; then @@ -150,7 +199,7 @@ set_brightness() { usage() { prog="$(basename "$0")" - printf "%s: usage: %s OPERATOR\n" "$prog" "$prog" + printf "%s: usage: %s [OPTION]... OPERATOR\n" "$prog" "$prog" printf "\n" printf "Operators:\n" printf " (none) print current brightness in percentages\n" @@ -163,12 +212,32 @@ usage() { printf " =-VAL decrease brightness by VAL raw value\n" printf " -L, --list list all found backlights\n" printf "\n" + printf "Options:\n" + printf " -c modify percentage curve to emphasize low raw values\n" + printf "\n" printf "Environment variables:\n" printf " BACKLIGHT the backlight to use (absolute path or a folder\n" printf " in %s or %s)\n" "$sys_backlight" "$sys_leds" } main() { + math= + if [ "${1:-}" = "-c" ]; then + if [ -n "${ZSH_VERSION:-}" ]; then + zmodload zsh/mathfunc + math="zsh" + elif command -v zsh >/dev/null; then + math="zshcmd" + elif command -v bc >/dev/null; then + math="bc" + elif command -v qalc >/dev/null; then + math="qalc" + else + printf "no supported math option (zsh, bc, qalc) found\n" >&2 + return 1 + fi + shift + fi if [ $# -gt 1 ]; then usage >&2 exit 2 @@ -181,6 +250,7 @@ main() { list_backlights exit fi + get_bldir get_brightness op="${1:-}" @@ -190,11 +260,11 @@ main() { -[0-9]*) set_brightness - "${op#-}" ;; [0-9]*) set_brightness "" "$op" ;; =) printf "%d\n" "$raw_curr" ;; - =+[0-9]*) set_brightness "=+" "${op#=+}" ;; + "="+[0-9]*) set_brightness "=+" "${op#=+}" ;; #+=[0-9]*) set_brightness "=+" "${op#+=}" ;; - =-[0-9]*) set_brightness "=-" "${op#=-}" ;; + "="-[0-9]*) set_brightness "=-" "${op#=-}" ;; #-=[0-9]*) set_brightness "=-" "${op#-=}" ;; - =[0-9]*) set_brightness "=" "${op#=}" ;; + "="[0-9]*) set_brightness "=" "${op#=}" ;; *) printf "invalid operator: '%s'\n" "$op" >&2 exit 2 -- cgit