SensorBase.cpp分析

 

int SensorBase::openInput(const char* inputName) { //inputName == proximity_sensor
    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);                        //devname = "/dev/input"
    filename = devname + strlen(devname);    // filename = ----------|  指向devname的结尾
    *filename++ = '/';            // filename = '/' 所以 devname = "dev/input/", filename继续指向devname的最后
    while((de = readdir(dir))) {
        if(de->d_name[0] == '.' &&
                (de->d_name[1] == '\0' ||
                        (de->d_name[1] == '.' && de->d_name[2] == '\0')))
            continue;

 //de->d_name == "event5"时, filename = "event5", 所以devname = "dev/input/event5"
        strcpy(filename, de->d_name);
        fd = open(devname, O_RDONLY);
        if (fd>=0) {
            char name[80];
   
  //   /dev/input/enent5 这个设备的名字是 name = proximity_sensor
            if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {  //通过ioctl命令EVIOCGNAME,能获取dev/input/event*对应的Device Name
                name[0] = '\0';
            }
            if (!strcmp(name, inputName)) { // 此时name == inputname == proximity_sensor
                strcpy(input_name, filename); // filename == event5 , 所以 input_name = event5
                break;
            } else {
                close(fd);
                fd = -1;
            }
        }
    }
    closedir(dir);
    LOGE_IF(fd<0, "couldn't find '%s' input device", inputName);
    return fd;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值