瀏覽代碼

Steam expects the gyro data to come before the accelerometer data

Sam Lantinga 4 周之前
父節點
當前提交
55a566a6b4
共有 2 個文件被更改,包括 24 次插入24 次删除
  1. 19 19
      src/joystick/hidapi/SDL_hidapi_sinput.c
  2. 5 5
      src/joystick/hidapi/SDL_hidapi_switch2.c

+ 19 - 19
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

+ 5 - 5
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;
         }
     }