From 773812ff53d799556a2b1b827467fb5ef2e1d969 Mon Sep 17 00:00:00 2001 From: Denis Benato Date: Thu, 7 Dec 2023 11:16:01 +0100 Subject: [PATCH] fix cmake and a bunch of warnings --- .vscode/settings.json | 5 +++ CMakeLists.txt | 17 +++++++- dev_out.c | 92 ++++++++++++++++++++++++++++++++++++++++++- main.c | 7 +++- rog_ally.c | 6 +-- rog_ally.h | 4 +- virt_ds4.c | 4 +- virt_mouse_kbd.c | 2 +- xbox360.c | 2 +- xbox360.h | 2 +- 10 files changed, 127 insertions(+), 14 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a2cdb86 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "virt_ds4.h": "c" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ef8738b..87a5209 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,22 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) # Adding something we can run - Output name matches target name -add_executable(${EXECUTABLE_NAME} dev_iio.c dev_in.c logic.c main.c output_dev.c platform.c queue.c settings.c virt_ds4.c virt_ds5.c virt_mouse_kbd.c virt_evdev.c devices_status.c) +add_executable(${EXECUTABLE_NAME} + dev_evdev.c + dev_iio.c + dev_in.c + dev_out.c + main.c + platform.c + settings.c + virt_ds4.c + virt_ds5.c + virt_mouse_kbd.c + virt_evdev.c + devices_status.c + rog_ally.c + xbox360.c +) target_link_libraries(${EXECUTABLE_NAME} PRIVATE Threads::Threads -levdev -ludev -lconfig -lm) diff --git a/dev_out.c b/dev_out.c index 4471209..883cfcc 100644 --- a/dev_out.c +++ b/dev_out.c @@ -1,25 +1,115 @@ #include "dev_out.h" +#include "virt_ds4.h" + +static void handle_incoming_message_gamepad_action( + in_message_gamepad_action_t *const msg_payload, + gamepad_status_t *const inout_gamepad +) { + +} + +static void handle_incoming_message_gamepad_set( + in_message_gamepad_set_element_t *const msg_payload, + gamepad_status_t *const inout_gamepad +) { + +} + +static void handle_incoming_message( + in_message_t *const msg, + devices_status_t *const dev_stats +) { + if (msg->type == GAMEPAD_SET_ELEMENT) { + handle_incoming_message_gamepad_set(&msg->data.gamepad_set, dev_stats); + } else if (msg->type == GAMEPAD_ACTION) { + handle_incoming_message_gamepad_action(&msg->data.action, dev_stats); + } +} + +uint64_t get_timediff_usec(struct timeval past, struct timeval now) { + return 0; +} + void *dev_out_thread_func(void *ptr) { dev_out_data_t *const dev_out = (dev_out_data_t*)ptr; + // Initialize device + devices_status_init(&dev_out->dev_stats); + dev_out_gamepad_device_t current_gamepad = dev_out->gamepad; + union { + virt_dualshock_t ds4; + } controller_data; + int current_gamepad_fd = -1; int current_keyboard_fd = -1; int current_mouse_fd = -1; + if (current_gamepad == GAMEPAD_DUALSENSE) { + + } else if (current_gamepad == GAMEPAD_DUALSHOCK) { + const int ds4_init_res = virt_dualshock_init(&controller_data.ds4); + if (ds4_init_res != 0) { + fprintf(stderr, "Unable to initialize the DualShock device: %d\n", ds4_init_res); + } else { + current_gamepad_fd = virt_dualshock_get_fd(&controller_data.ds4); + } + } // TODO: stats->gamepad.flags |= GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER; + struct timeval now = {0}; + gettimeofday(&now, NULL); + struct timeval gamepad_last_hid_report_sent = now; + struct timeval mouse_last_hid_report_sent = now; + struct timeval keyboard_last_hid_report_sent = now; + + fd_set read_fds; for (;;) { - + FD_ZERO(&read_fds); if (current_gamepad == GAMEPAD_DUALSENSE) { } else if (current_gamepad == GAMEPAD_DUALSHOCK) { + + } + FD_SET(dev_out->out_message_pipe_fd, &read_fds); + // TODO: FD_SET(current_mouse_fd, &read_fds); + // TODO: FD_SET(current_keyboard_fd, &read_fds); + FD_SET(current_gamepad_fd, &read_fds); + + // TODO: calculate the shortest time between multiple device + struct timeval timeout = { + //.tv_sec = (__time_t)devs->timeout_ms / (__time_t)1000, + //.tv_usec = ((__suseconds_t)devs->timeout_ms % (__suseconds_t)1000) * (__suseconds_t)1000000, + }; + + gettimeofday(&now, NULL); + + int ready_fds = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout); + + if (ready_fds == -1) { + const int err = errno; + fprintf(stderr, "Error reading devices: %d\n", err); + continue; + } else if (ready_fds == 0) { + // Timeout... simply retry + continue; + } + + if (FD_ISSET(dev_out->in_message_pipe_fd, &read_fds)) { + in_message_t incoming_message; + size_t in_message_pipe_read_res = read(dev_out->in_message_pipe_fd, (void*)&incoming_message, sizeof(in_message_t)); + if (in_message_pipe_read_res == sizeof(in_message_t)) { + handle_incoming_message(&incoming_message, &dev_out->dev_stats); + } else { + fprintf(stderr, "Error reading from out_message_pipe_fd: got %zu bytes, expected %zu butes", in_message_pipe_read_res, sizeof(in_message_t)); + } + } } diff --git a/main.c b/main.c index e072acc..a3147fb 100644 --- a/main.c +++ b/main.c @@ -39,6 +39,9 @@ int main(int argc, char ** argv) { input_dev_t **in_devs = rog_ally_device_def(); const size_t in_devs_sz = rog_ally_device_def_count(); + int dev_in_thread_creation = -1; + int dev_out_thread_creation = -1; + int out_message_pipes[2]; pipe(out_message_pipes); @@ -58,7 +61,7 @@ int main(int argc, char ** argv) { dev_out_thread_data.out_message_pipe_fd = out_message_pipes[1]; pthread_t dev_in_thread; - const int dev_in_thread_creation = pthread_create(&dev_in_thread, NULL, dev_in_thread_func, (void*)(&dev_in_thread_data)); + dev_in_thread_creation = pthread_create(&dev_in_thread, NULL, dev_in_thread_func, (void*)(&dev_in_thread_data)); if (dev_in_thread_creation != 0) { fprintf(stderr, "Error creating dev_in thread: %d\n", dev_in_thread_creation); ret = -1; @@ -67,7 +70,7 @@ int main(int argc, char ** argv) { } pthread_t dev_out_thread; - const int dev_out_thread_creation = pthread_create(&dev_out_thread, NULL, dev_out_thread_func, (void*)(&dev_out_thread_data)); + dev_out_thread_creation = pthread_create(&dev_out_thread, NULL, dev_out_thread_func, (void*)(&dev_out_thread_data)); if (dev_out_thread_creation != 0) { fprintf(stderr, "Error creating dev_out thread: %d\n", dev_out_thread_creation); ret = -1; diff --git a/rog_ally.c b/rog_ally.c index 273ba44..9af4175 100644 --- a/rog_ally.c +++ b/rog_ally.c @@ -70,10 +70,10 @@ input_dev_t *in_devs[] = { &in_asus_kb_3_dev, }; -size_t rog_ally_device_def_count() { +size_t rog_ally_device_def_count(void) { return sizeof(in_devs) / sizeof(input_dev_t*); } -input_dev_t **rog_ally_device_def() { +input_dev_t **rog_ally_device_def(void) { return in_devs; -} \ No newline at end of file +} diff --git a/rog_ally.h b/rog_ally.h index e59c137..c1d685d 100644 --- a/rog_ally.h +++ b/rog_ally.h @@ -3,6 +3,6 @@ #include "input_dev.h" #include "xbox360.h" -size_t rog_ally_device_def_count(); +size_t rog_ally_device_def_count(void); -input_dev_t **rog_ally_device_def(); \ No newline at end of file +input_dev_t **rog_ally_device_def(void); diff --git a/virt_ds4.c b/virt_ds4.c index 2a2b29c..d004831 100644 --- a/virt_ds4.c +++ b/virt_ds4.c @@ -515,7 +515,7 @@ int virt_dualshock_init(virt_dualshock_t *const out_gamepad) { out_gamepad->debug = false; - out_gamepad->fd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK); + out_gamepad->fd = open(path, O_RDWR | O_CLOEXEC /* | O_NONBLOCK */); if (out_gamepad->fd < 0) { fprintf(stderr, "Cannot open uhid-cdev %s: %d\n", path, out_gamepad->fd); ret = out_gamepad->fd; @@ -575,7 +575,7 @@ int virt_dualshock_event(virt_dualshock_t *const gamepad, gamepad_status_t *cons break; case UHID_CLOSE: if (gamepad->debug) { - printf(stderr, "UHID_CLOSE from uhid-dev\n"); + printf("UHID_CLOSE from uhid-dev\n"); } break; case UHID_OUTPUT: diff --git a/virt_mouse_kbd.c b/virt_mouse_kbd.c index c72c237..794e8c5 100644 --- a/virt_mouse_kbd.c +++ b/virt_mouse_kbd.c @@ -122,4 +122,4 @@ void *virt_mouse_kbd_thread_func(void *ptr) { } return NULL; -} \ No newline at end of file +} diff --git a/xbox360.c b/xbox360.c index d84e6f3..265a344 100644 --- a/xbox360.c +++ b/xbox360.c @@ -90,4 +90,4 @@ void xbox360_ev_map(const evdev_collected_t *const coll, int in_messages_pipe_fd fprintf(stderr, "Unable to write data to the in_message pipe: %zd\n", last_write_res); } } -} \ No newline at end of file +} diff --git a/xbox360.h b/xbox360.h index 8f3f49b..cf71568 100644 --- a/xbox360.h +++ b/xbox360.h @@ -6,4 +6,4 @@ typedef struct xbox360_settings { bool nintendo_layout; } xbox360_settings_t; -void xbox360_ev_map(const evdev_collected_t *const e, int in_messages_pipe_fd, void* user_data); \ No newline at end of file +void xbox360_ev_map(const evdev_collected_t *const e, int in_messages_pipe_fd, void* user_data);