4

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?

Community
  • 1
  • 1
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61

2 Answers2

7

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.

user1700934
  • 86
  • 1
  • 2
  • Is there anyway to get the /dev/input/event[n] file after the creation without reading from the filesystem? The new input device passthrough for qemu expects it like this -device virtio-input-host-pci,evdev=/dev/input/eventn – Vans S Aug 25 '16 at 18:37
0

I was in the boat of not having the UI_GET_SYSNAME function work for me (it executed, but returned nothing). Also, I wanted the "event handler path" which is a different (though related) dynamic value. As such, I forced into the ugliness of parsing the /proc/bus/input/devices file.

I posted my bash parser for this on the following StackExchange thread: https://unix.stackexchange.com/questions/82064/how-to-get-the-actual-keyboard-device-given-the-output-of-proc-bus-input-device/507209#507209

That will fetch either of these values for you on demand...

BuvinJ
  • 10,221
  • 5
  • 83
  • 96