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.cpp54
1 files changed, 28 insertions, 26 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 314b0e773..68672a92b 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -347,7 +347,9 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
347 return joystick->GetSDLJoystick() == sdl_joystick; 347 return joystick->GetSDLJoystick() == sdl_joystick;
348 }); 348 });
349 349
350 (*joystick_it)->SetSDLJoystick(nullptr, nullptr); 350 if (joystick_it != joystick_guid_list.end()) {
351 (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
352 }
351} 353}
352 354
353void SDLState::HandleGameControllerEvent(const SDL_Event& event) { 355void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
@@ -1480,51 +1482,51 @@ public:
1480 void Start(const std::string& device_id) override { 1482 void Start(const std::string& device_id) override {
1481 SDLPoller::Start(device_id); 1483 SDLPoller::Start(device_id);
1482 // Reset stored axes 1484 // Reset stored axes
1483 analog_x_axis = -1; 1485 first_axis = -1;
1484 analog_y_axis = -1;
1485 } 1486 }
1486 1487
1487 Common::ParamPackage GetNextInput() override { 1488 Common::ParamPackage GetNextInput() override {
1488 SDL_Event event; 1489 SDL_Event event;
1489 while (state.event_queue.Pop(event)) { 1490 while (state.event_queue.Pop(event)) {
1490 // Filter out axis events that are below a threshold 1491 if (event.type != SDL_JOYAXISMOTION) {
1491 if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { 1492 // Check for a button press
1492 continue;
1493 }
1494 if (event.type == SDL_JOYAXISMOTION) {
1495 const auto axis = event.jaxis.axis;
1496 // In order to return a complete analog param, we need inputs for both axes.
1497 // First we take the x-axis (horizontal) input, then the y-axis (vertical) input.
1498 if (analog_x_axis == -1) {
1499 analog_x_axis = axis;
1500 } else if (analog_y_axis == -1 && analog_x_axis != axis) {
1501 analog_y_axis = axis;
1502 }
1503 } else {
1504 // If the press wasn't accepted as a joy axis, check for a button press
1505 auto button_press = button_poller.FromEvent(event); 1493 auto button_press = button_poller.FromEvent(event);
1506 if (button_press) { 1494 if (button_press) {
1507 return *button_press; 1495 return *button_press;
1508 } 1496 }
1497 continue;
1498 }
1499 const auto axis = event.jaxis.axis;
1500
1501 // Filter out axis events that are below a threshold
1502 if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
1503 continue;
1504 }
1505
1506 // Filter out axis events that are the same
1507 if (first_axis == axis) {
1508 continue;
1509 }
1510
1511 // In order to return a complete analog param, we need inputs for both axes.
1512 // If the first axis isn't set we set the value then wait till next event
1513 if (first_axis == -1) {
1514 first_axis = axis;
1515 continue;
1509 } 1516 }
1510 }
1511 1517
1512 if (analog_x_axis != -1 && analog_y_axis != -1) {
1513 if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { 1518 if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {
1514 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), 1519 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
1515 analog_x_axis, analog_y_axis); 1520 first_axis, axis);
1516 analog_x_axis = -1; 1521 first_axis = -1;
1517 analog_y_axis = -1;
1518 return params; 1522 return params;
1519 } 1523 }
1520 } 1524 }
1521
1522 return {}; 1525 return {};
1523 } 1526 }
1524 1527
1525private: 1528private:
1526 int analog_x_axis = -1; 1529 int first_axis = -1;
1527 int analog_y_axis = -1;
1528 SDLButtonPoller button_poller; 1530 SDLButtonPoller button_poller;
1529}; 1531};
1530} // namespace Polling 1532} // namespace Polling