Skip to content

Instantly share code, notes, and snippets.

@fxthomas
Last active March 16, 2024 12:51
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fxthomas/9bdfadd972eaf7100b374042faac28c2 to your computer and use it in GitHub Desktop.
Save fxthomas/9bdfadd972eaf7100b374042faac28c2 to your computer and use it in GitHub Desktop.
Inhibit gnome screensaver for a given program
#!/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
"$@"
#!/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:])
@ChadBailey
Copy link

Excellent clean example of how to do this, thanks for sharing!

@maztan
Copy link

maztan commented Aug 22, 2019

Only place I could find it. Thanks!

@fxthomas
Copy link
Author

fxthomas commented Dec 3, 2019

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