Skip to content

Commit

Permalink
Wait up to two seconds for input device. (#215)
Browse files Browse the repository at this point in the history
It has been reported that even on modern Linux distributions that use
devtmpfs, it can take longer than 0.1 seconds before the input device
becomes available with the right permissions. If sysfs reports that a
specific input device should be used, but it cannot be opened
immediately, keep trying again for up to two seconds.
  • Loading branch information
KarsMulder authored Apr 1, 2024
1 parent c4dee7d commit 5fb3190
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions evdev/uinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,20 @@ def _find_device_linux(self, sysname):
# It is possible that there is some delay before /dev/input/event* shows
# up on old systems that do not use devtmpfs, so if the device cannot be
# found, wait for a short amount and then try again once.
try:
return device.InputDevice(device_path)
except FileNotFoundError:
time.sleep(0.1)
return device.InputDevice(device_path)
#
# Furthermore, even if devtmpfs is in use, it is possible that the device
# does show up immediately, but without the correct permissions that
# still need to be set by udev. Wait for up to two seconds for either the
# device to show up or the permissions to be set.
for attempt in range(19):
try:
return device.InputDevice(device_path)
except (FileNotFoundError, PermissionError):
time.sleep(0.1)

# Last attempt. If this fails, whatever exception the last attempt raises
# shall be the exception that this function raises.
return device.InputDevice(device_path)

def _find_device_fallback(self):
"""
Expand Down

0 comments on commit 5fb3190

Please sign in to comment.