From 1e10bef09e468bd4935563d15dc295fa9982ae05 Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Fri, 21 Aug 2020 20:00:22 +0100 Subject: [PATCH] SDLControllerInterface: Add support for optional game controller database If an optional "gamecontrollerdb.txt" file exists in the user directory, then SDL game controller mappings will be loaded from it. There is an officially endorsed community sourced database in https://github.com/gabomdq/SDL_GameControllerDB --- .../sdl_controller_interface.cpp | 17 +++++++++++++++++ src/frontend-common/sdl_controller_interface.h | 3 +++ 2 files changed, 20 insertions(+) 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;