diff options
| author | 2021-05-24 12:02:37 -0700 | |
|---|---|---|
| committer | 2021-05-24 12:02:37 -0700 | |
| commit | aee3b57c44aadca1b47726429d5b3d52b9ca575e (patch) | |
| tree | 6c852aa6befbd2d5cddda7edae7afb67870def0f /src/input_common/sdl/sdl_impl.cpp | |
| parent | Merge pull request #6347 from bunnei/ipc-improvements-next-2 (diff) | |
| parent | input_common: Fix crash when controller disconnects (diff) | |
| download | yuzu-aee3b57c44aadca1b47726429d5b3d52b9ca575e.tar.gz yuzu-aee3b57c44aadca1b47726429d5b3d52b9ca575e.tar.xz yuzu-aee3b57c44aadca1b47726429d5b3d52b9ca575e.zip | |
Merge pull request #6312 from german77/analogMapping
input_common: Rewrite sdl analog mapping and fix controller disconnection crash
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 54 |
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 | ||
| 329 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { | 331 | void 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 | ||
| 1360 | private: | 1363 | private: |
| 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 |