Last active
July 5, 2024 02:57
-
-
Save fxthomas/9bdfadd972eaf7100b374042faac28c2 to your computer and use it in GitHub Desktop.
Inhibit gnome screensaver for a given program
This file contains hidden or 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
#!/bin/bash | |
# Note: Doesn't work anymore (at least for Gnome 3.34, possibly later; see Python script instead) | |
# Usage: gnome-inhibit <command-line> | |
# Example: gnome-inhibit mpv video.mp4 | |
cookie=$(dbus-send \ | |
--session \ | |
--dest=org.freedesktop.ScreenSaver \ | |
--type=method_call \ | |
--print-reply=literal \ | |
--reply-timeout=20000 \ | |
/org/freedesktop/ScreenSaver \ | |
org.freedesktop.ScreenSaver.Inhibit \ | |
"string:$1" "string:gnome-inhibit" | awk '{print $2}') | |
uninhibit() { | |
dbus-send \ | |
--session \ | |
--dest=org.freedesktop.ScreenSaver \ | |
--type=method_call \ | |
--print-reply=literal \ | |
--reply-timeout=20000 \ | |
/org/freedesktop/ScreenSaver \ | |
org.freedesktop.ScreenSaver.UnInhibit uint32:$cookie | |
echo "Screensaver uninhibited (cookie:$cookie)" | |
} | |
echo "Screensaver inhibited (cookie:$cookie)" | |
trap uninhibit EXIT | |
"$@" |
This file contains hidden or 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/python | |
# coding=utf-8 | |
# Usage: gnome-inhibit <command-line> | |
# Example: gnome-inhibit mpv video.mp4 | |
import subprocess | |
import dbus | |
import sys | |
import os | |
bus = dbus.SessionBus() | |
proxy = bus.get_object('org.freedesktop.ScreenSaver','/org/freedesktop/ScreenSaver') | |
iface = dbus.Interface(proxy, 'org.freedesktop.ScreenSaver') | |
cookie = iface.Inhibit(sys.argv[1], "gnome-inhibit") | |
print("Inhibiting screensaver (pid: %d)" % os.getpid()) | |
print("Executing: %s" % subprocess.list2cmdline(sys.argv[1:])) | |
subprocess.check_call(sys.argv[1:]) |
Updated with a Python version - now apparently the Inhibit is released when the process dies, which means dbus-send
cannot be used.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only place I could find it. Thanks!