random-scripts/swayidle-lock-screen

62 lines
1.3 KiB
Plaintext
Raw Normal View History

2023-09-10 03:11:44 -07:00
#!/usr/bin/env zsh
2023-11-17 22:07:06 -08:00
LOCKFILE="${HOME}/.cache/swayidle-lock-screen.lock"
2023-09-10 03:26:19 -07:00
function run {
2023-11-17 22:07:06 -08:00
# ensure multiple instances do not run
mkdir -p "$(dirname "${LOCKFILE}")"
exec 4<>"${LOCKFILE}"
flock -n 4 || exit 0
2023-10-05 22:38:52 -07:00
dunstctl set-paused true
2023-09-10 03:26:19 -07:00
swayidle -w -C /dev/null \
2023-11-27 17:30:35 -08:00
timeout 15 "${suspend_command}" resume "${wake_command}" &
2023-09-10 18:51:02 -07:00
local swayidle_pid="${!}"
2023-11-14 20:54:20 -08:00
swaylock -e ${img_flags}
2023-09-10 03:26:19 -07:00
kill "${swayidle_pid}"
hyprctl dispatch dpms on
2023-10-05 22:38:52 -07:00
dunstctl set-paused false
2023-11-14 20:54:20 -08:00
fix_eww
2023-11-17 22:07:06 -08:00
flock -u 4
2023-11-14 20:54:20 -08:00
}
function fix_eww {
2023-11-14 22:00:14 -08:00
if (( ${#fix_eww} > 0 )); then
eww open-many ${fix_eww}
fi
2023-09-10 03:26:19 -07:00
}
2023-09-10 18:51:02 -07:00
local background=false
2023-11-27 17:30:35 -08:00
local suspend_command='hyprctl dispatch dpms off'
local wake_command='hyprctl dispatch dpms on'
2023-11-14 20:54:20 -08:00
local fix_eww=()
while [[ "${1}" =~ '^-' ]]; do
case "${1}" in
--)
shift
break
;;
2023-09-10 18:51:02 -07:00
-f)
background=true
2023-11-14 20:54:20 -08:00
;;
-b)
fix_eww+="${2}"
shift
2023-09-10 18:51:02 -07:00
;;
2023-11-27 17:30:35 -08:00
-s)
local suspend_command='sudo /usr/lib/systemd/systemd-sleep suspend &'
;;
2023-09-10 18:51:02 -07:00
-*)
2023-11-14 20:54:20 -08:00
printf "error: unknown flag '%s'\n" "${1}" >&2
2023-09-10 18:51:02 -07:00
;;
esac
2023-11-14 20:54:20 -08:00
shift
2023-09-10 18:51:02 -07:00
done
2023-11-14 20:54:20 -08:00
(( ${#} != 0 )) && img_flags=(-s fill -i "${1}")
2023-09-10 18:51:02 -07:00
if ${background}; then
2023-11-14 20:54:20 -08:00
run '${img_flags}' &
2023-09-10 03:26:19 -07:00
else
2023-11-14 20:54:20 -08:00
run '${img_flags}'
2023-09-10 03:26:19 -07:00
fi