Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name (path) of uinput created device

I have successfully set up a small program to create a uinput 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?

like image 634
Antonio Pérez Avatar asked Mar 14 '23 00:03

Antonio Pérez


1 Answers

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):

ioctl(fd, UI_DEV_CREATE);

char sysfs_device_name[16];
ioctl(fd, UI_GET_SYSNAME(sizeof(sysfs_device_name)), sysfs_device_name);
printf("/sys/devices/virtual/input/%s\n", sysfs_device_name);

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

I: Bus=0006 Vendor=0001 Product=0001 Version=0001
N: Name="your-uinput-device-name"
P: Phys=
S: Sysfs=/devices/virtual/input/input12
U: Uniq=
H: Handlers=sysrq kbd mouse0 event11 
B: PROP=0
B: EV=7
B: KEY=70000 0 0 0 0 0 7ffff ffffffff fffffffe
B: REL=143

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

like image 70
user1700934 Avatar answered Mar 27 '23 01:03

user1700934