From f351427a0581a6b5e32581b25ef7e30a7884ffb5 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 24 Dec 2023 00:08:18 +0100 Subject: [PATCH] do not fail on no fd --- dev_out.c | 29 ++++++++++++++++++++--------- devices_status.c | 8 +++++--- devices_status.h | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/dev_out.c b/dev_out.c index 2707747..fbaeb76 100644 --- a/dev_out.c +++ b/dev_out.c @@ -485,7 +485,7 @@ void *dev_out_thread_func(void *ptr) { } else if ((current_mouse_fd > 0) && (mouse_time_diff_usecs >= mouse_report_timing_us)) { mouse_last_hid_report_sent = now; - virt_mouse_send(&mouse_data, &dev_out_data->dev_stats.mouse, &now); + virt_mouse_send(&mouse_data, &dev_out_data->dev_stats.mouse, NULL); // reset mouse movements now dev_out_data->dev_stats.mouse.x = 0; @@ -496,7 +496,7 @@ void *dev_out_thread_func(void *ptr) { } else if ((current_keyboard_fd > 0) && (kbd_time_diff_usecs >= kbd_report_timing_us)) { keyboard_last_hid_report_sent = now; - virt_kbd_send(&keyboard_data, &dev_out_data->dev_stats.kbd, &now); + virt_kbd_send(&keyboard_data, &dev_out_data->dev_stats.kbd, NULL); // this does reset the for, ensuring every other device has nothing to say continue; @@ -525,7 +525,7 @@ void *dev_out_thread_func(void *ptr) { FD_SET(current_mouse_fd, &read_fds); } - if (current_mouse_fd > 0) { + if (current_keyboard_fd > 0) { FD_SET(current_keyboard_fd, &read_fds); } @@ -533,12 +533,23 @@ void *dev_out_thread_func(void *ptr) { FD_SET(current_gamepad_fd, &read_fds); } - const int64_t timeout_gamepad_time_diff_usecs = gamepad_report_timing_us - gamepad_time_diff_usecs; - const int64_t timeout_mouse_time_diff_usecs = mouse_report_timing_us - mouse_time_diff_usecs; - const int64_t timeout_kbd_time_diff_usecs = kbd_report_timing_us - kbd_time_diff_usecs; + const int64_t timeout_gamepad_time_diff_usecs = (current_gamepad_fd > 0) ? gamepad_report_timing_us - gamepad_time_diff_usecs : 5000; + const int64_t timeout_mouse_time_diff_usecs = (current_mouse_fd > 0) ? mouse_report_timing_us - mouse_time_diff_usecs : 5000; + const int64_t timeout_kbd_time_diff_usecs = (current_keyboard_fd > 0) ? kbd_report_timing_us - kbd_time_diff_usecs : 5000; - int64_t next_timing_out_device_diff_usecs = timeout_kbd_time_diff_usecs < timeout_mouse_time_diff_usecs ? timeout_kbd_time_diff_usecs : timeout_mouse_time_diff_usecs; - next_timing_out_device_diff_usecs = next_timing_out_device_diff_usecs < timeout_gamepad_time_diff_usecs ? next_timing_out_device_diff_usecs : timeout_gamepad_time_diff_usecs; + int64_t next_timing_out_device_diff_usecs = 5000; + + if ((timeout_kbd_time_diff_usecs > 0) && (timeout_kbd_time_diff_usecs < next_timing_out_device_diff_usecs)) { + next_timing_out_device_diff_usecs = timeout_kbd_time_diff_usecs; + } + + if ((timeout_mouse_time_diff_usecs > 0) && (timeout_mouse_time_diff_usecs < next_timing_out_device_diff_usecs)) { + next_timing_out_device_diff_usecs = timeout_mouse_time_diff_usecs; + } + + if ((timeout_gamepad_time_diff_usecs > 0) && (timeout_gamepad_time_diff_usecs < next_timing_out_device_diff_usecs)) { + next_timing_out_device_diff_usecs = timeout_gamepad_time_diff_usecs; + } // calculate the shortest timeout between one of the multiple device will needs to send out its hid report struct timeval timeout = { @@ -547,7 +558,7 @@ void *dev_out_thread_func(void *ptr) { }; int ready_fds = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout); - gamepad_status_qam_quirk_ext_time(&dev_out_data->dev_stats.gamepad, &now); + gamepad_status_qam_quirk_ext_time(&dev_out_data->dev_stats.gamepad); if (ready_fds == -1) { const int err = errno; diff --git a/devices_status.c b/devices_status.c index ddd1b97..f852b15 100644 --- a/devices_status.c +++ b/devices_status.c @@ -163,13 +163,15 @@ void gamepad_status_qam_quirk(gamepad_status_t *const gamepad_stats) { } } -void gamepad_status_qam_quirk_ext_time(gamepad_status_t *const gamepad_stats, struct timeval *now) { +void gamepad_status_qam_quirk_ext_time(gamepad_status_t *const gamepad_stats) { static struct timeval press_time; if (gamepad_stats->flags & GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER) { + struct timeval now; + gettimeofday(&now, NULL); // Calculate elapsed time in milliseconds - const int64_t elapsed_time = (now->tv_sec - press_time.tv_sec) * 1000 + - (now->tv_usec - press_time.tv_usec) / 1000; + const int64_t elapsed_time = (now.tv_sec - press_time.tv_sec) * 1000 + + (now.tv_usec - press_time.tv_usec) / 1000; if (gamepad_stats->center) { // If the center button is pressed and at least X ms have passed diff --git a/devices_status.h b/devices_status.h index e32f42d..8543f89 100644 --- a/devices_status.h +++ b/devices_status.h @@ -113,4 +113,4 @@ void devices_status_init(devices_status_t *const stats); void gamepad_status_qam_quirk(gamepad_status_t *const gamepad_stats); -void gamepad_status_qam_quirk_ext_time(gamepad_status_t *const gamepad_stats, struct timeval *now); +void gamepad_status_qam_quirk_ext_time(gamepad_status_t *const gamepad_stats);