summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/drivers/sdl_driver.cpp54
-rw-r--r--src/input_common/drivers/tas_input.cpp12
-rw-r--r--src/input_common/drivers/tas_input.h2
-rw-r--r--src/input_common/helpers/stick_from_buttons.cpp17
-rw-r--r--src/input_common/input_mapping.cpp2
-rw-r--r--src/input_common/main.cpp26
6 files changed, 83 insertions, 30 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp
index 4818bb744..9835d99d2 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};
@@ -942,18 +958,18 @@ MotionMapping SDLDriver::GetMotionMappingForDevice(const Common::ParamPackage& p
942 MotionMapping mapping = {}; 958 MotionMapping mapping = {};
943 joystick->EnableMotion(); 959 joystick->EnableMotion();
944 960
945 if (joystick->HasGyro() || joystick->HasAccel()) { 961 if (joystick->HasMotion()) {
946 mapping.insert_or_assign(Settings::NativeMotion::MotionRight, 962 mapping.insert_or_assign(Settings::NativeMotion::MotionRight,
947 BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); 963 BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
948 } 964 }
949 if (params.Has("guid2")) { 965 if (params.Has("guid2")) {
950 joystick2->EnableMotion(); 966 joystick2->EnableMotion();
951 if (joystick2->HasGyro() || joystick2->HasAccel()) { 967 if (joystick2->HasMotion()) {
952 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, 968 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
953 BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID())); 969 BuildMotionParam(joystick2->GetPort(), joystick2->GetGUID()));
954 } 970 }
955 } else { 971 } else {
956 if (joystick->HasGyro() || joystick->HasAccel()) { 972 if (joystick->HasMotion()) {
957 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft, 973 mapping.insert_or_assign(Settings::NativeMotion::MotionLeft,
958 BuildMotionParam(joystick->GetPort(), joystick->GetGUID())); 974 BuildMotionParam(joystick->GetPort(), joystick->GetGUID()));
959 } 975 }
diff --git a/src/input_common/drivers/tas_input.cpp b/src/input_common/drivers/tas_input.cpp
index f3ade90da..f3cb14c56 100644
--- a/src/input_common/drivers/tas_input.cpp
+++ b/src/input_common/drivers/tas_input.cpp
@@ -156,10 +156,12 @@ void Tas::RecordInput(u64 buttons, TasAnalog left_axis, TasAnalog right_axis) {
156 }; 156 };
157} 157}
158 158
159std::tuple<TasState, size_t, size_t> Tas::GetStatus() const { 159std::tuple<TasState, size_t, std::array<size_t, PLAYER_NUMBER>> Tas::GetStatus() const {
160 TasState state; 160 TasState state;
161 std::array<size_t, PLAYER_NUMBER> lengths{0};
161 if (is_recording) { 162 if (is_recording) {
162 return {TasState::Recording, 0, record_commands.size()}; 163 lengths[0] = record_commands.size();
164 return {TasState::Recording, record_commands.size(), lengths};
163 } 165 }
164 166
165 if (is_running) { 167 if (is_running) {
@@ -168,7 +170,11 @@ std::tuple<TasState, size_t, size_t> Tas::GetStatus() const {
168 state = TasState::Stopped; 170 state = TasState::Stopped;
169 } 171 }
170 172
171 return {state, current_command, script_length}; 173 for (size_t i = 0; i < PLAYER_NUMBER; i++) {
174 lengths[i] = commands[i].size();
175 }
176
177 return {state, current_command, lengths};
172} 178}
173 179
174void Tas::UpdateThread() { 180void Tas::UpdateThread() {
diff --git a/src/input_common/drivers/tas_input.h b/src/input_common/drivers/tas_input.h
index 38a27a230..5be66d142 100644
--- a/src/input_common/drivers/tas_input.h
+++ b/src/input_common/drivers/tas_input.h
@@ -124,7 +124,7 @@ public:
124 * Current playback progress ; 124 * Current playback progress ;
125 * Total length of script file currently loaded or being recorded 125 * Total length of script file currently loaded or being recorded
126 */ 126 */
127 std::tuple<TasState, size_t, size_t> GetStatus() const; 127 std::tuple<TasState, size_t, std::array<size_t, PLAYER_NUMBER>> GetStatus() const;
128 128
129private: 129private:
130 enum class TasAxis : u8; 130 enum class TasAxis : u8;
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp
index 82aa6ac2f..f3a0b3419 100644
--- a/src/input_common/helpers/stick_from_buttons.cpp
+++ b/src/input_common/helpers/stick_from_buttons.cpp
@@ -13,11 +13,11 @@ class Stick final : public Common::Input::InputDevice {
13public: 13public:
14 using Button = std::unique_ptr<Common::Input::InputDevice>; 14 using Button = std::unique_ptr<Common::Input::InputDevice>;
15 15
16 Stick(Button up_, Button down_, Button left_, Button right_, Button modifier_, 16 Stick(Button up_, Button down_, Button left_, Button right_, Button modifier_, Button updater_,
17 float modifier_scale_, float modifier_angle_) 17 float modifier_scale_, float modifier_angle_)
18 : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)), 18 : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
19 right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_), 19 right(std::move(right_)), modifier(std::move(modifier_)), updater(std::move(updater_)),
20 modifier_angle(modifier_angle_) { 20 modifier_scale(modifier_scale_), modifier_angle(modifier_angle_) {
21 up->SetCallback({ 21 up->SetCallback({
22 .on_change = 22 .on_change =
23 [this](const Common::Input::CallbackStatus& callback_) { 23 [this](const Common::Input::CallbackStatus& callback_) {
@@ -48,6 +48,9 @@ public:
48 UpdateModButtonStatus(callback_); 48 UpdateModButtonStatus(callback_);
49 }, 49 },
50 }); 50 });
51 updater->SetCallback({
52 .on_change = [this](const Common::Input::CallbackStatus& callback_) { SoftUpdate(); },
53 });
51 last_x_axis_value = 0.0f; 54 last_x_axis_value = 0.0f;
52 last_y_axis_value = 0.0f; 55 last_y_axis_value = 0.0f;
53 } 56 }
@@ -248,7 +251,7 @@ public:
248 modifier->ForceUpdate(); 251 modifier->ForceUpdate();
249 } 252 }
250 253
251 void SoftUpdate() override { 254 void SoftUpdate() {
252 Common::Input::CallbackStatus status{ 255 Common::Input::CallbackStatus status{
253 .type = Common::Input::InputType::Stick, 256 .type = Common::Input::InputType::Stick,
254 .stick_status = GetStatus(), 257 .stick_status = GetStatus(),
@@ -308,6 +311,7 @@ private:
308 Button left; 311 Button left;
309 Button right; 312 Button right;
310 Button modifier; 313 Button modifier;
314 Button updater;
311 float modifier_scale{}; 315 float modifier_scale{};
312 float modifier_angle{}; 316 float modifier_angle{};
313 float angle{}; 317 float angle{};
@@ -331,11 +335,12 @@ std::unique_ptr<Common::Input::InputDevice> StickFromButton::Create(
331 auto left = Common::Input::CreateInputDeviceFromString(params.Get("left", null_engine)); 335 auto left = Common::Input::CreateInputDeviceFromString(params.Get("left", null_engine));
332 auto right = Common::Input::CreateInputDeviceFromString(params.Get("right", null_engine)); 336 auto right = Common::Input::CreateInputDeviceFromString(params.Get("right", null_engine));
333 auto modifier = Common::Input::CreateInputDeviceFromString(params.Get("modifier", null_engine)); 337 auto modifier = Common::Input::CreateInputDeviceFromString(params.Get("modifier", null_engine));
338 auto updater = Common::Input::CreateInputDeviceFromString("engine:updater,button:0");
334 auto modifier_scale = params.Get("modifier_scale", 0.5f); 339 auto modifier_scale = params.Get("modifier_scale", 0.5f);
335 auto modifier_angle = params.Get("modifier_angle", 5.5f); 340 auto modifier_angle = params.Get("modifier_angle", 5.5f);
336 return std::make_unique<Stick>(std::move(up), std::move(down), std::move(left), 341 return std::make_unique<Stick>(std::move(up), std::move(down), std::move(left),
337 std::move(right), std::move(modifier), modifier_scale, 342 std::move(right), std::move(modifier), std::move(updater),
338 modifier_angle); 343 modifier_scale, modifier_angle);
339} 344}
340 345
341} // namespace InputCommon 346} // namespace InputCommon
diff --git a/src/input_common/input_mapping.cpp b/src/input_common/input_mapping.cpp
index edd5287c1..d6e49d2c5 100644
--- a/src/input_common/input_mapping.cpp
+++ b/src/input_common/input_mapping.cpp
@@ -76,7 +76,7 @@ void MappingFactory::RegisterButton(const MappingData& data) {
76 break; 76 break;
77 case EngineInputType::Analog: 77 case EngineInputType::Analog:
78 // Ignore mouse axis when mapping buttons 78 // Ignore mouse axis when mapping buttons
79 if (data.engine == "mouse") { 79 if (data.engine == "mouse" && data.index != 4) {
80 return; 80 return;
81 } 81 }
82 new_input.Set("axis", data.index); 82 new_input.Set("axis", data.index);
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
index 4dc92f482..e0b2131ed 100644
--- a/src/input_common/main.cpp
+++ b/src/input_common/main.cpp
@@ -28,6 +28,28 @@
28 28
29namespace InputCommon { 29namespace InputCommon {
30 30
31/// Dummy engine to get periodic updates
32class UpdateEngine final : public InputEngine {
33public:
34 explicit UpdateEngine(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
35 PreSetController(identifier);
36 }
37
38 void PumpEvents() {
39 SetButton(identifier, 0, last_state);
40 last_state = !last_state;
41 }
42
43private:
44 static constexpr PadIdentifier identifier = {
45 .guid = Common::UUID{},
46 .port = 0,
47 .pad = 0,
48 };
49
50 bool last_state{};
51};
52
31struct InputSubsystem::Impl { 53struct InputSubsystem::Impl {
32 template <typename Engine> 54 template <typename Engine>
33 void RegisterEngine(std::string name, std::shared_ptr<Engine>& engine) { 55 void RegisterEngine(std::string name, std::shared_ptr<Engine>& engine) {
@@ -45,6 +67,7 @@ struct InputSubsystem::Impl {
45 void Initialize() { 67 void Initialize() {
46 mapping_factory = std::make_shared<MappingFactory>(); 68 mapping_factory = std::make_shared<MappingFactory>();
47 69
70 RegisterEngine("updater", update_engine);
48 RegisterEngine("keyboard", keyboard); 71 RegisterEngine("keyboard", keyboard);
49 RegisterEngine("mouse", mouse); 72 RegisterEngine("mouse", mouse);
50 RegisterEngine("touch", touch_screen); 73 RegisterEngine("touch", touch_screen);
@@ -74,6 +97,7 @@ struct InputSubsystem::Impl {
74 } 97 }
75 98
76 void Shutdown() { 99 void Shutdown() {
100 UnregisterEngine(update_engine);
77 UnregisterEngine(keyboard); 101 UnregisterEngine(keyboard);
78 UnregisterEngine(mouse); 102 UnregisterEngine(mouse);
79 UnregisterEngine(touch_screen); 103 UnregisterEngine(touch_screen);
@@ -252,6 +276,7 @@ struct InputSubsystem::Impl {
252 } 276 }
253 277
254 void PumpEvents() const { 278 void PumpEvents() const {
279 update_engine->PumpEvents();
255#ifdef HAVE_SDL2 280#ifdef HAVE_SDL2
256 sdl->PumpEvents(); 281 sdl->PumpEvents();
257#endif 282#endif
@@ -263,6 +288,7 @@ struct InputSubsystem::Impl {
263 288
264 std::shared_ptr<MappingFactory> mapping_factory; 289 std::shared_ptr<MappingFactory> mapping_factory;
265 290
291 std::shared_ptr<UpdateEngine> update_engine;
266 std::shared_ptr<Keyboard> keyboard; 292 std::shared_ptr<Keyboard> keyboard;
267 std::shared_ptr<Mouse> mouse; 293 std::shared_ptr<Mouse> mouse;
268 std::shared_ptr<TouchScreen> touch_screen; 294 std::shared_ptr<TouchScreen> touch_screen;