From 55a566a6b407e2dd7a2b1d8a49f3de3e8e0d254c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 24 Nov 2025 18:04:11 -0800
Subject: [PATCH] Steam expects the gyro data to come before the accelerometer
data
---
src/joystick/hidapi/SDL_hidapi_sinput.c | 38 ++++++++++++------------
src/joystick/hidapi/SDL_hidapi_switch2.c | 10 +++----
2 files changed, 24 insertions(+), 24 deletions(-)
diff --git a/src/joystick/hidapi/SDL_hidapi_sinput.c b/src/joystick/hidapi/SDL_hidapi_sinput.c
index b53df62e293e0..f84dfaa871c00 100644
--- a/src/joystick/hidapi/SDL_hidapi_sinput.c
+++ b/src/joystick/hidapi/SDL_hidapi_sinput.c
@@ -735,14 +735,14 @@ static bool HIDAPI_DriverSInput_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joys
joystick->nhats = 1;
}
- if (ctx->accelerometer_supported) {
- SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 1000000.0f / ctx->polling_rate_us);
- }
-
if (ctx->gyroscope_supported) {
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 1000000.0f / ctx->polling_rate_us);
}
+ if (ctx->accelerometer_supported) {
+ SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 1000000.0f / ctx->polling_rate_us);
+ }
+
if (ctx->touchpad_supported) {
// If touchpad is supported, minimum 1, max is capped
ctx->touchpad_count = SDL_clamp(ctx->touchpad_count, 1, SINPUT_MAX_ALLOWED_TOUCHPADS);
@@ -995,21 +995,6 @@ static void HIDAPI_DriverSInput_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
// Update last timestamp
ctx->last_imu_timestamp_us = imu_timestamp_us;
- // Process Accelerometer
- if (ctx->accelerometer_supported) {
-
- accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_Y);
- imu_values[2] = -(float)accel * ctx->accelScale; // Y-axis acceleration
-
- accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_Z);
- imu_values[1] = (float)accel * ctx->accelScale; // Z-axis acceleration
-
- accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_X);
- imu_values[0] = -(float)accel * ctx->accelScale; // X-axis acceleration
-
- SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, ctx->imu_timestamp_ns, imu_values, 3);
- }
-
// Process Gyroscope
if (ctx->gyroscope_supported) {
@@ -1024,6 +1009,21 @@ static void HIDAPI_DriverSInput_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, ctx->imu_timestamp_ns, imu_values, 3);
}
+
+ // Process Accelerometer
+ if (ctx->accelerometer_supported) {
+
+ accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_Y);
+ imu_values[2] = -(float)accel * ctx->accelScale; // Y-axis acceleration
+
+ accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_Z);
+ imu_values[1] = (float)accel * ctx->accelScale; // Z-axis acceleration
+
+ accel = EXTRACTSINT16(data, SINPUT_REPORT_IDX_IMU_ACCEL_X);
+ imu_values[0] = -(float)accel * ctx->accelScale; // X-axis acceleration
+
+ SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, ctx->imu_timestamp_ns, imu_values, 3);
+ }
}
// Check if we should process touchpad
diff --git a/src/joystick/hidapi/SDL_hidapi_switch2.c b/src/joystick/hidapi/SDL_hidapi_switch2.c
index dc68b02e1b122..3d8bc779a258f 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch2.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch2.c
@@ -1195,8 +1195,8 @@ static void HIDAPI_DriverSwitch2_HandleStatePacket(SDL_HIDAPI_Device *device, SD
switch (ctx->device->product_id) {
case USB_PRODUCT_NINTENDO_SWITCH2_JOYCON_LEFT:
if (ctx->device->parent) {
- SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL_L, sensor_timestamp, accel_data, 3);
SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO_L, sensor_timestamp, gyro_data, 3);
+ SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL_L, sensor_timestamp, accel_data, 3);
} else {
float tmp = -accel_data[0];
accel_data[0] = accel_data[2];
@@ -1206,14 +1206,14 @@ static void HIDAPI_DriverSwitch2_HandleStatePacket(SDL_HIDAPI_Device *device, SD
gyro_data[0] = gyro_data[2];
gyro_data[2] = tmp;
- SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_data, 3);
SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, gyro_data, 3);
+ SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_data, 3);
}
break;
case USB_PRODUCT_NINTENDO_SWITCH2_JOYCON_RIGHT:
if (ctx->device->parent) {
- SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL_R, sensor_timestamp, accel_data, 3);
SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO_R, sensor_timestamp, gyro_data, 3);
+ SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL_R, sensor_timestamp, accel_data, 3);
} else {
float tmp = accel_data[0];
accel_data[0] = -accel_data[2];
@@ -1223,13 +1223,13 @@ static void HIDAPI_DriverSwitch2_HandleStatePacket(SDL_HIDAPI_Device *device, SD
gyro_data[0] = -gyro_data[2];
gyro_data[2] = tmp;
- SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_data, 3);
SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, gyro_data, 3);
+ SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_data, 3);
}
break;
default:
- SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_data, 3);
SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, gyro_data, 3);
+ SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_data, 3);
break;
}
}