random-scripts/swayidle-lock-screen

97 lines
2.4 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"
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
mkdir -p "$(dirname "${LOCKFILE}")"
exec 4<>"${LOCKFILE}"
flock -n 4 || exit 0
2023-10-05 22:38:52 -07:00
dunstctl set-paused true
if has-rootful-xwayland-p; then
swaylock ${empty_flag} --color '#000000'
else
swayidle -w -C /dev/null \
timeout 15 "${suspend_command}" resume "${wake_command}" &
local swayidle_pid="${!}"
swaylock ${empty_flag} ${img_flags}
kill "${swayidle_pid}"
eval "${wake_command}"
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
;;
2023-11-27 17:30:35 -08:00
-s)
2024-04-26 04:36:22 -07:00
local suspend_command="${2}"
shift
;;
-w)
local wake_command="${2}"
shift
2023-11-27 17:30:35 -08: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
2024-04-26 04:36:22 -07:00
if [[ -z "${suspend_command}" ]]; then
# default to turning off all displays
suspend_command="wlr-randr $(wlr-randr --json |
jq -r '[.[] | .modes = [.modes.[] |
select(.current)]] |
map("--output ''\(.name)'' --off") | join(" ")')"
fi
if [[ -z "${wake_command}" ]]; then
wake_command="wlr-randr $(wlr-randr --json | jq -r '[.[]| select(.enabled) | .modes = [.modes.[] | select(.current)].[]] | map("--output ''\(.name)'' --on --mode ''\(.modes.width)x\(.modes.height)@\(.modes.refresh)Hz'' --pos ''\(.position.x),\(.position.y)'' --transform \(.transform) --scale \(.scale)") | join(" ")')"
fi
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