如何确定与uinput设备相对应的文件? [英] How do I determine the files corresponding to a uinput device?

查看:194
本文介绍了如何确定与uinput设备相对应的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在linux中,当创建一个uinput设备时,将在文件系统中创建一个或多个与该设备相对应的事件文件. (例如,如果创建一个uinput鼠标,那么将创建文件/dev/input/mouseN.)但是,如何确定为给定的uinput设备创建的哪些文件? uinput内核模块似乎没有提供任何ioctl来获取该信息.一种可能的方法是在创建uinput设备之后立即轮询文件系统以查看出现了哪些文件,但是该方法不起作用,因为与其他设备(包括真实设备和uinput)的竞争也都在同一时间插入或创建.我是否正在忽略某些东西,还是必须破解内核才能获取此信息?

In linux when a uinput device is created, one or more event files corresponding to that device get created in the file system. (For example, if I create a uinput mouse, then the file /dev/input/mouseN is created.) But how do I determine which files got created for a given uinput device? The uinput kernel module does not appear to provide any ioctl for getting that information. One possible approach is to poll the file system immediately after creating the uinput device to see what files appear, but that approach does not work because of races with other devices, both real and uinput, that are also plugged in or created around the same time. Am I overlooking something, or must I hack the kernel to get this info?

推荐答案

如果您查看 sysfs ,则可以找到您的信息.创建uinput设备后,请执行以下操作:

If you look in sysfs you can find your information. Once you have created your uinput device do:

$ ls /sys/class/input/
event0 event1 ... eventN
input0 input2 ... input19 ... inputN
mouse0 mouse1 ... mouseN
mice

$ ls /sys/devices/virtual/input/
input19 mice

请注意,您可以在其他路径中找到虚拟设备.在这种情况下,input19是我的uinput设备.哪个对应的char设备?

Notice that you can find virtual device in a different path. In this case, input19 is my uinput device. Which is the correspondent char device?

$ ls /sys/devices/virtual/input/input19/
event14 name id ...

我的字符设备是/dev/input/event14.我知道input19是我的uinput设备,因为我是唯一创建uinput设备的用户.如果要确定,则必须阅读其sysfs属性 name 并确认它确实是您的设备

My char device is /dev/input/event14. I know that input19 is my uinput device because I'm the only user who is creating uinput devices. If you want to be sure, you must read its sysfs attribute name and verify that it is really your device

$ cat /sys/devices/virtual/input/input19/name
foo-keyboard-201303261446

您可以通过阅读内核消息来检索有关新uinput设备的信息:

You can retrieve information about your new uinput devices by reading kernel messages:

$ dmesg | tail -n 7
input: foo-keyboard-201303261445 as /devices/virtual/input/input14
input: foo-keyboard-201303261445 as /devices/virtual/input/input15
input: foo-keyboard-201303261445 as /devices/virtual/input/input16
input: foo-keyboard-201303261445 as /devices/virtual/input/input17
input: foo-keyboard-201303261446 as /devices/virtual/input/input18
input: foo-keyboard-201303261446 as /devices/virtual/input/input19
input: foo-keyboard-201303261446 as /devices/virtual/input/input20

您可以从程序中读取/dev/kmsg并捕获事件.也许您可以打开设备/dev/kmsg,刷新它,然后在select()上等待,直到收到uinput通知.

From your program you can read from /dev/kmsg and catch your event. Maybe you can open the device /dev/kmsg, flush it, wait on select() until you receive the uinput notification.

一种替代方法是使用 libudev 来检索您的uinput设备.看一下以下链接: libudev教程

An alternative is to use libudev to retrieve you uinput device. Take a look at the following link: libudev tutorial

更新:由于您的问题,我改进了我在github上可用的libuinput库: libuinput由Federico .我实现了使用hte kmsg 设备的解决方案.

UPDATE: thanks to your question I improved my libuinput library available on github: libuinput by Federico. I implemented the solution that use hte kmsg device.

更新:2014年,对Linux uinput驱动程序进行了改进(git SHA1 e3480a61fc).现在可以使用以下ioctl命令直接从uinput驱动程序获取sysfs路径:

UPDATE: in 2014 the Linux uinput driver has been improved (git SHA1 e3480a61fc). Now it is possible to get the sysfs path directly form the uinput driver using the following ioctl command:

/**
 * UI_GET_SYSNAME - get the sysfs name of the created uinput device
 *
 * @return the sysfs name of the created virtual input device.
 * The complete sysfs path is then /sys/devices/virtual/input/--NAME--
 * Usually, it is in the form "inputN"
 */
#define UI_GET_SYSNAME(len)    _IOC(_IOC_READ, UINPUT_IOCTL_BASE, 300, len)

因此,如果您有可能使用3.13之前的Linux内核,则可以使用上面的ioctl来改进使用uinput的代码.

So if you have the possibility to use a Linux kernel more recent than 3.13, you can use the above ioctl to improve your code that uses uinput.

这篇关于如何确定与uinput设备相对应的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆