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
This commit is contained in:
Hugo Hromic 2020-08-21 20:00:22 +01:00
parent f852be74e8
commit 1e10bef09e
2 changed files with 20 additions and 0 deletions

View File

@ -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 (;;)

View File

@ -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;