码迷,mamicode.com
首页 > 系统相关 > 详细

linux的Input子系统获取Device Name

时间:2015-08-14 18:27:57      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

linux内核提供了一个Input子系统来实现的,Input子系统会在/dev/input/路径下创建我们硬件输入设备的节点,一般情况下在我们的 手机中这些节点是以eventXX来命名的,如event0,event1等等,可以利用EVIOCGNAME获取此事件结点名称。这就是android 中对于input事件处理数据的来源点。


---------------


通过ioctl命令EVIOCGNAME,能获取dev/input/event*对应的Device Name。

 

实例:


openInput(const char* inputName) {
    int fd = -1;
    const char *dirname = "/dev/input";
    char devname[PATH_MAX];
    char *filename;
    DIR *dir;
    struct dirent *de;
    dir = opendir(dirname);
    if(dir == NULL)
        return -1;
    strcpy(devname, dirname);
    filename = devname + strlen(devname);
    *filename++ = ‘/‘;
    while((de = readdir(dir))) {
        if(de->d_name[0] == ‘.‘ &&
                (de->d_name[1] == ‘\0‘ ||
                        (de->d_name[1] == ‘.‘ && de->d_name[2] == ‘\0‘)))
            continue;
        strcpy(filename, de->d_name);
        fd = open(devname, O_RDONLY);
        if (fd>=0) {
            char name[80];
            if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
                name[0] = ‘\0‘;
            }
            if (!strcmp(name, inputName)) {
                break;
            } else {
                close(fd);
                fd = -1;
            }
        }
    }
    closedir(dir);
    LOGE_IF(fd<0, "couldn‘t find ‘%s‘ input device", inputName);
    LOGI_IF(fd>=0, "Found ‘%s‘ input device in %s", inputName, dirname);
    return fd;

}


===========================

http://blog.csdn.net/andyhuabing/article/details/7006688

linux的Input子系统获取Device Name

标签:

原文地址:http://www.cnblogs.com/seven-sky/p/4730578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!