tuhi/tuhi.py

90 lines
2.5 KiB
Python
Raw Normal View History

#!/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
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
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
class Tuhi(GObject.Object):
__gsignals__ = {
"device-added":
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
}
2018-01-12 04:52:10 +01:00
def __init__(self):
GObject.Object.__init__(self)
self.server = TuhiDBusServer(self)
self.server.connect('bus-name-acquired', self._on_bus_name_acquired)
2018-01-12 04:52:10 +01:00
self.bluez = BlueZDeviceManager()
self.bluez.connect('device-added', self._on_device_added)
self.devices = {}
self.drawings = []
def _on_bus_name_acquired(self, dbus_server):
self.bluez.connect_to_bluez()
2018-01-12 04:52:10 +01:00
def _on_device_added(self, manager, device):
if device.vendor_id != WACOM_COMPANY_ID:
return
d = WacomDevice(device)
d.connect('drawing', self._on_drawing_received)
self.devices[device.address] = d
2018-01-12 04:52:10 +01:00
device.connect('connected', self._on_device_connected)
device.connect('disconnected', self._on_device_disconnected)
2018-01-12 04:52:10 +01:00
device.connect_device()
self.emit('device-added', device)
2018-01-12 04:52:10 +01:00
def _on_device_connected(self, device):
logger.debug('{}: connected'.format(device.address))
d = self.devices[device.address]
2018-01-12 04:52:10 +01:00
d.start()
def _on_device_disconnected(self, device):
# FIXME: immediately try to reconnect, at least until the DBusServer
# is hooked up correctly
logger.debug('{}: disconnected'.format(device.address))
device.connect_device()
def _on_drawing_received(self, device, drawing):
logger.debug('Drawing received')
self.drawings.append(drawing)
def main(args):
Tuhi()
try:
GObject.MainLoop().run()
except KeyboardInterrupt:
pass
finally:
2018-01-12 04:52:10 +01:00
pass
if __name__ == "__main__":
main(sys.argv)