The scripts in this repo were created with one goal in mind:
- To detect user inactivity and pause all media players in the background until the user gets back.
- When the user returns to the computer, all players that were running before, but then got paused, will resume.
- If your system is using wayland, in order for the script to execute, you need to install swayidle.
- If your system is using x11, then you need to install xautolock.
- In case you don't know what display server protocol you are using, don't panic, just copy the following command in your terminal:
$ echo $XDG_SESSION_TYPE- If you would like to use other idle management daemon then the ones used in the scripts, feel free to research and edit the
detect_inactivity.shscript to suit your preference.
I used an interesting command-line utility and library for controlling media players named Playerctl.
Playerctl works on media players that implement the MPRIS D-Bus Interface Specification.
- MPRIS - Media Player Remote Interfacing Specification is a standard D-BUS interface which aims to provide a common programmatic API for controlling media players. Most modern browsers, music apps, such as Spotify, and others are using this interface.
Playerctl comes with a daemon that allows it to act on the currently active media player called playerctld.
This library offers a wide range of commands and options to be used, so more details can be found here: playerctl.
pause_media.sh script checks if there are any active media players running on your computer. If there are, we save them in a MEDIAS list.
- After checking the list to not be empty, we go through every media device that is active, and check for "Playing" status. Save it for later in a
TO_RESTARTlist, and pause it. - We pass the contents of
TO_RESTARTlist to a/tmp/media_to_resumetemporary file. - This will be the first step, after the user idles and is detected as inactive.
resume_media.sh script checks if the /tmp/media_to_resume exists, and then reads the contents of the file into the TO_RESTART list variable.
- We then go through the list and resume all media players, (basically set their status from "Paused" to "Playing"), and then delete the file.
- This step will take place after the user returns from being AFK.
detect_inactivity.sh script runs in a infinite loop, so it will constantly run in the background and check for idles.
- I gave the script the job to find the
pause_media.shandresume_media.shscripts on your system so that you dont have to, but feel free to put your paths instead of thefindcommands forPAUSE_MEDIAandRESUME_MEDIAvariables. - It detects that the user is inactive, by using swayidle for Wayland, and xautolock for X11, which are called idle management daemons. You can edit this script to use your prefered idle manager.
- After a 5 minutes idle, basically inactivity is detected and
pause_media.shscript is executed. After detecting activity again, theresume_media.shis executed and every media player that was active before IDLE will resume to its state.
This way, detect_inactivity.sh script can be used in your config files to detect inactivity and pause media players in the background.