From 68b6969b10a2a883139c41ed29996d0a93ff1db8 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Fri, 31 Jan 2025 14:46:08 +0100 Subject: [PATCH] Revert directional joystick code and fix a memory leak --- src/common/config.cpp | 12 +++------ src/input/input_handler.cpp | 50 +++++++++++++++++-------------------- src/input/input_handler.h | 8 +++--- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/common/config.cpp b/src/common/config.cpp index 2a1c84cf9..915365c1b 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -981,15 +981,11 @@ pad_right = pad_right options = options touchpad = back -axis_left_x_plus = axis_left_x_plus -axis_left_y_plus = axis_left_y_plus -axis_left_x_minus = axis_left_x_minus -axis_left_y_minus = axis_left_y_minus +axis_left_x = axis_left_x +axis_left_y = axis_left_y -axis_right_x_plus = axis_right_x_plus -axis_right_y_plus = axis_right_y_plus -axis_right_x_minus = axis_right_x_minus -axis_right_y_minus = axis_right_y_minus +axis_right_x = axis_right_x +axis_right_y = axis_right_y )"; } std::filesystem::path GetFoolproofKbmConfigFile(const std::string& game_id) { diff --git a/src/input/input_handler.cpp b/src/input/input_handler.cpp index 0b5e95844..b5ac4df39 100644 --- a/src/input/input_handler.cpp +++ b/src/input/input_handler.cpp @@ -84,14 +84,15 @@ auto output_array = std::array{ ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_RIGHT),// Right // Axis mappings - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX, false), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY, false), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX, false), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY, false), ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX), ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY), ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX), ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY), + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFT_TRIGGER), ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER), @@ -99,15 +100,10 @@ auto output_array = std::array{ }; void ControllerOutput::LinkJoystickAxes() { - for (int i = 17; i < 21; i++) { - delete output_array[i].new_param; - output_array[i].new_param = output_array[i + 4].new_param; - } - for (int i = 0; i < output_array.size(); i++) { - if (output_array[i].new_param == nullptr) { - LOG_ERROR(Input, "Output {} has a broken pointer!", i); - } - } + // for (int i = 17; i < 23; i += 2) { + // delete output_array[i].new_param; + // output_array[i].new_param = output_array[i + 1].new_param; + // } } static OrbisPadButtonDataOffset SDLGamepadToOrbisButton(u8 button) { @@ -447,7 +443,7 @@ void ControllerOutput::AddUpdate(InputEvent event) { bool temp = event.axis_value * (positive_axis ? 1 : -1) > 0x40; new_button_state |= event.active && event.axis_value * (positive_axis ? 1 : -1) > 0x40; if (temp) { - LOG_INFO(Input, "Toggled a button from an axis"); + LOG_DEBUG(Input, "Toggled a button from an axis"); } } else { new_button_state |= event.active; @@ -549,11 +545,11 @@ bool UpdatePressedKeys(InputEvent event) { // and from there, it only changes the parameter auto it = std::lower_bound(pressed_keys.begin(), pressed_keys.end(), input, [](const std::pair& e, InputID i) { - return e.first.input.type <= i.type && - e.first.input.sdl_id < i.sdl_id; - }); - if (it == pressed_keys.end()) { - pressed_keys.push_back({event, false}); + return std::tie(e.first.input.type, e.first.input.sdl_id) < + std::tie(i.type, i.sdl_id); + }); + if (it == pressed_keys.end() || it->first.input != input) { + pressed_keys.insert(it, {event, false}); LOG_DEBUG(Input, "Added axis {} to the input list", event.input.sdl_id); } else { it->first.axis_value = event.axis_value; @@ -563,9 +559,9 @@ bool UpdatePressedKeys(InputEvent event) { // Find the correct position for insertion to maintain order auto it = std::lower_bound(pressed_keys.begin(), pressed_keys.end(), input, [](const std::pair& e, InputID i) { - return e.first.input.type <= i.type && - e.first.input.sdl_id < i.sdl_id; - }); + return std::tie(e.first.input.type, e.first.input.sdl_id) < + std::tie(i.type, i.sdl_id); + }); // Insert only if 'value' is not already in the list if (it == pressed_keys.end() || it->first.input != input) { @@ -631,13 +627,13 @@ InputEvent BindingConnection::ProcessBinding() { bool key_found = false; while (pressed_it != pressed_keys.end()) { - if (pressed_it->first.input == key && pressed_it->second == false) { + if (pressed_it->first.input == key && (pressed_it->second == false)) { key_found = true; - flags_to_set.push_back(&pressed_it->second); + if (output->positive_axis) { + flags_to_set.push_back(&pressed_it->second); + } if (pressed_it->first.input.type == InputType::Axis) { - // clamp to + or - range based on whether it's the + or - axis direction output - event.axis_value = // pressed_it->first.axis_value; - SDL_clamp(pressed_it->first.axis_value, output->positive_axis ? 0 : -127, output->positive_axis ? 127 : 0); + event.axis_value = pressed_it->first.axis_value; } ++pressed_it; break; diff --git a/src/input/input_handler.h b/src/input/input_handler.h index 81a0ca4c8..15e530a88 100644 --- a/src/input/input_handler.h +++ b/src/input/input_handler.h @@ -118,10 +118,10 @@ const std::map string_to_axis_map = { {"r2", {SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 127}}, // should only use these to bind analog inputs to analog outputs - // {"axis_left_x", {Input::Axis::LeftX, 0}}, - // {"axis_left_y", {Input::Axis::LeftY, 0}}, - // {"axis_right_x", {Input::Axis::RightX, 0}}, - // {"axis_right_y", {Input::Axis::RightY, 0}}, + {"axis_left_x", {SDL_GAMEPAD_AXIS_LEFTX, 127}}, + {"axis_left_y", {SDL_GAMEPAD_AXIS_LEFTY, 127}}, + {"axis_right_x", {SDL_GAMEPAD_AXIS_RIGHTX, 127}}, + {"axis_right_y", {SDL_GAMEPAD_AXIS_RIGHTY, 127}}, }; const std::map string_to_keyboard_key_map = { {"a", SDLK_A},