diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index e9ae25f51e..d7fc0c3e76 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -257,6 +257,12 @@ target_link_libraries( $ ) +if (PA_SDL) + message(STATUS "Compiling with gamepad (via SDL) integration enabled") + target_compile_definitions(SerialProgramsLib PRIVATE PA_SDL) + target_compile_definitions(SerialPrograms PRIVATE PA_SDL) +endif() + # Add source code exclusive to the internal repo and add C++ macro of official release if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../Internal/SerialPrograms/Internal0.cpp") message(STATUS "Building internal SerialPrograms") diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index b4ef7a7ae1..c493f48b54 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -24,6 +24,9 @@ #include "CommonTools/OCR/OCR_Routines.h" #include "Tests/CommandLineTests.h" #include "ControllerInput/ControllerInput.h" +#ifdef PA_SDL +#include "ControllerInput/Gamepad/GlobalGamepadTracker.h" +#endif #include "Controllers/SerialPort/SerialPortPollerQt.h" #include "Integrations/DiscordSettingsOption.h" #include "Integrations/DiscordSocial/DiscordSocial.h" @@ -183,6 +186,9 @@ int run_program(int argc, char *argv[]){ ScopeExit cleanup([]{ +#ifdef PA_SDL + global_gamepad_tracker().stop(); +#endif SerialPortPoller::instance().stop(); GlobalMediaServices::instance().stop(); }); @@ -259,6 +265,9 @@ int run_program(int argc, char *argv[]){ #endif +#ifdef PA_SDL + global_gamepad_tracker().start(); +#endif MainWindow w; w.show(); w.raise(); // bring the window to front on macOS diff --git a/SerialPrograms/Source/ControllerInput/ControllerInput.cpp b/SerialPrograms/Source/ControllerInput/ControllerInput.cpp index 3e6b059654..0a445b889b 100644 --- a/SerialPrograms/Source/ControllerInput/ControllerInput.cpp +++ b/SerialPrograms/Source/ControllerInput/ControllerInput.cpp @@ -5,6 +5,9 @@ */ #include +#ifdef PA_SDL +#include "Gamepad/GlobalGamepadTracker.h" +#endif #include "Keyboard/GlobalKeyboardHidTracker.h" #include "ControllerInput.h" @@ -20,6 +23,9 @@ class GlobalInputSources{ // m_input_sources.emplace_back(&global_keyboard_tracker()); +#ifdef PA_SDL + m_input_sources.emplace_back(&global_gamepad_tracker()); +#endif } diff --git a/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.cpp b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.cpp new file mode 100644 index 0000000000..2800f52a96 --- /dev/null +++ b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.cpp @@ -0,0 +1,102 @@ +/* SDL Gamepad Input + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifdef PA_SDL + +#include +#include +#include +#include "GamepadInput_SDL.h" + +namespace PokemonAutomation{ + +// Rescale outside a radial deadzone so centered sticks are exactly neutral, +// while retaining the entire analog range. SDL's positive Y points down. +std::tuple gamepad_stick(int x, int y){ + constexpr double joystick_max_negative = -SDL_JOYSTICK_AXIS_MIN; + constexpr double joystick_max_positive = SDL_JOYSTICK_AXIS_MAX; + constexpr double deadzone = 0.15; + + // First normalize to [-1, 1] + double normalized_x = x / (x < 0 ? joystick_max_negative : joystick_max_positive); + double normalized_y = y / (y < 0 ? joystick_max_negative : joystick_max_positive); + + // Then apply a radial deadzone + double magnitude = std::hypot(normalized_x, normalized_y); + if (magnitude <= deadzone){ + return {0, 0}; + } + + // Rescale to the full range outside the deadzone + double output_magnitude = (std::min(magnitude, 1.0) - deadzone) / (1.0 - deadzone); + double scale = output_magnitude / magnitude; + return {normalized_x * scale, -normalized_y * scale}; +} + +bool gamepad_trigger(int value){ + constexpr double trigger_max = SDL_JOYSTICK_AXIS_MAX; + constexpr double deadzone = 0.20; + + return value > (trigger_max * deadzone); +} + +GamepadInputState read_gamepad(SDL_Gamepad* pad){ + using namespace NintendoSwitch; + + GamepadInputState state; + ProControllerState& output = state.controller_state; + // Match physical Nintendo positions, regardless of the input device's labels. + static constexpr std::pair mapping[] = { + {SDL_GAMEPAD_BUTTON_SOUTH, BUTTON_B}, + {SDL_GAMEPAD_BUTTON_EAST, BUTTON_A}, + {SDL_GAMEPAD_BUTTON_WEST, BUTTON_Y}, + {SDL_GAMEPAD_BUTTON_NORTH, BUTTON_X}, + {SDL_GAMEPAD_BUTTON_BACK, BUTTON_MINUS}, + {SDL_GAMEPAD_BUTTON_GUIDE, BUTTON_HOME}, + {SDL_GAMEPAD_BUTTON_START, BUTTON_PLUS}, + {SDL_GAMEPAD_BUTTON_LEFT_STICK, BUTTON_LCLICK}, + {SDL_GAMEPAD_BUTTON_RIGHT_STICK, BUTTON_RCLICK}, + {SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, BUTTON_L}, + {SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, BUTTON_R}, + {SDL_GAMEPAD_BUTTON_MISC1, BUTTON_CAPTURE}, + }; + for (const auto& [sdl_button, button] : mapping){ + if (SDL_GetGamepadButton(pad, sdl_button)){ + output.buttons |= button; + } + } + if (gamepad_trigger(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER))){ + output.buttons |= BUTTON_ZL; + } + if (gamepad_trigger(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER))){ + output.buttons |= BUTTON_ZR; + } + + int dpad_x = (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT) + - (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT); + int dpad_y = (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN) + - (int)SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP); + static constexpr DpadPosition dpad_positions[3][3] = { + {DPAD_UP_LEFT, DPAD_UP, DPAD_UP_RIGHT}, + {DPAD_LEFT, DPAD_NONE, DPAD_RIGHT}, + {DPAD_DOWN_LEFT, DPAD_DOWN, DPAD_DOWN_RIGHT}, + }; + output.dpad = dpad_positions[dpad_y + 1][dpad_x + 1]; + + std::tie(output.left_joystick.x, output.left_joystick.y) = gamepad_stick( + SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX), + SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY) + ); + std::tie(output.right_joystick.x, output.right_joystick.y) = gamepad_stick( + SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX), + SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTY) + ); + return state; +} + + +} +#endif diff --git a/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.h b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.h new file mode 100644 index 0000000000..b4c63b6030 --- /dev/null +++ b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.h @@ -0,0 +1,24 @@ +/* SDL Gamepad Input + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_ControllerInput_Gamepad_GamepadInput_SDL_H +#define PokemonAutomation_ControllerInput_Gamepad_GamepadInput_SDL_H + +#ifdef PA_SDL + +#include +#include +#include "GamepadInput_State.h" + +namespace PokemonAutomation{ + +std::tuple gamepad_stick(int x, int y); +bool gamepad_trigger(int value); +GamepadInputState read_gamepad(SDL_Gamepad* pad); + +} +#endif +#endif diff --git a/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.cpp b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.cpp new file mode 100644 index 0000000000..20a4054d36 --- /dev/null +++ b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.cpp @@ -0,0 +1,50 @@ +/* Standard Gamepad Input State + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "GamepadInput_State.h" + +namespace PokemonAutomation{ + +GamepadInputState::GamepadInputState() + : ControllerInputState(ControllerInputType::StandardGamepad) +{} + +GamepadInputState& GamepadInputState::operator=(const GamepadInputState& other){ + controller_state = other.controller_state; + return *this; +} + +void GamepadInputState::clear(){ + controller_state.clear(); +} + +bool GamepadInputState::is_neutral() const{ + return controller_state.is_neutral(); +} + +bool GamepadInputState::operator==(const ControllerInputState& state) const{ + const auto* other = dynamic_cast(&state); + if (other == nullptr){ + return false; + } + return controller_state == other->controller_state; +} + +void GamepadInputGate::reset(){ + m_armed = false; +} + +void GamepadInputGate::filter(GamepadInputState& state, bool has_listeners){ + if (!has_listeners){ + m_armed = false; + } + if (!m_armed){ + m_armed = has_listeners && state.is_neutral(); + state.clear(); + } +} + +} diff --git a/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.h b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.h new file mode 100644 index 0000000000..1811f05a2f --- /dev/null +++ b/SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.h @@ -0,0 +1,40 @@ +/* Standard Gamepad Input State + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H +#define PokemonAutomation_ControllerInput_Gamepad_GamepadInput_State_H + +#include "ControllerInput/ControllerInput.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProControllerState.h" + +namespace PokemonAutomation{ + +class GamepadInputState final : public ControllerInputState{ +public: + GamepadInputState(); + + GamepadInputState(const GamepadInputState&) = default; + + GamepadInputState& operator=(const GamepadInputState& other); + + virtual void clear() override; + virtual bool is_neutral() const override; + virtual bool operator==(const ControllerInputState& state) const override; + + NintendoSwitch::ProControllerState controller_state; +}; + +class GamepadInputGate{ +public: + void reset(); + void filter(GamepadInputState& state, bool has_listeners); + +private: + bool m_armed = false; +}; + +} +#endif diff --git a/SerialPrograms/Source/ControllerInput/Gamepad/GlobalGamepadTracker.cpp b/SerialPrograms/Source/ControllerInput/Gamepad/GlobalGamepadTracker.cpp new file mode 100644 index 0000000000..521ed5e41f --- /dev/null +++ b/SerialPrograms/Source/ControllerInput/Gamepad/GlobalGamepadTracker.cpp @@ -0,0 +1,131 @@ +/* Global Gamepad Tracker + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifdef PA_SDL + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +//#include "Common/Cpp/Logging/GlobalLogger.h" +#include "CommonFramework/Logging/Logger.h" +#include "CommonFramework/Tools/GlobalThreadPools.h" +#include "GamepadInput_SDL.h" +#include "GlobalGamepadTracker.h" + +//#include +//using std::cout; +//using std::endl; + +namespace PokemonAutomation{ + +GamepadTracker& global_gamepad_tracker(){ + static GamepadTracker tracker; + return tracker; +} + +GamepadTracker::~GamepadTracker(){ + stop(); +} + +void GamepadTracker::start(){ + if (m_initialized){ + return; + } + // Qt owns the window/event loop. SDL only owns joystick input. + SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); + if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)){ + global_logger_tagged().log( + std::string("Gamepad initialization failed: ") + SDL_GetError(), + COLOR_RED + ); + return; + } + m_initialized = true; + SDL_SetGamepadEventsEnabled(false); + SDL_SetJoystickEventsEnabled(false); + m_stopping.store(false, std::memory_order_release); + m_thread = GlobalThreadPools::unlimited_realtime().dispatch_now_blocking([this]{ + thread_loop(); + }); +} + +void GamepadTracker::stop() noexcept{ + m_stopping.store(true, std::memory_order_release); + { + std::lock_guard lg(m_sleep_lock); + } + m_cv.notify_all(); + if (m_thread){ + m_thread.wait_and_ignore_exceptions(); + } + if (m_initialized){ + SDL_QuitSubSystem(SDL_INIT_GAMEPAD); + m_initialized = false; + } +} + +void GamepadTracker::clear_state(){ + m_reset.store(true, std::memory_order_release); +} + +void GamepadTracker::thread_loop(){ + SDL_Gamepad* pad = nullptr; + GamepadInputState last; + GamepadInputGate gate; + auto refresh = std::chrono::steady_clock::now(); + auto discover = refresh; + while (!m_stopping.load(std::memory_order_acquire)){ + SDL_UpdateGamepads(); + auto now = std::chrono::steady_clock::now(); + if (pad && !SDL_GamepadConnected(pad)){ + SDL_CloseGamepad(pad); + pad = nullptr; + gate.reset(); + } + if (!pad && now >= discover){ + discover = now + std::chrono::seconds(1); + int count = 0; + SDL_JoystickID* ids = SDL_GetGamepads(&count); + for (int c = 0; c < count && !pad; c++){ + pad = SDL_OpenGamepad(ids[c]); + } + SDL_free(ids); + } + GamepadInputState current; + if (pad){ + current = read_gamepad(pad); + } + if (m_reset.exchange(false, std::memory_order_acq_rel)){ + gate.reset(); + } + // Require release after focus/console changes or automation. A held + // button must never leak to the newly selected console. + gate.filter(current, !m_listeners.empty()); + if (current != last || (!current.is_neutral() && now >= refresh)){ + try{ + m_listeners.run_method(&ControllerInputListener::run_controller_input, current); + }catch (Exception& error){ + error.log(global_logger_tagged()); + } + last = current; + refresh = now + std::chrono::seconds(1); + } + std::unique_lock lock(m_sleep_lock); + if (!m_stopping.load(std::memory_order_acquire)){ + auto wake = pad ? now + std::chrono::milliseconds(8) : discover; + m_cv.wait_until(lock, wake); + } + } + if (pad){ + SDL_CloseGamepad(pad); + } +} + +} + +#endif diff --git a/SerialPrograms/Source/ControllerInput/Gamepad/GlobalGamepadTracker.h b/SerialPrograms/Source/ControllerInput/Gamepad/GlobalGamepadTracker.h new file mode 100644 index 0000000000..2807f4e93a --- /dev/null +++ b/SerialPrograms/Source/ControllerInput/Gamepad/GlobalGamepadTracker.h @@ -0,0 +1,45 @@ +/* Global Gamepad Tracker + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_ControllerInput_Gamepad_GlobalGamepadTracker_H +#define PokemonAutomation_ControllerInput_Gamepad_GlobalGamepadTracker_H + +#ifdef PA_SDL + +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "ControllerInput/ControllerInput.h" + +namespace PokemonAutomation{ + +class GamepadTracker final : public ControllerInputSource{ +public: + ~GamepadTracker(); + + // Initialize and shut down SDL on the main thread, while QApplication lives. + void start(); + virtual void stop() noexcept override; + virtual void clear_state() override; + +private: + void thread_loop(); + +private: + bool m_initialized{false}; + std::atomic m_stopping{false}; + std::atomic m_reset{true}; + Mutex m_sleep_lock; + ConditionVariable m_cv; + AsyncTask m_thread; +}; + +GamepadTracker& global_gamepad_tracker(); + +} +#endif +#endif diff --git a/SerialPrograms/Source/GameConsole/Framework/ConsoleSystemSession.cpp b/SerialPrograms/Source/GameConsole/Framework/ConsoleSystemSession.cpp index 0e9e08a0a2..057853192f 100644 --- a/SerialPrograms/Source/GameConsole/Framework/ConsoleSystemSession.cpp +++ b/SerialPrograms/Source/GameConsole/Framework/ConsoleSystemSession.cpp @@ -174,24 +174,24 @@ std::string ConsoleSystemSession::update_status(){ } } if (!ok){ - m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_RED); + m_status_text = "Controller input: " + html_color_text("⬤", COLOR_RED); return m_status_text; } // Controllers are locked. (program is probably running) if (!m_lock_controllers_reason.empty() && !allow_commands_while_locked()){ - m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_PURPLE); + m_status_text = "Controller input: " + html_color_text("⬤", COLOR_PURPLE); return m_status_text; } // Not focused. if (!m_focused){ - m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_ORANGE); + m_status_text = "Controller input: " + html_color_text("⬤", COLOR_ORANGE); return m_status_text; } // Ready. - m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_DARKGREEN); + m_status_text = "Controller input: " + html_color_text("⬤", COLOR_DARKGREEN); return m_status_text; } void ConsoleSystemSession::on_focus_in(){ @@ -241,11 +241,11 @@ void ConsoleSystemSession::on_focus_out(){ void ConsoleSystemSession::run_controller_input(ControllerInputState& state){ std::lock_guard lg(m_lock); if (!m_focused){ - m_logger.log("Keyboard Command Suppressed: Not in focus.", COLOR_RED); + m_logger.log("Controller Input Suppressed: Not in focus.", COLOR_RED); return; } if (!m_lock_controllers_reason.empty() && !allow_commands_while_locked()){ - m_logger.log("Keyboard Command Suppressed: " + m_lock_controllers_reason, COLOR_RED); + m_logger.log("Controller Input Suppressed: " + m_lock_controllers_reason, COLOR_RED); return; } @@ -255,7 +255,7 @@ void ConsoleSystemSession::run_controller_input(ControllerInputState& state){ controller.run_controller_input(state); }); if (!error.empty()){ - controller.session.logger().log("Keyboard Command Failed: " + error, COLOR_RED); + controller.session.logger().log("Controller Input Failed: " + error, COLOR_RED); } } } diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_Joycon.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_Joycon.cpp index 58c2f474f5..d118b09a37 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_Joycon.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_Joycon.cpp @@ -7,6 +7,7 @@ #include "Common/Cpp/Strings/StringTools.h" #include "Common/Cpp/Containers/Pimpl.tpp" #include "ControllerInput/ControllerInput.h" +#include "ControllerInput/Gamepad/GamepadInput_State.h" #include "ControllerInput/Keyboard/KeyboardInput_State.h" #include "Controllers/RumbleListener.h" #include "NintendoSwitch/NintendoSwitch_Settings.h" @@ -115,39 +116,68 @@ bool JoyconController::run_string_command(Milliseconds duration, const std::stri void JoyconController::run_controller_input(const ControllerInputState& state){ - if (state.type() != ControllerInputType::HID_Keyboard){ - return; - } + const auto execute_state = [this](const JoyconState& controller_state){ + WallClock timestamp; + if (controller_state.is_neutral()){ + timestamp = current_time(); + cancel_all_commands(); + }else{ + replace_on_next_command(); - JoyconDeltas deltas; - - const KeyboardInputState& lstate = static_cast(state); - const std::map& map = m_data->m_keyboard_mapping; + timestamp = current_time(); + controller_state.execute(nullptr, false, *this, 2000ms); + } -// cout << "keys() = " << lstate.keys().size() << endl; + on_command_input(timestamp, controller_state); + }; - for (KeyboardKey key : lstate.keys()){ - auto iter = map.find(key); - if (iter != map.end()){ - deltas += iter->second; - } - } + switch (state.type()){ + case ControllerInputType::HID_Keyboard:{ + JoyconState controller_state; + JoyconDeltas deltas; + const KeyboardInputState& lstate = static_cast(state); + const std::map& map = m_data->m_keyboard_mapping; - JoyconState controller_state; - deltas.to_state(controller_state); + // cout << "keys() = " << lstate.keys().size() << endl; - WallClock timestamp; - if (controller_state.is_neutral()){ - timestamp = current_time(); - cancel_all_commands(); - }else{ - replace_on_next_command(); + for (KeyboardKey key : lstate.keys()){ + auto iter = map.find(key); + if (iter != map.end()){ + deltas += iter->second; + } + } - timestamp = current_time(); - controller_state.execute(nullptr, false, *this, 2000ms); + deltas.to_state(controller_state); + execute_state(controller_state); + break; + } + case ControllerInputType::StandardGamepad:{ + JoyconState controller_state; + const ProControllerState& pro = static_cast(state).controller_state; + controller_state.buttons = pro.buttons; + if (controller_class() == ControllerClass::NintendoSwitch_LeftJoycon){ + static constexpr Button dpad_buttons[] = { + BUTTON_UP, + BUTTON_UP | BUTTON_RIGHT, + BUTTON_RIGHT, + BUTTON_DOWN | BUTTON_RIGHT, + BUTTON_DOWN, + BUTTON_DOWN | BUTTON_LEFT, + BUTTON_LEFT, + BUTTON_UP | BUTTON_LEFT, + BUTTON_NONE, + }; + controller_state.buttons |= dpad_buttons[pro.dpad]; + controller_state.joystick = pro.left_joystick; + }else{ + controller_state.joystick = pro.right_joystick; + } + execute_state(controller_state); + break; + } + default: + return; } - - on_command_input(timestamp, controller_state); } diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.cpp index f9a8389e5c..f88d75ef65 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.cpp @@ -7,6 +7,7 @@ #include "Common/Cpp/Strings/StringTools.h" #include "Common/Cpp/Containers/Pimpl.tpp" #include "ControllerInput/ControllerInput.h" +#include "ControllerInput/Gamepad/GamepadInput_State.h" #include "ControllerInput/Keyboard/KeyboardInput_State.h" #include "Controllers/RumbleListener.h" #include "NintendoSwitch/NintendoSwitch_Settings.h" @@ -129,39 +130,49 @@ bool ProController::run_string_command(Milliseconds duration, const std::string& void ProController::run_controller_input(const ControllerInputState& state){ // cout << "run_controller_input()" << endl; - if (state.type() != ControllerInputType::HID_Keyboard){ - return; - } + const auto execute_state = [this](const ProControllerState& controller_state){ + WallClock timestamp; + if (controller_state.is_neutral()){ + timestamp = current_time(); + cancel_all_commands(); + }else{ + replace_on_next_command(); - ProControllerDeltas deltas; + timestamp = current_time(); + controller_state.execute(nullptr, false, *this, 2000ms); + } - const KeyboardInputState& lstate = static_cast(state); - const std::map& map = m_data->m_keyboard_mapping; + on_command_input(timestamp, controller_state); + }; -// cout << "keys() = " << lstate.keys().size() << endl; + switch (state.type()){ + case ControllerInputType::HID_Keyboard:{ + ProControllerState controller_state; + ProControllerDeltas deltas; + const KeyboardInputState& lstate = static_cast(state); + const std::map& map = m_data->m_keyboard_mapping; - for (KeyboardKey key : lstate.keys()){ - auto iter = map.find(key); - if (iter != map.end()){ - deltas += iter->second; - } - } - - ProControllerState controller_state; - deltas.to_state(controller_state); + // cout << "keys() = " << lstate.keys().size() << endl; - WallClock timestamp; - if (controller_state.is_neutral()){ - timestamp = current_time(); - cancel_all_commands(); - }else{ - replace_on_next_command(); + for (KeyboardKey key : lstate.keys()){ + auto iter = map.find(key); + if (iter != map.end()){ + deltas += iter->second; + } + } - timestamp = current_time(); - controller_state.execute(nullptr, false, *this, 2000ms); + deltas.to_state(controller_state); + execute_state(controller_state); + break; + } + case ControllerInputType::StandardGamepad:{ + const ProControllerState& controller_state = static_cast(state).controller_state; + execute_state(controller_state); + break; + } + default: + return; } - - on_command_input(timestamp, controller_state); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index d61f974988..f8077299f9 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -254,6 +254,8 @@ file(GLOB CORE_LIBRARY_SOURCES Source/CompileTimeBackends.h Source/ControllerInput/ControllerInput.cpp Source/ControllerInput/ControllerInput.h + Source/ControllerInput/Gamepad/GamepadInput_State.cpp + Source/ControllerInput/Gamepad/GamepadInput_State.h Source/ControllerInput/Keyboard/KeyBindingOption.cpp Source/ControllerInput/Keyboard/KeyBindingOption.h Source/ControllerInput/Keyboard/KeyboardHidButtons.h @@ -925,6 +927,10 @@ file(GLOB LIBRARY_SOURCES Source/ComputerPrograms/Framework/ComputerProgramSession.h Source/ComputerPrograms/Framework/ComputerProgramWidget.cpp Source/ComputerPrograms/Framework/ComputerProgramWidget.h + Source/ControllerInput/Gamepad/GlobalGamepadTracker.cpp + Source/ControllerInput/Gamepad/GlobalGamepadTracker.h + Source/ControllerInput/Gamepad/GamepadInput_SDL.cpp + Source/ControllerInput/Gamepad/GamepadInput_SDL.h Source/ControllerInput/Keyboard/GlobalKeyboardHidTracker.cpp Source/ControllerInput/Keyboard/GlobalKeyboardHidTracker.h Source/ControllerInput/Keyboard/GlobalQtKeyMap.cpp