diff --git a/99-js-block.rules b/99-js-block.rules new file mode 100644 index 0000000..da1ef16 --- /dev/null +++ b/99-js-block.rules @@ -0,0 +1 @@ +KERNEL=="js[0-9]*|event[0-9]*", SUBSYSTEM=="input", ATTRS{idVendor}=="045e", ATTRS{idProduct}=="028e", MODE="000", GROUP="root", TAG="", RUN+="/bin/chmod 000 /dev/input/%k" \ No newline at end of file diff --git a/99-xbox360-block.rules b/99-xbox360-block.rules new file mode 100644 index 0000000..b4e576a --- /dev/null +++ b/99-xbox360-block.rules @@ -0,0 +1 @@ +ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="045E", ATTRS{idProduct}=="028E", RUN+="/bin/sh -c 'echo 0 > /sys$DEVPATH/authorized'" \ No newline at end of file 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..b71eb9b 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; @@ -442,7 +442,7 @@ void *dev_out_thread_func(void *ptr) { printf("DualSense initialized: fd=%d\n", current_gamepad_fd); } } else if (current_gamepad == GAMEPAD_DUALSHOCK) { - const int ds4_init_res = virt_dualshock_init(&controller_data.ds4); + const int ds4_init_res = virt_dualshock_init(&controller_data.ds4, true); if (ds4_init_res != 0) { fprintf(stderr, "Unable to initialize the DualShock device: %d\n", ds4_init_res); } else { 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..6ccb11a 100644 --- a/virt_ds4.c +++ b/virt_ds4.c @@ -1,12 +1,7 @@ #include "virt_ds4.h" #include "message.h" -#include #include -#include -#include -#include -#include #define DS4_GYRO_RES_PER_DEG_S 1024 #define DS4_ACC_RES_PER_G 8192 @@ -17,6 +12,24 @@ #define DS4_OUTPUT_VALID_FLAG0_LED 0x02 #define DS4_OUTPUT_VALID_FLAG0_LED_BLINK 0x04 +#define DS4_INPUT_REPORT_USB 0x01 +#define DS4_INPUT_REPORT_USB_SIZE 64 +#define DS4_INPUT_REPORT_BT 0x11 +#define DS4_INPUT_REPORT_BT_SIZE 78 +#define DS4_OUTPUT_REPORT_USB 0x05 +#define DS4_OUTPUT_REPORT_USB_SIZE 32 +#define DS4_OUTPUT_REPORT_BT 0x11 +#define DS4_OUTPUT_REPORT_BT_SIZE 78 + +#define DS4_FEATURE_REPORT_CALIBRATION 0x02 +#define DS4_FEATURE_REPORT_CALIBRATION_SIZE 37 +#define DS4_FEATURE_REPORT_CALIBRATION_BT 0x05 +#define DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE 41 +#define DS4_FEATURE_REPORT_FIRMWARE_INFO 0xa3 +#define DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE 49 +#define DS4_FEATURE_REPORT_PAIRING_INFO 0x12 +#define DS4_FEATURE_REPORT_PAIRING_INFO_SIZE 16 + static const uint16_t gyro_pitch_bias = 0xfff9; static const uint16_t gyro_yaw_bias = 0x0009; static const uint16_t gyro_roll_bias = 0xfff9; @@ -37,6 +50,17 @@ static const uint16_t acc_z_minus = 0xe086; static const char* path = "/dev/uhid"; +/* Seed values for DualShock4 / DualSense CRC32 for different report types. */ +static uint8_t PS_INPUT_CRC32_SEED = 0xA1; +static uint8_t PS_OUTPUT_CRC32_SEED = 0xA2; +static uint8_t PS_FEATURE_CRC32_SEED = 0xA3; + +static uint32_t crc32_le(uint32_t crc_initial, const uint8_t *const buf, size_t len) { + return crc32(crc_initial ^ 0xffffffff, buf, len) ^ 0xffffffff; +} + +static const uint8_t MAC_ADDR[] = { 0xf2, 0xa5, 0x71, 0x68, 0xaf, 0xdc }; + static unsigned char rdesc[] = { 0x05, 0x01, /* Usage Page (Desktop), */ 0x09, 0x05, /* Usage (Gamepad), */ @@ -291,6 +315,37 @@ static unsigned char rdesc[] = { 0xC0 /* End Collection */ }; +static unsigned char rdesc_bt[] = { +0x05, 0x01, 0x09, 0x05, 0xa1, 0x01, 0x85, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, +0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x04, 0x81, 0x02, 0x09, 0x39, 0x15, 0x00, 0x25, +0x07, 0x75, 0x04, 0x95, 0x01, 0x81, 0x42, 0x05, 0x09, 0x19, 0x01, 0x29, 0x0e, 0x15, 0x00, 0x25, +0x01, 0x75, 0x01, 0x95, 0x0e, 0x81, 0x02, 0x75, 0x06, 0x95, 0x01, 0x81, 0x01, 0x05, 0x01, 0x09, +0x33, 0x09, 0x34, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x02, 0x81, 0x02, 0x06, 0x04, +0xff, 0x85, 0x02, 0x09, 0x24, 0x95, 0x24, 0xb1, 0x02, 0x85, 0xa3, 0x09, 0x25, 0x95, 0x30, 0xb1, +0x02, 0x85, 0x05, 0x09, 0x26, 0x95, 0x28, 0xb1, 0x02, 0x85, 0x06, 0x09, 0x27, 0x95, 0x34, 0xb1, +0x02, 0x85, 0x07, 0x09, 0x28, 0x95, 0x30, 0xb1, 0x02, 0x85, 0x08, 0x09, 0x29, 0x95, 0x2f, 0xb1, +0x02, 0x85, 0x09, 0x09, 0x2a, 0x95, 0x13, 0xb1, 0x02, 0x06, 0x03, 0xff, 0x85, 0x03, 0x09, 0x21, +0x95, 0x26, 0xb1, 0x02, 0x85, 0x04, 0x09, 0x22, 0x95, 0x2e, 0xb1, 0x02, 0x85, 0xf0, 0x09, 0x47, +0x95, 0x3f, 0xb1, 0x02, 0x85, 0xf1, 0x09, 0x48, 0x95, 0x3f, 0xb1, 0x02, 0x85, 0xf2, 0x09, 0x49, +0x95, 0x0f, 0xb1, 0x02, 0x06, 0x00, 0xff, 0x85, 0x11, 0x09, 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, +0x75, 0x08, 0x95, 0x4d, 0x81, 0x02, 0x09, 0x21, 0x91, 0x02, 0x85, 0x12, 0x09, 0x22, 0x95, 0x8d, +0x81, 0x02, 0x09, 0x23, 0x91, 0x02, 0x85, 0x13, 0x09, 0x24, 0x95, 0xcd, 0x81, 0x02, 0x09, 0x25, +0x91, 0x02, 0x85, 0x14, 0x09, 0x26, 0x96, 0x0d, 0x01, 0x81, 0x02, 0x09, 0x27, 0x91, 0x02, 0x85, +0x15, 0x09, 0x28, 0x96, 0x4d, 0x01, 0x81, 0x02, 0x09, 0x29, 0x91, 0x02, 0x85, 0x16, 0x09, 0x2a, +0x96, 0x8d, 0x01, 0x81, 0x02, 0x09, 0x2b, 0x91, 0x02, 0x85, 0x17, 0x09, 0x2c, 0x96, 0xcd, 0x01, +0x81, 0x02, 0x09, 0x2d, 0x91, 0x02, 0x85, 0x18, 0x09, 0x2e, 0x96, 0x0d, 0x02, 0x81, 0x02, 0x09, +0x2f, 0x91, 0x02, 0x85, 0x19, 0x09, 0x30, 0x96, 0x22, 0x02, 0x81, 0x02, 0x09, 0x31, 0x91, 0x02, +0x06, 0x80, 0xff, 0x85, 0x82, 0x09, 0x22, 0x95, 0x3f, 0xb1, 0x02, 0x85, 0x83, 0x09, 0x23, 0xb1, +0x02, 0x85, 0x84, 0x09, 0x24, 0xb1, 0x02, 0x85, 0x90, 0x09, 0x30, 0xb1, 0x02, 0x85, 0x91, 0x09, +0x31, 0xb1, 0x02, 0x85, 0x92, 0x09, 0x32, 0xb1, 0x02, 0x85, 0x93, 0x09, 0x33, 0xb1, 0x02, 0x85, +0x94, 0x09, 0x34, 0xb1, 0x02, 0x85, 0xa0, 0x09, 0x40, 0xb1, 0x02, 0x85, 0xa4, 0x09, 0x44, 0xb1, +0x02, 0x85, 0xa7, 0x09, 0x45, 0xb1, 0x02, 0x85, 0xa8, 0x09, 0x45, 0xb1, 0x02, 0x85, 0xa9, 0x09, +0x45, 0xb1, 0x02, 0x85, 0xaa, 0x09, 0x45, 0xb1, 0x02, 0x85, 0xab, 0x09, 0x45, 0xb1, 0x02, 0x85, +0xac, 0x09, 0x45, 0xb1, 0x02, 0x85, 0xad, 0x09, 0x45, 0xb1, 0x02, 0x85, 0xb3, 0x09, 0x45, 0xb1, +0x02, 0x85, 0xb4, 0x09, 0x46, 0xb1, 0x02, 0x85, 0xb5, 0x09, 0x47, 0xb1, 0x02, 0x85, 0xd0, 0x09, +0x40, 0xb1, 0x02, 0x85, 0xd4, 0x09, 0x44, 0xb1, 0x02, 0xc0, 0x00 +}; + static int uhid_write(int fd, const struct uhid_event *ev) { ssize_t ret; @@ -308,21 +363,25 @@ static int uhid_write(int fd, const struct uhid_event *ev) } } -static int create(int fd) +static int create(int fd, bool bluetooth) { struct uhid_event ev; memset(&ev, 0, sizeof(ev)); ev.type = UHID_CREATE; strcpy((char*)ev.u.create.name, "Sony Corp. DualShock 4 [CUH-ZCT2x]"); - ev.u.create.rd_data = rdesc; - ev.u.create.rd_size = sizeof(rdesc); - ev.u.create.bus = BUS_USB; + ev.u.create.rd_data = bluetooth ? rdesc_bt : rdesc; + ev.u.create.rd_size = bluetooth ? sizeof(rdesc_bt) : sizeof(rdesc); + ev.u.create.bus = bluetooth ? BUS_BLUETOOTH : BUS_USB; ev.u.create.vendor = 0x054C; ev.u.create.product = 0x09CC; ev.u.create.version = 0; ev.u.create.country = 0; + sprintf((char*)ev.u.create.uniq, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", + MAC_ADDR[5], MAC_ADDR[4], MAC_ADDR[3], MAC_ADDR[2], MAC_ADDR[1], MAC_ADDR[0] + ); + return uhid_write(fd, &ev); } @@ -370,7 +429,7 @@ static ds4_dpad_status_t ds4_dpad_from_gamepad(uint8_t dpad) { return DPAD_RELEASED; } -int virt_dualshock_init(virt_dualshock_t *const out_gamepad) { +int virt_dualshock_init(virt_dualshock_t *const out_gamepad, bool bluetooth) { int ret = 0; out_gamepad->dt_sum = 0; @@ -379,6 +438,7 @@ int virt_dualshock_init(virt_dualshock_t *const out_gamepad) { out_gamepad->debug = false; out_gamepad->empty_reports = 0; out_gamepad->last_time = 0; + out_gamepad->bluetooth = bluetooth; out_gamepad->fd = open(path, O_RDWR | O_CLOEXEC /* | O_NONBLOCK */); if (out_gamepad->fd < 0) { @@ -387,7 +447,7 @@ int virt_dualshock_init(virt_dualshock_t *const out_gamepad) { goto virt_dualshock_init_err; } - ret = create(out_gamepad->fd); + ret = create(out_gamepad->fd, out_gamepad->bluetooth); if (ret) { fprintf(stderr, "Error creating uhid device: %d\n", ret); close(out_gamepad->fd); @@ -452,57 +512,57 @@ int virt_dualshock_event(virt_dualshock_t *const gamepad, gamepad_status_t *cons if (ev.u.output.rtype != UHID_OUTPUT_REPORT) return 0; - /* - if (ds4->update_rumble) { - * Select classic rumble style haptics and enable it. * - common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_MOTOR; - common->motor_left = ds4->motor_left; - common->motor_right = ds4->motor_right; - ds4->update_rumble = false; - } - - if (ds4->update_lightbar) { - common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED; - * Comptabile behavior with hid-sony, which used a dummy global LED to - * allow enabling/disabling the lightbar. The global LED maps to - * lightbar_enabled. - * - common->lightbar_red = ds4->lightbar_enabled ? ds4->lightbar_red : 0; - common->lightbar_green = ds4->lightbar_enabled ? ds4->lightbar_green : 0; - common->lightbar_blue = ds4->lightbar_enabled ? ds4->lightbar_blue : 0; - ds4->update_lightbar = false; - } - - if (ds4->update_lightbar_blink) { - common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED_BLINK; - common->lightbar_blink_on = ds4->lightbar_blink_on; - common->lightbar_blink_off = ds4->lightbar_blink_off; - ds4->update_lightbar_blink = false; - } - */ - - if (ev.u.output.size != 32) { - fprintf(stderr, "Invalid data length: got %d, expected 32\n", (int)ev.u.output.size); + if ( + (!gamepad->bluetooth) && (ev.u.output.size != DS4_OUTPUT_REPORT_USB_SIZE) && + (gamepad->bluetooth) && (ev.u.output.size != DS4_OUTPUT_REPORT_BT_SIZE) + ) { + fprintf( + stderr, + "Invalid data length: got %d, expected %d\n", + (int)ev.u.output.size, + (gamepad->bluetooth) ? DS4_OUTPUT_REPORT_BT_SIZE : DS4_OUTPUT_REPORT_USB_SIZE + ); return 0; } // first byte is report-id which is 0x01 - if (ev.u.output.data[0] != 0x05) { - fprintf(stderr, "Unrecognised report-id: %d\n", (int)ev.u.output.data[0]); + if ( + (!gamepad->bluetooth) && (ev.u.output.data[0] != DS4_OUTPUT_REPORT_USB) && + (gamepad->bluetooth) && ((ev.u.output.data[0] != DS4_OUTPUT_REPORT_BT) && (ev.u.output.data[0] < 0x10)) + ) { + fprintf( + stderr, + "Unrecognised report-id: got 0x%x expected 0x%x\n", + (int)ev.u.output.data[0], + (gamepad->bluetooth) ? DS4_OUTPUT_REPORT_BT : DS4_OUTPUT_REPORT_USB + ); return 0; } - const uint8_t valid_flag0 = ev.u.output.data[1]; - const uint8_t valid_flag1 = ev.u.output.data[2]; - const uint8_t reserved = ev.u.output.data[3]; - const uint8_t motor_right = ev.u.output.data[4]; - const uint8_t motor_left = ev.u.output.data[5]; - const uint8_t lightbar_red = ev.u.output.data[6]; - const uint8_t lightbar_green = ev.u.output.data[7]; - const uint8_t lightbar_blue = ev.u.output.data[8]; - const uint8_t lightbar_blink_on = ev.u.output.data[9]; - const uint8_t lightbar_blink_off = ev.u.output.data[10]; + // When using bluetooth, the first byte after the reportID is uint8_t seq_tag, + // while the next one is uint8_t tag, following bytes are the same. + size_t offset = 1; + if ((gamepad->bluetooth) && (ev.u.output.data[0] > 0x10)) { + offset = 2; + } else if ((gamepad->bluetooth) && (ev.u.output.data[0] == 0x02)) { + offset = 3; + } else if ((gamepad->bluetooth) && (ev.u.output.data[0] == 0x01)) { + offset = 1; + } + + const uint8_t *const common_report = &ev.u.output.data[offset]; + + const uint8_t valid_flag0 = common_report[0]; + const uint8_t valid_flag1 = common_report[1]; + const uint8_t reserved = common_report[2]; + const uint8_t motor_right = common_report[3]; + const uint8_t motor_left = common_report[4]; + const uint8_t lightbar_red = common_report[5]; + const uint8_t lightbar_green = common_report[6]; + const uint8_t lightbar_blue = common_report[7]; + const uint8_t lightbar_blink_on = common_report[8]; + const uint8_t lightbar_blink_off = common_report[9]; if ((valid_flag0 & DS4_OUTPUT_VALID_FLAG0_LED)) { out_device_status->leds_colors[0] = lightbar_red; @@ -543,32 +603,40 @@ int virt_dualshock_event(virt_dualshock_t *const gamepad, gamepad_status_t *cons } if (ev.u.get_report.rnum == 18) { - const struct uhid_event mac_addr_response = { + struct uhid_event mac_addr_response = { .type = UHID_GET_REPORT_REPLY, .u = { .get_report_reply = { - .size = 16, + .size = DS4_FEATURE_REPORT_PAIRING_INFO_SIZE, .id = ev.u.get_report.id, .err = 0, .data = { - 0x12, 0xf2, 0xa5, 0x71, 0x68, 0xaf, 0xdc, 0x08, + DS4_FEATURE_REPORT_PAIRING_INFO, + MAC_ADDR[0], MAC_ADDR[1], MAC_ADDR[2], MAC_ADDR[3], MAC_ADDR[4], MAC_ADDR[5], + 0x08, 0x25, 0x00, 0x4c, 0x46, 0x49, 0x0e, 0x41, 0x00 } } } }; + if (gamepad->bluetooth) { + uint32_t crc = crc32_le(0xFFFFFFFFU, (const uint8_t*)&PS_FEATURE_CRC32_SEED, sizeof(PS_FEATURE_CRC32_SEED)); + crc = ~crc32_le(crc, (const Bytef *)&mac_addr_response.u.get_report_reply.data[0], mac_addr_response.u.get_report_reply.size - 4); + memcpy(&mac_addr_response.u.get_report_reply.data[mac_addr_response.u.get_report_reply.size - sizeof(crc)], &crc, sizeof(crc)); + } + uhid_write(gamepad->fd, &mac_addr_response); - } else if (ev.u.get_report.rnum == 0xa3) { - const struct uhid_event firmware_info_response = { + } else if (ev.u.get_report.rnum == DS4_FEATURE_REPORT_FIRMWARE_INFO) { + struct uhid_event firmware_info_response = { .type = UHID_GET_REPORT_REPLY, .u = { .get_report_reply = { - .size = 49, + .size = DS4_FEATURE_REPORT_FIRMWARE_INFO_SIZE, .id = ev.u.get_report.id, .err = 0, .data = { - 0xa3, 0x53, 0x65, 0x70, 0x20, 0x32, 0x31, 0x20, + DS4_FEATURE_REPORT_FIRMWARE_INFO, 0x53, 0x65, 0x70, 0x20, 0x32, 0x31, 0x20, 0x32, 0x30, 0x31, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x34, 0x3a, 0x35, 0x30, 0x3a, 0x35, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -580,22 +648,32 @@ int virt_dualshock_event(virt_dualshock_t *const gamepad, gamepad_status_t *cons } }; + if (gamepad->bluetooth) { + uint32_t crc = crc32_le(0xFFFFFFFFU, (const uint8_t*)&PS_FEATURE_CRC32_SEED, sizeof(PS_FEATURE_CRC32_SEED)); + crc = ~crc32_le(crc, (const Bytef *)&firmware_info_response.u.get_report_reply.data[0], firmware_info_response.u.get_report_reply.size - 4); + memcpy(&firmware_info_response.u.get_report_reply.data[firmware_info_response.u.get_report_reply.size - sizeof(crc)], &crc, sizeof(crc)); + } + uhid_write(gamepad->fd, &firmware_info_response); - } else if (ev.u.get_report.rnum == 0x02) { // dualshock4_get_calibration_data - struct uhid_event firmware_info_response = { + } else if ( + ((gamepad->bluetooth) && (ev.u.get_report.rnum == DS4_FEATURE_REPORT_CALIBRATION_BT)) || + ((!gamepad->bluetooth) && (ev.u.get_report.rnum == DS4_FEATURE_REPORT_CALIBRATION)) + ) { // dualshock4_get_calibration_data + struct uhid_event calibration_response = { .type = UHID_GET_REPORT_REPLY, .u = { .get_report_reply = { - .size = 37, + .size = gamepad->bluetooth ? DS4_FEATURE_REPORT_CALIBRATION_BT_SIZE : DS4_FEATURE_REPORT_CALIBRATION_SIZE, .id = ev.u.get_report.id, .err = 0, .data = { - 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + gamepad->bluetooth ? DS4_FEATURE_REPORT_CALIBRATION_BT : DS4_FEATURE_REPORT_CALIBRATION, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x06, 0x00, - + 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, + 0x00, } } } @@ -605,25 +683,31 @@ int virt_dualshock_event(virt_dualshock_t *const gamepad, gamepad_status_t *cons // speed_2x = speed_2x*DS4_GYRO_RES_PER_DEG_S; calculated by the kernel will be 1080. // As a consequence sens_numer (for every axis) is 1080*1024. // that number will be 1105920 - memcpy((void*)&firmware_info_response.u.get_report_reply.data[1], (const void*)&gyro_pitch_bias, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[3], (const void*)&gyro_yaw_bias, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[5], (const void*)&gyro_roll_bias, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[7], (const void*)&gyro_pitch_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[9], (const void*)&gyro_pitch_minus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[11], (const void*)&gyro_yaw_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[13], (const void*)&gyro_yaw_minus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[15], (const void*)&gyro_roll_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[17], (const void*)&gyro_roll_minus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[19], (const void*)&gyro_speed_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[21], (const void*)&gyro_speed_minus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[23], (const void*)&acc_x_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[25], (const void*)&acc_x_minus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[27], (const void*)&acc_y_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[29], (const void*)&acc_y_minus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[31], (const void*)&acc_z_plus, sizeof(int16_t)); - memcpy((void*)&firmware_info_response.u.get_report_reply.data[33], (const void*)&acc_z_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[1], (const void*)&gyro_pitch_bias, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[3], (const void*)&gyro_yaw_bias, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[5], (const void*)&gyro_roll_bias, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[7], (const void*)&gyro_pitch_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[9], (const void*)&gyro_pitch_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[11], (const void*)&gyro_yaw_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[13], (const void*)&gyro_yaw_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[15], (const void*)&gyro_roll_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[17], (const void*)&gyro_roll_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[19], (const void*)&gyro_speed_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[21], (const void*)&gyro_speed_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[23], (const void*)&acc_x_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[25], (const void*)&acc_x_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[27], (const void*)&acc_y_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[29], (const void*)&acc_y_minus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[31], (const void*)&acc_z_plus, sizeof(int16_t)); + memcpy((void*)&calibration_response.u.get_report_reply.data[33], (const void*)&acc_z_minus, sizeof(int16_t)); - uhid_write(gamepad->fd, &firmware_info_response); + if (gamepad->bluetooth) { + uint32_t crc = crc32_le(0xFFFFFFFFU, (const uint8_t*)&PS_FEATURE_CRC32_SEED, sizeof(PS_FEATURE_CRC32_SEED)); + crc = ~crc32_le(crc, (const Bytef *)&calibration_response.u.get_report_reply.data[0], calibration_response.u.get_report_reply.size - 4); + memcpy(&calibration_response.u.get_report_reply.data[calibration_response.u.get_report_reply.size - sizeof(crc)], &crc, sizeof(crc)); + } + + uhid_write(gamepad->fd, &calibration_response); } break; @@ -642,9 +726,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; @@ -720,18 +804,21 @@ void virt_dualshock_compose(virt_dualshock_t *const gamepad, gamepad_status_t *c const int16_t a_y = (int16_t)(-1) * in_device_status->raw_accel[1]; // Swap Y and Z const int16_t a_z = (int16_t)(-1) * in_device_status->raw_accel[2]; // Swap Y and Z - out_buf[0] = 0x01; // [00] report ID (0x01) - out_buf[1] = ((uint64_t)((int64_t)in_device_status->joystick_positions[0][0] + (int64_t)32768) >> (uint64_t)8); // L stick, X axis - out_buf[2] = ((uint64_t)((int64_t)in_device_status->joystick_positions[0][1] + (int64_t)32768) >> (uint64_t)8); // L stick, Y axis - out_buf[3] = ((uint64_t)((int64_t)in_device_status->joystick_positions[1][0] + (int64_t)32768) >> (uint64_t)8); // R stick, X axis - out_buf[4] = ((uint64_t)((int64_t)in_device_status->joystick_positions[1][1] + (int64_t)32768) >> (uint64_t)8); // R stick, Y axis - out_buf[5] = + out_buf[0] = gamepad->bluetooth ? DS4_INPUT_REPORT_BT : DS4_INPUT_REPORT_USB; // [00] report ID (0x01) + + uint8_t *const out_shifted_buf = gamepad->bluetooth ? &out_buf[1] : &out_buf[0]; + + out_shifted_buf[1] = ((uint64_t)((int64_t)in_device_status->joystick_positions[0][0] + (int64_t)32768) >> (uint64_t)8); // L stick, X axis + out_shifted_buf[2] = ((uint64_t)((int64_t)in_device_status->joystick_positions[0][1] + (int64_t)32768) >> (uint64_t)8); // L stick, Y axis + out_shifted_buf[3] = ((uint64_t)((int64_t)in_device_status->joystick_positions[1][0] + (int64_t)32768) >> (uint64_t)8); // R stick, X axis + out_shifted_buf[4] = ((uint64_t)((int64_t)in_device_status->joystick_positions[1][1] + (int64_t)32768) >> (uint64_t)8); // R stick, Y axis + out_shifted_buf[5] = (in_device_status->triangle ? 0x80 : 0x00) | (in_device_status->circle ? 0x40 : 0x00) | (in_device_status->cross ? 0x20 : 0x00) | (in_device_status->square ? 0x10 : 0x00) | (uint8_t)ds4_dpad_from_gamepad(in_device_status->dpad); - out_buf[6] = + out_shifted_buf[6] = (in_device_status->r3 ? 0x80 : 0x00) | (in_device_status->l3 ? 0x40 : 0x00) | (in_device_status->share ? 0x20 : 0x00) | @@ -746,27 +833,33 @@ void virt_dualshock_compose(virt_dualshock_t *const gamepad, gamepad_status_t *c buf[7] = (((counter++) % (uint8_t)64) << ((uint8_t)2)) | get_buttons_byte3_by_gs(&gs); */ - out_buf[7] = in_device_status->center ? 0x01 : 0x00; + out_shifted_buf[7] = in_device_status->center ? 0x01 : 0x00; - out_buf[8] = in_device_status->l2_trigger; - out_buf[9] = in_device_status->r2_trigger; - memcpy(&out_buf[10], ×tamp, sizeof(timestamp)); - out_buf[12] = 0x20; // [12] battery level | this is called sensor_temparature in the kernel driver but is never used... - memcpy(&out_buf[13], &g_x, sizeof(int16_t)); - memcpy(&out_buf[15], &g_y, sizeof(int16_t)); - memcpy(&out_buf[17], &g_z, sizeof(int16_t)); - memcpy(&out_buf[19], &a_x, sizeof(int16_t)); - memcpy(&out_buf[21], &a_y, sizeof(int16_t)); - memcpy(&out_buf[23], &a_z, sizeof(int16_t)); + out_shifted_buf[8] = in_device_status->l2_trigger; + out_shifted_buf[9] = in_device_status->r2_trigger; + memcpy(&out_shifted_buf[10], ×tamp, sizeof(timestamp)); + out_shifted_buf[12] = 0x20; // [12] battery level | this is called sensor_temparature in the kernel driver but is never used... + memcpy(&out_shifted_buf[13], &g_x, sizeof(int16_t)); + memcpy(&out_shifted_buf[15], &g_y, sizeof(int16_t)); + memcpy(&out_shifted_buf[17], &g_z, sizeof(int16_t)); + memcpy(&out_shifted_buf[19], &a_x, sizeof(int16_t)); + memcpy(&out_shifted_buf[21], &a_y, sizeof(int16_t)); + memcpy(&out_shifted_buf[23], &a_z, sizeof(int16_t)); - out_buf[30] = 0x1b; // no headset attached + out_shifted_buf[30] = 0x1b; // no headset attached - out_buf[62] = 0x80; // IDK... it seems constant... - out_buf[57] = 0x80; // IDK... it seems constant... - out_buf[53] = 0x80; // IDK... it seems constant... - out_buf[48] = 0x80; // IDK... it seems constant... - out_buf[35] = 0x80; // IDK... it seems constant... - out_buf[44] = 0x80; // IDK... it seems constant... + out_shifted_buf[62] = 0x80; // IDK... it seems constant... + out_shifted_buf[57] = 0x80; // IDK... it seems constant... + out_shifted_buf[53] = 0x80; // IDK... it seems constant... + out_shifted_buf[48] = 0x80; // IDK... it seems constant... + out_shifted_buf[35] = 0x80; // IDK... it seems constant... + out_shifted_buf[44] = 0x80; // IDK... it seems constant... + + if (gamepad->bluetooth) { + uint32_t crc = crc32_le(0xFFFFFFFFU, (const uint8_t*)&PS_FEATURE_CRC32_SEED, sizeof(PS_FEATURE_CRC32_SEED)); + crc = ~crc32_le(crc, (const Bytef *)&out_shifted_buf[0], DS4_INPUT_REPORT_BT_SIZE - 4); + memcpy(&out_shifted_buf[DS4_INPUT_REPORT_BT_SIZE - sizeof(crc)], &crc, sizeof(crc)); + } } int virt_dualshock_send(virt_dualshock_t *const gamepad, uint8_t *const out_buf) { @@ -774,12 +867,18 @@ int virt_dualshock_send(virt_dualshock_t *const gamepad, uint8_t *const out_buf) .type = UHID_INPUT2, .u = { .input2 = { - .size = 64, + .size = gamepad->bluetooth ? DS4_INPUT_REPORT_BT_SIZE : DS4_INPUT_REPORT_USB_SIZE, } } }; memcpy(&l.u.input2.data[0], &out_buf[0], l.u.input2.size); + if (gamepad->bluetooth) { + uint32_t crc = crc32_le(0xFFFFFFFFU, (const uint8_t*)&PS_INPUT_CRC32_SEED, sizeof(PS_INPUT_CRC32_SEED)); + crc = ~crc32_le(crc, (const uint8_t *)&l.u.input2.data[0], l.u.input2.size - 4); + memcpy(&l.u.input2.data[l.u.input2.size - sizeof(crc)], &crc, sizeof(crc)); + } + return uhid_write(gamepad->fd, &l); } diff --git a/virt_ds4.h b/virt_ds4.h index 986e011..b9faa7f 100644 --- a/virt_ds4.h +++ b/virt_ds4.h @@ -13,15 +13,17 @@ typedef struct virt_dualshock { bool debug; + bool bluetooth; + uint32_t dt_sum; uint8_t dt_buffer_current; 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); +int virt_dualshock_init(virt_dualshock_t *const gamepad, bool bluetooth); int virt_dualshock_get_fd(virt_dualshock_t *const gamepad); diff --git a/virt_ds5.c b/virt_ds5.c index da896e6..ee5d274 100644 --- a/virt_ds5.c +++ b/virt_ds5.c @@ -1,7 +1,6 @@ #include "virt_ds5.h" #include "message.h" -#include #include #define DS_FEATURE_REPORT_PAIRING_INFO 0x09 @@ -31,15 +30,6 @@ #define DS5_SPEC_DELTA_TIME 4096.0f -static uint32_t le(uint32_t num) { - const uint32_t b0 = (num & 0x000000ff) << 24u; - const uint32_t b1 = (num & 0x0000ff00) << 8u; - const uint32_t b2 = (num & 0x00ff0000) >> 8u; - const uint32_t b3 = (num & 0xff000000) >> 24u; - - return b0 | b1 | b2 | b3; -} - static uint32_t crc32_le(uint32_t crc_initial, const uint8_t *const buf, size_t len) { return crc32(crc_initial ^ 0xffffffff, buf, len) ^ 0xffffffff; } @@ -1084,21 +1074,26 @@ int virt_dualsense_event(virt_dualsense_t *const gamepad, gamepad_status_t *cons uint8_t lightbar_green = common_report[45]; uint8_t lightbar_blue = common_report[46]; - if ((valid_flag0 & DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT)) { + if ((valid_flag0 & DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT) || ((motor_left == 0) && (motor_right == 0))) { + uint8_t motors_shift = 0; if ((valid_flag2 & DS_OUTPUT_VALID_FLAG2_COMPATIBLE_VIBRATION2) || (valid_flag0 & DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION)) { + out_device_status->motors_intensity[0] = (motor_left << 1) | ((motor_left == 0) ? 0 : 1); + out_device_status->motors_intensity[1] = (motor_right << 1) | ((motor_right == 0) ? 0 : 1); + } else { out_device_status->motors_intensity[0] = motor_left; out_device_status->motors_intensity[1] = motor_right; - ++out_device_status->rumble_events_count; + } - if (gamepad->debug) { - printf( - "Updated rumble -- motor_left: %d, motor_right: %d, valid_flag0; %d, valid_flag1: %d\n", - motor_left, - motor_right, - valid_flag0, - valid_flag1 - ); - } + ++out_device_status->rumble_events_count; + + if (gamepad->debug) { + printf( + "Updated rumble -- motor_left: %d, motor_right: %d, valid_flag0; %d, valid_flag1: %d\n", + motor_left, + motor_right, + valid_flag0, + valid_flag1 + ); } } @@ -1243,9 +1238,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; @@ -1323,10 +1318,10 @@ void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *c memcpy(&out_shifted_buf[28], ×tamp, sizeof(timestamp)); // point of contact number 0 - out_shifted_buf[33] = in_device_status->touchpad_touch_num == -1 ? 0x80 : 0x7F; //contact - out_shifted_buf[34] = 0x7F/*in_device_status->touchpad_x & (int16_t)0x00FF*/; //x_lo - out_shifted_buf[35] = (((in_device_status->touchpad_x & (int16_t)0x0F00) >> (int16_t)8) /*| ((in_device_status->touchpad_y & (int16_t)0x000F) << (int16_t)4)*/); // x_hi:4 y_lo:4 - out_shifted_buf[36] = 0 /*((in_device_status->touchpad_y & (int16_t)0x0FF0) >> (int16_t)4)*/; //y_hi + out_shifted_buf[33] = ((in_device_status->touchpad_touch_num == -1) && (!in_device_status->touchpad_press)) ? 0x80 : 0x7F; //contact + out_shifted_buf[34] = in_device_status->touchpad_x & (int16_t)0x00FF; //x_lo + out_shifted_buf[35] = (((in_device_status->touchpad_x & (int16_t)0x0F00) >> (int16_t)8) | ((in_device_status->touchpad_y & (int16_t)0x000F) << (int16_t)4)); // x_hi:4 y_lo:4 + out_shifted_buf[36] = (in_device_status->touchpad_y & (int16_t)0x0FF0) >> (int16_t)4; //y_hi // point of contact number 1 out_shifted_buf[37] = 0x80; //contact @@ -1339,10 +1334,6 @@ void virt_dualsense_compose(virt_dualsense_t *const gamepad, gamepad_status_t *c crc = ~crc32_le(crc, (const Bytef *)&out_shifted_buf[0], DS_INPUT_REPORT_BT_SIZE - 4); memcpy(&out_shifted_buf[DS_INPUT_REPORT_BT_SIZE - sizeof(crc)], &crc, sizeof(crc)); } - - if (in_device_status->touchpad_touch_num != -1) { - printf("%4x -> %2x @\n", (int)in_device_status->touchpad_x, (int)out_shifted_buf[35]); - } } int virt_dualsense_send(virt_dualsense_t *const gamepad, uint8_t *const out_shifted_buf) { 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);