Skip to content

Commit

Permalink
scan /dev/input for device detection (#75)
Browse files Browse the repository at this point in the history
* scan /dev/input for device detection

* fix xinput, filter virtual test devices

* fix xinput, filter virtual test devices
  • Loading branch information
JarbasAl committed Oct 17, 2022
1 parent e70f335 commit c83000d
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions ovos_utils/device_input.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import os
import subprocess
from distutils.spawn import find_executable

from ovos_utils.log import LOG


class InputDeviceHelper:
def __init__(self) -> None:
self.libinput_devices_list = []
self.xinput_devices_list = []
self.dev_input_devices_list = []
if not find_executable("libinput") and not find_executable("xinput"):
raise Exception("xinput/libinput executable not found")
LOG.warning("Could not find libinput or xinput, input device detection will be inaccurate")

# ToDo: add support for discovring the input device based of a connected
# monitors, currently linux only supports input listing directly from the
# system

def _build_linput_devices_list(self):
# Always clear the list before building it
self.libinput_devices_list.clear()
Expand Down Expand Up @@ -76,7 +79,8 @@ def _build_xinput_devices_list(self):
# Get the list of devices from xinput
proc_output = subprocess.check_output(['xinput', 'list']).decode('utf-8')
for line in proc_output.splitlines():
if "id=" not in line:
# skip virtual devices always present
if "↳" not in line or "XTEST" in line:
continue
line = line.replace("↳", "").replace("⎡", "").replace("⎣", "").replace("⎜", "").strip()

Expand All @@ -93,13 +97,28 @@ def _get_xinput_devices_list(self):
self._build_xinput_devices_list()
return self.xinput_devices_list

def _build_dev_input_devices_list(self):
# Always clear the list before building it
self.dev_input_devices_list.clear()

# Get the list of devices from naive scanning of /dev/input
for d in os.listdir("/dev/input"):
if d.startswith("mouse") or d.startswith("mice"):
dev = {"Device": d,
"Capabilities": ["mouse"]}
self.dev_input_devices_list.append(dev)

def get_input_device_list(self):
# check if any of the devices support touch or mouse
if find_executable("libinput"):
self._build_linput_devices_list()
if find_executable("xinput"):
self._build_xinput_devices_list()
return self.libinput_devices_list + self.xinput_devices_list
self._build_dev_input_devices_list()

return self.libinput_devices_list + \
self.xinput_devices_list + \
self.dev_input_devices_list

def can_use_touch_mouse(self):
for device in self.get_input_device_list():
Expand All @@ -123,3 +142,7 @@ def can_use_touch_mouse():

def can_use_keyboard():
return InputDeviceHelper().can_use_keyboard()


if __name__ == "__main__":
can_use_touch_mouse()

0 comments on commit c83000d

Please sign in to comment.