Skip to content

Instantly share code, notes, and snippets.

@auroracramer
Last active September 8, 2022 14:02
Show Gist options
  • Save auroracramer/b8f678bab424ae1cfab81fc741fa67b0 to your computer and use it in GitHub Desktop.
Save auroracramer/b8f678bab424ae1cfab81fc741fa67b0 to your computer and use it in GitHub Desktop.
Startup script to pause audio if you're idle
#!/bin/bash
# Courtesy of https://superuser.com/a/638487
IDLE_TIME_MIN=15
IDLE_TIME_SEC=$((IDLE_TIME_MIN*60))
IDLE_TIME_MS=$((IDLE_TIME_SEC*1000))
sleep_time_ms=$IDLE_TIME_MS
triggered=false
while sleep $(((sleep_time_ms + 999)/1000)); do
# idle=$(xprintidle) # use instead of you're using X window manager instead of Wayland
idle=$(dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor /org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime | tail -n 1 | sed 's/ *$//' | sed 's/^ *//' | cut -d' ' -f 2)
if [[ $idle -ge $IDLE_TIME_MS ]]; then
if ! $triggered ; then
######
# To install on Ubuntu 18.04: https://askubuntu.com/a/1088268
/usr/bin/playerctl pause
######
triggered=true
sleep_time_ms=$IDLE_TIME_MS
fi
else
triggered=false
# Give 100 ms buffer to avoid frantic loops shortly before triggers.
sleep_time_ms=$((IDLE_TIME_MS-idle+100))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment