Skip to content

Commit

Permalink
linux gamecube sdl support (#416)
Browse files Browse the repository at this point in the history
* add stuff to handle gamecube layouts in sdl

* fix copypasta mistake

* clang-format
  • Loading branch information
briaguya-ai committed Jan 30, 2024
1 parent 3892965 commit 0833afa
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(LUSDeviceIndex lusDeviceInd
return std::vector<std::shared_ptr<ControllerButtonMapping>>();
}

bool isGameCube = sdlIndexMapping->GetSDLControllerName() == "Nintendo GameCube Controller";

switch (bitmask) {
case BTN_A:
mappings.push_back(
Expand All @@ -557,8 +559,10 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(LUSDeviceIndex lusDeviceInd
std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_B, SDL_CONTROLLER_BUTTON_B));
break;
case BTN_L:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_L,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
if (!isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_L,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
}
break;
case BTN_R:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(lusDeviceIndex, portIndex, BTN_R,
Expand All @@ -579,14 +583,26 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(LUSDeviceIndex lusDeviceInd
case BTN_CDOWN:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(lusDeviceIndex, portIndex, BTN_CDOWN,
SDL_CONTROLLER_AXIS_RIGHTY, 1));
if (isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_CDOWN,
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
}
break;
case BTN_CLEFT:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(lusDeviceIndex, portIndex, BTN_CLEFT,
SDL_CONTROLLER_AXIS_RIGHTX, -1));
if (isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_CLEFT,
SDL_CONTROLLER_BUTTON_Y));
}
break;
case BTN_CRIGHT:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(lusDeviceIndex, portIndex, BTN_CRIGHT,
SDL_CONTROLLER_AXIS_RIGHTX, 1));
if (isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_CRIGHT,
SDL_CONTROLLER_BUTTON_X));
}
break;
case BTN_DUP:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(lusDeviceIndex, portIndex, BTN_DUP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ SDLAxisDirectionToAnyMapping::~SDLAxisDirectionToAnyMapping() {
std::string SDLAxisDirectionToAnyMapping::GetPhysicalInputName() {
switch (mControllerAxis) {
case SDL_CONTROLLER_AXIS_LEFTX:
return StringHelper::Sprintf("Left Stick %s",
return StringHelper::Sprintf(UsesGameCubeLayout() ? "Analog Stick %s" : "Left Stick %s",
mAxisDirection == NEGATIVE ? ICON_FA_ARROW_LEFT : ICON_FA_ARROW_RIGHT);
case SDL_CONTROLLER_AXIS_LEFTY:
return StringHelper::Sprintf("Left Stick %s",
return StringHelper::Sprintf(UsesGameCubeLayout() ? "Analog Stick %s" : "Left Stick %s",
mAxisDirection == NEGATIVE ? ICON_FA_ARROW_UP : ICON_FA_ARROW_DOWN);
case SDL_CONTROLLER_AXIS_RIGHTX:
return StringHelper::Sprintf("Right Stick %s",
return StringHelper::Sprintf(UsesGameCubeLayout() ? "C Stick %s" : "Left Stick %s",
mAxisDirection == NEGATIVE ? ICON_FA_ARROW_LEFT : ICON_FA_ARROW_RIGHT);
case SDL_CONTROLLER_AXIS_RIGHTY:
return StringHelper::Sprintf("Right Stick %s",
return StringHelper::Sprintf(UsesGameCubeLayout() ? "C Stick %s" : "Left Stick %s",
mAxisDirection == NEGATIVE ? ICON_FA_ARROW_UP : ICON_FA_ARROW_DOWN);
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
if (UsesPlaystationLayout()) {
Expand All @@ -38,6 +38,9 @@ std::string SDLAxisDirectionToAnyMapping::GetPhysicalInputName() {
if (UsesXboxLayout()) {
return "LT";
}
if (UsesGameCubeLayout()) {
return "L";
}
break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
if (UsesPlaystationLayout()) {
Expand All @@ -49,6 +52,9 @@ std::string SDLAxisDirectionToAnyMapping::GetPhysicalInputName() {
if (UsesXboxLayout()) {
return "RT";
}
if (UsesGameCubeLayout()) {
return "R";
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,44 @@ std::string SDLButtonToAnyMapping::GetPhysicalInputName() {
return GetXboxButtonName();
}

if (UsesGameCubeLayout()) {
return GetGameCubeButtonName();
}

return GetGenericButtonName();
}

std::string SDLButtonToAnyMapping::GetGenericButtonName() {
return StringHelper::Sprintf("B%d", mControllerButton);
}

std::string SDLButtonToAnyMapping::GetGameCubeButtonName() {
switch (mControllerButton) {
case SDL_CONTROLLER_BUTTON_A:
return "A";
case SDL_CONTROLLER_BUTTON_B:
return "B";
case SDL_CONTROLLER_BUTTON_X:
return "X";
case SDL_CONTROLLER_BUTTON_Y:
return "Y";
case SDL_CONTROLLER_BUTTON_START:
return "Start";
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
return "Z";
case SDL_CONTROLLER_BUTTON_DPAD_UP:
return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_UP);
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_DOWN);
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_LEFT);
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
return StringHelper::Sprintf("D-Pad %s", ICON_FA_ARROW_RIGHT);
}

return GetGenericButtonName();
}

std::string SDLButtonToAnyMapping::GetPlaystationButtonName() {
switch (mControllerButton) {
case SDL_CONTROLLER_BUTTON_A:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SDLButtonToAnyMapping : virtual public ControllerInputMapping, public SDLM
std::string GetPlaystationButtonName();
std::string GetSwitchButtonName();
std::string GetXboxButtonName();
std::string GetGameCubeButtonName();
std::string GetGenericButtonName();
};
} // namespace LUS
23 changes: 23 additions & 0 deletions src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ SDL_GameControllerType SDLMapping::GetSDLControllerType() {
return SDL_GameControllerGetType(mController);
}

uint16_t SDLMapping::GetSDLControllerVendorId() {
if (!ControllerLoaded()) {
return 0;
}

return SDL_GameControllerGetVendor(mController);
}

uint16_t SDLMapping::GetSDLControllerProductId() {
if (!ControllerLoaded()) {
return 0;
}

return SDL_GameControllerGetProduct(mController);
}

bool SDLMapping::UsesPlaystationLayout() {
auto type = GetSDLControllerType();
return type == SDL_CONTROLLER_TYPE_PS3 || type == SDL_CONTROLLER_TYPE_PS4 || type == SDL_CONTROLLER_TYPE_PS5;
Expand All @@ -91,6 +107,13 @@ bool SDLMapping::UsesXboxLayout() {
return type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE;
}

bool SDLMapping::UsesGameCubeLayout() {
auto vid = GetSDLControllerVendorId();
auto pid = GetSDLControllerProductId();

return vid == 0x57e && pid == 0x337;
}

int32_t SDLMapping::GetSDLDeviceIndex() {
auto deviceIndexMapping = std::static_pointer_cast<LUSDeviceIndexToSDLDeviceIndexMapping>(
LUS::Context::GetInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ class SDLMapping : public ControllerMapping {

protected:
SDL_GameControllerType GetSDLControllerType();
uint16_t GetSDLControllerVendorId();
uint16_t GetSDLControllerProductId();
bool UsesPlaystationLayout();
bool UsesSwitchLayout();
bool UsesXboxLayout();
bool UsesGameCubeLayout();
std::string GetSDLDeviceName();
int32_t GetSDLDeviceIndex();

Expand Down

0 comments on commit 0833afa

Please sign in to comment.