From 358fba86d65f13f6f7e70be0a129180862673b05 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 3 Jan 2024 00:59:57 +0100 Subject: [PATCH] allow disabling touchbar + use timestamp from the bmi323 --- allynone.c | 41 +++++++++++++++++++++++------------------ dev_in.c | 23 +++-------------------- dev_out.c | 4 ++-- devices_status.h | 8 ++++---- main.c | 19 ++++++++++++------- message.h | 4 ++-- rog_ally.c | 7 ++++++- rog_ally.h | 2 +- settings.c | 7 +++++++ settings.h | 1 + stray_ally.c | 16 ++++++++++------ virt_ds4.c | 4 ++-- virt_ds4.h | 2 +- virt_ds5.c | 4 ++-- virt_ds5.h | 2 +- 15 files changed, 77 insertions(+), 67 deletions(-) diff --git a/allynone.c b/allynone.c index 987a55f..ba343d1 100644 --- a/allynone.c +++ b/allynone.c @@ -15,6 +15,26 @@ static const char* configuration_file = "/etc/ROGueENEMY/config.cfg"; int main(int argc, char ** argv) { int ret = 0; + // fill in configuration from file: automatic fallback to default + dev_in_settings_t in_settings = { + .enable_qam = true, + .ff_gain = 0xFFFF, + .rumble_on_mode_switch = true, + .m1m2_mode = 0, + .touchbar = true, + }; + + load_in_config(&in_settings, configuration_file); + + dev_out_settings_t out_settings = { + .default_gamepad = 0, + .nintendo_layout = false, + .gamepad_leds_control = true, + .gamepad_rumble_control = true, + }; + + load_out_config(&out_settings, configuration_file); + input_dev_composite_t* in_devs = NULL; int dmi_name_fd = open("/sys/class/dmi/id/board_name", O_RDONLY | O_NONBLOCK); @@ -28,7 +48,7 @@ int main(int argc, char ** argv) { read(dmi_name_fd, bname, sizeof(bname)); if (strstr(bname, "RC71L") != NULL) { printf("Running in an Asus ROG Ally device\n"); - in_devs = rog_ally_device_def(); + in_devs = rog_ally_device_def(&in_settings); } else if (strstr(bname, "LNVNB161216")) { printf("Running in an Lenovo Legion Go device\n"); in_devs = legion_go_device_def(); @@ -85,17 +105,9 @@ int main(int argc, char ** argv) { } } }, - .settings = { - .enable_qam = true, - .ff_gain = 0xFFFF, - .rumble_on_mode_switch = true, - .m1m2_mode = 0, - } + .settings = in_settings, }; - // fill in configuration from file: automatic fallback to default - load_in_config(&dev_in_thread_data.settings, configuration_file); - // populate the output device thread data dev_out_data_t dev_out_thread_data = { .flags = 0x00000000U, @@ -108,16 +120,9 @@ int main(int argc, char ** argv) { } } }, - .settings = { - .default_gamepad = 0, - .nintendo_layout = false, - .gamepad_leds_control = true, - .gamepad_rumble_control = true, - } + .settings = out_settings, }; - load_out_config(&dev_out_thread_data.settings, configuration_file); - pthread_t dev_in_thread; 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) { diff --git a/dev_in.c b/dev_in.c index bd513b7..3f2418e 100644 --- a/dev_in.c +++ b/dev_in.c @@ -84,14 +84,6 @@ static int map_message_from_iio(dev_in_iio_t *const in_iio, in_message_t *const uint8_t data[32]; -/* - struct timeval read_time; - gettimeofday(&read_time, NULL); -*/ - - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[0], sizeof(data)); if (res == -1) { res = errno; @@ -106,27 +98,18 @@ static int map_message_from_iio(dev_in_iio_t *const in_iio, in_message_t *const } uint16_t *const scan_elements = (uint16_t*)&data[0]; + int64_t *const timestamp = (int64_t*)&data[16]; // either that or (int64_t*)&data[12] messages[0].type = GAMEPAD_SET_ELEMENT; messages[0].data.gamepad_set.element = GAMEPAD_ACCELEROMETER; - messages[1].data.gamepad_set.status.accel.sample_time.tv_sec = now.tv_sec; - messages[1].data.gamepad_set.status.accel.sample_time.tv_usec = now.tv_nsec / 1000; - //messages[0].data.gamepad_set.status.accel.sample_time = read_time; - //messages[0].data.gamepad_set.status.accel.x = scan_elements[0]; - //messages[0].data.gamepad_set.status.accel.y = scan_elements[1]; - //messages[0].data.gamepad_set.status.accel.z = scan_elements[2]; + messages[1].data.gamepad_set.status.accel.sample_timestamp_ns = *timestamp; messages[0].data.gamepad_set.status.accel.x = scan_elements[0]; messages[0].data.gamepad_set.status.accel.y = (uint16_t)(-1) * scan_elements[2]; messages[0].data.gamepad_set.status.accel.z = scan_elements[1]; messages[1].type = GAMEPAD_SET_ELEMENT; messages[1].data.gamepad_set.element = GAMEPAD_GYROSCOPE; - messages[1].data.gamepad_set.status.gyro.sample_time.tv_sec = now.tv_sec; - messages[1].data.gamepad_set.status.gyro.sample_time.tv_usec = now.tv_nsec / 1000; - //messages[0].data.gamepad_set.status.gyro.sample_time = read_time; - //messages[1].data.gamepad_set.status.gyro.x = scan_elements[3]; - //messages[1].data.gamepad_set.status.gyro.y = scan_elements[4]; - //messages[1].data.gamepad_set.status.gyro.z = scan_elements[5]; + messages[1].data.gamepad_set.status.gyro.sample_timestamp_ns = *timestamp; messages[1].data.gamepad_set.status.gyro.x = scan_elements[3]; messages[1].data.gamepad_set.status.gyro.y = (uint16_t)(-1) * scan_elements[5]; messages[1].data.gamepad_set.status.gyro.z = scan_elements[4]; diff --git a/dev_out.c b/dev_out.c index 36e9f76..ee10a5d 100644 --- a/dev_out.c +++ b/dev_out.c @@ -179,14 +179,14 @@ static void handle_incoming_message_gamepad_set( break; } case GAMEPAD_GYROSCOPE: { - inout_gamepad->last_gyro_motion_time = msg_payload->status.gyro.sample_time; + inout_gamepad->last_gyro_motion_timestamp_ns = msg_payload->status.gyro.sample_timestamp_ns; inout_gamepad->raw_gyro[0] = msg_payload->status.gyro.x; inout_gamepad->raw_gyro[1] = msg_payload->status.gyro.y; inout_gamepad->raw_gyro[2] = msg_payload->status.gyro.z; break; } case GAMEPAD_ACCELEROMETER: { - inout_gamepad->last_accel_motion_time = msg_payload->status.accel.sample_time; + inout_gamepad->last_accel_motion_timestamp_ns = msg_payload->status.accel.sample_timestamp_ns; inout_gamepad->raw_accel[0] = msg_payload->status.accel.x; inout_gamepad->raw_accel[1] = msg_payload->status.accel.y; inout_gamepad->raw_accel[2] = msg_payload->status.accel.z; diff --git a/devices_status.h b/devices_status.h index c137df1..51f8ec9 100644 --- a/devices_status.h +++ b/devices_status.h @@ -47,11 +47,11 @@ typedef struct gamepad_status { uint8_t touchpad_press; int16_t touchpad_touch_num; // touchpad is inactive when this is -1 - int16_t touchpad_x; - int16_t touchpad_y; + int16_t touchpad_x; // 0 to 1920 + int16_t touchpad_y; // 0 to 1080 - struct timeval last_gyro_motion_time; - struct timeval last_accel_motion_time; + int64_t last_gyro_motion_timestamp_ns; + int64_t last_accel_motion_timestamp_ns; double gyro[3]; // | x, y, z| right-hand-rules -- in rad/s double accel[3]; // | x, y, z| positive: right, up, towards player -- in m/s^2 diff --git a/main.c b/main.c index 62f4a24..389fb3d 100644 --- a/main.c +++ b/main.c @@ -15,6 +15,16 @@ static const char* configuration_file = "/etc/ROGueENEMY/config.cfg"; int main(int argc, char ** argv) { int ret = 0; + dev_in_settings_t in_settings = { + .enable_qam = true, + .ff_gain = 0xFFFF, + .rumble_on_mode_switch = true, + .m1m2_mode = 1, + .touchbar = true, + }; + + load_in_config(&in_settings, configuration_file); + input_dev_composite_t* in_devs = NULL; int dmi_name_fd = open("/sys/class/dmi/id/board_name", O_RDONLY | O_NONBLOCK); @@ -28,7 +38,7 @@ int main(int argc, char ** argv) { read(dmi_name_fd, bname, sizeof(bname)); if (strstr(bname, "RC71L") != NULL) { printf("Running in an Asus ROG Ally device\n"); - in_devs = rog_ally_device_def(); + in_devs = rog_ally_device_def(&in_settings); } else if (strstr(bname, "LNVNB161216")) { printf("Running in an Lenovo Legion Go device\n"); in_devs = legion_go_device_def(); @@ -51,12 +61,7 @@ int main(int argc, char ** argv) { }, } }, - .settings = { - .enable_qam = true, - .ff_gain = 0xFFFF, - .rumble_on_mode_switch = true, - .m1m2_mode = 0, - } + .settings = in_settings, }; // fill in configuration from file: automatic fallback to default diff --git a/message.h b/message.h index 4fe7632..00f2303 100644 --- a/message.h +++ b/message.h @@ -54,7 +54,7 @@ typedef struct in_message_gamepad_touchpad_active { } in_message_gamepad_touchpad_active_t; typedef struct in_message_gamepad_gyro { - struct timeval sample_time; + int64_t sample_timestamp_ns; uint16_t x; uint16_t y; @@ -62,7 +62,7 @@ typedef struct in_message_gamepad_gyro { } in_message_gamepad_gyro_t; typedef struct in_message_gamepad_accel { - struct timeval sample_time; + int64_t sample_timestamp_ns; uint16_t x; uint16_t y; diff --git a/rog_ally.c b/rog_ally.c index 03d2e0a..25c9b16 100644 --- a/rog_ally.c +++ b/rog_ally.c @@ -1439,6 +1439,11 @@ input_dev_composite_t rc71l_composite = { .leds_fn = rc71l_platform_leds, }; -input_dev_composite_t* rog_ally_device_def(void) { +input_dev_composite_t* rog_ally_device_def(const dev_in_settings_t *const settings) { + if (!settings->touchbar) { + // this is because the touchscreen is the latest in the list + rc71l_composite.dev_count -= 1; + } + return &rc71l_composite; } diff --git a/rog_ally.h b/rog_ally.h index 204614d..9152b22 100644 --- a/rog_ally.h +++ b/rog_ally.h @@ -3,4 +3,4 @@ #include "input_dev.h" #include "settings.h" -input_dev_composite_t* rog_ally_device_def(void); +input_dev_composite_t* rog_ally_device_def(const dev_in_settings_t *const settings); diff --git a/settings.c b/settings.c index 6a2ef48..7efab3c 100644 --- a/settings.c +++ b/settings.c @@ -48,6 +48,13 @@ void load_in_config(dev_in_settings_t *const out_conf, const char* const filepat fprintf(stderr, "m1m2_mode (int) configuration not found. Default value will be used.\n"); } + int touchbar; + if (config_lookup_bool(&cfg, "touchbar", &touchbar) != CONFIG_FALSE) { + out_conf->touchbar = touchbar; + } else { + fprintf(stderr, "touchbar (bool) configuration not found. Default value will be used.\n"); + } + config_destroy(&cfg); load_in_config_err: diff --git a/settings.h b/settings.h index 878ae1a..4ab4f63 100644 --- a/settings.h +++ b/settings.h @@ -7,6 +7,7 @@ typedef struct dev_in_settings { bool rumble_on_mode_switch; uint16_t ff_gain; uint8_t m1m2_mode; + bool touchbar; } dev_in_settings_t; void load_in_config(dev_in_settings_t *const out_conf, const char* const filepath); diff --git a/stray_ally.c b/stray_ally.c index fb165e2..0af98a3 100644 --- a/stray_ally.c +++ b/stray_ally.c @@ -12,6 +12,15 @@ static const char* configuration_file = "/etc/ROGueENEMY/config.cfg"; int main(int argc, char ** argv) { int ret = 0; + dev_out_settings_t out_settings = { + .default_gamepad = 0, + .nintendo_layout = false, + .gamepad_leds_control = true, + .gamepad_rumble_control = true, + }; + + load_out_config(&out_settings, configuration_file); + // Create a signal set containing only SIGTERM sigset_t mask; sigemptyset(&mask); @@ -42,12 +51,7 @@ int main(int argc, char ** argv) { } } }, - .settings = { - .default_gamepad = 0, - .nintendo_layout = false, - .gamepad_leds_control = true, - .gamepad_rumble_control = true, - } + .settings = out_settings, }; load_out_config(&dev_out_thread_data.settings, configuration_file); diff --git a/virt_ds4.c b/virt_ds4.c index 1f2a376..d35627d 100644 --- a/virt_ds4.c +++ b/virt_ds4.c @@ -642,9 +642,9 @@ void virt_dualshock_close(virt_dualshock_t *const out_gamepad) { * This function arranges HID packets as described on https://www.psdevwiki.com/ps4/DS4-USB */ void virt_dualshock_compose(virt_dualshock_t *const gamepad, gamepad_status_t *const in_device_status, uint8_t *const out_buf) { - const int64_t time_us = in_device_status->last_gyro_motion_time.tv_sec * 1000000 + in_device_status->last_gyro_motion_time.tv_usec; + const int64_t time_us = in_device_status->last_gyro_motion_timestamp_ns / (int64_t)1000; - const int delta_time = time_us - gamepad->last_time; + const int64_t delta_time = time_us - gamepad->last_time; gamepad->last_time = time_us; // find the average Δt in the last 30 non-zero inputs; diff --git a/virt_ds4.h b/virt_ds4.h index 986e011..83c10c1 100644 --- a/virt_ds4.h +++ b/virt_ds4.h @@ -18,7 +18,7 @@ typedef struct virt_dualshock { uint32_t dt_buffer[30]; uint32_t empty_reports; - uint64_t last_time; + int64_t last_time; } virt_dualshock_t; int virt_dualshock_init(virt_dualshock_t *const gamepad); diff --git a/virt_ds5.c b/virt_ds5.c index c4308a2..6b4ec07 100644 --- a/virt_ds5.c +++ b/virt_ds5.c @@ -1248,9 +1248,9 @@ void virt_dualsense_close(virt_dualsense_t *const out_gamepad) { } void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *const in_device_status, uint8_t *const out_buf) { - const int64_t time_us = in_device_status->last_gyro_motion_time.tv_sec * 1000000 + in_device_status->last_gyro_motion_time.tv_usec; + const int64_t time_us = in_device_status->last_gyro_motion_timestamp_ns / (int64_t)1000; - const int delta_time = time_us - gamepad->last_time; + const int64_t delta_time = time_us - gamepad->last_time; gamepad->last_time = time_us; // find the average Δt in the last 30 non-zero inputs; diff --git a/virt_ds5.h b/virt_ds5.h index 6f9d63d..0d111a9 100644 --- a/virt_ds5.h +++ b/virt_ds5.h @@ -22,7 +22,7 @@ typedef struct virt_dualsense { uint32_t dt_buffer[30]; uint32_t empty_reports; - uint64_t last_time; + int64_t last_time; } virt_dualsense_t; int virt_dualsense_init(virt_dualsense_t *const gamepad, bool bluetooth);