dotfiles/.local/bin/i3exit

89 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-06-07 11:50:08 +02:00
#!/bin/bash
2022-06-08 21:23:51 +02:00
# i3exit
#
# Provide simple arguments for various locking and power related tasks.
2022-06-07 11:50:08 +02:00
# with openrc use loginctl
[[ $(cat /proc/1/comm) == "systemd" ]] && logind=systemctl || logind=loginctl
2022-06-10 17:30:12 +02:00
LOCK_SCREEN_BLUR=${LOCK_SCREEN_BLUR:-false}
2022-06-07 11:50:08 +02:00
LOCK_SCREEN_COLOR=${LOCK_SCREEN_COLOR:-29414f}
2022-08-03 12:20:09 +02:00
I3LOCK_RE="i3lock -(c|i) "
2022-06-07 11:50:08 +02:00
2022-06-10 17:30:12 +02:00
_lock() {
i3lock -c "$LOCK_SCREEN_COLOR"
}
_unlock() {
2022-08-03 12:20:09 +02:00
pkill -u "$USER" -f "$I3LOCK_RE"
}
is_locked() {
pgrep -u "$USER" -f "$I3LOCK_RE" >/dev/null
2022-06-10 17:30:12 +02:00
}
2022-06-07 11:50:08 +02:00
lock() {
2022-06-10 17:50:07 +02:00
local abort=false screen=/tmp/screenshot.png blurred=/tmp/screenshotblur.png
2022-06-10 17:30:12 +02:00
2022-06-10 17:50:07 +02:00
$LOCK_SCREEN_BLUR && sleep 0.25 && scrot -q 100 $screen # Take screenshot
2022-06-30 12:48:08 +02:00
2022-06-10 17:30:12 +02:00
_lock # Lock immediately
2022-06-30 12:48:08 +02:00
if [ -f $screen ]; then
# Blur screenshot
2022-06-10 17:50:07 +02:00
scrot -q 100 $screen || abort=true # Larger file size, but faster
convert -scale 10% -blur 0x1.25 -resize 1000% $screen $blurred || abort=true
2022-06-10 17:30:12 +02:00
rm $screen
2022-06-10 17:50:07 +02:00
$abort && return 1
2022-06-10 17:30:12 +02:00
# Now try to replace the solid color with the blurred image.
_unlock
i3lock -i $blurred || _lock
2022-06-07 11:50:08 +02:00
fi
2022-06-10 17:30:12 +02:00
# Add a small delay to prevent possible race conditions with suspend
sleep 1
2022-06-07 11:50:08 +02:00
}
case "$1" in
lock)
2022-08-03 12:20:09 +02:00
is_locked || lock
2022-06-07 11:50:08 +02:00
;;
logout)
i3-msg exit
;;
switch_user)
dm-tool switch-to-greeter
;;
suspend)
2022-06-10 17:50:07 +02:00
export LOCK_SCREEN_BLUR=false
2022-06-07 11:50:08 +02:00
lock && $logind suspend
;;
hibernate)
2022-06-10 17:50:07 +02:00
export LOCK_SCREEN_BLUR=false
2022-06-07 11:50:08 +02:00
lock && $logind hibernate
;;
reboot)
$logind reboot
;;
shutdown)
$logind poweroff
;;
commands)
echo "lock,logout,switch_user,suspend,hibernate,reboot,shutdown"
;;
help)
cmds="$($0 commands)"
echo "Usage: $0 ${cmds//\,/\, }"
;;
2022-06-07 11:50:08 +02:00
*)
echo "== ! i3exit: missing or invalid argument ! =="
$0 help
2022-06-07 11:50:08 +02:00
exit 2
2022-06-10 17:30:12 +02:00
;;
2022-06-07 11:50:08 +02:00
esac
exit 0