diff options
| author | David Moc <personal@cdatgoose.org> | 2026-05-24 11:48:07 +0200 |
|---|---|---|
| committer | David Moc <personal@cdatgoose.org> | 2026-05-24 11:48:07 +0200 |
| commit | 5fb19f10389b70ee2389755106092a9ef6c64598 (patch) | |
| tree | 969eb0466de581cb14aa09f751a95138620fc720 /polybar/scripts/display-brightness.sh | |
Go
Diffstat (limited to 'polybar/scripts/display-brightness.sh')
| -rwxr-xr-x | polybar/scripts/display-brightness.sh | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/polybar/scripts/display-brightness.sh b/polybar/scripts/display-brightness.sh new file mode 100755 index 0000000..ea3d04c --- /dev/null +++ b/polybar/scripts/display-brightness.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -euo pipefail + +direction="${1:-up}" +state_dir="${XDG_RUNTIME_DIR:-/tmp}" +if [ ! -w "$state_dir" ]; then + state_dir="${XDG_CACHE_HOME:-$HOME/.cache}" + mkdir -p "$state_dir" +fi +if [ ! -w "$state_dir" ]; then + state_dir="/tmp" +fi +state_file="$state_dir/polybar-display-brightness.state" + +export DISPLAY="${DISPLAY:-:0}" +if [ -z "${XAUTHORITY:-}" ] && [ -r "$HOME/.Xauthority" ]; then + export XAUTHORITY="$HOME/.Xauthority" +fi + +output="$( + xrandr --query 2>/dev/null | + awk '/ connected primary/ { print $1; exit } / connected/ && !out { out = $1 } END { if (out) print out }' +)" + +[ -n "$output" ] || exit 0 + +brightness="$(cat "$state_file" 2>/dev/null || printf '1.0')" + +case "$direction" in + up) + brightness="$(awk -v v="$brightness" 'BEGIN { v += 0.05; if (v > 1.00) v = 1.00; printf "%.2f", v }')" + ;; + down) + brightness="$(awk -v v="$brightness" 'BEGIN { v -= 0.05; if (v < 0.30) v = 0.30; printf "%.2f", v }')" + ;; +esac + +printf '%s\n' "$brightness" >"$state_file" +xrandr --output "$output" --brightness "$brightness" |
