commit fd33a1963add19833014792c9426b12cb82d1133
parent c2ce482035b8b853aa29cf44b18be2cff4c51c6c
Author: Luxferre <lux@ferre>
Date: Fri, 12 Jan 2024 13:01:32 +0200
introduced daemon mode for nnlight
Diffstat:
M | nnlight.sh | | | 42 | ++++++++++++++++++++++++++++++++++-------- |
1 file changed, 34 insertions(+), 8 deletions(-)
diff --git a/nnlight.sh b/nnlight.sh
@@ -1,13 +1,16 @@
#!/bin/sh
# no-nonsense backlight manager (Linux-specific)
-# usage: sh nnlight.sh [value|+value|-value|?]
-# the script must be run as root
+# usage: sh nnlight.sh [value|+value|-value|?|daemon|stop-daemon]
+# the script must be run as root (unless the daemon is running)
# or your user must have write permissions
# to the /sys/class/backlight/*/brightness files
-# Created by Luxferre in 2023, released into public domain
+# if the daemon is running, it will try to communicate with it instead
+# Created by Luxferre in 2023-2024, released into public domain
RAWVAL="$1"
-AWK_CMD=nawk # override if you need
+AWK_CMD=nawk # override if you need
+DAEMONFIFO="/tmp/nnlight.fifo" # override if you need
+DAEMONPID="/tmp/nnlight.pid" # override if you need
# attempt to autodetect the backlight driver
# you can override the BRFILE variable manually if it fails
@@ -16,12 +19,35 @@ RBLD="/sys/class/backlight"
BNAME="$(ls -l $RBLD | $AWK_CMD -F / '/LVDS|eDP/{print $NF}')"
BRFILE="$RBLD/$BNAME/brightness"
+DPROC="while true;do read -r b < $DAEMONFIFO && echo \$b > $BRFILE;wait;done"
+
CURVAL="$(cat $BRFILE)" # get current brightness
SUBVAL="${RAWVAL#?}" # remove the first char from input
case "$RAWVAL" in
- \?) printf "%u\n" "$CURVAL" ;;
- +*) printf "%u" "$((CURVAL + SUBVAL))" > $BRFILE ;;
- -*) printf "%u" "$((CURVAL - SUBVAL))" > $BRFILE ;;
- *) printf "%u" "$RAWVAL" > $BRFILE ;;
+ daemon) # start nnlight daemon (requires root)
+ mkfifo $DAEMONFIFO
+ chmod a+w $DAEMONFIFO
+ nohup $SHELL -c "$DPROC" </dev/null 1>/dev/null 2>&1 &
+ echo "$!" > $DAEMONPID
+ echo "nnlight daemon started"
+ ;;
+ stop-daemon) # stop nnlight daemon (requires root)
+ kill $(cat $DAEMONPID)
+ rm -f $DAEMONFIFO $DAEMONPID
+ echo "nnlight daemon stopped"
+ ;;
+ \?) printf "%u\n" "$CURVAL" ;; # show brightness
+ +*) # increase brightness
+ [[ -p "$DAEMONFIFO" ]] && BRFILE="$DAEMONFIFO"
+ echo "$((CURVAL + SUBVAL))" > $BRFILE
+ ;;
+ -*) # decrease brightness
+ [[ -p "$DAEMONFIFO" ]] && BRFILE="$DAEMONFIFO"
+ echo "$((CURVAL - SUBVAL))" > $BRFILE
+ ;;
+ *) # set brightness as user
+ [[ -p "$DAEMONFIFO" ]] && BRFILE="$DAEMONFIFO"
+ echo "$RAWVAL" > $BRFILE
+ ;;
esac