nnscripts

no-nonsense shell script colection for Linux-based environments
git clone git://git.luxferre.top/nnscripts.git
Log | Files | Refs | README | LICENSE

nnlight.sh (1841B)


      1 #!/bin/sh
      2 # no-nonsense backlight manager (Linux-specific)
      3 # usage: sh nnlight.sh [value|+value|-value|?|daemon|stop-daemon]
      4 # the script must be run as root (unless the daemon is running)
      5 # or your user must have write permissions 
      6 # to the /sys/class/backlight/*/brightness files
      7 # if the daemon is running, it will try to communicate with it instead
      8 # Created by Luxferre in 2023-2024, released into public domain
      9 
     10 RAWVAL="$1"
     11 AWK_CMD=nawk                   # override if you need
     12 DAEMONFIFO="/tmp/nnlight.fifo" # override if you need
     13 DAEMONPID="/tmp/nnlight.pid"   # override if you need
     14 
     15 # attempt to autodetect the backlight driver
     16 # you can override the BRFILE variable manually if it fails
     17 
     18 RBLD="/sys/class/backlight"
     19 BNAME="$(ls -l $RBLD | $AWK_CMD -F / '/LVDS|eDP/{print $NF}')"
     20 BRFILE="$RBLD/$BNAME/brightness"
     21 
     22 DPROC="while true;do read -r b < $DAEMONFIFO && echo \$b > $BRFILE;wait;done"
     23 
     24 CURVAL="$(cat $BRFILE)" # get current brightness
     25 SUBVAL="${RAWVAL#?}" # remove the first char from input
     26 case "$RAWVAL" in
     27   daemon) # start nnlight daemon (requires root)
     28     mkfifo $DAEMONFIFO
     29     chmod a+w $DAEMONFIFO
     30     nohup $SHELL -c "$DPROC" </dev/null 1>/dev/null 2>&1 &
     31     echo "$!" > $DAEMONPID
     32     echo "nnlight daemon started"
     33     ;;
     34   stop-daemon) # stop nnlight daemon (requires root)
     35       kill $(cat $DAEMONPID)
     36       rm -f $DAEMONFIFO $DAEMONPID
     37       echo "nnlight daemon stopped"
     38     ;;
     39   \?) printf "%u\n" "$CURVAL" ;; # show brightness
     40   +*) # increase brightness
     41     [[ -p "$DAEMONFIFO" ]] && BRFILE="$DAEMONFIFO"
     42     echo "$((CURVAL + SUBVAL))" > $BRFILE
     43     ;;
     44   -*) # decrease brightness
     45     [[ -p "$DAEMONFIFO" ]] && BRFILE="$DAEMONFIFO"
     46     echo "$((CURVAL - SUBVAL))" > $BRFILE
     47     ;;
     48   *) # set brightness as user
     49     [[ -p "$DAEMONFIFO" ]] && BRFILE="$DAEMONFIFO"
     50     echo "$RAWVAL" > $BRFILE
     51     ;;
     52 esac
     53