Files
random-scripts/swayidle-lock-screen
T

107 lines
2.6 KiB
Bash
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"
2025-11-27 03:12:08 -08:00
ENABLE_DISPLAYS="${HOME}/scripts/enable-displays-for-sleep"
2025-12-14 09:01:20 -08:00
RESET_SAFEEYES="${HOME}/scripts/reset-safeeyes-time"
2023-11-17 22:07:06 -08:00
function has-rootful-xwayland-p {
for pid in $(pgrep Xwayland); do
local cmdline=(${(0)"$(cat /proc/"${pid}"/cmdline)"})
if ! ((${cmdline[(Ie)-rootless]})); then
return 0
fi
done
return 1
}
2026-03-04 15:31:34 -08:00
function safeeyes-running-p {
[[ "$(safeeyes --status)" = Next* ]]
}
2023-09-10 03:26:19 -07:00
function run {
2023-11-17 22:07:06 -08:00
# ensure multiple instances do not run
2025-02-04 21:02:12 -08:00
mkdir -p "${LOCKFILE:h}"
2023-11-17 22:07:06 -08:00
exec 4<>"${LOCKFILE}"
flock -n 4 || exit 0
2023-10-05 22:38:52 -07:00
dunstctl set-paused true
2026-03-04 15:31:34 -08:00
let need_restore_safeeyes=0
if safeeyes-running-p; then
safeeyes --disable
need_restore_safeeyes=1
fi
2025-02-04 21:02:12 -08:00
if [[ "${XDG_CURRENT_DESKTOP}" == 'river' ]] && has-rootful-xwayland-p; then
swaylock ${empty_flag} --color '#000000'
else
2025-11-27 03:12:08 -08:00
local swayidle_pid
if ((dont_sleep)); then
# get the list of currently disabled monitors so we don't touch them
local ignored=("${(0)$("${ENABLE_DISPLAYS}" -l)}")
local ignored_args=()
for name in ${ignored}; do
# quote so it can be safely passed to swayidle
ignored_args+=(-i "${(q)name}")
done
swayidle -w -C /dev/null \
timeout 15 "${(q)ENABLE_DISPLAYS} -d ${ignored_args}" \
resume "${(q)ENABLE_DISPLAYS} {$ignored_args}" &
swayidle_pid="${!}"
else
2025-12-14 09:01:20 -08:00
swayidle -w -C /dev/null \
2026-01-14 03:56:53 -08:00
timeout 15 "systemctl sleep" &
2025-11-27 03:12:08 -08:00
swayidle_pid="${!}"
fi
swaylock ${empty_flag} ${img_flags}
kill "${swayidle_pid}"
fi
2026-03-04 15:31:34 -08:00
(( need_restore_safeeyes )) && safeeyes --enable
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
2024-04-26 04:36:22 -07:00
local suspend_command=''
local wake_command=''
2024-03-20 09:24:42 -07:00
local empty_flag=''
2023-11-14 20:54:20 -08:00
local fix_eww=()
2025-11-27 03:12:08 -08:00
let dont_sleep=0
2023-11-14 20:54:20 -08:00
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
;;
2024-03-20 09:24:42 -07:00
-e)
empty_flag='-e'
;;
2025-11-27 03:12:08 -08:00
-d)
dont_sleep=1
;;
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