Linux获取/dev/input目录下的event对应的设备
来源:互联网 发布:限制p2p网络连接数 编辑:程序博客网 时间:2024/05/22 23:40
当我们在Linux操作系统下使用input子系统时,当我们先插鼠标,在插上摄像头与先插摄像头,在插鼠标,操作系统为两个设备分配的event号不是固定的,先插上的是event0,后插上的是event1 。那么问题来了,我们写应用程序,我们怎么知道那个设备对应那个event接口,我们不可能认为指定使用那个接口,因为有时候插播顺序并不一致,下面我用代码来获取event接口。
使用cat /proc/bus/input/devices
I: Bus=0011 Vendor=0001 Product=0001 Version=ab41
N: Name="AT Translated Set 2 keyboard"
P: Phys=isa0060/serio0/input0
S: Sysfs=/devices/platform/i8042/serio0/input/input2
U: Uniq=
H: Handlers=kbd event2
B: EV=120013
B: KEY=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffe
B: MSC=10
B: LED=7
I: Bus=0011 Vendor=0002 Product=0005 Version=0000
N: Name="ImPS/2 Generic Wheel Mouse"
P: Phys=isa0060/serio1/input0
S: Sysfs=/devices/platform/i8042/serio1/input/input3
U: Uniq=
H: Handlers=mouse1 event3
B: EV=7
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=103
N: Name="AT Translated Set 2 keyboard"
P: Phys=isa0060/serio0/input0
S: Sysfs=/devices/platform/i8042/serio0/input/input2
U: Uniq=
H: Handlers=kbd event2
B: EV=120013
B: KEY=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffe
B: MSC=10
B: LED=7
I: Bus=0011 Vendor=0002 Product=0005 Version=0000
N: Name="ImPS/2 Generic Wheel Mouse"
P: Phys=isa0060/serio1/input0
S: Sysfs=/devices/platform/i8042/serio1/input/input3
U: Uniq=
H: Handlers=mouse1 event3
B: EV=7
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=103
主要观察打印信息,Name项是不一样的,我们就可以从这里下手,读取到这个名字,然后在这一类中读取event的值。
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/mman.h>
- //#define DBG_PRINTF(...)
- #define DBG_PRINTF printf
- char *command="cat /proc/bus/input/devices > log.txt" ;
- char *file_path="./log.txt";
- char *file_buff;
- int number;
- int find_event()
- {
- int iFd;
- FILE *tFp;
- struct stat tStat;
- char *sub_buff="Handlers=mouse1";
- /* according to mouse name find event number*/
- char *buff;
- tFp=fopen(file_path,"r+"); /* check if have log.txt file */
- if(NULL!=tFp)
- {
- fclose(tFp);
- system("rm log.txt");
- }
- system(command);
- /* 打开文件 */
- tFp = fopen(file_path, "r+");
- if (tFp == NULL)
- {
- DBG_PRINTF("can't open %s\n", file_path);
- return -1;
- }
- iFd = fileno(tFp);
- fstat(iFd, &tStat);
- /* mmap the file to mem */
- file_buff = (unsigned char *)mmap(NULL , tStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, iFd, 0);
- if (file_buff== (unsigned char *)-1)
- {
- DBG_PRINTF("mmap error!\n");
- return -1;
- }
- buff=strstr(file_buff,sub_buff);/* in the mem file_buff find sub_buff name */
- if(NULL==buff)
- {
- DBG_PRINTF("can't find %s\n",sub_buff);
- munmap(file_buff, tStat.st_size);
- return -1;
- }
- number=*(buff+strlen(sub_buff)+6);/* 6== event */
- munmap(file_buff, tStat.st_size);
- fclose(tFp);
- return 0;
- }
- int main(int argc, char **argv)
- {
- find_event();
- DBG_PRINTF("event%d\n",number-'0');
- return 0;
- }
上面的方法,通过查找关键字,可以找到对应的是哪个event, 即event3. 但如果,应用层需要打开文件路径是 /dev/input/event3时, 则需要如下继续处理一下:
(这部分内容是自己添加的)
char event_node_buf[20] = {0};int number;char lenth;lenth = sprintf(event_node_buf, "/dev/input/event");lenth = sprintf(event_node_buf+lenth, "%d", number-'0');printf("file_node: %s\n", event_node_buf);
遇到同样的问题我们可以采取同样的措施,先映射到内存上,再来查找。也可以直接使用fopen打开文件,然后使用fgets函数来读取到buf中,在使用strstr来查找。
查看内存信息:cat /proc/meminfo
查看USB设备:cat /proc/bus/usb/devices
查看键盘和鼠标:cat /proc/bus/input/devices
查看各分区使用情况:df
查看体系结构:busybox uname -a
查看中断信息:cat /proc/interrupts
阅读全文
0 0
- Linux获取/dev/input目录下的event对应的设备
- Linux获取/dev/input目录下的event对应的设备
- 使用Python获取/dev/input目录下event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input目录下的event对应的设备
- Linux如何查看与/dev/input/event目录下对应的设备
- 如何查看与/dev/input目录下的event对应的设备
- Z-turn Board 中如何查看与/dev/input目录下的event对应的设备
- dev下没有input 目录和event设备节点
- 如何查看/dev/input/下的设备节点对应的是什么设备
- AngularJs的forEach循环
- jquery 无法获取表单数据的问题
- PHP源码分析-数组
- 7.14 vector 南阳理工测评的数据结构题
- java学习【web基础-jQuery入门】
- Linux获取/dev/input目录下的event对应的设备
- va_start和va_end使用详解
- C/C++语言字符串操作总结
- 查找算法
- apk检查更新下载安装
- c# my sql 字段出现0000-00-00 00:00:00时实体转换失败
- Android6.0如何在运行期间申请权限Requesting Permissions at Run Time
- 关于HTTP协议,一篇就够了
- easyui根据值设置Radio选中