Skip to content
Advertisement

How to get name (path) of uinput created device

I have successfully set up a small program to create a device which I plan to use to automate testing of an application receiving keyboard input events.

I have followed both tutorials as found in this very nice answer.

When my program creates the uinput device by calling ioctl(fd, UI_DEV_CREATE) a new device appears in the file system so my application under test can attach to it and wait for events. My target system already has a /dev/input/event0 device so the new one gets the path /dev/input/event1. If I compile and run the program for my desktop system, where there are existing devices /dev/input/event[0-15], when the program is run the new device gets /dev/input/event16.

I’d like my program to report the new device name after creating it. Is there a way to get it?

Advertisement

Answer

Yes, you can use UI_GET_SYSNAME (defined in /usr/include/linux/uinput.h) if it’s available on your platform (Android, for instance, does not define it for some reason). It will give you a name for the device created in /sys/devices/virtual/input. Once you know the device in sysfs, you can figure out the device(s) created in /dev/input by reading this SO question.

Use it after calling UI_DEV_CREATE like so (omitting error/sanity checking):

JavaScript

If it is not available, you can try looking up the sysfs device in /proc/bus/input/devices which should contain an entry like:

JavaScript

..which is a bit messier. But as you can see it’ll also give you a shortcut to the device created in /dev/input.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement