--- hsp3dish.cpp.orig 2021-03-05 19:44:59.138854253 +0900 +++ hsp3dish.cpp 2021-03-07 01:41:56.857210031 +0900 @@ -163,10 +163,14 @@ static int GetIniFileInt( char *keyword return atoi( s ); } + /*----------------------------------------------------------*/ -static int mouseFd = -1; -static int keyboardFd = -1; +static const int dev_max = 16; +static int devFd[dev_max]; +static int devMouseFg[dev_max]; +static int devIdx = 0; + static int quit_flag = 0; static int mouse_x, mouse_y, mouse_btn1, mouse_btn2; @@ -181,12 +185,18 @@ static void initKeyboard( void ) { DIR *dirp; struct dirent *dp; - regex_t kbd,mouse; + regex_t mouse; + regex_t kbd; char fullPath[1024]; static char *dirName = "/dev/input/by-id"; int i; + for (i = 0; i < dev_max; i++) { + devFd[i] = -1; + devMouseFg[i] = 0; + } + if(regcomp(&kbd,"event-kbd",0)!=0) { printf("regcomp for kbd failed\n"); @@ -205,8 +216,9 @@ static void initKeyboard( void ) return; } - // Find any files that match the regex for keyboard or mouse + // Find any files that match the regex for keyboard and mouse + devIdx = 0; do { errno = 0; if ((dp = readdir(dirp)) != NULL) @@ -214,31 +226,36 @@ static void initKeyboard( void ) 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); + int fd; sprintf(fullPath,"%s/%s",dirName,dp->d_name); - keyboardFd = open(fullPath, O_RDONLY | O_NONBLOCK); - printf("%s keyboardFd = %d\n", fullPath, keyboardFd); - + fd = open(fullPath, O_RDONLY | O_NONBLOCK); + devFd[devIdx] = fd; + devIdx++; + printf("match kbd : %s , devFd : %d\n", fullPath, fd); } + if(regexec (&mouse, dp->d_name, 0, NULL, 0) == 0) { int result; - printf("match for the mouse = %s\n",dp->d_name); + int fd; sprintf(fullPath,"%s/%s",dirName,dp->d_name); - mouseFd = open(fullPath, O_RDONLY | O_NONBLOCK); - printf("%s mouseFd = %d\n", fullPath, mouseFd); - printf("Getting exclusive access: "); - result = ioctl(mouseFd, EVIOCGRAB, 1); - printf("%s\n", (result == 0) ? "SUCCESS" : "FAILURE"); + fd = open(fullPath, O_RDONLY | O_NONBLOCK); + devFd[devIdx] = fd; + devMouseFg[devIdx] = 1; + devIdx++; + printf("match mouse : %s , devFd : %d\n", fullPath, fd); + + result = ioctl(fd, EVIOCGRAB, 1); + printf("Getting exclusive access: %s\n", ((result == 0) ? "SUCCESS" : "FAILURE")); } } - } while (dp != NULL); + } while (dp != NULL && devIdx < dev_max); closedir(dirp); - regfree(&kbd); regfree(&mouse); + regfree(&kbd); mouse_x = (int)mem_engine.width / 2; mouse_y = (int)mem_engine.height / 2; @@ -254,15 +271,18 @@ static void updateKeyboard( void ) { int rd; int sx,sy; - if((keyboardFd == -1) || (mouseFd == -1)) return; + if (devIdx <= 0) return; sx = (int)mem_engine.width; sy = (int)mem_engine.height; - // Read events from mouse + // Read events from keyboard or mouse + for (int idx = 0; idx < devIdx; idx ++) { + if (devFd[idx] == -1) continue; + + rd = read(devFd[idx], ev, sizeof(ev)); + if (rd <= 0) continue; - rd = read(mouseFd,ev,sizeof(ev)); - if(rd > 0) { int count,n; struct input_event *evp; @@ -270,7 +290,18 @@ static void updateKeyboard( void ) n = 0; while(count--) { evp = &ev[n++]; + if(evp->type == 1) { + // check keyboard + if (( evp->code >= 0 )&&( evp->code < KEY_MAX )) { + key_map[evp->code] = evp->value; + } + + if((evp->code == KEY_ESC) && (evp->value == 1)) { + quit_flag = 1; + } + + // check mouse button if(evp->code == BTN_LEFT) { //printf("Left button(%d)\n",evp->value); mouse_btn1 = evp->value; @@ -282,7 +314,7 @@ static void updateKeyboard( void ) } if(evp->code == 0) { - // Mouse Left/Right + // Mouse move Left/Right //printf("Mouse moved left/right %d\n",evp->value); mouse_x += evp->value; if ( mouse_x < 0 ) mouse_x = 0; @@ -290,7 +322,7 @@ static void updateKeyboard( void ) } if(evp->code == 1) { - // Mouse Up/Down + // Mouse move Up/Down //printf("Mouse moved up/down %d\n",evp->value); mouse_y += evp->value; if ( mouse_y < 0 ) mouse_y = 0; @@ -299,36 +331,15 @@ static void updateKeyboard( void ) } } - // Read events from keyboard - - rd = read(keyboardFd,ev,sizeof(ev)); - if(rd > 0) { - int count,n; - struct input_event *evp; - count = rd / sizeof(struct input_event); - n = 0; - while(count--) { - evp = &ev[n++]; - if(evp->type == 1) { - if (( evp->code >= 0 )&&( evp->code < KEY_MAX )) { - key_map[evp->code] = evp->value; - } - if((evp->code == KEY_ESC) && (evp->value == 1)) { - quit_flag = 1; - } - } - } - } - } static void doneKeyboard( void ) { - if (keyboardFd!=-1) close(keyboardFd); - if (mouseFd!=-1) { - ioctl(mouseFd, EVIOCGRAB, 0); - close(mouseFd); + for (int i = 0; i < dev_max; i++ ) { + if (devFd[i] == -1) continue; + if (devMouseFg[i] != 0) ioctl(devFd[i], EVIOCGRAB, 0); + close(devFd[i]); } }