Browse Source

Update gamepad sensor rate
SDL_HINT_JOYSTICK_HIDAPI_PS4_REPORT_INTERVAL allows to change sensor rate. Make SDL_GetGamepadSensorDataRate() returns the updated value.

meyraud705 10 months ago
parent
commit
4c9a91b62e
3 changed files with 20 additions and 2 deletions
  1. 12 0
      src/joystick/SDL_joystick.c
  2. 1 0
      src/joystick/SDL_joystick_c.h
  3. 7 2
      src/joystick/hidapi/SDL_hidapi_ps4.c

+ 12 - 0
src/joystick/SDL_joystick.c

@@ -2023,6 +2023,18 @@ void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, f
     }
 }
 
+void SDL_PrivateJoystickSensorRate(SDL_Joystick *joystick, SDL_SensorType type, float rate)
+{
+    int i;
+    SDL_AssertJoysticksLocked();
+
+    for (i = 0; i < joystick->nsensors; ++i) {
+        if (joystick->sensors[i].type == type) {
+            joystick->sensors[i].rate = rate;
+        }
+    }
+}
+
 void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
 {
     SDL_JoystickDriver *driver;

+ 1 - 0
src/joystick/SDL_joystick_c.h

@@ -156,6 +156,7 @@ extern SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid
 /* Internal event queueing functions */
 extern void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers);
 extern void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, float rate);
+extern void SDL_PrivateJoystickSensorRate(SDL_Joystick *joystick, SDL_SensorType type, float rate);
 extern void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id);
 extern SDL_bool SDL_IsJoystickBeingAdded(void);
 extern void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id);

+ 7 - 2
src/joystick/hidapi/SDL_hidapi_ps4.c

@@ -717,8 +717,8 @@ static void HIDAPI_DriverPS4_SetEnhancedModeAvailable(SDL_DriverPS4_Context *ctx
     }
 
     if (ctx->sensors_supported) {
-        SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO, 250.0f);
-        SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, 250.0f);
+        SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO, (float)(1000 / ctx->report_interval));
+        SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, (float)(1000 / ctx->report_interval));
     }
 
     if (ctx->official_controller) {
@@ -818,6 +818,10 @@ static void SDLCALL SDL_PS4ReportIntervalHintChanged(void *userdata, const char
         ctx->report_interval = (Uint8)new_report_interval;
 
         HIDAPI_DriverPS4_UpdateEffects(ctx, SDL_FALSE);
+        SDL_LockJoysticks();
+        SDL_PrivateJoystickSensorRate(ctx->joystick, SDL_SENSOR_GYRO, (float)(1000 / ctx->report_interval));
+        SDL_PrivateJoystickSensorRate(ctx->joystick, SDL_SENSOR_ACCEL, (float)(1000 / ctx->report_interval));
+        SDL_UnlockJoysticks();
     }
 }
 
@@ -849,6 +853,7 @@ static SDL_bool HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
     ctx->rumble_left = 0;
     ctx->rumble_right = 0;
     ctx->color_set = SDL_FALSE;
+    ctx->report_interval = 4;
     SDL_zero(ctx->last_state);
 
     /* Initialize player index (needed for setting LEDs) */