Add the signals to expose the device over Tuhi's DBus interface
bus_own_name is asynchronous, so we first need to send a signal back and then we can start connecting to the devices. Otherwise we'll have to implement a queue which would be a lot harder than just waiting.
This commit is contained in:
parent
8e907ba81a
commit
4aca12b6e7
14
tuhi.py
14
tuhi.py
|
@ -26,21 +26,31 @@ WACOM_COMPANY_ID = 0x4755
|
|||
|
||||
|
||||
class Tuhi(GObject.Object):
|
||||
__gsignals__ = {
|
||||
"device-added":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.server = TuhiDBusServer()
|
||||
GObject.Object.__init__(self)
|
||||
self.server = TuhiDBusServer(self)
|
||||
self.server.connect('bus-name-owned', self._on_bus_name_owned)
|
||||
self.bluez = BlueZDeviceManager()
|
||||
|
||||
self.bluez.connect('device-added', self._on_device_added)
|
||||
self.bluez.connect_to_bluez()
|
||||
|
||||
self.drawings = []
|
||||
|
||||
def _on_bus_name_owned(self, dbus_server):
|
||||
self.bluez.connect_to_bluez()
|
||||
|
||||
def _on_device_added(self, manager, device):
|
||||
if device.vendor_id != WACOM_COMPANY_ID:
|
||||
return
|
||||
|
||||
device.connect('connected', self._on_device_connected)
|
||||
device.connect_device()
|
||||
self.emit('device-added', device)
|
||||
|
||||
def _on_device_connected(self, device):
|
||||
logger.debug('{}: connected'.format(device.address))
|
||||
|
|
|
@ -80,17 +80,20 @@ class TuhiDrawing(object):
|
|||
return json.dumps(json_data)
|
||||
|
||||
|
||||
class TuhiDBusDevice(object):
|
||||
class TuhiDBusDevice(GObject.Object):
|
||||
"""
|
||||
Class representing a DBus object for a Tuhi device. This class only
|
||||
handles the DBus bits, communication with the device is done elsewhere.
|
||||
"""
|
||||
def __init__(self, btaddr, connection):
|
||||
self.name = 'UNKNOWN'
|
||||
self.btaddr = btaddr
|
||||
def __init__(self, device, connection):
|
||||
GObject.Object.__init__(self)
|
||||
|
||||
self.name = device.name
|
||||
self.btaddr = device.address
|
||||
self.width, self.height = 0, 0
|
||||
self.drawings = []
|
||||
self.objpath = "{}/{}".format(BASE_PATH, btaddr)
|
||||
objpath = device.address.replace(':', '_')
|
||||
self.objpath = "{}/{}".format(BASE_PATH, objpath)
|
||||
|
||||
self._register_object(connection)
|
||||
|
||||
|
@ -144,17 +147,25 @@ class TuhiDBusDevice(object):
|
|||
return self.drawings[index].json()
|
||||
|
||||
|
||||
class TuhiDBusServer(object):
|
||||
class TuhiDBusServer(GObject.Object):
|
||||
"""
|
||||
Class for the DBus server.
|
||||
"""
|
||||
def __init__(self):
|
||||
__gsignals__ = {
|
||||
"bus-name-owned":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, ()),
|
||||
}
|
||||
|
||||
def __init__(self, tuhi):
|
||||
GObject.Object.__init__(self)
|
||||
self._devices = []
|
||||
self._dbus = Gio.bus_own_name(Gio.BusType.SESSION,
|
||||
BUS_NAME,
|
||||
Gio.BusNameOwnerFlags.NONE,
|
||||
self._bus_aquired,
|
||||
self._bus_name_aquired,
|
||||
self._bus_name_lost)
|
||||
tuhi.connect('device-added', self._on_device_added)
|
||||
|
||||
def _bus_aquired(self, connection, name):
|
||||
introspection = Gio.DBusNodeInfo.new_for_xml(INTROSPECTION_XML)
|
||||
|
@ -165,9 +176,8 @@ class TuhiDBusServer(object):
|
|||
self._method_cb,
|
||||
self._property_read_cb,
|
||||
self._property_write_cb)
|
||||
|
||||
# FIXME this shold be using the btaddr
|
||||
self._devices = [TuhiDBusDevice(0, connection)]
|
||||
self._connection = connection
|
||||
self.emit('bus-name-owned')
|
||||
|
||||
def _bus_name_aquired(self, connection, name):
|
||||
pass
|
||||
|
@ -193,6 +203,10 @@ class TuhiDBusServer(object):
|
|||
def cleanup(self):
|
||||
Gio.bus_unown_name(self._dbus)
|
||||
|
||||
def _on_device_added(self, tuhi, device):
|
||||
dev = TuhiDBusDevice(device, self._connection)
|
||||
self._devices.append(dev)
|
||||
|
||||
|
||||
def main(args):
|
||||
t = TuhiDBusServer()
|
||||
|
|
Loading…
Reference in New Issue