Skip to content

Commit

Permalink
scan /dev/input for device detection
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Oct 17, 2022
1 parent e70f335 commit 94a5777
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 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 @@ -93,13 +96,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 +141,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 94a5777

Please sign in to comment.