kete: move the MainLoop into the manager so we can watch the bus name
And the basic mainloop code is the same everywhere anyway
This commit is contained in:
parent
44a2d5e8eb
commit
415ecd39a5
41
tuhi-kete.py
41
tuhi-kete.py
|
@ -163,6 +163,13 @@ class TuhiKeteManager(_DBusObject):
|
||||||
ORG_FREEDESKTOP_TUHI1_MANAGER,
|
ORG_FREEDESKTOP_TUHI1_MANAGER,
|
||||||
ROOT_PATH)
|
ROOT_PATH)
|
||||||
|
|
||||||
|
Gio.bus_watch_name(Gio.BusType.SESSION,
|
||||||
|
TUHI_DBUS_NAME,
|
||||||
|
Gio.BusNameWatcherFlags.NONE,
|
||||||
|
None,
|
||||||
|
self._on_name_vanished)
|
||||||
|
|
||||||
|
self.mainloop = GObject.MainLoop()
|
||||||
self._devices = {}
|
self._devices = {}
|
||||||
self._pairable_devices = {}
|
self._pairable_devices = {}
|
||||||
self._searching = False
|
self._searching = False
|
||||||
|
@ -186,6 +193,16 @@ class TuhiKeteManager(_DBusObject):
|
||||||
self.proxy.StopSearch()
|
self.proxy.StopSearch()
|
||||||
self._pairable_devices = {}
|
self._pairable_devices = {}
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
try:
|
||||||
|
self.mainloop.run()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('\r', end='') # to remove the ^C
|
||||||
|
self.mainloop.quit()
|
||||||
|
|
||||||
|
def quit(self):
|
||||||
|
self.mainloop.quit()
|
||||||
|
|
||||||
def _on_properties_changed(self, proxy, changed_props, invalidated_props):
|
def _on_properties_changed(self, proxy, changed_props, invalidated_props):
|
||||||
if changed_props is None:
|
if changed_props is None:
|
||||||
return
|
return
|
||||||
|
@ -211,6 +228,10 @@ class TuhiKeteManager(_DBusObject):
|
||||||
logger.debug('Found pairable device: {}'.format(device))
|
logger.debug('Found pairable device: {}'.format(device))
|
||||||
self.emit('pairable-device', device)
|
self.emit('pairable-device', device)
|
||||||
|
|
||||||
|
def _on_name_vanished(self, connection, name):
|
||||||
|
logger.error('Tuhi daemon went away')
|
||||||
|
self.mainloop.quit()
|
||||||
|
|
||||||
def __getitem__(self, btaddr):
|
def __getitem__(self, btaddr):
|
||||||
return self._devices[btaddr]
|
return self._devices[btaddr]
|
||||||
|
|
||||||
|
@ -225,7 +246,6 @@ class Searcher(GObject.Object):
|
||||||
def __init__(self, manager, address=None):
|
def __init__(self, manager, address=None):
|
||||||
GObject.GObject.__init__(self)
|
GObject.GObject.__init__(self)
|
||||||
self.manager = manager
|
self.manager = manager
|
||||||
self.mainloop = GObject.MainLoop()
|
|
||||||
self.address = address
|
self.address = address
|
||||||
self.is_pairing = False
|
self.is_pairing = False
|
||||||
|
|
||||||
|
@ -234,10 +254,7 @@ class Searcher(GObject.Object):
|
||||||
self.manager.connect('pairable-device', self._on_pairable_device)
|
self.manager.connect('pairable-device', self._on_pairable_device)
|
||||||
self.manager.start_search()
|
self.manager.start_search()
|
||||||
logger.debug('Started searching')
|
logger.debug('Started searching')
|
||||||
try:
|
self.manager.run()
|
||||||
self.mainloop.run()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
self.manager.stop_search()
|
|
||||||
|
|
||||||
if self.manager.searching:
|
if self.manager.searching:
|
||||||
logger.debug('Stopping search')
|
logger.debug('Stopping search')
|
||||||
|
@ -246,7 +263,7 @@ class Searcher(GObject.Object):
|
||||||
def _on_notify_search(self, manager, pspec):
|
def _on_notify_search(self, manager, pspec):
|
||||||
logger.info('Search timeout')
|
logger.info('Search timeout')
|
||||||
if not self.is_pairing:
|
if not self.is_pairing:
|
||||||
self.mainloop.quit()
|
self.manager.quit()
|
||||||
|
|
||||||
def _on_pairable_device(self, manager, device):
|
def _on_pairable_device(self, manager, device):
|
||||||
print('Pairable device: {}'.format(device))
|
print('Pairable device: {}'.format(device))
|
||||||
|
@ -272,6 +289,7 @@ class Listener(GObject.Object):
|
||||||
GObject.GObject.__init__(self)
|
GObject.GObject.__init__(self)
|
||||||
self.mainloop = GObject.MainLoop()
|
self.mainloop = GObject.MainLoop()
|
||||||
|
|
||||||
|
self.manager = manager
|
||||||
self.device = None
|
self.device = None
|
||||||
for d in manager.devices:
|
for d in manager.devices:
|
||||||
if d.address == address:
|
if d.address == address:
|
||||||
|
@ -297,16 +315,13 @@ class Listener(GObject.Object):
|
||||||
self.device.connect('notify::drawings-available', self._on_drawings_available)
|
self.device.connect('notify::drawings-available', self._on_drawings_available)
|
||||||
self.device.start_listening()
|
self.device.start_listening()
|
||||||
|
|
||||||
try:
|
self.manager.run()
|
||||||
self.mainloop.run()
|
logger.debug("{}: stopping listening".format(self.device))
|
||||||
except KeyboardInterrupt:
|
self.device.stop_listening()
|
||||||
print('\r', end='') # to remove the ^C
|
|
||||||
logger.debug("{}: stopping listening".format(self.device))
|
|
||||||
self.device.stop_listening()
|
|
||||||
|
|
||||||
def _on_device_listening(self, device, pspec):
|
def _on_device_listening(self, device, pspec):
|
||||||
logger.info('{}: Listening stopped, exiting'.format(device))
|
logger.info('{}: Listening stopped, exiting'.format(device))
|
||||||
self.mainloop.quit()
|
self.manager.quit()
|
||||||
|
|
||||||
def _on_drawings_available(self, device, pspec):
|
def _on_drawings_available(self, device, pspec):
|
||||||
logger.info('{}: drawings available: {}'.format(device, device.drawings_available))
|
logger.info('{}: drawings available: {}'.format(device, device.drawings_available))
|
||||||
|
|
Loading…
Reference in New Issue