Skip to content

Instantly share code, notes, and snippets.

@Half-Shot
Created April 17, 2023 13:57
Show Gist options
  • Save Half-Shot/a8d9c7b6cdfc46dd7ace7d050787f04c to your computer and use it in GitHub Desktop.
Save Half-Shot/a8d9c7b6cdfc46dd7ace7d050787f04c to your computer and use it in GitHub Desktop.
Autopause notifications when screensharing in Wayland!
#!/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