Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ target_link_libraries(
$<LINK_LIBRARY:WHOLE_ARCHIVE,SerialProgramsLib>
)

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")
Expand Down
9 changes: 9 additions & 0 deletions SerialPrograms/Source/CommonFramework/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions SerialPrograms/Source/ControllerInput/ControllerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

#include <vector>
#ifdef PA_SDL
#include "Gamepad/GlobalGamepadTracker.h"
#endif
#include "Keyboard/GlobalKeyboardHidTracker.h"
#include "ControllerInput.h"

Expand All @@ -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

}

Expand Down
102 changes: 102 additions & 0 deletions SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* SDL Gamepad Input
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifdef PA_SDL

#include <algorithm>
#include <cmath>
#include <utility>
#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<double, double> gamepad_stick(int x, int y){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review: Minor: gamepad_stick() (and gamepad_trigger() below) are file-local helpers but have external linkage — they're free functions in namespace PokemonAutomation that the header doesn't declare. Make them static or put them in an anonymous namespace (the codebase's usual convention) so they can't collide with symbols from other translation units.

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<SDL_GamepadButton, Button> 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
24 changes: 24 additions & 0 deletions SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_SDL.h
Original file line number Diff line number Diff line change
@@ -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 <tuple>
#include <SDL3/SDL.h>
#include "GamepadInput_State.h"

namespace PokemonAutomation{

std::tuple<double, double> gamepad_stick(int x, int y);
bool gamepad_trigger(int value);
GamepadInputState read_gamepad(SDL_Gamepad* pad);

}
#endif
#endif
Original file line number Diff line number Diff line change
@@ -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<const GamepadInputState*>(&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();
}
}

}
40 changes: 40 additions & 0 deletions SerialPrograms/Source/ControllerInput/Gamepad/GamepadInput_State.h
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading