Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scan /dev/input for device detection #75

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()