From 00c05b7a73c9a96d5091bae3fa65d0a4b00a19e8 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 20 Nov 2023 22:02:30 +0100 Subject: [PATCH] Test mode switch --- logic.c | 4 +++- logic.h | 10 +++++++++- output_dev.c | 18 ++++++++++++------ platform.c | 2 +- virt_ds4.c | 4 +--- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/logic.c b/logic.c index 2c80494..56fadf4 100644 --- a/logic.c +++ b/logic.c @@ -33,9 +33,11 @@ int logic_create(logic_t *const logic) { const int virt_ds4_thread_creation = pthread_create(&logic->virt_ds4_thread, NULL, virt_ds4_thread_func, (void*)(logic)); if (virt_ds4_thread_creation != 0) { - fprintf(stderr, "Error creating virtual DualShock4 thread: %d\n", virt_ds4_thread_creation); + fprintf(stderr, "Error creating virtual DualShock4 thread: %d. Will use evdev as output.\n", virt_ds4_thread_creation); } else { + printf("Creation of virtual DualShock4 succeeded: using it as the defout output.\n"); logic->flags |= LOGIC_FLAGS_VIRT_DS4_ENABLE; + logic->gamepad_output = GAMEPAD_OUTPUT_DS4; } if (queue_init_res < 0) { diff --git a/logic.h b/logic.h index 840571a..c520069 100644 --- a/logic.h +++ b/logic.h @@ -46,6 +46,11 @@ typedef struct gamepad_status { #define LOGIC_FLAGS_VIRT_DS4_ENABLE 0x00000001U #define LOGIC_FLAGS_PLATFORM_ENABLE 0x00000002U +typedef enum gamepad_output { + GAMEPAD_OUTPUT_EVDEV = 0, + GAMEPAD_OUTPUT_DS4, +} gamepad_output_t; + typedef struct logic { rc71l_platform_t platform; @@ -57,8 +62,11 @@ typedef struct logic { pthread_t virt_ds4_thread; - volatile uint32_t flags; + uint32_t flags; + // the mutex is not needed if only one thread is writing this and others are checking with equality + //pthread_mutex_t gamepad_output_mutex; + gamepad_output_t gamepad_output; } logic_t; int logic_create(logic_t *const logic); diff --git a/output_dev.c b/output_dev.c index 3b2c164..30ac496 100644 --- a/output_dev.c +++ b/output_dev.c @@ -539,16 +539,22 @@ static void decode_ev(output_dev_t *const out_dev, message_t *const msg) { static int gyroscope_mouse_translation = 0; if ( + (msg->data.event.ev_count >= 2) && + (msg->data.event.ev[0].type == EV_MSC) && + (msg->data.event.ev[0].code == MSC_SCAN) && (msg->data.event.ev[0].value == -13565784) && (msg->data.event.ev[1].type == EV_KEY) && (msg->data.event.ev[1].code == KEY_F18) && - (msg->data.event.ev[1].value == 1) && - (msg->data.event.ev_count >= 2) && - (msg->data.event.ev[0].type == EV_MSC) && - (msg->data.event.ev[0].code == MSC_SCAN) + (msg->data.event.ev[1].value == 1) ) { printf("Detected mode switch command, switching mode...\n"); - cycle_mode(&out_dev->logic->platform); + const int new_mode = cycle_mode(&out_dev->logic->platform); + + if (new_mode < 0) { + fprintf(stderr, "Error in mode switching: %d\n", new_mode); + } else { + printf("Mode correctly switched to %d\n", new_mode); + } //msg->flags |= INPUT_FILTER_FLAGS_DO_NOT_EMIT; } @@ -765,7 +771,7 @@ static void handle_msg(output_dev_t *const out_dev, message_t *const msg) { fprintf(stderr, "[ev] Unable to begin the gamepad status update: %d\n", upd_beg_res); } - if ((out_dev->logic->flags & LOGIC_FLAGS_VIRT_DS4_ENABLE) == 0) { + if (out_dev->logic->gamepad_output == GAMEPAD_OUTPUT_EVDEV) { emit_ev(out_dev, msg); } } else if (msg->type == MSG_TYPE_IMU) { diff --git a/platform.c b/platform.c index 091a741..4af3efa 100644 --- a/platform.c +++ b/platform.c @@ -61,7 +61,7 @@ int cycle_mode(rc71l_platform_t *const platform) { fclose(mode_file); - return 0; + return platform->mode; } int is_gamepad_mode(rc71l_platform_t *const platform) { diff --git a/virt_ds4.c b/virt_ds4.c index 86964e4..a0c528b 100644 --- a/virt_ds4.c +++ b/virt_ds4.c @@ -705,11 +705,9 @@ void *virt_ds4_thread_func(void *ptr) { for (;;) { usleep(1250); - if ((logic->flags & LOGIC_FLAGS_VIRT_DS4_ENABLE) != 0) { + if (logic->gamepad_output == GAMEPAD_OUTPUT_DS4) { event(fd); - - const int res = send_data(fd, logic); if (res < 0) { fprintf(stderr, "Error sending HID report: %d", res);