五、应用层测试代码编写
在应用层编写测试鼠标的测试程序,在我的系统中,鼠标设备为/dev/input/event3. 测试代码如下:
/*
* usb_mouse_test.c
*by lht
*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
int main (void)
{
int fd,i,count;
struct input_event ev_mouse[2];
fd = open ("/dev/input/event3",O_RDWR);
if (fd < 0) {
printf ("fd open failed\n");
exit(0);
}
printf ("\nmouse opened, fd=%d\n",fd);
while(1)
{
printf("...............................................\n");
count=read(fd, ev_mouse, sizeof(struct input_event));
for(i=0;i<(int)count/sizeof(struct input_event);i++)
{
printf("type=%d\n",ev_mouse[i].type);
if(EV_REL==ev_mouse[i].type)
{
printf("time:%ld.%d",ev_mouse[i].time.tv_sec,ev_mouse[i].time.tv_usec);
printf(" type:%d code:%d value:%d\n",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
}
if(EV_KEY==ev_mouse[i].type)
{
printf("time:%ld.%d",ev_mouse[i].time.tv_sec,ev_mouse[i].time.tv_usec);
printf(" type:%d code:%d value:%d\n",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
}
}
}
close (fd);
return 0;
}
运行结果如下:

根据type、code、value的值,可以判断出鼠标的状态,具体值参考include/linux/input.h
