fix(input): load per-game config beside executable

This commit is contained in:
MartinCajiao 2026-09-27 20:20:17 -05:00
parent 6dfa449e52
commit 1007f6d64e
2 changed files with 10 additions and 2 deletions

View File

@ -6,12 +6,14 @@
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_set>
#include "SDL_keyboard.h"
#include "SDL_filesystem.h"
namespace {
@ -123,13 +125,19 @@ Pad::InputBinding parseBinding(const InputAction& action, std::string_view sourc
throw std::runtime_error("Pad: invalid input mapping " + path.string() + ":" + std::to_string(line) + ": " + reason);
}
std::filesystem::path defaultConfigPath() {
std::unique_ptr<char, decltype(&SDL_free)> basePath(SDL_GetBasePath(), SDL_free);
if (basePath) return std::filesystem::path(basePath.get()) / "anyps5-input.ini";
return "anyps5-input.ini";
}
}
std::vector<Pad::InputBinding> Pad::LoadInputMapping() {
std::vector<InputBinding> bindings(InputMapping.begin(), InputMapping.end());
const char* configuredPath = std::getenv("ANYPS5_INPUT_CONFIG");
const bool explicitPath = configuredPath != nullptr && configuredPath[0] != '\0';
const std::filesystem::path path = explicitPath ? configuredPath : "anyps5-input.ini";
const std::filesystem::path path = explicitPath ? configuredPath : defaultConfigPath();
std::ifstream file(path);
if (!file) {
if (explicitPath || std::filesystem::exists(path)) {

View File

@ -1,6 +1,6 @@
# Keyboard and mouse mapping
AnyPS5 uses its built-in keyboard and mouse bindings when no configuration file is present. To change selected bindings, create `anyps5-input.ini` in the game's working directory. Set `ANYPS5_INPUT_CONFIG` to use a file at another path.
AnyPS5 uses its built-in keyboard and mouse bindings when no configuration file is present. To change selected bindings, create `anyps5-input.ini` beside the generated game executable. Set `ANYPS5_INPUT_CONFIG` to use a file at another path.
Each non-empty line has the form `Action = Type:Value`. Action names are case-insensitive. A `#` or `;` starts a comment. The first line for an action replaces its built-in bindings; later lines for the same action add alternate inputs. Duplicate entries for the same action are ignored. Actions omitted from the file keep their built-in bindings. UTF-8 files with or without a byte-order mark are supported.