Search code examples
linuxsysfsxinputevdev

Map XInput2 devices to sysfs nodes


How can I turn an XInput2 device, e.g. as reported by XIQueryDevice, into an appropriate sysfs node? The device is a generic HID device, handled by the evdev input driver.

I know I can get the name of the device. I might be able to look at the Xorg.0.log and try to find the appropriate log message of when this device was added, hoping that it mentions the /dev/input/event* device node associated with that. Or I could look at all input events in sysfs, look for one with that name, and hope that the name is unique and identical with the one reported via XInput. But I hope there is a cleaner solution than either of these.


Solution

  • You can get the Device Id using xinput command. From that you get can get device node path using xinput list-props <device id>. Property 261 is the device node.

    Once you have the device node, you can get the sysfs node path using udevadm info -p $(udevadm info -q path -n <device node path>).

    Lazy oneliner is

     udevadm info  -q path -n $(xinput list-props `xinput | grep "search term" | awk -F "id=" '{print $2}' | awk  '{print $1}'` | grep "261" | awk -F '"' '{print $2}')
    

    `

    To do this programmatically you want to call XIGetProperty with the deviceid from XIDeviceInfo (e.g XIDeviceInfo->deviceid), example calling syntax is here.

    To get the sysfs path from the device path, you use udev_device_new_from_devnum with stat (as demonstrated here), to make a udev_device from the device path and then call udev_device_get_syspath with that udev_device as the argument.