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.cpp53
1 files changed, 37 insertions, 16 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 675b477fa..3c1820c4a 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -49,7 +49,8 @@ static int SDLEventWatcher(void* user_data, SDL_Event* event) {
49class SDLJoystick { 49class SDLJoystick {
50public: 50public:
51 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick) 51 SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick)
52 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose} {} 52 : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose} {
53 }
53 54
54 void SetButton(int button, bool value) { 55 void SetButton(int button, bool value) {
55 std::lock_guard lock{mutex}; 56 std::lock_guard lock{mutex};
@@ -97,6 +98,7 @@ public:
97 std::lock_guard lock{mutex}; 98 std::lock_guard lock{mutex};
98 return (state.hats.at(hat) & direction) != 0; 99 return (state.hats.at(hat) & direction) != 0;
99 } 100 }
101
100 /** 102 /**
101 * The guid of the joystick 103 * The guid of the joystick
102 */ 104 */
@@ -125,6 +127,7 @@ private:
125 std::unordered_map<int, Sint16> axes; 127 std::unordered_map<int, Sint16> axes;
126 std::unordered_map<int, Uint8> hats; 128 std::unordered_map<int, Uint8> hats;
127 } state; 129 } state;
130
128 std::string guid; 131 std::string guid;
129 int port; 132 int port;
130 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; 133 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
@@ -155,7 +158,8 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
155 if (map_it != joystick_map.end()) { 158 if (map_it != joystick_map.end()) {
156 const auto vec_it = 159 const auto vec_it =
157 std::find_if(map_it->second.begin(), map_it->second.end(), 160 std::find_if(map_it->second.begin(), map_it->second.end(),
158 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { 161 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick)
162 {
159 return sdl_joystick == joystick->GetSDLJoystick(); 163 return sdl_joystick == joystick->GetSDLJoystick();
160 }); 164 });
161 if (vec_it != map_it->second.end()) { 165 if (vec_it != map_it->second.end()) {
@@ -166,7 +170,8 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
166 170
167 // Search for a SDLJoystick without a mapped SDL_Joystick... 171 // Search for a SDLJoystick without a mapped SDL_Joystick...
168 const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), 172 const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(),
169 [](const std::shared_ptr<SDLJoystick>& joystick) { 173 [](const std::shared_ptr<SDLJoystick>& joystick)
174 {
170 return !joystick->GetSDLJoystick(); 175 return !joystick->GetSDLJoystick();
171 }); 176 });
172 if (nullptr_it != map_it->second.end()) { 177 if (nullptr_it != map_it->second.end()) {
@@ -223,7 +228,8 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
223 const auto& joystick_guid_list = joystick_map[guid]; 228 const auto& joystick_guid_list = joystick_map[guid];
224 const auto joystick_it = 229 const auto joystick_it =
225 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), 230 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
226 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { 231 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick)
232 {
227 return joystick->GetSDLJoystick() == sdl_joystick; 233 return joystick->GetSDLJoystick() == sdl_joystick;
228 }); 234 });
229 joystick = *joystick_it; 235 joystick = *joystick_it;
@@ -279,7 +285,8 @@ void SDLState::CloseJoysticks() {
279class SDLButton final : public Input::ButtonDevice { 285class SDLButton final : public Input::ButtonDevice {
280public: 286public:
281 explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_) 287 explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_)
282 : joystick(std::move(joystick_)), button(button_) {} 288 : joystick(std::move(joystick_)), button(button_) {
289 }
283 290
284 bool GetStatus() const override { 291 bool GetStatus() const override {
285 return joystick->GetButton(button); 292 return joystick->GetButton(button);
@@ -293,7 +300,8 @@ private:
293class SDLDirectionButton final : public Input::ButtonDevice { 300class SDLDirectionButton final : public Input::ButtonDevice {
294public: 301public:
295 explicit SDLDirectionButton(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_) 302 explicit SDLDirectionButton(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_)
296 : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {} 303 : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {
304 }
297 305
298 bool GetStatus() const override { 306 bool GetStatus() const override {
299 return joystick->GetHatDirection(hat, direction); 307 return joystick->GetHatDirection(hat, direction);
@@ -310,7 +318,8 @@ public:
310 explicit SDLAxisButton(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_, 318 explicit SDLAxisButton(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_,
311 bool trigger_if_greater_) 319 bool trigger_if_greater_)
312 : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_), 320 : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_),
313 trigger_if_greater(trigger_if_greater_) {} 321 trigger_if_greater(trigger_if_greater_) {
322 }
314 323
315 bool GetStatus() const override { 324 bool GetStatus() const override {
316 const float axis_value = joystick->GetAxis(axis); 325 const float axis_value = joystick->GetAxis(axis);
@@ -330,7 +339,8 @@ private:
330class SDLAnalog final : public Input::AnalogDevice { 339class SDLAnalog final : public Input::AnalogDevice {
331public: 340public:
332 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_) 341 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_)
333 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {} 342 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {
343 }
334 344
335 std::tuple<float, float> GetStatus() const override { 345 std::tuple<float, float> GetStatus() const override {
336 const auto [x, y] = joystick->GetAnalog(axis_x, axis_y); 346 const auto [x, y] = joystick->GetAnalog(axis_x, axis_y);
@@ -368,7 +378,9 @@ private:
368/// A button device factory that creates button devices from SDL joystick 378/// A button device factory that creates button devices from SDL joystick
369class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { 379class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> {
370public: 380public:
371 explicit SDLButtonFactory(SDLState& state_) : state(state_) {} 381 explicit SDLButtonFactory(SDLState& state_)
382 : state(state_) {
383 }
372 384
373 /** 385 /**
374 * Creates a button device from a joystick button 386 * Creates a button device from a joystick button
@@ -443,7 +455,10 @@ private:
443/// An analog device factory that creates analog devices from SDL joystick 455/// An analog device factory that creates analog devices from SDL joystick
444class SDLAnalogFactory final : public Input::Factory<Input::AnalogDevice> { 456class SDLAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
445public: 457public:
446 explicit SDLAnalogFactory(SDLState& state_) : state(state_) {} 458 explicit SDLAnalogFactory(SDLState& state_)
459 : state(state_) {
460 }
461
447 /** 462 /**
448 * Creates analog device from joystick axes 463 * Creates analog device from joystick axes
449 * @param params contains parameters for creating the device: 464 * @param params contains parameters for creating the device:
@@ -490,7 +505,8 @@ SDLState::SDLState() {
490 505
491 initialized = true; 506 initialized = true;
492 if (start_thread) { 507 if (start_thread) {
493 poll_thread = std::thread([this] { 508 poll_thread = std::thread([this]
509 {
494 using namespace std::chrono_literals; 510 using namespace std::chrono_literals;
495 while (initialized) { 511 while (initialized) {
496 SDL_PumpEvents(); 512 SDL_PumpEvents();
@@ -576,7 +592,9 @@ namespace Polling {
576 592
577class SDLPoller : public InputCommon::Polling::DevicePoller { 593class SDLPoller : public InputCommon::Polling::DevicePoller {
578public: 594public:
579 explicit SDLPoller(SDLState& state_) : state(state_) {} 595 explicit SDLPoller(SDLState& state_)
596 : state(state_) {
597 }
580 598
581 void Start() override { 599 void Start() override {
582 state.event_queue.Clear(); 600 state.event_queue.Clear();
@@ -593,7 +611,9 @@ protected:
593 611
594class SDLButtonPoller final : public SDLPoller { 612class SDLButtonPoller final : public SDLPoller {
595public: 613public:
596 explicit SDLButtonPoller(SDLState& state_) : SDLPoller(state_) {} 614 explicit SDLButtonPoller(SDLState& state_)
615 : SDLPoller(state_) {
616 }
597 617
598 Common::ParamPackage GetNextInput() override { 618 Common::ParamPackage GetNextInput() override {
599 SDL_Event event; 619 SDL_Event event;
@@ -602,8 +622,7 @@ public:
602 case SDL_JOYAXISMOTION: 622 case SDL_JOYAXISMOTION:
603 if (std::abs(event.jaxis.value / 32767.0) < 0.5) { 623 if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
604 break; 624 break;
605 } 625 }[[fallthrough]];
606 [[fallthrough]];
607 case SDL_JOYBUTTONUP: 626 case SDL_JOYBUTTONUP:
608 case SDL_JOYHATMOTION: 627 case SDL_JOYHATMOTION:
609 return SDLEventToButtonParamPackage(state, event); 628 return SDLEventToButtonParamPackage(state, event);
@@ -615,7 +634,9 @@ public:
615 634
616class SDLAnalogPoller final : public SDLPoller { 635class SDLAnalogPoller final : public SDLPoller {
617public: 636public:
618 explicit SDLAnalogPoller(SDLState& state_) : SDLPoller(state_) {} 637 explicit SDLAnalogPoller(SDLState& state_)
638 : SDLPoller(state_) {
639 }
619 640
620 void Start() override { 641 void Start() override {
621 SDLPoller::Start(); 642 SDLPoller::Start();