From c049f6814bf374337a960158607c08938ab2801e Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 7 Jul 2023 17:52:55 +1000 Subject: [PATCH] Input/SDL: Load controller DB after init Tries to lock a non-existant mutex on Windows otherwise. --- pcsx2/Input/SDLInputSource.cpp | 47 ++++++++++++++++++++++++---------- pcsx2/Input/SDLInputSource.h | 3 ++- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/pcsx2/Input/SDLInputSource.cpp b/pcsx2/Input/SDLInputSource.cpp index 2802e798bd..236e52a56c 100644 --- a/pcsx2/Input/SDLInputSource.cpp +++ b/pcsx2/Input/SDLInputSource.cpp @@ -1,5 +1,5 @@ /* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2022 PCSX2 Dev Team + * Copyright (C) 2002-2023 PCSX2 Dev Team * * PCSX2 is free software: you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Found- @@ -14,16 +14,24 @@ */ #include "PrecompiledHeader.h" +#include "Config.h" #include "Input/SDLInputSource.h" #include "Input/InputManager.h" #include "Host.h" + #include "common/Assertions.h" -#include "common/StringUtil.h" #include "common/Console.h" +#include "common/Error.h" +#include "common/FileSystem.h" +#include "common/Path.h" +#include "common/StringUtil.h" + #include #include "GS/GSIntrin.h" // _BitScanForward +static constexpr const char* CONTROLLER_DB_FILENAME = "game_controller_db.txt"; + static constexpr const char* s_sdl_axis_names[] = { "LeftX", // SDL_CONTROLLER_AXIS_LEFTX "LeftY", // SDL_CONTROLLER_AXIS_LEFTY @@ -118,18 +126,6 @@ SDLInputSource::~SDLInputSource() bool SDLInputSource::Initialize(SettingsInterface& si, std::unique_lock& settings_lock) { - std::optional> controller_db_data = Host::ReadResourceFile("game_controller_db.txt"); - if (controller_db_data.has_value()) - { - SDL_RWops* ops = SDL_RWFromConstMem(controller_db_data->data(), static_cast(controller_db_data->size())); - if (SDL_GameControllerAddMappingsFromRW(ops, true) < 0) - Console.Error("SDL_GameControllerAddMappingsFromRW() failed: %s", SDL_GetError()); - } - else - { - Console.Error("Controller database resource is missing."); - } - LoadSettings(si); settings_lock.unlock(); SetHints(); @@ -226,6 +222,28 @@ void SDLInputSource::SetHints() SDL_SetHint(hint.first.c_str(), hint.second.c_str()); } +bool SDLInputSource::LoadControllerDB() +{ + Error error; + std::FILE* fp = + FileSystem::OpenCFile(Path::Combine(EmuFolders::Resources, CONTROLLER_DB_FILENAME).c_str(), "rb", &error); + if (!fp) + { + Console.Error(fmt::format("SDLInputSource: Failed to open controller database: {}", error.GetDescription())); + return false; + } + + if (SDL_GameControllerAddMappingsFromRW(SDL_RWFromFP(fp, SDL_TRUE), SDL_TRUE) < 0) + { + Console.Error(fmt::format("SDLInputSource: SDL_GameControllerAddMappingsFromRW() failed: {}", SDL_GetError())); + return false; + } + + Console.WriteLn(Color_StrongGreen, fmt::format("SDLInputSource: Loaded {} controller mappings from {}.", + SDL_GameControllerNumMappings(), CONTROLLER_DB_FILENAME)); + return true; +} + bool SDLInputSource::InitializeSubsystem() { if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0) @@ -236,6 +254,7 @@ bool SDLInputSource::InitializeSubsystem() // we should open the controllers as the connected events come in, so no need to do any more here m_sdl_subsystem_initialized = true; + LoadControllerDB(); return true; } diff --git a/pcsx2/Input/SDLInputSource.h b/pcsx2/Input/SDLInputSource.h index 64494d8bb1..8014413a2c 100644 --- a/pcsx2/Input/SDLInputSource.h +++ b/pcsx2/Input/SDLInputSource.h @@ -1,5 +1,5 @@ /* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2022 PCSX2 Dev Team + * Copyright (C) 2002-2023 PCSX2 Dev Team * * PCSX2 is free software: you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Found- @@ -79,6 +79,7 @@ private: void ShutdownSubsystem(); void LoadSettings(SettingsInterface& si); void SetHints(); + bool LoadControllerDB(); ControllerDataVector::iterator GetControllerDataForJoystickId(int id); ControllerDataVector::iterator GetControllerDataForPlayerId(int id);