dotfiles/scripts/.local/bin/lockscreen
Marty Oehme c00cbdbade
services: Add swayidle processing as service
swayidle is now also presented as a user service managed by runit on
voidlinux. It comes with the same defaults as before (300 seconds to
lockscreen, 600 seconds to screen dimming and 900 seconds before
suspending).

Additionally the lockscreen script has been updated to correctly tell a
wayland from a non-wayland session without logind being available on the
system, though it still defaults to using loginctl if it finds it.

The service runs as swayidle in the user services directory and can be
confirured using a 'conf' file which would be placed in the 'swayidle'
service directory. Timeouts can be set with `time_to_lockscreen`,
`time_to_screendim` and `time_to_suspend`.
2025-02-25 23:25:30 +01:00

72 lines
2.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# locker script -- makes sure that x does not give up the lock easily (using i3lock)
# simply locks the screen on wayland (using waylock)
## CONFIGURATION ##############################################################
is_wayland() {
if command -v loginctl && loginctl show-session "$(loginctl show-user "$(whoami)" -p Display --value)" -p Type --value | grep -q wayland; then
true
elif [ "$XDG_SESSION_TYPE" = "wayland" ] || [ -n "$WAYLAND_DISPLAY" ]; then
true
else
false
fi
}
# Options to pass to i3lock
i3lock_options="-e -f -c 1d2021"
waylock_options="-init-color 0x223344 -input-color 0x224444 -fail-color 0x554444 -fork-on-lock"
# Run before starting the locker
pre_lock() {
# pause all currently playing media and mute system
command -v mpc >/dev/null 2>&1 && mpc -q pause
command -v playerctl >/dev/null 2>&1 && playerctl -s pause
command -v amixer >/dev/null 2>&1 && amixer -q set Master mute
# lock any pass coffins if we have them
command -v pass >/dev/null 2>&1 && pass close >/dev/null 2>&1
return
}
# Run after the locker exits
post_lock() {
return
}
###############################################################################
pre_lock
if is_wayland; then
# shellcheck disable=SC2086
waylock $waylock_options
elif [ -e "/dev/fd/${XSS_SLEEP_LOCK_FD:--1}" ]; then
# On X, we set a trap to kill the locker if we get killed, then start the locker and
# wait for it to exit. The waiting is not that straightforward when the locker
# forks, so we use this polling only if we have a sleep lock to deal with.
kill_i3lock() {
pkill -xu "$EUID" "$@" i3lock
}
trap kill_i3lock TERM INT
# we have to make sure the locker does not inherit a copy of the lock fd
i3lock "$i3lock_options" {XSS_SLEEP_LOCK_FD}<&-
# now close our fd (only remaining copy) to indicate we're ready to sleep
exec {XSS_SLEEP_LOCK_FD}<&-
while kill_i3lock -0; do
sleep 0.5
done
else
trap 'kill %%' TERM INT
i3lock -n "$i3lock_options" &
wait
fi
post_lock