轉自:https://blog.csdn.net/qq_21792169/article/details/51458855html
當咱們在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的值。
- #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;
- }
遇到一樣的問題咱們能夠採起一樣的措施,先映射到內存上,再來查找。也能夠直接使用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 函數