From 85eeae7aad96fd5272f77ce0e24608417ad5c68e Mon Sep 17 00:00:00 2001
From: german77 <juangerman-13@hotmail.com>
Date: Fri, 14 May 2021 21:17:08 -0500
Subject: input_common: Rewrite sdl analog mapping

---
 src/input_common/sdl/sdl_impl.cpp | 50 +++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 25 deletions(-)

(limited to 'src/input_common/sdl/sdl_impl.cpp')

diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index f682a6db40..c918a333df 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -1168,51 +1168,51 @@ public:
     void Start(const std::string& device_id) override {
         SDLPoller::Start(device_id);
         // Reset stored axes
-        analog_x_axis = -1;
-        analog_y_axis = -1;
+        first_axis = -1;
     }
 
     Common::ParamPackage GetNextInput() override {
         SDL_Event event;
         while (state.event_queue.Pop(event)) {
-            // Filter out axis events that are below a threshold
-            if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) {
-                continue;
-            }
-            if (event.type == SDL_JOYAXISMOTION) {
-                const auto axis = event.jaxis.axis;
-                // In order to return a complete analog param, we need inputs for both axes.
-                // First we take the x-axis (horizontal) input, then the y-axis (vertical) input.
-                if (analog_x_axis == -1) {
-                    analog_x_axis = axis;
-                } else if (analog_y_axis == -1 && analog_x_axis != axis) {
-                    analog_y_axis = axis;
-                }
-            } else {
-                // If the press wasn't accepted as a joy axis, check for a button press
+            if (event.type != SDL_JOYAXISMOTION) {
+                // Check for a button press
                 auto button_press = button_poller.FromEvent(event);
                 if (button_press) {
                     return *button_press;
                 }
+                continue;
+            }
+            const auto axis = event.jaxis.axis;
+
+            // Filter out axis events that are below a threshold
+            if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
+                continue;
+            }
+
+            // Filter out axis events that are the same
+            if (first_axis == axis) {
+                continue;
+            }
+
+            // In order to return a complete analog param, we need inputs for both axes.
+            // If the first axis isn't set we set the value then wait till next event
+            if (first_axis == -1) {
+                first_axis = axis;
+                continue;
             }
-        }
 
-        if (analog_x_axis != -1 && analog_y_axis != -1) {
             if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {
                 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
-                                                         analog_x_axis, analog_y_axis);
-                analog_x_axis = -1;
-                analog_y_axis = -1;
+                                                         first_axis, axis);
+                first_axis = -1;
                 return params;
             }
         }
-
         return {};
     }
 
 private:
-    int analog_x_axis = -1;
-    int analog_y_axis = -1;
+    int first_axis = -1;
     SDLButtonPoller button_poller;
 };
 } // namespace Polling
-- 
cgit v1.2.3-70-g09d2


From edd498f6e0c8343a1368bdb49f5f8a4919ff89f0 Mon Sep 17 00:00:00 2001
From: german77 <juangerman-13@hotmail.com>
Date: Fri, 14 May 2021 21:56:39 -0500
Subject: input_common: Fix crash when controller disconnects

---
 src/input_common/sdl/sdl_impl.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

(limited to 'src/input_common/sdl/sdl_impl.cpp')

diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index c918a333df..288557968a 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -267,7 +267,9 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
                                               return joystick->GetSDLJoystick() == sdl_joystick;
                                           });
 
-    (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
+    if (joystick_it != joystick_guid_list.end()) {
+        (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
+    }
 }
 
 void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
-- 
cgit v1.2.3-70-g09d2