summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp55
1 files changed, 51 insertions, 4 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 1656b85fb..7778b3562 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
@@ -679,7 +726,7 @@ public:
679 const int button = params.Get("button", 0); 726 const int button = params.Get("button", 0);
680 // This is necessary so accessing GetButton with button won't crash 727 // This is necessary so accessing GetButton with button won't crash
681 joystick->SetButton(button, false); 728 joystick->SetButton(button, false);
682 return std::make_unique<SDLButton>(joystick, button); 729 return std::make_unique<SDLButton>(joystick, button, toggle);
683 } 730 }
684 731
685private: 732private: