From 4661cf9ae820356e1b753804bc8563ef794b2d6c Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Sun, 11 Feb 2018 20:35:07 +1000 Subject: [PATCH] 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) --- tuhi/base.py | 5 ++++- tuhi/wacom.py | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/tuhi/base.py b/tuhi/base.py index c2b9d8a..94b5e08 100644 --- a/tuhi/base.py +++ b/tuhi/base.py @@ -139,7 +139,10 @@ class TuhiDevice(GObject.Object): 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.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): logger.debug(f'{bluez_device.address}: disconnected') diff --git a/tuhi/wacom.py b/tuhi/wacom.py index b4f70b4..62c608c 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -47,6 +47,12 @@ class Protocol(enum.Enum): INTUOS_PRO = 'intuos-pro' +@enum.unique +class DeviceMode(enum.Enum): + REGISTER = 1 + LISTEN = 2 + + def signed_char_to_int(v): return int.from_bytes([v], byteorder='little', signed=True) @@ -803,16 +809,18 @@ class WacomDevice(GObject.Object): logger.info('registration completed') self.notify('uuid') - def run(self): + def _run(self, *args, **kwargs): if self._is_running: logger.error(f'{self._device.address}: already synching, ignoring this request') return + mode = args[0] + logger.debug(f'{self._device.address}: starting') self._is_running = True exception = None try: - if self._register_mode: + if mode == DeviceMode.REGISTER: self.register_device() else: assert self._wacom_protocol is not None @@ -821,11 +829,13 @@ class WacomDevice(GObject.Object): logger.error(f'**** Exception: {e} ****') exception = e finally: - self._register_mode = False self._is_running = False self.emit('done', exception) - def start(self, register_mode): - self._register_mode = register_mode - self.thread = threading.Thread(target=self.run) + def start_listen(self): + self.thread = threading.Thread(target=self._run, args=(DeviceMode.LISTEN,)) + self.thread.start() + + def start_register(self): + self.thread = threading.Thread(target=self._run, args=(DeviceMode.REGISTER,)) self.thread.start()