Files
random-scripts/swayidle-lock-screen
T

85 lines
2.0 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"
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
}
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
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-08-18 19:53:14 -07:00
# get the list of currently disabled monitors so we don't touch them
local ignored=("${(0)$(way-displays-enable -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 \
2025-08-18 19:53:14 -07:00
timeout 15 "way-displays-enable -d ${ignored}" \
resume "way-displays-enable {$ignored}" &
local swayidle_pid="${!}"
swaylock ${empty_flag} ${img_flags}
kill "${swayidle_pid}"
fi
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=()
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'
;;
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