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;
}