29 lines
767 B
Bash
Executable File
29 lines
767 B
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
BACKLIGHT="/sys/class/backlight"
|
|
DEV=$(ls "$BACKLIGHT" 2>/dev/null | head -n1)
|
|
|
|
gen_output() {
|
|
if [ -z "$DEV" ]; then
|
|
echo '{"percent":0,"available":false}'
|
|
return
|
|
fi
|
|
max=$(cat "$BACKLIGHT/$DEV/max_brightness")
|
|
cur=$(cat "$BACKLIGHT/$DEV/actual_brightness" 2>/dev/null || cat "$BACKLIGHT/$DEV/brightness")
|
|
percent=$(awk -v c="$cur" -v m="$max" 'BEGIN{print int(c/m*100+0.5)}')
|
|
printf '{"percent":%d,"available":true}\n' "$percent"
|
|
}
|
|
|
|
case "$1" in
|
|
set)
|
|
[ -z "$DEV" ] && exit 0
|
|
brightnessctl -d "$DEV" set "${2}%" -q 2>/dev/null
|
|
;;
|
|
*)
|
|
gen_output
|
|
[ -z "$DEV" ] && exit 0
|
|
# Poll for changes every 2s (sysfs files don't support inotify reliably)
|
|
while true; do sleep 2; gen_output; done
|
|
;;
|
|
esac
|