2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

Merge pull request #6312 from german77/analogMapping

input_common: Rewrite sdl analog mapping and fix controller disconnection crash
This commit is contained in:
bunnei 2021-05-24 12:02:37 -07:00 committed by GitHub
commit aee3b57c44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -323,7 +323,9 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
return joystick->GetSDLJoystick() == sdl_joystick; return joystick->GetSDLJoystick() == sdl_joystick;
}); });
if (joystick_it != joystick_guid_list.end()) {
(*joystick_it)->SetSDLJoystick(nullptr, nullptr); (*joystick_it)->SetSDLJoystick(nullptr, nullptr);
}
} }
void SDLState::HandleGameControllerEvent(const SDL_Event& event) { void SDLState::HandleGameControllerEvent(const SDL_Event& event) {
@ -1315,51 +1317,51 @@ public:
void Start(const std::string& device_id) override { void Start(const std::string& device_id) override {
SDLPoller::Start(device_id); SDLPoller::Start(device_id);
// Reset stored axes // Reset stored axes
analog_x_axis = -1; first_axis = -1;
analog_y_axis = -1;
} }
Common::ParamPackage GetNextInput() override { Common::ParamPackage GetNextInput() override {
SDL_Event event; SDL_Event event;
while (state.event_queue.Pop(event)) { while (state.event_queue.Pop(event)) {
// Filter out axis events that are below a threshold if (event.type != SDL_JOYAXISMOTION) {
if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { // Check for a button press
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
auto button_press = button_poller.FromEvent(event); auto button_press = button_poller.FromEvent(event);
if (button_press) { if (button_press) {
return *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)) { if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {
auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
analog_x_axis, analog_y_axis); first_axis, axis);
analog_x_axis = -1; first_axis = -1;
analog_y_axis = -1;
return params; return params;
} }
} }
return {}; return {};
} }
private: private:
int analog_x_axis = -1; int first_axis = -1;
int analog_y_axis = -1;
SDLButtonPoller button_poller; SDLButtonPoller button_poller;
}; };
} // namespace Polling } // namespace Polling