summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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 822d0b555..b9b584b2a 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -323,7 +323,9 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
323 return joystick->GetSDLJoystick() == sdl_joystick; 323 return joystick->GetSDLJoystick() == sdl_joystick;
324 }); 324 });
325 325
326 (*joystick_it)->SetSDLJoystick(nullptr, nullptr); 326 if (joystick_it != joystick_guid_list.end()) {
327 (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
328 }
327} 329}
328 330
329void SDLState::HandleGameControllerEvent(const SDL_Event& event) { 331void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
@@ -1315,51 +1317,51 @@ public:
1315 void Start(const std::string& device_id) override { 1317 void Start(const std::string& device_id) override {
1316 SDLPoller::Start(device_id); 1318 SDLPoller::Start(device_id);
1317 // Reset stored axes 1319 // Reset stored axes
1318 analog_x_axis = -1; 1320 first_axis = -1;
1319 analog_y_axis = -1;
1320 } 1321 }
1321 1322
1322 Common::ParamPackage GetNextInput() override { 1323 Common::ParamPackage GetNextInput() override {
1323 SDL_Event event; 1324 SDL_Event event;
1324 while (state.event_queue.Pop(event)) { 1325 while (state.event_queue.Pop(event)) {
1325 // Filter out axis events that are below a threshold 1326 if (event.type != SDL_JOYAXISMOTION) {
1326 if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { 1327 // Check for a button press
1327 continue;
1328 }
1329 if (event.type == SDL_JOYAXISMOTION) {
1330 const auto axis = event.jaxis.axis;
1331 // In order to return a complete analog param, we need inputs for both axes.
1332 // First we take the x-axis (horizontal) input, then the y-axis (vertical) input.
1333 if (analog_x_axis == -1) {
1334 analog_x_axis = axis;
1335 } else if (analog_y_axis == -1 && analog_x_axis != axis) {
1336 analog_y_axis = axis;
1337 }
1338 } else {
1339 // If the press wasn't accepted as a joy axis, check for a button press
1340 auto button_press = button_poller.FromEvent(event); 1328 auto button_press = button_poller.FromEvent(event);
1341 if (button_press) { 1329 if (button_press) {
1342 return *button_press; 1330 return *button_press;
1343 } 1331 }
1332 continue;
1333 }
1334 const auto axis = event.jaxis.axis;
1335
1336 // Filter out axis events that are below a threshold
1337 if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
1338 continue;
1339 }
1340
1341 // Filter out axis events that are the same
1342 if (first_axis == axis) {
1343 continue;
1344 }
1345
1346 // In order to return a complete analog param, we need inputs for both axes.
1347 // If the first axis isn't set we set the value then wait till next event
1348 if (first_axis == -1) {
1349 first_axis = axis;
1350 continue;
1344 } 1351 }
1345 }
1346 1352
1347 if (analog_x_axis != -1 && analog_y_axis != -1) {
1348 if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { 1353 if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {
1349 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), 1354 auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
1350 analog_x_axis, analog_y_axis); 1355 first_axis, axis);
1351 analog_x_axis = -1; 1356 first_axis = -1;
1352 analog_y_axis = -1;
1353 return params; 1357 return params;
1354 } 1358 }
1355 } 1359 }
1356
1357 return {}; 1360 return {};
1358 } 1361 }
1359 1362
1360private: 1363private:
1361 int analog_x_axis = -1; 1364 int first_axis = -1;
1362 int analog_y_axis = -1;
1363 SDLButtonPoller button_poller; 1365 SDLButtonPoller button_poller;
1364}; 1366};
1365} // namespace Polling 1367} // namespace Polling