summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/sdl_driver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/drivers/sdl_driver.cpp')
-rw-r--r--src/input_common/drivers/sdl_driver.cpp54
1 files changed, 35 insertions, 19 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp
index a0103edde..d975eb815 100644
--- a/src/input_common/drivers/sdl_driver.cpp
+++ b/src/input_common/drivers/sdl_driver.cpp
@@ -40,25 +40,26 @@ public:
40 } 40 }
41 41
42 void EnableMotion() { 42 void EnableMotion() {
43 if (sdl_controller) { 43 if (!sdl_controller) {
44 SDL_GameController* controller = sdl_controller.get(); 44 return;
45 has_accel = SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE; 45 }
46 has_gyro = SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE; 46 SDL_GameController* controller = sdl_controller.get();
47 if (has_accel) { 47 if (HasMotion()) {
48 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE); 48 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_FALSE);
49 } 49 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_FALSE);
50 if (has_gyro) { 50 }
51 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE); 51 has_accel = SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE;
52 } 52 has_gyro = SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE;
53 if (has_accel) {
54 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE);
55 }
56 if (has_gyro) {
57 SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, SDL_TRUE);
53 } 58 }
54 } 59 }
55 60
56 bool HasGyro() const { 61 bool HasMotion() const {
57 return has_gyro; 62 return has_gyro || has_accel;
58 }
59
60 bool HasAccel() const {
61 return has_accel;
62 } 63 }
63 64
64 bool UpdateMotion(SDL_ControllerSensorEvent event) { 65 bool UpdateMotion(SDL_ControllerSensorEvent event) {
@@ -85,6 +86,20 @@ public:
85 if (time_difference == 0) { 86 if (time_difference == 0) {
86 return false; 87 return false;
87 } 88 }
89
90 // Motion data is invalid
91 if (motion.accel_x == 0 && motion.gyro_x == 0 && motion.accel_y == 0 &&
92 motion.gyro_y == 0 && motion.accel_z == 0 && motion.gyro_z == 0) {
93 if (motion_error_count++ < 200) {
94 return false;
95 }
96 // Try restarting the sensor
97 motion_error_count = 0;
98 EnableMotion();
99 return false;
100 }
101
102 motion_error_count = 0;
88 motion.delta_timestamp = time_difference * 1000; 103 motion.delta_timestamp = time_difference * 1000;
89 return true; 104 return true;
90 } 105 }
@@ -250,6 +265,7 @@ private:
250 mutable std::mutex mutex; 265 mutable std::mutex mutex;
251 266
252 u64 last_motion_update{}; 267 u64 last_motion_update{};
268 std::size_t motion_error_count{};
253 bool has_gyro{false}; 269 bool has_gyro{false};
254 bool has_accel{false}; 270 bool has_accel{false};
255 bool has_vibration{false}; 271 bool has_vibration{false};
@@ -955,18 +971,18 @@ MotionMapping SDLDriver::GetMotionMappingForDevice(const Common::ParamPackage& p
955 MotionMapping mapping = {}; 971 MotionMapping mapping = {};
956 joystick->EnableMotion(); 972 joystick->EnableMotion();
957 973
958 if (joystick->HasGyro() || joystick->HasAccel()) { 974 if (joystick->HasMotion()) {
959 mapping.insert_or_assign(Settings::NativeMotion::MotionRight, 975 mapping.insert_or_assign(Settings::NativeMotion::MotionRight,
960 BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); 976 BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
961 } 977 }
962 if (params.Has("guid2")) { 978 if (params.Has("guid2")) {
963 joystick2->EnableMotion(); 979 joystick2->EnableMotion();
964 if (joystick2->HasGyro() || joystick2->HasAccel()) { 980 if (joystick2->HasMotion()) {
965 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, 981 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
966 BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID())); 982 BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID()));
967 } 983 }
968 } else { 984 } else {
969 if (joystick->HasGyro() || joystick->HasAccel()) { 985 if (joystick->HasMotion()) {
970 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, 986 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
971 BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); 987 BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
972 } 988 }