Last active
July 28, 2022 21:42
-
-
Save vdbsh/a9f0723708a4393d42a0d768d831c4df to your computer and use it in GitHub Desktop.
Old-school screensaver functionality for GNOME/Wayland desktops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# afterdark.sh: Old-school screensaver functionality for GNOME/Wayland desktops | |
# This script will launch arbitrary command after specified time period if there is no inhibiting applications running | |
# Started process will be terminated after the first sight of user activity | |
# You supposed to launch something like full-screen video or cmatrix :) | |
start_after=15 # minutes | |
cmd="mpv /home/test/Videos/after_dark.mp4 --fs --loop --no-osc" | |
lock_screen=false | |
get_idle_minutes() { | |
idle_time=$(dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor \ | |
/org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime | grep -Po '(?<=uint64\s)\d+') | |
if [ -n "$idle_time" ]; then | |
echo "$idle_time" / 60000 | bc | |
else | |
echo "0" | |
fi | |
} | |
get_inhibitors() { | |
for i in $(dbus-send --print-reply --dest=org.gnome.SessionManager \ | |
/org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors | grep -Po 'Inhibitor\d+'); do | |
if dbus-send --print-reply --dest=org.gnome.SessionManager \ | |
/org/gnome/SessionManager/"$i" org.gnome.SessionManager.Inhibitor.GetFlags | grep -q 'uint32\s8'; then | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
while :; do | |
if [ "$(get_idle_minutes)" -ge $start_after ] && [ -z "$screensaver" ]; then | |
if ! get_inhibitors; then | |
gnome-session-inhibit $cmd & | |
screensaver=$! | |
fi | |
elif [ "$(get_idle_minutes)" -lt $start_after ] && [ -n "$screensaver" ]; then | |
if $lock_screen; then | |
dbus-send --type=method_call --dest=org.gnome.ScreenSaver \ | |
/org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock | |
fi | |
pkill -P $screensaver | |
screensaver="" | |
fi | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment