summaryrefslogtreecommitdiff
path: root/src/input_common/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/sdl')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp61
1 files changed, 54 insertions, 7 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 1656b85fb..70a0ba09c 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -115,6 +115,41 @@ public:
115 return state.buttons.at(button); 115 return state.buttons.at(button);
116 } 116 }
117 117
118 bool ToggleButton(int button) {
119 std::lock_guard lock{mutex};
120
121 if (!state.toggle_buttons.contains(button) || !state.lock_buttons.contains(button)) {
122 state.toggle_buttons.insert_or_assign(button, false);
123 state.lock_buttons.insert_or_assign(button, false);
124 }
125
126 const bool button_state = state.toggle_buttons.at(button);
127 const bool button_lock = state.lock_buttons.at(button);
128
129 if (button_lock) {
130 return button_state;
131 }
132
133 state.lock_buttons.insert_or_assign(button, true);
134
135 if (button_state) {
136 state.toggle_buttons.insert_or_assign(button, false);
137 } else {
138 state.toggle_buttons.insert_or_assign(button, true);
139 }
140
141 return !button_state;
142 }
143
144 bool UnlockButton(int button) {
145 std::lock_guard lock{mutex};
146 if (!state.toggle_buttons.contains(button)) {
147 return false;
148 }
149 state.lock_buttons.insert_or_assign(button, false);
150 return state.toggle_buttons.at(button);
151 }
152
118 void SetAxis(int axis, Sint16 value) { 153 void SetAxis(int axis, Sint16 value) {
119 std::lock_guard lock{mutex}; 154 std::lock_guard lock{mutex};
120 state.axes.insert_or_assign(axis, value); 155 state.axes.insert_or_assign(axis, value);
@@ -241,6 +276,8 @@ public:
241private: 276private:
242 struct State { 277 struct State {
243 std::unordered_map<int, bool> buttons; 278 std::unordered_map<int, bool> buttons;
279 std::unordered_map<int, bool> toggle_buttons{};
280 std::unordered_map<int, bool> lock_buttons{};
244 std::unordered_map<int, Sint16> axes; 281 std::unordered_map<int, Sint16> axes;
245 std::unordered_map<int, Uint8> hats; 282 std::unordered_map<int, Uint8> hats;
246 } state; 283 } state;
@@ -402,16 +439,25 @@ void SDLState::CloseJoysticks() {
402 439
403class SDLButton final : public Input::ButtonDevice { 440class SDLButton final : public Input::ButtonDevice {
404public: 441public:
405 explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_) 442 explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_, bool toggle_)
406 : joystick(std::move(joystick_)), button(button_) {} 443 : joystick(std::move(joystick_)), button(button_), toggle(toggle_) {}
407 444
408 bool GetStatus() const override { 445 bool GetStatus() const override {
409 return joystick->GetButton(button); 446 const bool button_state = joystick->GetButton(button);
447 if (!toggle) {
448 return button_state;
449 }
450
451 if (button_state) {
452 return joystick->ToggleButton(button);
453 }
454 return joystick->UnlockButton(button);
410 } 455 }
411 456
412private: 457private:
413 std::shared_ptr<SDLJoystick> joystick; 458 std::shared_ptr<SDLJoystick> joystick;
414 int button; 459 int button;
460 bool toggle;
415}; 461};
416 462
417class SDLDirectionButton final : public Input::ButtonDevice { 463class SDLDirectionButton final : public Input::ButtonDevice {
@@ -635,6 +681,7 @@ public:
635 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override { 681 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override {
636 const std::string guid = params.Get("guid", "0"); 682 const std::string guid = params.Get("guid", "0");
637 const int port = params.Get("port", 0); 683 const int port = params.Get("port", 0);
684 const auto toggle = params.Get("toggle", false);
638 685
639 auto joystick = state.GetSDLJoystickByGUID(guid, port); 686 auto joystick = state.GetSDLJoystickByGUID(guid, port);
640 687
@@ -660,7 +707,8 @@ public:
660 707
661 if (params.Has("axis")) { 708 if (params.Has("axis")) {
662 const int axis = params.Get("axis", 0); 709 const int axis = params.Get("axis", 0);
663 const float threshold = params.Get("threshold", 0.5f); 710 // Convert range from (0.0, 1.0) to (-1.0, 1.0)
711 const float threshold = (params.Get("threshold", 0.5f) - 0.5f) * 2.0f;
664 const std::string direction_name = params.Get("direction", ""); 712 const std::string direction_name = params.Get("direction", "");
665 bool trigger_if_greater; 713 bool trigger_if_greater;
666 if (direction_name == "+") { 714 if (direction_name == "+") {
@@ -679,7 +727,7 @@ public:
679 const int button = params.Get("button", 0); 727 const int button = params.Get("button", 0);
680 // This is necessary so accessing GetButton with button won't crash 728 // This is necessary so accessing GetButton with button won't crash
681 joystick->SetButton(button, false); 729 joystick->SetButton(button, false);
682 return std::make_unique<SDLButton>(joystick, button); 730 return std::make_unique<SDLButton>(joystick, button, toggle);
683 } 731 }
684 732
685private: 733private:
@@ -933,12 +981,11 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid
933 params.Set("port", port); 981 params.Set("port", port);
934 params.Set("guid", std::move(guid)); 982 params.Set("guid", std::move(guid));
935 params.Set("axis", axis); 983 params.Set("axis", axis);
984 params.Set("threshold", "0.5");
936 if (value > 0) { 985 if (value > 0) {
937 params.Set("direction", "+"); 986 params.Set("direction", "+");
938 params.Set("threshold", "0.5");
939 } else { 987 } else {
940 params.Set("direction", "-"); 988 params.Set("direction", "-");
941 params.Set("threshold", "-0.5");
942 } 989 }
943 return params; 990 return params;
944} 991}