More robust logic in case of joystick device lost while the backend is running

This commit is contained in:
Fabio Manganiello 2021-05-16 00:06:20 +02:00
parent 93c3327bcd
commit 6cc28a3c3b
1 changed files with 12 additions and 9 deletions

View File

@ -22,6 +22,9 @@ class JoystickState:
return obj.axes == self.axes and obj.buttons == self.buttons return obj.axes == self.axes and obj.buttons == self.buttons
def __sub__(self, obj) -> dict: def __sub__(self, obj) -> dict:
if len(obj.axes) < len(self.axes) or len(obj.buttons) < len(self.buttons):
return {}
diff = { diff = {
'axes': { 'axes': {
axis: obj.axes[axis] axis: obj.axes[axis]
@ -114,7 +117,7 @@ class JoystickJstestBackend(Backend):
buttons = [] buttons = []
line = '' line = ''
while not self.should_stop(): while os.path.exists(self.device) and not self.should_stop():
ch = self._process.stdout.read(1).decode() ch = self._process.stdout.read(1).decode()
if not ch: if not ch:
continue continue
@ -127,7 +130,7 @@ class JoystickJstestBackend(Backend):
if line.endswith('Axes: '): if line.endswith('Axes: '):
break break
while not self.should_stop() and len(axes) < len(self._state.axes): while os.path.exists(self.device) and not self.should_stop() and len(axes) < len(self._state.axes):
ch = ' ' ch = ' '
while ch == ' ': while ch == ' ':
ch = self._process.stdout.read(1).decode() ch = self._process.stdout.read(1).decode()
@ -135,7 +138,7 @@ class JoystickJstestBackend(Backend):
self._process.stdout.read(len(f'{len(axes)}')) self._process.stdout.read(len(f'{len(axes)}'))
value = '' value = ''
while not self.should_stop(): while os.path.exists(self.device) and not self.should_stop():
ch = self._process.stdout.read(1).decode() ch = self._process.stdout.read(1).decode()
if ch == ' ': if ch == ' ':
if not value: if not value:
@ -152,7 +155,7 @@ class JoystickJstestBackend(Backend):
line = '' line = ''
while not self.should_stop(): while os.path.exists(self.device) and not self.should_stop():
ch = self._process.stdout.read(1).decode() ch = self._process.stdout.read(1).decode()
if not ch: if not ch:
continue continue
@ -161,7 +164,7 @@ class JoystickJstestBackend(Backend):
if line.endswith('Buttons: '): if line.endswith('Buttons: '):
break break
while not self.should_stop() and len(buttons) < len(self._state.buttons): while os.path.exists(self.device) and not self.should_stop() and len(buttons) < len(self._state.buttons):
ch = ' ' ch = ' '
while ch == ' ': while ch == ' ':
ch = self._process.stdout.read(1).decode() ch = self._process.stdout.read(1).decode()
@ -169,7 +172,7 @@ class JoystickJstestBackend(Backend):
self._process.stdout.read(len(f'{len(buttons)}')) self._process.stdout.read(len(f'{len(buttons)}'))
value = '' value = ''
while not self.should_stop(): while os.path.exists(self.device) and not self.should_stop():
ch = self._process.stdout.read(1).decode() ch = self._process.stdout.read(1).decode()
if ch == ' ': if ch == ' ':
continue continue
@ -182,7 +185,7 @@ class JoystickJstestBackend(Backend):
return JoystickState(axes=axes, buttons=buttons) return JoystickState(axes=axes, buttons=buttons)
def _initialize(self): def _initialize(self):
while not self.should_stop() and not self._state: while os.path.exists(self.device) and not self.should_stop() and not self._state:
line = b'' line = b''
ch = None ch = None
@ -249,12 +252,12 @@ class JoystickJstestBackend(Backend):
self._initialize() self._initialize()
for state in self._read_states(): for state in self._read_states():
self._process_state(state)
if not os.path.exists(self.device): if not os.path.exists(self.device):
self.logger.warning(f'Connection to {self.device} lost') self.logger.warning(f'Connection to {self.device} lost')
self.bus.post(JoystickDisconnectedEvent(self.device)) self.bus.post(JoystickDisconnectedEvent(self.device))
break break
self._process_state(state)
finally: finally:
self._process = None self._process = None