分享

linux应用层获取鼠标坐标

 mediatv 2012-11-30

linux应用层获取鼠标坐标  

补充:在这个函数中要打开一个鼠标设备,但是在嵌入式的文件系统中没有这个设备节点,
鼠标设备的主设备号和次设备号有是如何确定的呢?
在  linux 系统中,鼠标和键盘的设备号都是固定的,可以在 PC 上查看,再在嵌入式的文件系统中自己建立。

qu@ubuntu:/tmp$ ls -l /dev/input/
total 0
drwxr-xr-x 2 root root     120 2008-11-06 08:49 by-path
crw-rw---- 1 root root 13,  64 2008-11-06 08:49 event0
crw-rw---- 1 root root 13,  65 2008-11-06 08:49 event1
crw-rw---- 1 root root 13,  66 2008-11-06 08:49 event2
crw-rw---- 1 root root 13,  67 2008-11-06 08:49 event3
crw-rw---- 1 root root 13,  68 2008-11-06 08:48 event4
crw-rw---- 1 root root 13,  69 2008-11-06 08:48 event5
crw-rw---- 1 root root 13,  70 2008-11-06 08:48 event6
crw-rw---- 1 root root 13,  63 2008-11-06 16:48 mice
crw-rw---- 1 root root 13,  32 2008-11-06 16:48 mouse0
crw-rw---- 1 root root 13,  33 2008-11-06 08:49 mouse1
crw-rw---- 1 root root 13, 128 2008-11-06 08:49 ts0
crw-rw---- 1 root root 13, 129 2008-11-06 08:49 ts1

鼠标设备一般为  mice  设备,主设备号为  13, 次设备号为  63。
键盘设备一般为  event 设备,主设备号为 13, 次设备号为 64 ~ 70。
备注:以下程序在虚拟机下输出的数据时错误的,需要在板子上跑才会显示正确的相对坐标信息。

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



int main(int argc,char **argv)
{
    int fd, retval;
    char buf[6];
    fd_set readfds;
    struct timeval tv;
 
   if(( fd = open("/dev/input/mice", O_RDONLY))<0)
    {
        printf("Failed to open \"/dev/input/mice\".\n");
        exit(1);
    }
    else
    {
        printf("open \"/dev/input/mice\" successfuly.\n");
    }

    while(1)
    {
        tv.tv_sec = 5;
        tv.tv_usec = 0;

        FD_ZERO(&readfds);
        FD_SET(fd, &readfds);

        retval = select(fd+1, &readfds, NULL, NULL, &tv);
        if(retval==0)
            printf("Time out!\n");
        if(FD_ISSET(fd,&readfds))
        {
            if(read(fd, buf, 6) <= 0)//终端设备,一次只能读取一行
            {
                continue;
            }
            printf("Button type = %d, X = %d, Y = %d, Z = %d\n", (buf[0] & 0x07), buf[1], buf[2],   buf[3]);
        }
    }
    close(fd);
    return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多