Proper initialization for device attribute in parent joystick backend class

This commit is contained in:
Fabio Manganiello 2021-05-15 23:34:41 +02:00
parent f296f4b161
commit b2e2ae9538

View file

@ -87,17 +87,16 @@ class JoystickJstestBackend(JoystickBackend):
:param jstest_path: Path to the ``jstest`` executable that comes with the ``joystick`` system package :param jstest_path: Path to the ``jstest`` executable that comes with the ``joystick`` system package
(default: ``/usr/bin/jstest``). (default: ``/usr/bin/jstest``).
""" """
super().__init__(**kwargs) super().__init__(device=device, **kwargs)
self.device_path = device
self.jstest_path = jstest_path self.jstest_path = jstest_path
self._process: Optional[subprocess.Popen] = None self._process: Optional[subprocess.Popen] = None
self._state: Optional[JoystickState] = None self._state: Optional[JoystickState] = None
def _wait_ready(self): def _wait_ready(self):
self.logger.info(f'Waiting for joystick device on {self.device_path}') self.logger.info(f'Waiting for joystick device on {self.device}')
while not self.should_stop() or not os.path.exists(self.device_path): while not self.should_stop() or not os.path.exists(self.device):
time.sleep(1) time.sleep(1)
if self.should_stop(): if self.should_stop():
@ -243,7 +242,7 @@ class JoystickJstestBackend(JoystickBackend):
self._wait_ready() self._wait_ready()
with subprocess.Popen( with subprocess.Popen(
[self.jstest_path, '--normal', self.device_path], [self.jstest_path, '--normal', self.device],
stdout=subprocess.PIPE) as self._process: stdout=subprocess.PIPE) as self._process:
self.logger.info('Device opened') self.logger.info('Device opened')
self._initialize() self._initialize()
@ -251,9 +250,9 @@ class JoystickJstestBackend(JoystickBackend):
for state in self._read_states(): for state in self._read_states():
self._process_state(state) self._process_state(state)
if not os.path.exists(self.device_path): if not os.path.exists(self.device):
self.logger.warning(f'Connection to {self.device_path} lost') self.logger.warning(f'Connection to {self.device} lost')
self.bus.post(JoystickDisconnectedEvent(self.device_path)) self.bus.post(JoystickDisconnectedEvent(self.device))
break break
finally: finally:
self._process = None self._process = None