博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux获取/dev/input目录下的event对应的设备
阅读量:4283 次
发布时间:2019-05-27

本文共 2742 字,大约阅读时间需要 9 分钟。

当我们在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
主要观察打印信息,Name项是不一样的,我们就可以从这里下手,读取到这个名字,然后在这一类中读取event的值。
[html]
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <unistd.h>  
  6. #include <string.h>  
  7.  #include <sys/mman.h>  
  8.   
  9. //#define DBG_PRINTF(...)    
  10. #define DBG_PRINTF printf  
  11.   
  12. char *command="cat /proc/bus/input/devices > log.txt" ;  
  13. char *file_path="./log.txt";  
  14. char  *file_buff;  
  15.  int number;  
  16. int find_event()  
  17. {  
  18.     int iFd;  
  19.     FILE *tFp;  
  20.     struct stat tStat;  
  21.     char *sub_buff="Handlers=mouse1";    
  22.     /* according  to mouse name find event number*/  
  23.       
  24.     char *buff;  
  25.   
  26.     tFp=fopen(file_path,"r+");    /* check if have log.txt file */  
  27.     if(NULL!=tFp)  
  28.     {  
  29.       fclose(tFp);  
  30.       system("rm log.txt");  
  31.     }  
  32.   
  33.     system(command);  
  34.     /* 打开文件 */  
  35.     tFp = fopen(file_path, "r+");  
  36.     if (tFp == NULL)  
  37.     {  
  38.         DBG_PRINTF("can't open %s\n", file_path);  
  39.         return -1;  
  40.     }  
  41.       
  42.     iFd = fileno(tFp);  
  43.   
  44.     fstat(iFd, &tStat);  
  45.      /* mmap the file to mem */  
  46.     file_buff = (char *)mmap(NULL , tStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, iFd, 0);  
  47.     if (file_buff== (char *)-1)  
  48.     {  
  49.         DBG_PRINTF("mmap error!\n");  
  50.         return -1;  
  51.     }  
  52.     buff=strstr(file_buff,sub_buff);/* in the mem file_buff  find sub_buff name */  
  53.     if(NULL==buff)  
  54.     {  
  55.         DBG_PRINTF("can't find %s\n",sub_buff);  
  56.         munmap(file_buff, tStat.st_size);  
  57.         return -1;  
  58.     } 
  59.     number=*(buff+strlen(sub_buff)+6);/* 6== event */  
  60.     munmap(file_buff, tStat.st_size);  
  61.     fclose(tFp);  
  62.     return  0;  
  63.   
  64. }  
  65.   
  66. int main(int argc, char **argv)  
  67. {  
  68.    find_event();  
  69.   DBG_PRINTF("event%d\n",number-'0');  
  70.   return 0;  
  71. }  
上面的方法,通过查找关键字,可以找到对应的是哪个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来查找。
查看CPU信息:cat /proc/cpuinfo  
  
查看内存信息:cat /proc/meminfo  
  
查看USB设备:cat /proc/bus/usb/devices  
  
查看键盘和鼠标:cat /proc/bus/input/devices  
  
查看各分区使用情况:df  
查看体系结构:busybox uname -a  
  

查看中断信息:cat /proc/interrupts

转载地址:http://vpngi.baihongyu.com/

你可能感兴趣的文章
ARM处理器比较:A8/A9
查看>>
ARM处理器工作模式
查看>>
ARM处理器寄存器
查看>>
汇编语言学习
查看>>
ARM寻址方式
查看>>
uboot工作流程分析
查看>>
不錯的技術論壇
查看>>
GDB 常用參數
查看>>
pthread man page
查看>>
Linux 如何修改 root 密碼
查看>>
nc 傳輸
查看>>
vi 與 vim 的指令整理
查看>>
console & telnet判斷
查看>>
sqlite3
查看>>
關於如何快速切換目錄(Linux)
查看>>
Save Time with minicom macros
查看>>
svn : how to set the executable bit on a file?
查看>>
vim 取代指令
查看>>
git 修改過檔案後,如何commit上server
查看>>
git log 應用
查看>>