选择()不响应在/ dev /输入/写小鼠 [英] select() isn't responding to writing on /dev/input/mice

查看:200
本文介绍了选择()不响应在/ dev /输入/写小鼠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的键盘和鼠标设备文件的程序它通过监视选择()。它等待任何写操作对这些文件,一旦有,一些工作被执行写操作(当有一个键盘或鼠标移动发生这种情况)。

I am writing a program which monitors through select() on the keyboard and mouse device files. It waits for any write operation (this should happen when there is a keystroke or mouse movement) on those files and as soon as there is a write operation, some jobs are executed.

但它不工作。我的code如下。

But it's not working. My code is given below.

#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");
            }
   }
}

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

When I execute the program it produces output like this,

[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

虽然我不是pressing任意键。此外,鼠标设备文件从未被选择()中选择,即使有一个物理鼠标移动。

even though I am not pressing any key. Also, the mouse device file is never selected by select(), even though there is a physical mouse movement.

我在做什么错了?

推荐答案

您需要每选择调用之前重新初始化FD集。所以,在你的程序中的循环将如下所示:

You need to reinitialize your fd sets before every select call. So, the loop in your program would look like:

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
}

此外,每<一href=\"http://stackoverflow.com/questions/12619683/select-isnt-responding-to-writing-on-dev-input-mice#comment17015708_12619683\">User1's对堆栈溢出你同样的问题发表评论,你需要打开设备的阅读,因为你试图读取他们的数据:

Also, per User1's comment on your same question on Stack Overflow, you need to open the devices for reading, because you are trying to read data from them:

// 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)教程手册页与如何使用说明选择和其中一个例子可以作为参考有用的程序。 选择法第11提醒你,你需要在每次调用选择之前,重新初始化FD集<​​/ code>。

Linux includes a select_tut(2) tutorial manpage with explanations of how to use select and an example program which can be useful as a reference. "Select Law" #11 reminds you that you need to reinitialize your fd sets before each call to select.

这篇关于选择()不响应在/ dev /输入/写小鼠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆