--- hsp3dish.cpp.orig 2021-03-05 19:44:59.138854253 +0900 +++ hsp3dish.cpp 2021-03-05 20:29:47.583075002 +0900 @@ -163,6 +163,59 @@ return atoi( s ); } +static std::string getInputDevPath() +{ + const char *pdevsName = "/proc/bus/input/devices"; + std::string devPath = ""; + char buf[8196]; + FILE *fp; + + if ((fp = fopen(pdevsName, "r")) == NULL) + { + printf("[ERR] Cannot open '%s'\n", pdevsName); + return ""; + } + + int rd = fread(buf, sizeof(*buf), sizeof(buf) / sizeof(*buf), fp); + + fclose(fp); + + if (rd < 6) + { + printf("[ERR] Wrong size was read from devs file\n"); + return ""; + } + buf[rd] = 0; + + char *pHandlers, *pEV = buf; + do + { + pHandlers = strstr(pEV, "Handlers="); + pEV = strstr(pHandlers, "EV="); + } while (pHandlers && pEV && 0 != strncmp(pEV + 3, "1200", 4)); + + if (pHandlers && pEV) + { + char *pevent = strstr(pHandlers, "event"); + if (pevent) + { + devPath += "/dev/input/event"; + devPath.push_back(pevent[5]); + } + else + { + printf("[ERR] Abnormal keyboard event device\n"); + } + } + else + { + printf("[ERR] Keyboard event device not found\n"); + } + + return devPath; +} + + /*----------------------------------------------------------*/ static int mouseFd = -1; @@ -181,18 +234,14 @@ { DIR *dirp; struct dirent *dp; - regex_t kbd,mouse; + regex_t mouse; char fullPath[1024]; static char *dirName = "/dev/input/by-id"; int i; - if(regcomp(&kbd,"event-kbd",0)!=0) - { - printf("regcomp for kbd failed\n"); - return; + std::string kbdfullpath; - } if(regcomp(&mouse,"event-mouse",0)!=0) { printf("regcomp for mouse failed\n"); @@ -205,21 +254,23 @@ return; } - // Find any files that match the regex for keyboard or mouse + // Find any files that match the regex for keyboard + kbdfullpath = getInputDevPath(); + if (kbdfullpath[0] != '\0') { + keyboardFd = open(kbdfullpath.c_str(), O_RDONLY | O_NONBLOCK); + printf("match for the kbd = %s\n", kbdfullpath.c_str()); + printf("keyboardFd = %d\n", keyboardFd); + } + + + // Find any files that match the regex for mouse do { errno = 0; if ((dp = readdir(dirp)) != NULL) { printf("readdir (%s)\n",dp->d_name); - if(regexec (&kbd, dp->d_name, 0, NULL, 0) == 0) - { - printf("match for the kbd = %s\n",dp->d_name); - sprintf(fullPath,"%s/%s",dirName,dp->d_name); - keyboardFd = open(fullPath, O_RDONLY | O_NONBLOCK); - printf("%s keyboardFd = %d\n", fullPath, keyboardFd); - } if(regexec (&mouse, dp->d_name, 0, NULL, 0) == 0) { int result; @@ -237,7 +288,6 @@ closedir(dirp); - regfree(&kbd); regfree(&mouse); mouse_x = (int)mem_engine.width / 2;