Skip to content

Commit

Permalink
#540: listen for screensaver dbus messages
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@18240 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Feb 1, 2018
1 parent 1913db0 commit f6117fe
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 24 deletions.
35 changes: 35 additions & 0 deletions src/tests/xpra/test_apps/screensaver_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file is part of Xpra.
# Copyright (C) 2018 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.


try:
#new recommended way of using the glib main loop:
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
except:
#beware: this import has side-effects:
import dbus.glib
assert dbus.glib
import dbus #@Reimport


NAME = "org.freedesktop.ScreenSaver"
PATH = "/org/freedesktop/ScreenSaver"

def main():
from xpra.platform import program_context
with program_context("ScreenSaver-Listener", "ScreenSaver Listener"):
dbus_session = dbus.SessionBus()
def active_changed(active):
print("screensaver active status changed: %s" % (active, ))
dbus_session.add_signal_receiver(active_changed, "ActiveChanged", NAME, path=PATH)
from xpra.gtk_common.gobject_compat import import_glib
glib = import_glib()
loop = glib.MainLoop()
loop.run()


if __name__ == "__main__":
main()
89 changes: 65 additions & 24 deletions src/xpra/platform/xposix/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ def __init__(self, client, _opts):
self.upower_resuming_match = None
self.upower_sleeping_match = None
self.login1_match = None
self.screensaver_match = None
self.x11_filter = None
if client.xsettings_enabled:
self.setup_xprops()
Expand Down Expand Up @@ -721,10 +722,17 @@ def cleanup(self):
self.system_bus = None
if self.upower_resuming_match:
bus._clean_up_signal_match(self.upower_resuming_match)
self.upower_resuming_match = None
if self.upower_sleeping_match:
bus._clean_up_signal_match(self.upower_sleeping_match)
self.upower_sleeping_match = None
if self.login1_match:
bus._clean_up_signal_match(self.login1_match)
self.login1_match = None
if self.session_bus:
if self.screensaver_match:
self.session_bus._clean_up_signal_match(self.screensaver_match)
self.screensaver_match = None
global WINDOW_METHOD_OVERRIDES
WINDOW_METHOD_OVERRIDES = {}

Expand All @@ -747,41 +755,74 @@ def setup_dbus_signals(self):
dbuslog.info(" no support for power events")
return
try:
from xpra.dbus.common import init_system_bus
from xpra.dbus.common import init_system_bus, init_session_bus
except ImportError as e:
dbuslog("setup_dbus_signals()", exc_info=True)
dbuslog.error("Error: dbus bindings are missing,")
dbuslog.error(" cannot setup event listeners:")
dbuslog.error(" %s", e)
return

try:
bus = init_system_bus()
self.system_bus = bus
dbuslog("setup_dbus_signals() system bus=%s", bus)
except Exception as e:
dbuslog("setup_dbus_signals()", exc_info=True)
dbuslog.error("Error setting up dbus signals:")
dbuslog.error(" %s", e)
return
else:
#the UPower signals:
try:
bus_name = 'org.freedesktop.UPower'
dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name))
iface_name = 'org.freedesktop.UPower'
self.upower_resuming_match = bus.add_signal_receiver(self.resuming_callback, 'Resuming', iface_name, bus_name)
self.upower_sleeping_match = bus.add_signal_receiver(self.sleeping_callback, 'Sleeping', iface_name, bus_name)
dbuslog("listening for 'Resuming' and 'Sleeping' signals on %s", iface_name)
except Exception as e:
dbuslog("failed to setup UPower event listener: %s", e)

#the "logind" signals:
try:
bus_name = 'org.freedesktop.login1'
dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name))
def sleep_event_handler(suspend):
if suspend:
self.sleeping_callback()
else:
self.resuming_callback()
iface_name = 'org.freedesktop.login1.Manager'
self.login1_match = bus.add_signal_receiver(sleep_event_handler, 'PrepareForSleep', iface_name, bus_name)
dbuslog("listening for 'PrepareForSleep' signal on %s", iface_name)
except Exception as e:
dbuslog("failed to setup login1 event listener: %s", e)

#the UPower signals:
try:
bus_name = 'org.freedesktop.UPower'
dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name))
iface_name = 'org.freedesktop.UPower'
self.upower_resuming_match = bus.add_signal_receiver(self.resuming_callback, 'Resuming', iface_name, bus_name)
self.upower_sleeping_match = bus.add_signal_receiver(self.sleeping_callback, 'Sleeping', iface_name, bus_name)
dbuslog("listening for 'Resuming' and 'Sleeping' signals on %s", iface_name)
session_bus = init_session_bus()
self.session_bus = session_bus
dbuslog("setup_dbus_signals() session bus=%s", session_bus)
except Exception as e:
dbuslog("failed to setup UPower event listener: %s", e)
dbuslog("setup_dbus_signals()", exc_info=True)
dbuslog.error("Error setting up dbus signals:")
dbuslog.error(" %s", e)
else:
#screensaver signals:
try:
bus_name = "org.freedesktop.ScreenSaver"
iface_name = bus_name
self.screensaver_match = bus.add_signal_receiver(self.ActiveChanged, "ActiveChanged", iface_name, bus_name)
dbuslog("listening for 'ActiveChanged' signal on %s", iface_name)
except Exception as e:
dbuslog.warn("Warning: failed to setup screensaver event listener: %s", e)

def ActiveChanged(self, active):
log("ActiveChanged(%s)", active)
if active:
self.client.suspend()
else:
self.client.resume()

#the "logind" signals:
try:
bus_name = 'org.freedesktop.login1'
dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name))
def sleep_event_handler(suspend):
if suspend:
self.sleeping_callback()
else:
self.resuming_callback()
iface_name = 'org.freedesktop.login1.Manager'
self.login1_match = bus.add_signal_receiver(sleep_event_handler, 'PrepareForSleep', iface_name, bus_name)
dbuslog("listening for 'PrepareForSleep' signal on %s", iface_name)
except Exception as e:
dbuslog("failed to setup login1 event listener: %s", e)

def setup_xprops(self):
#wait for handshake to complete:
Expand Down

0 comments on commit f6117fe

Please sign in to comment.