Merge pull request #9640 from german77/why_sdl

input_common: reset sdl motion if data is invalid
This commit is contained in:
bunnei 2023-01-19 18:47:21 -08:00 committed by GitHub
commit 7d77798f0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,25 +40,26 @@ public:
} }
void EnableMotion() { void EnableMotion() {
if (sdl_controller) { if (!sdl_controller) {
SDL_GameController* controller = sdl_controller.get(); return;
has_accel = SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE; }
has_gyro = SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE; SDL_GameController* controller = sdl_controller.get();
if (has_accel) { if (HasMotion()) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE); SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_FALSE);
} SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_FALSE);
if (has_gyro) { }
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE); has_accel = SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE;
} has_gyro = SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE;
if (has_accel) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE);
}
if (has_gyro) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE);
} }
} }
bool HasGyro() const { bool HasMotion() const {
return has_gyro; return has_gyro || has_accel;
}
bool HasAccel() const {
return has_accel;
} }
bool UpdateMotion(SDL_ControllerSensorEvent event) { bool UpdateMotion(SDL_ControllerSensorEvent event) {
@ -85,6 +86,20 @@ public:
if (time_difference == 0) { if (time_difference == 0) {
return false; return false;
} }
// Motion data is invalid
if (motion.accel_x == 0 && motion.gyro_x == 0 && motion.accel_y == 0 &&
motion.gyro_y == 0 && motion.accel_z == 0 && motion.gyro_z == 0) {
if (motion_error_count++ < 200) {
return false;
}
// Try restarting the sensor
motion_error_count = 0;
EnableMotion();
return false;
}
motion_error_count = 0;
motion.delta_timestamp = time_difference * 1000; motion.delta_timestamp = time_difference * 1000;
return true; return true;
} }
@ -250,6 +265,7 @@ private:
mutable std::mutex mutex; mutable std::mutex mutex;
u64 last_motion_update{}; u64 last_motion_update{};
std::size_t motion_error_count{};
bool has_gyro{false}; bool has_gyro{false};
bool has_accel{false}; bool has_accel{false};
bool has_vibration{false}; bool has_vibration{false};
@ -942,18 +958,18 @@ MotionMapping SDLDriver::GetMotionMappingForDevice(const Common::ParamPackage& p
MotionMapping mapping = {}; MotionMapping mapping = {};
joystick->EnableMotion(); joystick->EnableMotion();
if (joystick->HasGyro() || joystick->HasAccel()) { if (joystick->HasMotion()) {
mapping.insert_or_assign(Settings::NativeMotion::MotionRight, mapping.insert_or_assign(Settings::NativeMotion::MotionRight,
BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
} }
if (params.Has("guid2")) { if (params.Has("guid2")) {
joystick2->EnableMotion(); joystick2->EnableMotion();
if (joystick2->HasGyro() || joystick2->HasAccel()) { if (joystick2->HasMotion()) {
mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID())); BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID()));
} }
} else { } else {
if (joystick->HasGyro() || joystick->HasAccel()) { if (joystick->HasMotion()) {
mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
} }