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.cpp59
1 files changed, 53 insertions, 6 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 2f7ccdd37..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);
@@ -130,10 +165,10 @@ public:
130 165
131 if (sdl_controller) { 166 if (sdl_controller) {
132 return SDL_GameControllerRumble(sdl_controller.get(), amp_low, amp_high, 167 return SDL_GameControllerRumble(sdl_controller.get(), amp_low, amp_high,
133 rumble_max_duration_ms) == 0; 168 rumble_max_duration_ms) != -1;
134 } else if (sdl_joystick) { 169 } else if (sdl_joystick) {
135 return SDL_JoystickRumble(sdl_joystick.get(), amp_low, amp_high, 170 return SDL_JoystickRumble(sdl_joystick.get(), amp_low, amp_high,
136 rumble_max_duration_ms) == 0; 171 rumble_max_duration_ms) != -1;
137 } 172 }
138 173
139 return false; 174 return false;
@@ -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
@@ -680,7 +727,7 @@ public:
680 const int button = params.Get("button", 0); 727 const int button = params.Get("button", 0);
681 // This is necessary so accessing GetButton with button won't crash 728 // This is necessary so accessing GetButton with button won't crash
682 joystick->SetButton(button, false); 729 joystick->SetButton(button, false);
683 return std::make_unique<SDLButton>(joystick, button); 730 return std::make_unique<SDLButton>(joystick, button, toggle);
684 } 731 }
685 732
686private: 733private: