Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

143 lines
4.0KB

  1. #!/usr/bin/python3
  2. import os
  3. import sys
  4. import time
  5. import dbus
  6. import dbus.service
  7. import dbus.mainloop.glib
  8. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  9. from gi.repository import GObject as gobject
  10. # DBus connection information for MATE screensaver
  11. DBUS_MSCR_WAIT = 60
  12. DBUS_MSCR_DBUS = dbus.SessionBus()
  13. DBUS_MSCR_NAME = "org.mate.ScreenSaver"
  14. DBUS_MSCR_PATH = "/"
  15. DBUS_MSCR_IFACE_MAIN = "org.mate.ScreenSaver"
  16. # DBus listening information for the Idle Inhibition Spec
  17. DBUS_IIS_DBUS = dbus.SessionBus()
  18. DBUS_IIS_NAME = "org.freedesktop.ScreenSaver"
  19. DBUS_IIS_PATH = "/org/freedesktop/ScreenSaver"
  20. DBUS_IIS_PATH2= "/ScreenSaver"
  21. DBUS_IIS_IFACE_MAIN = "org.freedesktop.ScreenSaver"
  22. # Connect to the system and session bus
  23. bus_system = dbus.SystemBus()
  24. bus_session = dbus.SessionBus()
  25. # Connect to MATE screensaver
  26. print("Waiting for screensaver...")
  27. for i in range(DBUS_MSCR_WAIT):
  28. try:
  29. mate_screensaver = dbus.Interface(
  30. bus_session.get_object(DBUS_MSCR_NAME, DBUS_MSCR_PATH),
  31. DBUS_MSCR_IFACE_MAIN
  32. )
  33. except dbus.exceptions.DBusException:
  34. # Screensaver not running yet
  35. if i < (DBUS_MSCR_WAIT - 1):
  36. time.sleep(1)
  37. else:
  38. print("MATE screensaver not running, aborting!", file=sys.stderr)
  39. sys.exit(2)
  40. else:
  41. print("MATE screensaver connected!")
  42. break
  43. class IdleInhibitor(object):
  44. """
  45. Represents a single client that inhibits the screensaver
  46. """
  47. def __init__(self, screensaver, name, reason, cookie, unique_name):
  48. self.screensaver = screensaver
  49. self.name = name
  50. self.reason = reason
  51. self.cookie = cookie
  52. self.unique_name = unique_name
  53. # Tell MATE screensaver about the application we are proxying
  54. self._cookie = self.screensaver.Inhibit(name, reason)
  55. def destroy(self):
  56. # Stop bothering MATE screensaver
  57. self.screensaver.UnInhibit(self._cookie)
  58. # Create DBus interface
  59. class IdleInhibitionService(dbus.service.Object):
  60. def __init__(self, mate_screensaver):
  61. # Store connection to screensaver
  62. self.screensaver = mate_screensaver
  63. # Counter for inhibition requests (used for cookies)
  64. self.counter = 1
  65. # Storage for clients
  66. self.inhibitors = []
  67. # Store constants
  68. self.bus = DBUS_IIS_DBUS
  69. self.path = DBUS_IIS_PATH
  70. self.path2 = DBUS_IIS_PATH2
  71. # Claim bus name with both paths used by GNOME
  72. dbus.service.Object.__init__(self,
  73. self.bus, self.path, dbus.service.BusName(DBUS_IIS_NAME, self.bus)
  74. )
  75. dbus.service.Object.__init__(self,
  76. self.bus, self.path2, dbus.service.BusName(DBUS_IIS_NAME, self.bus)
  77. )
  78. def on_name_change(self, sender, unique_name):
  79. print("mate-screensaver-helper-inhibition: on_name_change: '%s', '%s'" % (sender, unique_name))
  80. if sender != unique_name:
  81. for inhibitor in self.inhibitors:
  82. if inhibitor.unique_name == sender:
  83. self.UnInhibit(inhibitor.cookie, sender)
  84. @dbus.service.method(DBUS_IIS_IFACE_MAIN, "ss", "u", sender_keyword="sender")
  85. def Inhibit(self, application_name, reason_for_inhibit, sender):
  86. print("mate-screensaver-helper-inhibition: Inhibit: '%s', '%s' -> %d (from '%s')"
  87. % (application_name, reason_for_inhibit, self.counter + 1, sender))
  88. # Create new cookie
  89. self.counter += 1
  90. # Add inhibitor
  91. self.inhibitors.append(IdleInhibitor(
  92. self.screensaver,
  93. application_name,
  94. reason_for_inhibit,
  95. self.counter,
  96. sender
  97. ))
  98. # Watch if bus name owner disappears
  99. self.bus.watch_name_owner(sender, lambda name: self.on_name_change(sender, name))
  100. return self.counter
  101. @dbus.service.method(DBUS_IIS_IFACE_MAIN, "u", "", sender_keyword="sender")
  102. def UnInhibit(self, cookie, sender):
  103. print("mate-screensaver-helper-inhibition: UnInhibit: %d (from '%s')" % (cookie, sender))
  104. for inhibitor in self.inhibitors:
  105. if inhibitor.cookie == cookie and inhibitor.unique_name == sender:
  106. # Stop inhibiting
  107. inhibitor.destroy()
  108. # Remove inhibitor from list
  109. self.inhibitors.remove(inhibitor)
  110. # Create service
  111. service = IdleInhibitionService(mate_screensaver)
  112. # Wait for stuff to happen
  113. gobject.MainLoop().run()