disable the device when unused

This commit is contained in:
Denis 2023-12-04 00:30:06 +01:00
parent 350151627e
commit ca037b1080
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03

View file

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