čtvrtek 14. října 2021

mpv: disable screensaver

By default mpv tries to disable screensaver during the playback. It uses Screen Saver extension (XSS) on X11 and idle-inhibit on Wayland for it. Unfortunately, a lot of desktop environments (DEs) don't support XSS. For them mpv had a hack. It called 'xdg-screensaver reset' in 10 second intervals. This hack was removed in the mpv-0.33.0. Usually, the screensaver can be inhibited through the D-Bus, but mpv upstream don't want to add support for it. It seems they did it mostly because they don't like GNOME. Although I don't like it I understand it - which sane developer likes GNOME :) From the mpv manual page:
GNOME is one of the worst offenders, and ignores even the now widely supported idle-inhibit protocol. (This is either due to a combination of malice and incompetence, but since implementing this protocol would only take a few lines of code, it is most likely the former. You will also notice how GNOME advocates react offended whenever their sabotage is pointed out, which indicates either hypocrisy, or even worse ignorance.) Such incompatible desktop environments (i.e. which ignore standards) typically require using a DBus API. This is ridiculous in several ways. The immediate practical problem is that it would require adding a quite unwieldy dependency for a DBus library, somehow integrating its main‐ loop into mpv, and other generally unacceptable things.
Unfortunately, non GNOME DEs like e.g. Xfce are also affected. Fortunately, another hack exists - the Lua script. Put the following Lua script under your ~/.config/mpv/scripts directory and name it e.g. stop-screensaver.lua:
local timeout_sec = 60

function timeout_fn()
  mp.commandv("run", "/usr/bin/bash", "-c", "/usr/bin/xdg-screensaver reset &>/dev/null")
end

mp.add_periodic_timer(timeout_sec, timeout_fn)
This script will periodically calls the 'xdg-screensaver reset', similarly as the original mpv hack did. You may need customize the timeout - I used 60 seconds, because my screensaver timout is much higher - and maybe the paths to the bash and xdg-screensaver. This script was tested on Fedora 33.