如何获取uinput创建的设备的名称(路径) [英] How to get name (path) of uinput created device
问题描述
我已经成功设置了一个小程序来创建uinput 我打算用来自动测试接收键盘输入事件的应用程序的设备.
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.
当我的程序通过调用ioctl(fd, UI_DEV_CREATE)
创建uinput设备时,文件系统中会出现一个新设备,因此我的被测试应用程序可以连接到该设备并等待事件.我的目标系统已经有一个/dev/input/event0
设备,因此新的系统将获得路径/dev/input/event1
.如果我为台式机系统(存在现有设备/dev/input/event[0-15]
)编译并运行该程序,则在运行该程序时,新设备将获得/dev/input/event16
.
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?
推荐答案
是的,如果平台上可用UI_GET_SYSNAME
(在/usr/include/linux/uinput.h
中定义),则可以使用UI_GET_SYSNAME
(例如,Android在某些情况下未对其进行定义)原因).它会为您提供在/sys/devices/virtual/input
中创建的设备的名称.知道 sysfs 中的设备后,您可以通过阅读
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.
像这样调用UI_DEV_CREATE
后使用它(忽略错误/健全性检查):
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);
如果不可用,则可以尝试在/proc/bus/input/devices
中查找sysfs设备,该设备应包含以下条目:
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
..有点混乱.但是正如您所看到的,它还会为您提供在/dev/input
中创建的设备的快捷方式.
..which is a bit messier. But as you can see it'll also give you a shortcut to the device created in /dev/input
.
这篇关于如何获取uinput创建的设备的名称(路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!