Skip to content

Instantly share code, notes, and snippets.

@Half-Shot
Created April 23, 2024 15:05
Show Gist options
  • Save Half-Shot/999ff8c393f9ea8bd2ecca2995ef400d to your computer and use it in GitHub Desktop.
Save Half-Shot/999ff8c393f9ea8bd2ecca2995ef400d to your computer and use it in GitHub Desktop.
Mute notifications whenever a screenshare is detected (assumes you use dunstctl / org.freedesktop.impl.portal.ScreenCast), but can be adapted.
#!/usr/bin/env python3
import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
import threading
import os
def main():
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
obj_dbus = bus.get_object('org.freedesktop.DBus',
'/org/freedesktop/DBus')
obj_dbus.BecomeMonitor(["interface='org.freedesktop.impl.portal.Session'", "interface='org.freedesktop.impl.portal.ScreenCast'"],
dbus.UInt32(0))
bus.add_message_filter(msg_cb)
print("Started up, listening on dbus for sessions")
mainloop = GLib.MainLoop()
mainloop.run()
def msg_cb(bus, msg):
member = msg.get_member()
write_opt = None
try:
if member == 'Start':
print("Detected started screenshare")
write_opt = True
elif member == "Close":
print("Detected end of screensharing")
write_opt = False
if write_opt is not None:
# Don't judge me, you try doing a dbus call in another thread without
# python complaining.
os.system(f"dunstctl set-paused {str(write_opt).lower()}")
except Exception as ex:
print("Encountered an error while trying to set the paused flag", ex)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment