2022-09-06 23:34:19 -06:00
|
|
|
#!/usr/bin/env bash
|
2023-03-06 12:32:04 -07:00
|
|
|
# Locks the screens and lowers-brightness. Restores brightness on unlock
|
|
|
|
|
|
|
|
# Defaults for levels when restored
|
|
|
|
declare light_restore=3
|
|
|
|
declare ddc_restore=3
|
|
|
|
|
|
|
|
# Levels when dimmed
|
|
|
|
declare -ri light_dim=1
|
|
|
|
declare -ri ddc_dim=1
|
|
|
|
|
|
|
|
get_brightness() {
|
|
|
|
local lvl
|
|
|
|
|
2023-03-06 18:53:23 -07:00
|
|
|
if command -v light &>/dev/null; then
|
2023-03-06 12:32:04 -07:00
|
|
|
lvl="$(light -G)"
|
|
|
|
|
|
|
|
if [[ $lvl =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
|
|
|
|
light_restore="$lvl"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-03-06 18:53:23 -07:00
|
|
|
if command -v ddcutil &>/dev/null; then
|
2023-03-06 12:32:04 -07:00
|
|
|
lvl="$(ddcutil getvcp 10 | awk '{
|
|
|
|
gsub(" ",""); # Remove spaces
|
|
|
|
split($0, a, "=");
|
2023-03-06 18:53:23 -07:00
|
|
|
split(a[2], a, ","); print a[1]
|
2023-03-06 12:32:04 -07:00
|
|
|
}')"
|
|
|
|
|
|
|
|
if [[ $lvl =~ ^[0-9]+$ ]]; then
|
|
|
|
ddc_restore="$lvl"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
set_brightness() {
|
|
|
|
local -r light_lvl="$1"
|
|
|
|
local -r ddc_lvl="$2"
|
|
|
|
|
2023-03-06 18:53:23 -07:00
|
|
|
if command -v light &>/dev/null; then
|
2023-03-06 12:32:04 -07:00
|
|
|
light -S "$light_lvl"
|
2022-09-06 23:34:19 -06:00
|
|
|
fi
|
|
|
|
|
2023-03-06 18:53:23 -07:00
|
|
|
if command -v ddcutil &>/dev/null; then
|
2023-03-06 12:32:04 -07:00
|
|
|
ddcutil setvcp 10 "$ddc_lvl"
|
|
|
|
fi
|
|
|
|
}
|
2022-09-06 23:34:19 -06:00
|
|
|
|
2023-03-06 18:53:23 -07:00
|
|
|
swaylock &
|
|
|
|
|
2023-03-06 12:32:04 -07:00
|
|
|
get_brightness
|
|
|
|
set_brightness $light_dim $ddc_dim
|
2023-03-06 18:53:23 -07:00
|
|
|
|
|
|
|
wait %swaylock
|
|
|
|
set_brightness $light_restore $ddc_restore
|