wacom: remove state-based decision of whether to listen or register

Previously, we set self._mode and used that to determine what connection to
attempt. This can sometimes lead to a device attempting to register in
response to a listen() request (and vice versa).

Make this dependent on the caller arguments only. So when listen() is
requested do exactly that and generate the right exception if the device is in
the wrong mode.

Fixes #79 (multiple patches required)
pull/70/head
Peter Hutterer 2018-02-11 20:35:07 +10:00 committed by Benjamin Tissoires
parent 0d825e2e3c
commit 4661cf9ae8
2 changed files with 20 additions and 7 deletions

View File

@ -139,7 +139,10 @@ class TuhiDevice(GObject.Object):
self._wacom_device.connect('notify::uuid', self._on_uuid_updated, bluez_device) self._wacom_device.connect('notify::uuid', self._on_uuid_updated, bluez_device)
self._wacom_device.connect('battery-status', self._on_battery_status, bluez_device) self._wacom_device.connect('battery-status', self._on_battery_status, bluez_device)
self._wacom_device.start(not self.registered) if not self.registered:
self._wacom_device.start_register()
else:
self._wacom_device.start_listen()
def _on_bluez_device_disconnected(self, bluez_device): def _on_bluez_device_disconnected(self, bluez_device):
logger.debug(f'{bluez_device.address}: disconnected') logger.debug(f'{bluez_device.address}: disconnected')

View File

@ -47,6 +47,12 @@ class Protocol(enum.Enum):
INTUOS_PRO = 'intuos-pro' INTUOS_PRO = 'intuos-pro'
@enum.unique
class DeviceMode(enum.Enum):
REGISTER = 1
LISTEN = 2
def signed_char_to_int(v): def signed_char_to_int(v):
return int.from_bytes([v], byteorder='little', signed=True) return int.from_bytes([v], byteorder='little', signed=True)
@ -803,16 +809,18 @@ class WacomDevice(GObject.Object):
logger.info('registration completed') logger.info('registration completed')
self.notify('uuid') self.notify('uuid')
def run(self): def _run(self, *args, **kwargs):
if self._is_running: if self._is_running:
logger.error(f'{self._device.address}: already synching, ignoring this request') logger.error(f'{self._device.address}: already synching, ignoring this request')
return return
mode = args[0]
logger.debug(f'{self._device.address}: starting') logger.debug(f'{self._device.address}: starting')
self._is_running = True self._is_running = True
exception = None exception = None
try: try:
if self._register_mode: if mode == DeviceMode.REGISTER:
self.register_device() self.register_device()
else: else:
assert self._wacom_protocol is not None assert self._wacom_protocol is not None
@ -821,11 +829,13 @@ class WacomDevice(GObject.Object):
logger.error(f'**** Exception: {e} ****') logger.error(f'**** Exception: {e} ****')
exception = e exception = e
finally: finally:
self._register_mode = False
self._is_running = False self._is_running = False
self.emit('done', exception) self.emit('done', exception)
def start(self, register_mode): def start_listen(self):
self._register_mode = register_mode self.thread = threading.Thread(target=self._run, args=(DeviceMode.LISTEN,))
self.thread = threading.Thread(target=self.run) self.thread.start()
def start_register(self):
self.thread = threading.Thread(target=self._run, args=(DeviceMode.REGISTER,))
self.thread.start() self.thread.start()