问题
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?
回答1:
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
.
回答2:
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...
来源:https://stackoverflow.com/questions/34064047/how-to-get-name-path-of-uinput-created-device