tools: add a --hid toggle to libinput-list-kernel-devices

Lists all SUBSYSTEM=hid devices, including the respective hidraw and
evdev nodes.

Note that this takes a shortcut in the udev handling: in theory we
*should* compare the hidraw/evdev parent device with our hid device. In
practice, checking if the devpath starts with the same substring is good
enough.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2022-12-08 09:59:39 +10:00
parent 1250407c7c
commit e6a5c4e175
2 changed files with 56 additions and 2 deletions

View file

@ -8,11 +8,14 @@ libinput\-list\-kernel\-devices \- list all kernel input devices
The
.B "libinput list\-kernel\-devices"
tool iterates through the list of available kernel devices and prints
their device node and device name.
their device node and device name. By default this tool lists evdev event nodes.
.SH OPTIONS
.TP 8
.B \-\-help
Print help
.TP 8
.B \-\-hid
List HID devices instead of evdev device nodes
.SH NOTES
.PP
A device may be available but not recognized by libinput. This may indicate

View file

@ -55,11 +55,62 @@ def list_devices():
print(f"{k}:\t{devices[k]}")
class HidDevice:
def __init__(self, name, driver, vendor, product, devpath):
self.name = name
self.driver = driver
self.vendor = vendor
self.product = product
self.devpath = devpath
self.hidraws = []
self.evdevs = []
def list_hid_devices():
devices = []
context = pyudev.Context()
for device in context.list_devices(subsystem="hid"):
name = device.properties.get("HID_NAME")
driver = device.properties.get("DRIVER")
devpath = device.properties.get("DEVPATH")
id = device.properties.get("HID_ID") or "0:0:0"
_, vendor, product = (int(x, 16) for x in id.split(":"))
devices.append(HidDevice(name, driver, vendor, product, devpath))
for device in context.list_devices(subsystem="hidraw"):
devpath = device.properties["DEVPATH"]
for hid in devices:
if devpath.startswith(hid.devpath):
hid.hidraws.append(f"'{device.device_node}'")
for device in context.list_devices(subsystem="input"):
if (device.device_node or "").startswith("/dev/input/event"):
devpath = device.properties["DEVPATH"]
for hid in devices:
if devpath.startswith(hid.devpath):
hid.evdevs.append(f"'{device.device_node}'")
print("hid:")
for d in devices:
print(f"- name: '{d.name}'")
print(f" id: '{d.vendor:04x}:{d.product:04x}'")
print(f" driver: '{d.driver}'")
print(f" hidraw: [{', '.join(h for h in d.hidraws)}]")
print(f" evdev: [{', '.join(h for h in d.evdevs)}]")
print("")
def main():
parser = argparse.ArgumentParser(description="List kernel devices")
parser.add_argument("--hid", action="store_true", default=False)
args = parser.parse_args()
list_devices()
if args.hid:
list_hid_devices()
else:
list_devices()
if __name__ == "__main__":