tools: add a --hid toggle to libinput-list-kernel-devices
authorPeter Hutterer <peter.hutterer@who-t.net>
Wed, 7 Dec 2022 23:59:39 +0000 (09:59 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Thu, 8 Dec 2022 00:08:44 +0000 (10:08 +1000)
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>
tools/libinput-list-kernel-devices.man
tools/libinput-list-kernel-devices.py

index b6dc685c33d73eb63dcca1a3faa72047249bbe7e..b389d265e9d4dd6728d88c10a618208dc88135d4 100644 (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
index 95caa08b667c256351435dd89d26775f9bfd5b78..10fc4d1c61703f255cb2d4429c6e77ad6eda04d0 100755 (executable)
@@ -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__":