Skip to content

Instantly share code, notes, and snippets.

@nilninull
Last active March 6, 2023 13:35
Show Gist options
  • Save nilninull/f366f6feeeb0b73dbaeff59f681cb1d2 to your computer and use it in GitHub Desktop.
Save nilninull/f366f6feeeb0b73dbaeff59f681cb1d2 to your computer and use it in GitHub Desktop.
A mpv script for turn off dpms while playing video automatically
-- save this file to $XDG_CONFIG_HOME/mpv/scripts/
local dpms_mod = nil
-- local function dpms_mod_start(event)
-- print("RUN dpms mod start", dpms_mod)
-- if dpms_mod == nil then
-- -- This code was checked on xset 1.2.3
-- if os.execute("[ `xset q | sed -n '/^DPMS/,${/^ DPMS/s/^ DPMS is //p}'` == Enabled ]") == 0 then
-- dpms_mod = true
-- else
-- dpms_mod = false
-- end
-- end
-- end
-- mp.register_event("video-reconfig", dpms_mod_start)
local function on_property_change(name, value)
print('RUN on property change', value, dpms_mod)
if dpms_mod == nil and value then
-- This code was checked on xset 1.2.3
if os.execute("[ `xset q | sed -n '/^DPMS/,${/^ DPMS/s/^ DPMS is //p}'` == Enabled ]") == 0 then
dpms_mod = true
else
dpms_mod = false
end
end
end
mp.observe_property("vo-configured", "bool", on_property_change)
local function dpms_mod_stop(event)
if dpms_mod then
os.execute("xset +dpms")
end
end
mp.register_event("shutdown", dpms_mod_stop)
local function dpms_on_play(event)
if dpms_mod then
os.execute("xset -dpms")
end
end
mp.register_event("playback-restart", dpms_on_play)
local function dpms_on_pause(name, value)
if dpms_mod then
if value then
os.execute("xset +dpms")
else
os.execute("xset -dpms")
end
end
end
mp.observe_property("pause", "bool", dpms_on_pause)
@hboetes
Copy link

hboetes commented Apr 25, 2021

Wouldn't checking the return value of grep(1) also work? For example

if xset q|grep -q 'DPMS.*Enabled'; then
    echo DPMS is enabled
else
    echo DPMS is disabled
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment