【问题标题】:select() isn't responding to writing on /dev/input/miceselect() 没有响应 /dev/input/mice 上的写入
【发布时间】:2012-09-19 21:40:21
【问题描述】:

我正在编写一个程序,它通过select() 监视键盘和鼠标设备文件。它等待对这些文件的任何写操作(这应该在有击键或鼠标移动时发生),一旦有写操作,就会执行一些作业。

但它不起作用。我的代码如下。

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<linux/input.h>
#include<linux/uinput.h>
#include<sys/time.h>
#include<unistd.h>

void main()
{
    int mouse_fd,kbd_fd,fd_max;
    struct input_event ev;
    fd_set rfs,wfs;

    if((mouse_fd=open("/dev/input/event3",O_WRONLY))==-1)
            {
                printf("opening mouse device file has failed \n");
            }
    else
            {
                printf("opening mouse device file has been successfull \n");
            }
    if((kbd_fd=open("/dev/input/event2",O_WRONLY))==-1)
            {
                printf("opening keyboard device file has failed \n");
            }
    else
        {
            printf("opening keyboard device file has been successfull \n");
        }

    FD_ZERO(&rfs);
    FD_ZERO(&wfs);
    FD_SET(mouse_fd,&rfs);
    FD_SET(kbd_fd,&rfs);
    FD_SET(mouse_fd,&wfs);
    FD_SET(kbd_fd,&wfs);

    if(mouse_fd>kbd_fd)
        {
            fd_max=mouse_fd;
        }
    else
        {
         fd_max=kbd_fd;
        }

    while(1)
    {
        select((fd_max+1),&rfs,NULL,NULL,NULL);
        sleep(2);
        if(FD_ISSET(mouse_fd,&rfs))
            {
                printf("test mouse \n");
            }
        if(FD_ISSET(kbd_fd,&rfs))
            {
                printf("test keyboard \n");
            }
   }
}

当我执行程序时,它会产生这样的输出,

[root@localhost Project]# gcc select.c
[root@localhost Project]# ./a.out
opening mouse device file has been successfull 
opening keyboard device file has been successfull 
test keyboard 
test keyboard 
test keyboard 
test keyboard 
test keyboard 
test keyboard 
test keyboard 
test keyboard 
test keyboard

即使我没有按任何键。此外,即使有物理鼠标移动,也不会通过 select() 选择鼠标设备文件。

我做错了什么?

【问题讨论】:

    标签: io device c input


    【解决方案1】:

    您需要在每次调用 select 之前重新初始化您的 fd 集。因此,您的程序中的循环如下所示:

    while(1)
    {
        FD_ZERO(&rfs);
        FD_ZERO(&wfs);
        FD_SET(mouse_fd, &rfs);
        FD_SET(kbd_fd, &rfs);
        FD_SET(mouse_fd, &wfs);
        FD_SET(kbd_fd, &wfs);
    
        select((fd_max+1),&rfs,NULL,NULL,NULL);
    
        // proceed normally
    }
    

    另外,根据User1's comment on your same question on Stack Overflow,您需要打开设备进行读取,因为您正在尝试从中读取数据:

    // Open device for reading (do you mean to use "/dev/input/mice" here?)
    if ((mouse_fd = open("/dev/input/event3", O_RDONLY)) == -1)
    

    Linux 包含一个 select_tut(2) 教程手册页,其中解释了如何使用 select 和一个示例程序,可用作参考。 “Select Law”#11 提醒您在每次调用 select 之前需要重新初始化 fd 集。

    【讨论】:

    • 先生,我在我的一个程序中遇到了同样的问题,您对此的回答非常有帮助。
    • 那个,或者只使用poll,不需要每次都重新初始化。
    • @mrb....刚刚完成了您的建议...1.每次重新初始化 fd_set & 2.将鼠标设备文件的权限设置为 O-RDONLY
    猜你喜欢
    • 2012-09-19
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多