diff --git a/src/frontend-common/sdl_controller_interface.cpp b/src/frontend-common/sdl_controller_interface.cpp index 7b2c93b0e..96a07b04d 100644 --- a/src/frontend-common/sdl_controller_interface.cpp +++ b/src/frontend-common/sdl_controller_interface.cpp @@ -1,5 +1,6 @@ #include "sdl_controller_interface.h" #include "common/assert.h" +#include "common/file_system.h" #include "common/log.h" #include "core/controller.h" #include "core/host_interface.h" @@ -23,6 +24,17 @@ bool SDLControllerInterface::Initialize(CommonHostInterface* host_interface) FrontendCommon::EnsureSDLInitialized(); + const std::string gcdb_file_name = GetGameControllerDBFileName(); + if (FileSystem::FileExists(gcdb_file_name.c_str())) + { + Log_InfoPrintf("Loading game controller mappings from '%s'", gcdb_file_name.c_str()); + if (SDL_GameControllerAddMappingsFromFile(gcdb_file_name.c_str()) < 0) + { + Log_ErrorPrintf("SDL_GameControllerAddMappingsFromFile(%s) failed: %s", + gcdb_file_name.c_str(), SDL_GetError()); + } + } + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0) { Log_ErrorPrintf("SDL_InitSubSystem(SDL_INIT_JOYSTICK |SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) failed"); @@ -48,6 +60,11 @@ void SDLControllerInterface::Shutdown() ControllerInterface::Shutdown(); } +std::string SDLControllerInterface::GetGameControllerDBFileName() const +{ + return m_host_interface->GetUserDirectoryRelativePath("gamecontrollerdb.txt"); +} + void SDLControllerInterface::PollEvents() { for (;;) diff --git a/src/frontend-common/sdl_controller_interface.h b/src/frontend-common/sdl_controller_interface.h index 1aacab523..7636d9372 100644 --- a/src/frontend-common/sdl_controller_interface.h +++ b/src/frontend-common/sdl_controller_interface.h @@ -17,6 +17,9 @@ public: bool Initialize(CommonHostInterface* host_interface) override; void Shutdown() override; + /// Returns the path of the optional game controller database file. + std::string GetGameControllerDBFileName() const; + // Removes all bindings. Call before setting new bindings. void ClearBindings() override;