From 53f7255d4ead70091817b4bce425d26671bfe2cb Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Sat, 23 Dec 2023 20:14:11 -0700 Subject: [PATCH] Update: brightness script for external monitors --- sway/brightness_lock.sh | 66 ++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/sway/brightness_lock.sh b/sway/brightness_lock.sh index acb7b91..25ba6dc 100755 --- a/sway/brightness_lock.sh +++ b/sway/brightness_lock.sh @@ -1,28 +1,52 @@ #!/usr/bin/env bash -# Locks the screen and lowers-brightness. Restores brightness on unlock -declare brightness_lvl; +# Locks the screens and lowers-brightness. Restores brightness on unlock -if ! command -v ddcutil &>/dev/null && command -v light &>/dev/null; then - lvl="$(light -G)" +# Defaults for levels when restored +declare light_restore=3 +declare ddc_restore=3 - (swaylock; light -S "$lvl") & - light -S 1 -elif command -v ddcutil &>/dev/null; then - lvl="$(ddcutil getvcp 10 | awk '{ - gsub(" ",""); # Remove spaces - split($0, a, "="); - split(a[2], a, ","); - print a[1] - }')" +# Levels when dimmed +declare -ri light_dim=1 +declare -ri ddc_dim=1 - if [[ "$lvl" =~ ^[0-9]+$ ]]; then - (swaylock; ddcutil setvcp 10 "$lvl") & - ddcutil setvcp 10 1 - else - swaylock +get_brightness() { + local lvl + + if command light &>/dev/null; then + lvl="$(light -G)" + + if [[ $lvl =~ ^[0-9]+(\.[0-9]+)?$ ]]; then + light_restore="$lvl" + fi fi -else - swaylock -fi + if command ddcutil &>/dev/null; then + lvl="$(ddcutil getvcp 10 | awk '{ + gsub(" ",""); # Remove spaces + split($0, a, "="); + split(a[2], a, ","); + print a[1] + }')" + if [[ $lvl =~ ^[0-9]+$ ]]; then + ddc_restore="$lvl" + fi + fi +} + +set_brightness() { + local -r light_lvl="$1" + local -r ddc_lvl="$2" + + if command light &>/dev/null; then + light -S "$light_lvl" + fi + + if command ddcutil &>/dev/null; then + ddcutil setvcp 10 "$ddc_lvl" + fi +} + +get_brightness +(swaylock; set_brightness $light_restore $ddc_restore) & +set_brightness $light_dim $ddc_dim