diff --git a/virt_ds5.c b/virt_ds5.c index e7adb61..5a65020 100644 --- a/virt_ds5.c +++ b/virt_ds5.c @@ -495,34 +495,46 @@ static int send_data(int fd, logic_t *const logic) { void *virt_ds5_thread_func(void *ptr) { logic_t *const logic = (logic_t*)ptr; - fprintf(stderr, "Open uhid-cdev %s\n", path); - int fd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK); - if (fd < 0) { - fprintf(stderr, "Cannot open uhid-cdev %s: %d\n", path, fd); - return NULL; - } - - fprintf(stderr, "Create uhid device\n"); - int ret = create(fd); - if (ret) { - close(fd); - return NULL; - } - for (;;) { - usleep(1250); - - event(fd, logic); + if (logic->gamepad_output != GAMEPAD_OUTPUT_DS5) { + // sleep for 500ms before re-checking + usleep(500000); + } - if (logic->gamepad_output == GAMEPAD_OUTPUT_DS5) { - const int res = send_data(fd, logic); - if (res < 0) { - fprintf(stderr, "Error sending HID report: %d\n", res); + fprintf(stderr, "Open uhid-cdev %s\n", path); + int fd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK); + if (fd < 0) { + fprintf(stderr, "Cannot open uhid-cdev %s: %d\n", path, fd); + continue; + } + + fprintf(stderr, "Create uhid device\n"); + int ret = create(fd); + if (ret) { + close(fd); + continue; + } + + for (;;) { + usleep(1250); + + event(fd, logic); + + if (logic->gamepad_output == GAMEPAD_OUTPUT_DS5) { + const int res = send_data(fd, logic); + if (res < 0) { + fprintf(stderr, "Error sending HID report: %d\n", res); + } + } else { + printf("DualSense has been terminated: closing the device.\n"); + goto virt_ds5_thread_func_reset; } } + +virt_ds5_thread_func_reset: + destroy(fd); } - destroy(fd); return NULL; }