2018-01-11 06:21:02 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
|
2018-01-12 04:52:10 +01:00
|
|
|
import logging
|
2018-01-11 06:21:02 +01:00
|
|
|
import sys
|
2018-01-12 04:52:10 +01:00
|
|
|
from gi.repository import GObject
|
|
|
|
|
|
|
|
from tuhi.dbusserver import TuhiDBusServer
|
|
|
|
from tuhi.ble import BlueZDeviceManager
|
2018-01-12 06:30:46 +01:00
|
|
|
from tuhi.wacom import WacomDevice
|
2018-01-12 04:52:10 +01:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logger = logging.getLogger('tuhi')
|
|
|
|
|
|
|
|
WACOM_COMPANY_ID = 0x4755
|
|
|
|
|
|
|
|
|
2018-01-15 05:25:16 +01:00
|
|
|
class TuhiDevice(GObject.Object):
|
|
|
|
"""
|
|
|
|
Glue object to combine the backend bluez DBus object (that talks to the
|
|
|
|
real device) with the frontend DBusServer object that exports the device
|
|
|
|
over Tuhi's DBus interface
|
|
|
|
"""
|
|
|
|
__gsignals__ = {
|
|
|
|
"drawings-updated":
|
|
|
|
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, bluez_device, tuhi_dbus_device):
|
|
|
|
GObject.Object.__init__(self)
|
|
|
|
self._tuhi_dbus_device = tuhi_dbus_device
|
|
|
|
self._wacom_device = WacomDevice(bluez_device)
|
|
|
|
self._wacom_device.connect('drawing', self._on_drawing_received)
|
|
|
|
self.drawings = []
|
|
|
|
|
|
|
|
bluez_device.connect('connected', self._on_bluez_device_connected)
|
|
|
|
bluez_device.connect('disconnected', self._on_bluez_device_disconnected)
|
|
|
|
bluez_device.connect_device()
|
|
|
|
|
|
|
|
def _on_bluez_device_connected(self, bluez_device):
|
|
|
|
logger.debug('{}: connected'.format(bluez_device.address))
|
|
|
|
self._wacom_device.start()
|
|
|
|
|
|
|
|
def _on_bluez_device_disconnected(self, bluez_device):
|
|
|
|
# FIXME: immediately try to reconnect, at least until the DBusServer
|
|
|
|
# is hooked up correctly
|
|
|
|
logger.debug('{}: disconnected'.format(bluez_device.address))
|
|
|
|
bluez_device.connect_device()
|
|
|
|
|
|
|
|
def _on_drawing_received(self, device, drawing):
|
|
|
|
logger.debug('Drawing received')
|
|
|
|
self.drawings.append(drawing)
|
|
|
|
self.emit('drawings-updated', self.drawings)
|
|
|
|
|
|
|
|
|
2018-01-12 04:52:10 +01:00
|
|
|
class Tuhi(GObject.Object):
|
2018-01-12 11:18:26 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
"device-added":
|
|
|
|
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
|
2018-01-15 05:25:16 +01:00
|
|
|
"device-connected":
|
|
|
|
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
|
2018-01-12 11:18:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 04:52:10 +01:00
|
|
|
def __init__(self):
|
2018-01-12 11:18:26 +01:00
|
|
|
GObject.Object.__init__(self)
|
2018-01-15 05:25:16 +01:00
|
|
|
self.server = TuhiDBusServer()
|
|
|
|
self.server.connect('bus-name-acquired', self._on_tuhi_bus_name_acquired)
|
2018-01-12 04:52:10 +01:00
|
|
|
self.bluez = BlueZDeviceManager()
|
2018-01-15 05:25:16 +01:00
|
|
|
self.bluez.connect('device-added', self._on_bluez_device_added)
|
2018-01-12 04:52:10 +01:00
|
|
|
|
2018-01-15 03:38:57 +01:00
|
|
|
self.devices = {}
|
2018-01-12 07:18:40 +01:00
|
|
|
|
2018-01-15 05:25:16 +01:00
|
|
|
def _on_tuhi_bus_name_acquired(self, dbus_server):
|
2018-01-12 11:18:26 +01:00
|
|
|
self.bluez.connect_to_bluez()
|
|
|
|
|
2018-01-15 05:25:16 +01:00
|
|
|
def _on_bluez_device_added(self, manager, bluez_device):
|
|
|
|
if bluez_device.vendor_id != WACOM_COMPANY_ID:
|
2018-01-12 04:52:10 +01:00
|
|
|
return
|
|
|
|
|
2018-01-15 05:25:16 +01:00
|
|
|
tuhi_dbus_device = self.server.create_device(bluez_device)
|
|
|
|
d = TuhiDevice(bluez_device, tuhi_dbus_device)
|
|
|
|
self.devices[bluez_device.address] = d
|
2018-01-12 07:18:40 +01:00
|
|
|
|
2018-01-11 06:21:02 +01:00
|
|
|
|
|
|
|
def main(args):
|
2018-01-12 20:15:15 +01:00
|
|
|
Tuhi()
|
2018-01-11 06:21:02 +01:00
|
|
|
try:
|
|
|
|
GObject.MainLoop().run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
finally:
|
2018-01-12 04:52:10 +01:00
|
|
|
pass
|
|
|
|
|
2018-01-11 06:21:02 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|