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