From bb9c223d42ed4ba5e6a1fb7cfb2fe97a27eaaefb Mon Sep 17 00:00:00 2001 From: Stephen Miller <56742918+StevenMiller123@users.noreply.github.com> Date: Tue, 24 Mar 2026 01:40:18 -0500 Subject: [PATCH 01/14] Core: Minor code cleanup (#4166) * Log filters cleanup * Clearer dialog options for config update * Smaller button labels These don't auto-resize, and I don't want to read SDL's docs for something so small. --- src/common/logging/filter.cpp | 5 +---- src/common/logging/types.h | 7 ++----- src/core/emulator_settings.cpp | 38 +++++++++++++++++----------------- src/core/emulator_settings.h | 18 ++++++++-------- src/core/user_settings.cpp | 12 +++++------ 5 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/common/logging/filter.cpp b/src/common/logging/filter.cpp index f2597603e..fd48faf72 100644 --- a/src/common/logging/filter.cpp +++ b/src/common/logging/filter.cpp @@ -68,6 +68,7 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) { CLS(Common) \ SUB(Common, Filesystem) \ SUB(Common, Memory) \ + CLS(KeyManager) \ CLS(Core) \ SUB(Core, Linker) \ SUB(Core, Devices) \ @@ -80,7 +81,6 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) { SUB(Kernel, Event) \ SUB(Kernel, Sce) \ CLS(Lib) \ - SUB(Lib, LibC) \ SUB(Lib, LibcInternal) \ SUB(Lib, Kernel) \ SUB(Lib, Pad) \ @@ -117,7 +117,6 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) { SUB(Lib, NpSnsFacebookDialog) \ SUB(Lib, NpPartner) \ SUB(Lib, Screenshot) \ - SUB(Lib, LibCInternal) \ SUB(Lib, AppContent) \ SUB(Lib, Rtc) \ SUB(Lib, Rudp) \ @@ -163,8 +162,6 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) { CLS(ImGui) \ CLS(Input) \ CLS(Tty) \ - CLS(KeyManager) \ - CLS(EmuSettings) \ CLS(Loader) // GetClassName is a macro defined by Windows.h, grrr... diff --git a/src/common/logging/types.h b/src/common/logging/types.h index 4c6e53453..2c6edef3b 100644 --- a/src/common/logging/types.h +++ b/src/common/logging/types.h @@ -34,6 +34,7 @@ enum class Class : u8 { Common, ///< Library routines Common_Filesystem, ///< Filesystem interface library Common_Memory, ///< Memory mapping and management functions + KeyManager, ///< Key management system Core, ///< LLE emulation core Core_Linker, ///< The module linker Core_Devices, ///< Devices emulation @@ -44,10 +45,9 @@ enum class Class : u8 { Kernel_Fs, ///< The filesystem implementation of the kernel. Kernel_Vmm, ///< The virtual memory implementation of the kernel. Kernel_Event, ///< The event management implementation of the kernel. - Kernel_Sce, ///< The sony specific interfaces provided by the kernel. + Kernel_Sce, ///< The Sony-specific interfaces provided by the kernel. Lib, ///< HLE implementation of system library. Each major library ///< should have its own subclass. - Lib_LibC, ///< The LibC implementation. Lib_LibcInternal, ///< The LibcInternal implementation. Lib_Kernel, ///< The LibKernel implementation. Lib_Pad, ///< The LibScePad implementation. @@ -83,7 +83,6 @@ enum class Class : u8 { Lib_NpProfileDialog, ///< The LibSceNpProfileDialog implementation Lib_NpSnsFacebookDialog, ///< The LibSceNpSnsFacebookDialog implementation Lib_Screenshot, ///< The LibSceScreenshot implementation - Lib_LibCInternal, ///< The LibCInternal implementation. Lib_AppContent, ///< The LibSceAppContent implementation. Lib_Rtc, ///< The LibSceRtc implementation. Lib_Rudp, ///< The LibSceRudp implementation. @@ -131,8 +130,6 @@ enum class Class : u8 { Loader, ///< ROM loader Input, ///< Input emulation Tty, ///< Debug output from emu - KeyManager, ///< Key management system - EmuSettings, /// Emulator settings system Count ///< Total number of logging classes }; diff --git a/src/core/emulator_settings.cpp b/src/core/emulator_settings.cpp index 8f26485e6..f738dc1b5 100644 --- a/src/core/emulator_settings.cpp +++ b/src/core/emulator_settings.cpp @@ -81,12 +81,12 @@ std::optional get_optional(const toml::value& v, const std::string& key) { void EmulatorSettingsImpl::PrintChangedSummary(const std::vector& changed) { if (changed.empty()) { - LOG_DEBUG(EmuSettings, "No game-specific overrides applied"); + LOG_DEBUG(Config, "No game-specific overrides applied"); return; } - LOG_DEBUG(EmuSettings, "Game-specific overrides applied:"); + LOG_DEBUG(Config, "Game-specific overrides applied:"); for (const auto& k : changed) - LOG_DEBUG(EmuSettings, " * {}", k); + LOG_DEBUG(Config, " * {}", k); } // ── Singleton ──────────────────────────────────────────────────────── @@ -212,7 +212,7 @@ void EmulatorSettingsImpl::ClearGameSpecificOverrides() { ClearGroupOverrides(m_audio); ClearGroupOverrides(m_gpu); ClearGroupOverrides(m_vulkan); - LOG_DEBUG(EmuSettings, "All game-specific overrides cleared"); + LOG_DEBUG(Config, "All game-specific overrides cleared"); } void EmulatorSettingsImpl::ResetGameSpecificValue(const std::string& key) { @@ -238,7 +238,7 @@ void EmulatorSettingsImpl::ResetGameSpecificValue(const std::string& key) { return; if (tryGroup(m_vulkan)) return; - LOG_WARNING(EmuSettings, "ResetGameSpecificValue: key '{}' not found", key); + LOG_WARNING(Config, "ResetGameSpecificValue: key '{}' not found", key); } bool EmulatorSettingsImpl::Save(const std::string& serial) { @@ -276,7 +276,7 @@ bool EmulatorSettingsImpl::Save(const std::string& serial) { std::ofstream out(path); if (!out) { - LOG_ERROR(EmuSettings, "Failed to open game config for writing: {}", path.string()); + LOG_ERROR(Config, "Failed to open game config for writing: {}", path.string()); return false; } out << std::setw(2) << j; @@ -317,14 +317,14 @@ bool EmulatorSettingsImpl::Save(const std::string& serial) { std::ofstream out(path); if (!out) { - LOG_ERROR(EmuSettings, "Failed to open config for writing: {}", path.string()); + LOG_ERROR(Config, "Failed to open config for writing: {}", path.string()); return false; } out << std::setw(2) << existing; return !out.fail(); } } catch (const std::exception& e) { - LOG_ERROR(EmuSettings, "Error saving settings: {}", e.what()); + LOG_ERROR(Config, "Error saving settings: {}", e.what()); return false; } } @@ -337,7 +337,7 @@ bool EmulatorSettingsImpl::Load(const std::string& serial) { // ── Global config ────────────────────────────────────────── const auto userDir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); const auto configPath = userDir / "config.json"; - LOG_DEBUG(EmuSettings, "Loading global config from: {}", configPath.string()); + LOG_DEBUG(Config, "Loading global config from: {}", configPath.string()); if (std::ifstream in{configPath}; in.good()) { json gj; @@ -358,13 +358,13 @@ bool EmulatorSettingsImpl::Load(const std::string& serial) { mergeGroup(m_gpu, "GPU"); mergeGroup(m_vulkan, "Vulkan"); - LOG_DEBUG(EmuSettings, "Global config loaded successfully"); + LOG_DEBUG(Config, "Global config loaded successfully"); } else { if (std::filesystem::exists(Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "config.toml")) { SDL_MessageBoxButtonData btns[2]{ - {0, 0, "No"}, - {0, 1, "Yes"}, + {0, 0, "Defaults"}, + {0, 1, "Update"}, }; SDL_MessageBoxData msg_box{ 0, @@ -392,7 +392,7 @@ bool EmulatorSettingsImpl::Load(const std::string& serial) { } } } - LOG_DEBUG(EmuSettings, "Global config not found - using defaults"); + LOG_DEBUG(Config, "Global config not found - using defaults"); SetDefaultValues(); Save(); } @@ -408,16 +408,16 @@ bool EmulatorSettingsImpl::Load(const std::string& serial) { // base configuration. const auto gamePath = Common::FS::GetUserPath(Common::FS::PathType::CustomConfigs) / (serial + ".json"); - LOG_DEBUG(EmuSettings, "Applying game config: {}", gamePath.string()); + LOG_DEBUG(Config, "Applying game config: {}", gamePath.string()); if (!std::filesystem::exists(gamePath)) { - LOG_DEBUG(EmuSettings, "No game-specific config found for {}", serial); + LOG_DEBUG(Config, "No game-specific config found for {}", serial); return false; } std::ifstream in(gamePath); if (!in) { - LOG_ERROR(EmuSettings, "Failed to open game config: {}", gamePath.string()); + LOG_ERROR(Config, "Failed to open game config: {}", gamePath.string()); return false; } @@ -448,7 +448,7 @@ bool EmulatorSettingsImpl::Load(const std::string& serial) { return true; } } catch (const std::exception& e) { - LOG_ERROR(EmuSettings, "Error loading settings: {}", e.what()); + LOG_ERROR(Config, "Error loading settings: {}", e.what()); return false; } } @@ -611,7 +611,7 @@ bool EmulatorSettingsImpl::TransferSettings() { } s.install_dirs.value = settings_install_dirs; } catch (const std::exception& e) { - LOG_WARNING(EmuSettings, "Failed to transfer install directories: {}", e.what()); + LOG_WARNING(Config, "Failed to transfer install directories: {}", e.what()); } // Transfer addon install directory @@ -627,7 +627,7 @@ bool EmulatorSettingsImpl::TransferSettings() { } } } catch (const std::exception& e) { - LOG_WARNING(EmuSettings, "Failed to transfer addon install directory: {}", e.what()); + LOG_WARNING(Config, "Failed to transfer addon install directory: {}", e.what()); } } diff --git a/src/core/emulator_settings.h b/src/core/emulator_settings.h index fab94c6ff..ad9cfe227 100644 --- a/src/core/emulator_settings.h +++ b/src/core/emulator_settings.h @@ -112,26 +112,26 @@ inline OverrideItem make_override(const char* key, Setting Struct::* member) return OverrideItem{ key, [member, key](void* base, const nlohmann::json& entry, std::vector& changed) { - LOG_DEBUG(EmuSettings, "[make_override] Processing key: {}", key); - LOG_DEBUG(EmuSettings, "[make_override] Entry JSON: {}", entry.dump()); + LOG_DEBUG(Config, "[make_override] Processing key: {}", key); + LOG_DEBUG(Config, "[make_override] Entry JSON: {}", entry.dump()); Struct* obj = reinterpret_cast(base); Setting& dst = obj->*member; try { T newValue = entry.get(); - LOG_DEBUG(EmuSettings, "[make_override] Parsed value: {}", newValue); - LOG_DEBUG(EmuSettings, "[make_override] Current value: {}", dst.value); + LOG_DEBUG(Config, "[make_override] Parsed value: {}", newValue); + LOG_DEBUG(Config, "[make_override] Current value: {}", dst.value); if (dst.value != newValue) { std::ostringstream oss; oss << key << " ( " << dst.value << " → " << newValue << " )"; changed.push_back(oss.str()); - LOG_DEBUG(EmuSettings, "[make_override] Recorded change: {}", oss.str()); + LOG_DEBUG(Config, "[make_override] Recorded change: {}", oss.str()); } dst.game_specific_value = newValue; - LOG_DEBUG(EmuSettings, "[make_override] Successfully updated {}", key); + LOG_DEBUG(Config, "[make_override] Successfully updated {}", key); } catch (const std::exception& e) { - LOG_ERROR(EmuSettings, "[make_override] ERROR parsing {}: {}", key, e.what()); - LOG_ERROR(EmuSettings, "[make_override] Entry was: {}", entry.dump()); - LOG_ERROR(EmuSettings, "[make_override] Type name: {}", entry.type_name()); + LOG_ERROR(Config, "[make_override] ERROR parsing {}: {}", key, e.what()); + LOG_ERROR(Config, "[make_override] Entry was: {}", entry.dump()); + LOG_ERROR(Config, "[make_override] Type name: {}", entry.type_name()); } }, diff --git a/src/core/user_settings.cpp b/src/core/user_settings.cpp index a2142dd9a..cf569a68a 100644 --- a/src/core/user_settings.cpp +++ b/src/core/user_settings.cpp @@ -44,13 +44,13 @@ bool UserSettingsImpl::Save() const { std::ofstream out(path); if (!out) { - LOG_ERROR(EmuSettings, "Failed to open user settings for writing: {}", path.string()); + LOG_ERROR(Config, "Failed to open user settings for writing: {}", path.string()); return false; } out << std::setw(2) << j; return !out.fail(); } catch (const std::exception& e) { - LOG_ERROR(EmuSettings, "Error saving user settings: {}", e.what()); + LOG_ERROR(Config, "Error saving user settings: {}", e.what()); return false; } } @@ -59,7 +59,7 @@ bool UserSettingsImpl::Load() { const auto path = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "users.json"; try { if (!std::filesystem::exists(path)) { - LOG_DEBUG(EmuSettings, "User settings file not found: {}", path.string()); + LOG_DEBUG(Config, "User settings file not found: {}", path.string()); // Create default user if no file exists if (m_userManager.GetUsers().user.empty()) { m_userManager.GetUsers() = m_userManager.CreateDefaultUsers(); @@ -70,7 +70,7 @@ bool UserSettingsImpl::Load() { std::ifstream in(path); if (!in) { - LOG_ERROR(EmuSettings, "Failed to open user settings: {}", path.string()); + LOG_ERROR(Config, "Failed to open user settings: {}", path.string()); return false; } @@ -97,10 +97,10 @@ bool UserSettingsImpl::Load() { Save(); } - LOG_DEBUG(EmuSettings, "User settings loaded successfully"); + LOG_DEBUG(Config, "User settings loaded successfully"); return true; } catch (const std::exception& e) { - LOG_ERROR(EmuSettings, "Error loading user settings: {}", e.what()); + LOG_ERROR(Config, "Error loading user settings: {}", e.what()); // Fall back to defaults if (m_userManager.GetUsers().user.empty()) { m_userManager.GetUsers() = m_userManager.CreateDefaultUsers(); From 3cc56bf84ce03dc049baa200a10a449ca84afe2d Mon Sep 17 00:00:00 2001 From: Stephen Miller <56742918+StevenMiller123@users.noreply.github.com> Date: Wed, 25 Mar 2026 16:43:30 -0500 Subject: [PATCH 02/14] More verbose errors for file opening failures (#4173) * Verbose errors for file opening failures These can be pretty helpful, and games don't usually spam them outside loading screens. * oops --- src/core/libraries/kernel/file_system.cpp | 14 +++++++++++++- .../libraries/libc_internal/libc_internal_io.cpp | 6 +++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 184343801..085e18b9f 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -87,11 +87,13 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { if (!read && !write && !rdwr) { // Start by checking for invalid flags. *__Error() = POSIX_EINVAL; + LOG_ERROR(Kernel_Fs, "Opening path {} failed, invalid flags {:#x}", raw_path, flags); return -1; } if (strlen(raw_path) > 255) { *__Error() = POSIX_ENAMETOOLONG; + LOG_ERROR(Kernel_Fs, "Opening path {} failed, path is too long", raw_path); return -1; } @@ -137,6 +139,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Error if file exists h->DeleteHandle(handle); *__Error() = POSIX_EEXIST; + LOG_ERROR(Kernel_Fs, "Creating {} failed, file already exists", raw_path); return -1; } @@ -145,6 +148,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Can't create files in a read only directory h->DeleteHandle(handle); *__Error() = POSIX_EROFS; + LOG_ERROR(Kernel_Fs, "Creating {} failed, path is read-only", raw_path); return -1; } // Create a file if it doesn't exist @@ -154,6 +158,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // If we're not creating a file, and it doesn't exist, return ENOENT h->DeleteHandle(handle); *__Error() = POSIX_ENOENT; + LOG_ERROR(Kernel_Fs, "Opening path {} failed, file does not exist", raw_path); return -1; } @@ -169,6 +174,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // This will trigger when create & directory is specified, this is expected. h->DeleteHandle(handle); *__Error() = POSIX_ENOTDIR; + LOG_ERROR(Kernel_Fs, "Opening directory {} failed, file is not a directory", raw_path); return -1; } @@ -176,6 +182,8 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Cannot open directories with any type of write access h->DeleteHandle(handle); *__Error() = POSIX_EISDIR; + LOG_ERROR(Kernel_Fs, "Opening directory {} failed, cannot open directories for writing", + raw_path); return -1; } @@ -183,6 +191,8 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Cannot open directories with truncate h->DeleteHandle(handle); *__Error() = POSIX_EISDIR; + LOG_ERROR(Kernel_Fs, "Opening directory {} failed, cannot truncate directories", + raw_path); return -1; } @@ -201,6 +211,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Can't open files with truncate flag in a read only directory h->DeleteHandle(handle); *__Error() = POSIX_EROFS; + LOG_ERROR(Kernel_Fs, "Truncating {} failed, path is read-only", raw_path); return -1; } else if (truncate) { // Open the file as read-write so we can truncate regardless of flags. @@ -219,6 +230,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Can't open files with write/read-write access in a read only directory h->DeleteHandle(handle); *__Error() = POSIX_EROFS; + LOG_ERROR(Kernel_Fs, "Opening {} for writing failed, path is read-only", raw_path); return -1; } else if (write) { if (append) { @@ -244,6 +256,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Open failed in platform-specific code, errno needs to be converted. h->DeleteHandle(handle); SetPosixErrno(e); + LOG_ERROR(Kernel_Fs, "Opening {} failed, error = {}", raw_path, *__Error()); return -1; } @@ -258,7 +271,6 @@ s32 PS4_SYSV_ABI posix_open(const char* filename, s32 flags, u16 mode) { s32 PS4_SYSV_ABI sceKernelOpen(const char* path, s32 flags, /* SceKernelMode*/ u16 mode) { s32 result = open(path, flags, mode); if (result < 0) { - LOG_ERROR(Kernel_Fs, "error = {}", *__Error()); return ErrnoToSceKernelError(*__Error()); } return result; diff --git a/src/core/libraries/libc_internal/libc_internal_io.cpp b/src/core/libraries/libc_internal/libc_internal_io.cpp index 504ba5b48..0bb23eb78 100644 --- a/src/core/libraries/libc_internal/libc_internal_io.cpp +++ b/src/core/libraries/libc_internal/libc_internal_io.cpp @@ -199,7 +199,11 @@ OrbisFILE* PS4_SYSV_ABI internal_fopen(const char* path, const char* mode) { std::scoped_lock lk{g_file_mtx}; LOG_INFO(Lib_LibcInternal, "called, path {}, mode {}", path, mode); OrbisFILE* file = internal__Fofind(); - return internal__Foprep(path, mode, file, -1, 0, 0); + OrbisFILE* ret_file = internal__Foprep(path, mode, file, -1, 0, 0); + if (ret_file == nullptr) { + LOG_ERROR(Lib_LibcInternal, "failed to open file {}", path); + } + return ret_file; } s32 PS4_SYSV_ABI internal_fflush(OrbisFILE* file) { From 650652db4213cfef60f4c2dcdee2942d572ec344 Mon Sep 17 00:00:00 2001 From: rainmakerv2 <30595646+rainmakerv3@users.noreply.github.com> Date: Thu, 26 Mar 2026 06:13:43 +0800 Subject: [PATCH 03/14] Vblank frequency setting fix + utf 8 encoding for paths (#4174) * vblank frequency setting fix * force utf-8 encoding upon json serialization/deserialization for path strings --- src/core/emulator_settings.cpp | 9 ++++++--- src/core/emulator_settings.h | 15 ++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/core/emulator_settings.cpp b/src/core/emulator_settings.cpp index f738dc1b5..79d9a86f8 100644 --- a/src/core/emulator_settings.cpp +++ b/src/core/emulator_settings.cpp @@ -25,10 +25,13 @@ namespace nlohmann { template <> struct adl_serializer { static void to_json(json& j, const std::filesystem::path& p) { - j = p.string(); + const auto u8 = p.u8string(); + j = std::string(reinterpret_cast(u8.data()), u8.size()); } static void from_json(const json& j, std::filesystem::path& p) { - p = j.get(); + const std::string s = j.get(); + p = std::filesystem::path( + std::u8string_view(reinterpret_cast(s.data()), s.size())); } }; } // namespace nlohmann @@ -647,4 +650,4 @@ std::vector EmulatorSettingsImpl::GetAllOverrideableKeys() const { addGroup(m_gpu.GetOverrideableFields()); addGroup(m_vulkan.GetOverrideableFields()); return keys; -} \ No newline at end of file +} diff --git a/src/core/emulator_settings.h b/src/core/emulator_settings.h index ad9cfe227..370fb0ab0 100644 --- a/src/core/emulator_settings.h +++ b/src/core/emulator_settings.h @@ -594,16 +594,17 @@ public: SETTING_FORWARD_BOOL_READONLY(m_gpu, PatchShaders, patch_shaders) u32 GetVblankFrequency() { - if (m_gpu.vblank_frequency.value < 60) { - m_gpu.vblank_frequency.value = 60; + if (m_gpu.vblank_frequency.value < 30) { + return 30; } - return m_gpu.vblank_frequency.value; + return m_gpu.vblank_frequency.get(); } - void SetVblankFrequency(const u32& v) { - if (v < 60) { - m_gpu.vblank_frequency.value = 60; + void SetVblankFrequency(const u32& v, bool is_specific = false) { + u32 val = v < 30 ? 30 : v; + if (is_specific) { + m_gpu.vblank_frequency.game_specific_value = val; } else { - m_gpu.vblank_frequency.value = v; + m_gpu.vblank_frequency.value = val; } } From 31b2d9ccd77a9ca1bc3fde66f0d214429c9b5811 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Thu, 26 Mar 2026 00:31:53 +0200 Subject: [PATCH 04/14] Introducing testing suite using google test (#4170) * initial tests for testing Setting * argg * keep switch off * added ci * fix ci * merging tests to build.yml * fixing linux ttests/? * one more try for ci * more linux fix for ci * more ci fixes * try to fix ctests now * should fix now * trying fixing linux tests * some more tests * more tests (68 tests) and clang format --- .github/workflows/build.yml | 77 +++ CMakeLists.txt | 8 + tests/.clang-format | 259 ++++++++++ tests/CMakeLists.txt | 84 ++++ tests/stubs/log_stub.cpp | 27 + tests/stubs/scm_rev_stub.cpp | 22 + tests/stubs/sdl_stub.cpp | 18 + tests/test_emulator_settings.cpp | 837 +++++++++++++++++++++++++++++++ 8 files changed, 1332 insertions(+) create mode 100644 tests/.clang-format create mode 100644 tests/CMakeLists.txt create mode 100644 tests/stubs/log_stub.cpp create mode 100644 tests/stubs/scm_rev_stub.cpp create mode 100644 tests/stubs/sdl_stub.cpp create mode 100644 tests/test_emulator_settings.cpp diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 26c8e20fd..96f5e33d7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,6 +14,8 @@ on: - "documents/**" - "**/*.md" + workflow_dispatch: + concurrency: group: ci-${{ github.event_name }}-${{ github.ref }} cancel-in-progress: ${{ github.event_name == 'push' }} @@ -65,6 +67,81 @@ jobs: echo "shorthash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT echo "fullhash=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT + test: + name: Run C++ Tests on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + include: + - os: windows-latest + compiler_cxx: clang-cl + compiler_c: clang-cl + - os: ubuntu-latest + compiler_cxx: clang++ + compiler_c: clang + - os: macos-latest + compiler_cxx: clang++ + compiler_c: clang + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup CMake + uses: lukka/get-cmake@latest + + - name: Setup Visual Studio shell (Windows only) + if: runner.os == 'Windows' + uses: egor-tensin/vs-shell@v2 + with: + arch: x64 + + - name: Install dependencies (Linux) + if: runner.os == 'Linux' + run: | + sudo apt-get update + sudo apt-get install -y ninja-build libx11-dev libxext-dev libwayland-dev libdecor-0-dev libxkbcommon-dev libxcursor-dev libxi-dev libxss-dev libxtst-dev libxrandr-dev libxfixes-dev libudev-dev uuid-dev uuid-dev + + - name: Install dependencies (macOS) + if: runner.os == 'macOS' + run: | + brew install ninja + + - name: Configure CMake + run: | + cmake -B build -G Ninja \ + -DCMAKE_CXX_COMPILER="${{ matrix.compiler_cxx }}" \ + -DCMAKE_C_COMPILER="${{ matrix.compiler_c }}" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DENABLE_TESTS=ON \ + ${{ runner.os == 'macOS' && '-DCMAKE_OSX_ARCHITECTURES=x86_64' || '' }} + shell: bash + + - name: Create shadPS4 user data directory (Linux) + if: runner.os == 'Linux' + run: mkdir -p ~/.local/share/shadPS4 + + - name: Create shadPS4 user data directory (macOS) + if: runner.os == 'macOS' + run: mkdir -p ~/Library/Application\ Support/shadPS4 + + - name: Create shadPS4 user data directory (Windows) + if: runner.os == 'Windows' + run: mkdir -p "$APPDATA/shadPS4" + shell: bash + + - name: Build all tests + run: cmake --build build + shell: bash + + - name: Run tests with CTest + run: ctest --test-dir build --output-on-failure --progress + shell: bash + windows-sdl: runs-on: windows-2025 needs: get-info diff --git a/CMakeLists.txt b/CMakeLists.txt index 06f8cb6ff..cbf373e57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ endif() option(ENABLE_DISCORD_RPC "Enable the Discord RPC integration" ON) option(ENABLE_UPDATER "Enables the options to updater" ON) +option(ENABLE_TESTS "Build unit tests (requires GTest)" OFF) # First, determine whether to use CMAKE_OSX_ARCHITECTURES or CMAKE_SYSTEM_PROCESSOR. if (APPLE AND CMAKE_OSX_ARCHITECTURES) @@ -1114,6 +1115,8 @@ set(EMULATOR src/emulator.cpp src/sdl_window.cpp ) +if(NOT ENABLE_TESTS) + add_executable(shadps4 ${AUDIO_CORE} ${IMGUI} @@ -1267,3 +1270,8 @@ endif() # Install rules install(TARGETS shadps4 BUNDLE DESTINATION .) + +else() + enable_testing() + add_subdirectory(tests) +endif() \ No newline at end of file diff --git a/tests/.clang-format b/tests/.clang-format new file mode 100644 index 000000000..d69044b0f --- /dev/null +++ b/tests/.clang-format @@ -0,0 +1,259 @@ +# SPDX-FileCopyrightText: 2016 Emmanuel Gil Peyrot +# SPDX-License-Identifier: GPL-2.0-or-later + +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: false +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 100 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeCategories: + - Regex: '^\<[^Q][^/.>]*\>' + Priority: -2 + - Regex: '^\<' + Priority: -1 + - Regex: '^\"' + Priority: 0 +IndentCaseLabels: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 150 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never +--- +Language: Java +# BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: false +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 100 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +IncludeCategories: + - Regex: '^\<[^Q][^/.>]*\>' + Priority: -2 + - Regex: '^\<' + Priority: -1 + - Regex: '^\"' + Priority: 0 +IndentCaseLabels: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 150 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never +--- +Language: ObjC +# BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: false +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 100 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +IncludeCategories: + - Regex: '^\<[^Q][^/.>]*\>' + Priority: -2 + - Regex: '^\<' + Priority: -1 + - Regex: '^\"' + Priority: 0 +IndentCaseLabels: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 150 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never +... diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 000000000..6b656762a --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,84 @@ +# SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +# SPDX-License-Identifier: GPL-2.0-or-later + +# Find or download Google Test +include(FetchContent) + FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip +) +FetchContent_MakeAvailable(googletest) + +set(SETTINGS_TEST_SOURCES + # Under test + ${CMAKE_SOURCE_DIR}/src/core/emulator_settings.cpp + ${CMAKE_SOURCE_DIR}/src/core/emulator_state.cpp + + # Minimal common support + ${CMAKE_SOURCE_DIR}/src/common/path_util.cpp + ${CMAKE_SOURCE_DIR}/src/common/assert.cpp + ${CMAKE_SOURCE_DIR}/src/common/error.cpp + ${CMAKE_SOURCE_DIR}/src/common/string_util.cpp + ${CMAKE_SOURCE_DIR}/src/common/logging/filter.cpp + ${CMAKE_SOURCE_DIR}/src/common/logging/text_formatter.cpp + + # Stubs that replace dependencies + stubs/log_stub.cpp + stubs/scm_rev_stub.cpp + stubs/sdl_stub.cpp + + # Tests + test_emulator_settings.cpp +) + +add_executable(shadps4_settings_test ${SETTINGS_TEST_SOURCES}) + +target_include_directories(shadps4_settings_test PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR} +) + +target_link_libraries(shadps4_settings_test PRIVATE + GTest::gtest_main + fmt::fmt + nlohmann_json::nlohmann_json + toml11::toml11 + SDL3::SDL3 +) + +target_compile_features(shadps4_settings_test PRIVATE cxx_std_23) + +target_compile_definitions(shadps4_settings_test PRIVATE BOOST_ASIO_STANDALONE) + +if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR + CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + include(CheckCXXSymbolExists) + check_cxx_symbol_exists(_LIBCPP_VERSION version LIBCPP) + if (LIBCPP) + target_compile_options(shadps4_settings_test PRIVATE -fexperimental-library) + endif() +endif() + +if (WIN32) + target_compile_definitions(shadps4_settings_test PRIVATE + NOMINMAX + WIN32_LEAN_AND_MEAN + NTDDI_VERSION=0x0A000006 + _WIN32_WINNT=0x0A00 + WINVER=0x0A00 + ) + if (MSVC) + target_compile_definitions(shadps4_settings_test PRIVATE + _CRT_SECURE_NO_WARNINGS + _CRT_NONSTDC_NO_DEPRECATE + _SCL_SECURE_NO_WARNINGS + _TIMESPEC_DEFINED + ) + endif() +endif() + +include(GoogleTest) +gtest_discover_tests(shadps4_settings_test + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + PROPERTIES TIMEOUT 60 +) diff --git a/tests/stubs/log_stub.cpp b/tests/stubs/log_stub.cpp new file mode 100644 index 000000000..64d6df644 --- /dev/null +++ b/tests/stubs/log_stub.cpp @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include "common/logging/backend.h" +#include "common/logging/filter.h" +#include "common/logging/log.h" + +namespace Common::Log { + + void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename, + unsigned int line_num, const char* function, const char* format, + const fmt::format_args& args) { + } + + void Initialize(std::string_view) {} + bool IsActive() { return false; } + void SetGlobalFilter(const Filter&) {} + void SetColorConsoleBackendEnabled(bool) {} + void Start() {} + void Stop() {} + void Denitializer() {} + void SetAppend() {} + +} // namespace Common::Log diff --git a/tests/stubs/scm_rev_stub.cpp b/tests/stubs/scm_rev_stub.cpp new file mode 100644 index 000000000..0f8a5af03 --- /dev/null +++ b/tests/stubs/scm_rev_stub.cpp @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include "common/scm_rev.h" + +namespace Common { + + constexpr char g_version[] = "0.0.0 TEST"; + constexpr bool g_is_release = false; + constexpr char g_scm_rev[] = "test_rev_hash"; + constexpr char g_scm_branch[] = "test_branch"; + constexpr char g_scm_desc[] = "test_desc"; + constexpr char g_scm_remote_name[] = "origin"; + constexpr char g_scm_remote_url[] = "https://github.com/test/shadPS4"; + constexpr char g_scm_date[] = "2026-03-23"; + + const std::string GetRemoteNameFromLink() { + return "test"; + } + +} // namespace Common diff --git a/tests/stubs/sdl_stub.cpp b/tests/stubs/sdl_stub.cpp new file mode 100644 index 000000000..859147696 --- /dev/null +++ b/tests/stubs/sdl_stub.cpp @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: Copyright 2025-2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +extern "C" { + + bool SDL_ShowMessageBox(const SDL_MessageBoxData* /* messageboxdata */, int* buttonid) { + if (buttonid) *buttonid = 0; // "No",skip migration + return true; + } + + bool SDL_ShowSimpleMessageBox(SDL_MessageBoxFlags /* flags */, const char* /* title */, + const char* /* message */, SDL_Window* /* window */) { + return true; + } + +} // extern "C" diff --git a/tests/test_emulator_settings.cpp b/tests/test_emulator_settings.cpp new file mode 100644 index 000000000..1a4a667b3 --- /dev/null +++ b/tests/test_emulator_settings.cpp @@ -0,0 +1,837 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "common/path_util.h" +#include "common/scm_rev.h" +#include "core/emulator_settings.h" +#include "core/emulator_state.h" + +namespace fs = std::filesystem; +using json = nlohmann::json; + +class TempDir { +public: + TempDir() { + auto ns = std::chrono::steady_clock::now().time_since_epoch().count(); + temp_path = fs::temp_directory_path() / ("shadps4_test_" + std::to_string(ns) + "_" + + std::to_string(reinterpret_cast(this))); + fs::create_directories(temp_path); + } + ~TempDir() { + std::error_code ec; + fs::remove_all(temp_path, ec); + } + const fs::path& path() const { + return temp_path; + } + +private: + fs::path temp_path; +}; + +static void WriteJson(const fs::path& p, const json& j) { + std::ofstream out(p); + ASSERT_TRUE(out.is_open()) << "Cannot write: " << p; + out << std::setw(2) << j; +} + +static json ReadJson(const fs::path& p) { + std::ifstream in(p); + EXPECT_TRUE(in.is_open()) << "Cannot read: " << p; + json j; + in >> j; + return j; +} + +class EmulatorSettingsTest : public ::testing::Test { +protected: + void SetUp() override { + temp_dir = std::make_unique(); + const fs::path root = temp_dir->path(); + + using PT = Common::FS::PathType; + const struct { + PT type; + const char* sub; + } dirs[] = { + {PT::UserDir, ""}, + {PT::LogDir, "log"}, + {PT::ScreenshotsDir, "screenshots"}, + {PT::ShaderDir, "shader"}, + {PT::GameDataDir, "data"}, + {PT::TempDataDir, "temp"}, + {PT::SysModuleDir, "sys_modules"}, + {PT::DownloadDir, "download"}, + {PT::CapturesDir, "captures"}, + {PT::CheatsDir, "cheats"}, + {PT::PatchesDir, "patches"}, + {PT::MetaDataDir, "game_data"}, + {PT::CustomTrophy, "custom_trophy"}, + {PT::CustomConfigs, "custom_configs"}, + {PT::CacheDir, "cache"}, + {PT::FontsDir, "fonts"}, + {PT::HomeDir, "home"}, + }; + for (const auto& d : dirs) { + fs::path p = d.sub[0] ? (root / d.sub) : root; + fs::create_directories(p); + Common::FS::SetUserPath(d.type, p); + } + + temp_state = std::make_shared(); + EmulatorState::SetInstance(temp_state); + + temp_settings = std::make_shared(); + EmulatorSettingsImpl::SetInstance(temp_settings); + } + + void TearDown() override { + EmulatorSettingsImpl::SetInstance(nullptr); + EmulatorState::SetInstance(nullptr); + temp_settings.reset(); + temp_state.reset(); + temp_dir.reset(); + } + + fs::path ConfigJson() const { + return temp_dir->path() / "config.json"; + } + fs::path GameConfig(const std::string& serial) const { + return temp_dir->path() / "custom_configs" / (serial + ".json"); + } + + std::unique_ptr temp_dir; + std::shared_ptr temp_settings; + std::shared_ptr temp_state; +}; + +// tests Settting template , default , Global override modes + +TEST(SettingTest, DefaultCtorZeroInitialises) { + Setting s; + EXPECT_EQ(s.value, 0); + EXPECT_EQ(s.default_value, 0); + EXPECT_FALSE(s.game_specific_value.has_value()); +} + +TEST(SettingTest, ValueCtorSetsBothValueAndDefault) { + Setting s{42}; + EXPECT_EQ(s.value, 42); + EXPECT_EQ(s.default_value, 42); +} + +TEST(SettingTest, GetDefaultPrefersGameSpecificOverBase) { + Setting s{10}; + s.value = 20; + s.game_specific_value = 99; + EXPECT_EQ(s.get(ConfigMode::Default), 99); +} + +TEST(SettingTest, GetDefaultFallsBackToBaseWhenNoOverride) { + Setting s{10}; + s.value = 20; + EXPECT_EQ(s.get(ConfigMode::Default), 20); +} + +TEST(SettingTest, GetGlobalIgnoresGameSpecific) { + Setting s{10}; + s.value = 20; + s.game_specific_value = 99; + EXPECT_EQ(s.get(ConfigMode::Global), 20); +} + +TEST(SettingTest, GetCleanAlwaysReturnsFactoryDefault) { + Setting s{10}; + s.value = 20; + s.game_specific_value = 99; + EXPECT_EQ(s.get(ConfigMode::Clean), 10); +} + +TEST(SettingTest, SetWritesToBaseOnly) { + Setting s{0}; + s.game_specific_value = 55; + s.set(77); + EXPECT_EQ(s.value, 77); + EXPECT_EQ(s.game_specific_value.value(), 55); // override untouched +} + +TEST(SettingTest, ResetGameSpecificClearsOverride) { + Setting s{0}; + s.game_specific_value = 55; + s.reset_game_specific(); + EXPECT_FALSE(s.game_specific_value.has_value()); + // base and default must be intact + EXPECT_EQ(s.value, 0); + EXPECT_EQ(s.default_value, 0); +} + +TEST(SettingTest, BoolSettingAllModes) { + Setting s{false}; + s.value = true; + s.game_specific_value = false; + EXPECT_FALSE(s.get(ConfigMode::Default)); + EXPECT_TRUE(s.get(ConfigMode::Global)); + EXPECT_FALSE(s.get(ConfigMode::Clean)); +} + +TEST(SettingTest, StringSettingAllModes) { + Setting s{"shadow"}; + s.value = "rule"; + s.game_specific_value = "override"; + EXPECT_EQ(s.get(ConfigMode::Default), "override"); + EXPECT_EQ(s.get(ConfigMode::Global), "rule"); + EXPECT_EQ(s.get(ConfigMode::Clean), "shadow"); +} + +TEST(SettingTest, NoGameSpecificDefaultAndGlobalAgree) { + Setting s{7}; + s.value = 7; + EXPECT_EQ(s.get(ConfigMode::Default), s.get(ConfigMode::Global)); +} + +// tests for default settings + +TEST_F(EmulatorSettingsTest, SetDefaultValuesResetsAllGroupsToFactory) { + // set random values + temp_settings->SetNeo(true); + temp_settings->SetWindowWidth(3840u); + temp_settings->SetGpuId(2); + temp_settings->SetDebugDump(true); + temp_settings->SetCursorState(HideCursorState::Always); + + temp_settings->SetDefaultValues(); // reset to defaults + // check if values are reset to defaults + EXPECT_FALSE(temp_settings->IsNeo()); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280u); + EXPECT_EQ(temp_settings->GetGpuId(), -1); + EXPECT_FALSE(temp_settings->IsDebugDump()); + EXPECT_EQ(temp_settings->GetCursorState(), static_cast(HideCursorState::Idle)); +} + +TEST_F(EmulatorSettingsTest, SetDefaultValuesClearsGameSpecificOverrides) { + // check that game-specific overrides are cleared by SetDefaultValues + json game; + game["General"]["neo_mode"] = true; + WriteJson(GameConfig("CUSA00001"), game); + temp_settings->Load("CUSA00001"); + + temp_settings->SetDefaultValues(); + temp_settings->SetConfigMode(ConfigMode::Default); + + EXPECT_FALSE(temp_settings->IsNeo()); // default is false should be loaded instead of override +} + +// configModes tests + +TEST_F(EmulatorSettingsTest, ConfigModeSetAndGetRoundTrips) { + temp_settings->SetConfigMode(ConfigMode::Clean); + EXPECT_EQ(temp_settings->GetConfigMode(), ConfigMode::Clean); + temp_settings->SetConfigMode(ConfigMode::Global); + EXPECT_EQ(temp_settings->GetConfigMode(), ConfigMode::Global); + temp_settings->SetConfigMode(ConfigMode::Default); + EXPECT_EQ(temp_settings->GetConfigMode(), ConfigMode::Default); +} + +TEST_F(EmulatorSettingsTest, ConfigModeCleanReturnFactoryDefaults) { + temp_settings->SetWindowWidth(3840u); + json game; + game["GPU"]["window_width"] = 2560; + WriteJson(GameConfig("CUSA00001"), game); + temp_settings->Load("CUSA00001"); + + temp_settings->SetConfigMode(ConfigMode::Clean); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280); // factory default +} + +TEST_F(EmulatorSettingsTest, ConfigModeGlobalIgnoresGameSpecific) { + temp_settings->SetNeo(false); + json game; + game["General"]["neo_mode"] = true; + WriteJson(GameConfig("CUSA00001"), game); + temp_settings->Load("CUSA00001"); + + temp_settings->SetConfigMode(ConfigMode::Global); + EXPECT_FALSE(temp_settings->IsNeo()); +} + +TEST_F(EmulatorSettingsTest, ConfigModeDefaultResolvesGameSpecificWhenPresent) { + temp_settings->SetNeo(false); + json game; + game["General"]["neo_mode"] = true; + WriteJson(GameConfig("CUSA00001"), game); + temp_settings->Load("CUSA00001"); + + temp_settings->SetConfigMode(ConfigMode::Default); + EXPECT_TRUE(temp_settings->IsNeo()); +} + +// tests for global config.json file + +TEST_F(EmulatorSettingsTest, SaveCreatesConfigJson) { + ASSERT_TRUE(temp_settings->Save()); + EXPECT_TRUE(fs::exists(ConfigJson())); +} + +TEST_F(EmulatorSettingsTest, SaveWritesAllExpectedSections) { + ASSERT_TRUE(temp_settings->Save()); + json j = ReadJson(ConfigJson()); + for (const char* section : {"General", "Debug", "Input", "Audio", "GPU", "Vulkan"}) + EXPECT_TRUE(j.contains(section)) << "Missing section: " << section; +} + +TEST_F(EmulatorSettingsTest, LoadReturnsTrueForExistingFile) { + temp_settings->Save(); + auto fresh = std::make_shared(); + EmulatorSettingsImpl::SetInstance(fresh); + EXPECT_TRUE(fresh->Load()); +} + +TEST_F(EmulatorSettingsTest, RoundTripAllGroups) { + temp_settings->SetNeo(true); + temp_settings->SetDebugDump(true); + temp_settings->SetWindowWidth(1920u); + temp_settings->SetGpuId(1); + temp_settings->SetCursorState(HideCursorState::Always); + temp_settings->SetAudioBackend(AudioBackend::OpenAL); + temp_settings->Save(); + + auto f = std::make_shared(); + EmulatorSettingsImpl::SetInstance(f); + f->Load(); + EXPECT_TRUE(f->IsNeo()); + EXPECT_TRUE(f->IsDebugDump()); + EXPECT_EQ(f->GetWindowWidth(), 1920u); + EXPECT_EQ(f->GetGpuId(), 1); + EXPECT_EQ(f->GetCursorState(), static_cast(HideCursorState::Always)); + EXPECT_EQ(f->GetAudioBackend(), static_cast(AudioBackend::OpenAL)); +} + +TEST_F(EmulatorSettingsTest, LoadMissingFileCreatesDefaultsOnDisk) { + ASSERT_FALSE(fs::exists(ConfigJson())); + temp_settings->Load(); + EXPECT_TRUE(fs::exists(ConfigJson())); + EXPECT_FALSE(temp_settings->IsNeo()); // defaults +} + +TEST_F(EmulatorSettingsTest, LoadMissingSectionDoesNotZeroOtherSections) { + temp_settings->SetNeo(true); + temp_settings->Save(); + json j = ReadJson(ConfigJson()); + j.erase("GPU"); + WriteJson(ConfigJson(), j); + + auto f = std::make_shared(); + EmulatorSettingsImpl::SetInstance(f); + f->Load(); + + EXPECT_TRUE(f->IsNeo()); // belongs to General, should be loaded + EXPECT_EQ(f->GetWindowWidth(), 1280); // GPU fell back to default +} + +TEST_F(EmulatorSettingsTest, LoadPreservesUnknownKeysOnResave) { + temp_settings->Save(); + json j = ReadJson(ConfigJson()); + j["General"]["future_feature"] = "preserved"; + WriteJson(ConfigJson(), j); + + // A fresh load + save (triggered by version mismatch) must keep the key + auto f = std::make_shared(); + EmulatorSettingsImpl::SetInstance(f); + f->Load(); + f->Save(); + + json after = ReadJson(ConfigJson()); + EXPECT_EQ(after["General"]["future_feature"], "preserved"); +} + +TEST_F(EmulatorSettingsTest, LoadUnknownTopLevelSectionPreserved) { + temp_settings->Save(); + json j = ReadJson(ConfigJson()); + j["FutureSection"]["key"] = 42; + WriteJson(ConfigJson(), j); + + temp_settings->SetNeo(true); + temp_settings->Save(); // merge path + + json after = ReadJson(ConfigJson()); + EXPECT_TRUE(after.contains("FutureSection")); + EXPECT_EQ(after["FutureSection"]["key"], 42); +} + +TEST_F(EmulatorSettingsTest, LoadCorruptJsonDoesNotCrash) { + { + std::ofstream out(ConfigJson()); + out << "{NOT VALID JSON!!!"; + } + EXPECT_NO_THROW(temp_settings->Load()); +} + +TEST_F(EmulatorSettingsTest, LoadEmptyJsonObjectDoesNotCrash) { + WriteJson(ConfigJson(), json::object()); + EXPECT_NO_THROW(temp_settings->Load()); +} + +// tests for per game config + +TEST_F(EmulatorSettingsTest, SaveSerialCreatesPerGameFile) { + ASSERT_TRUE(temp_settings->Save("CUSA01234")); + EXPECT_TRUE(fs::exists(GameConfig("CUSA01234"))); +} + +TEST_F(EmulatorSettingsTest, LoadSerialReturnsFalseWhenFileAbsent) { + EXPECT_FALSE(temp_settings->Load("CUSA99999")); +} + +TEST_F(EmulatorSettingsTest, LoadSerialAppliesOverrideToGameSpecificValue) { + temp_settings->SetNeo(false); + json game; + game["General"]["neo_mode"] = true; + WriteJson(GameConfig("CUSA01234"), game); + + ASSERT_TRUE(temp_settings->Load("CUSA01234")); + temp_settings->SetConfigMode(ConfigMode::Default); + EXPECT_TRUE(temp_settings->IsNeo()); +} + +TEST_F(EmulatorSettingsTest, LoadSerialBaseValueUntouched) { + temp_settings->SetWindowWidth(1280); + json game; + game["GPU"]["window_width"] = 3840; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->SetConfigMode(ConfigMode::Global); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280); +} + +TEST_F(EmulatorSettingsTest, LoadSerialOverridesMultipleGroups) { + temp_settings->SetNeo(false); + temp_settings->SetWindowWidth(1280u); + temp_settings->SetDebugDump(false); + + json game; + game["General"]["neo_mode"] = true; + game["GPU"]["window_width"] = 3840; + game["Debug"]["debug_dump"] = true; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->SetConfigMode(ConfigMode::Default); + EXPECT_TRUE(temp_settings->IsNeo()); + EXPECT_EQ(temp_settings->GetWindowWidth(), 3840); + EXPECT_TRUE(temp_settings->IsDebugDump()); +} + +TEST_F(EmulatorSettingsTest, LoadSerialUnrecognisedKeyIgnored) { + json game; + game["GPU"]["key_that_does_not_exist"] = 999; + WriteJson(GameConfig("CUSA01234"), game); + EXPECT_NO_THROW(temp_settings->Load("CUSA01234")); +} + +TEST_F(EmulatorSettingsTest, LoadSerialTypeMismatch_DoesNotCrash) { + json game; + game["GPU"]["window_width"] = "not_a_number"; + WriteJson(GameConfig("CUSA01234"), game); + EXPECT_NO_THROW(temp_settings->Load("CUSA01234")); + // base unchanged + temp_settings->SetConfigMode(ConfigMode::Global); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280u); +} + +TEST_F(EmulatorSettingsTest, LoadSerialCorruptFileDoesNotCrash) { + { + std::ofstream out(GameConfig("CUSA01234")); + out << "{{{{totally broken"; + } + EXPECT_NO_THROW(temp_settings->Load("CUSA01234")); +} + +TEST_F(EmulatorSettingsTest, SaveSerialWritesGameSpecificValueWhenOverrideLoaded) { + temp_settings->SetWindowWidth(1280); + json game; + game["GPU"]["window_width"] = 3840; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->Save("CUSA01234"); + + json saved = ReadJson(GameConfig("CUSA01234")); + EXPECT_EQ(saved["GPU"]["window_width"].get(), 3840); +} + +TEST_F(EmulatorSettingsTest, SaveSerialWritesBaseValueWhenNoOverrideSet) { + temp_settings->SetWindowWidth(2560); + temp_settings->Save("CUSA01234"); + + json saved = ReadJson(GameConfig("CUSA01234")); + EXPECT_EQ(saved["GPU"]["window_width"].get(), 2560); +} + +TEST_F(EmulatorSettingsTest, MultipleSerialsDoNotInterfere) { + json g1; + g1["General"]["neo_mode"] = true; + g1["GPU"]["window_width"] = 3840; + WriteJson(GameConfig("CUSA00001"), g1); + + json g2; + g2["General"]["neo_mode"] = false; + g2["GPU"]["window_width"] = 1920; + WriteJson(GameConfig("CUSA00002"), g2); + + { + auto s = std::make_shared(); + EmulatorSettingsImpl::SetInstance(s); + s->Load(); + s->Load("CUSA00001"); + s->SetConfigMode(ConfigMode::Default); + EXPECT_TRUE(s->IsNeo()); + EXPECT_EQ(s->GetWindowWidth(), 3840); + } + { + auto s = std::make_shared(); + EmulatorSettingsImpl::SetInstance(s); + s->Load(); + s->Load("CUSA00002"); + s->SetConfigMode(ConfigMode::Default); + EXPECT_FALSE(s->IsNeo()); + EXPECT_EQ(s->GetWindowWidth(), 1920); + } +} + +// ClearGameSpecificOverrides tests + +TEST_F(EmulatorSettingsTest, ClearGameSpecificOverridesRemovesAllGroups) { + json game; + game["General"]["neo_mode"] = true; + game["GPU"]["window_width"] = 3840; + game["Debug"]["debug_dump"] = true; + game["Input"]["cursor_state"] = 2; + game["Audio"]["audio_backend"] = 1; + game["Vulkan"]["gpu_id"] = 2; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->ClearGameSpecificOverrides(); + temp_settings->SetConfigMode(ConfigMode::Default); + + EXPECT_FALSE(temp_settings->IsNeo()); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280); + EXPECT_FALSE(temp_settings->IsDebugDump()); + EXPECT_EQ(temp_settings->GetCursorState(), static_cast(HideCursorState::Idle)); + EXPECT_EQ(temp_settings->GetGpuId(), -1); +} + +TEST_F(EmulatorSettingsTest, ClearGameSpecificOverridesDoesNotTouchBaseValues) { + temp_settings->SetWindowWidth(1920); + json game; + game["GPU"]["window_width"] = 3840; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->ClearGameSpecificOverrides(); + + temp_settings->SetConfigMode(ConfigMode::Global); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1920); +} + +TEST_F(EmulatorSettingsTest, ClearGameSpecificOverrides_NoopWhenNothingLoaded) { + EXPECT_NO_THROW(temp_settings->ClearGameSpecificOverrides()); +} + +// ResetGameSpecificValue tests + +TEST_F(EmulatorSettingsTest, ResetGameSpecificValue_ClearsNamedKey) { + temp_settings->SetWindowWidth(1280); + json game; + game["GPU"]["window_width"] = 3840; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->SetConfigMode(ConfigMode::Default); + ASSERT_EQ(temp_settings->GetWindowWidth(), 3840); + + temp_settings->ResetGameSpecificValue("window_width"); + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280); +} + +TEST_F(EmulatorSettingsTest, ResetGameSpecificValueOnlyAffectsTargetKey) { + json game; + game["GPU"]["window_width"] = 3840; + game["General"]["neo_mode"] = true; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + + temp_settings->ResetGameSpecificValue("window_width"); + temp_settings->SetConfigMode(ConfigMode::Default); + + EXPECT_EQ(temp_settings->GetWindowWidth(), 1280); // cleared + EXPECT_TRUE(temp_settings->IsNeo()); // still set +} + +TEST_F(EmulatorSettingsTest, ResetGameSpecificValueUnknownKeyNoOp) { + EXPECT_NO_THROW(temp_settings->ResetGameSpecificValue("does_not_exist")); +} + +// GameInstallDir tests + +TEST_F(EmulatorSettingsTest, AddGameInstallDirAddsEnabled) { + fs::path dir = temp_dir->path() / "games"; + fs::create_directories(dir); + EXPECT_TRUE(temp_settings->AddGameInstallDir(dir)); + ASSERT_EQ(temp_settings->GetGameInstallDirs().size(), 1u); + EXPECT_EQ(temp_settings->GetGameInstallDirs()[0], dir); +} + +TEST_F(EmulatorSettingsTest, AddGameInstallDirRejectsDuplicate) { + fs::path dir = temp_dir->path() / "games"; + fs::create_directories(dir); + temp_settings->AddGameInstallDir(dir); + EXPECT_FALSE(temp_settings->AddGameInstallDir(dir)); + EXPECT_EQ(temp_settings->GetGameInstallDirs().size(), 1u); +} + +TEST_F(EmulatorSettingsTest, RemoveGameInstallDirRemovesEntry) { + fs::path dir = temp_dir->path() / "games"; + fs::create_directories(dir); + temp_settings->AddGameInstallDir(dir); + temp_settings->RemoveGameInstallDir(dir); + EXPECT_TRUE(temp_settings->GetGameInstallDirs().empty()); +} + +TEST_F(EmulatorSettingsTest, RemoveGameInstallDirNoopForMissing) { + EXPECT_NO_THROW(temp_settings->RemoveGameInstallDir("/nonexistent/path")); +} + +TEST_F(EmulatorSettingsTest, SetGameInstallDirEnabledDisablesDir) { + fs::path dir = temp_dir->path() / "games"; + fs::create_directories(dir); + temp_settings->AddGameInstallDir(dir, true); + temp_settings->SetGameInstallDirEnabled(dir, false); + EXPECT_TRUE(temp_settings->GetGameInstallDirs().empty()); +} + +TEST_F(EmulatorSettingsTest, SetGameInstallDirEnabledReEnablesDir) { + fs::path dir = temp_dir->path() / "games"; + fs::create_directories(dir); + temp_settings->AddGameInstallDir(dir, false); + ASSERT_TRUE(temp_settings->GetGameInstallDirs().empty()); + temp_settings->SetGameInstallDirEnabled(dir, true); + EXPECT_EQ(temp_settings->GetGameInstallDirs().size(), 1u); +} + +TEST_F(EmulatorSettingsTest, SetAllGameInstallDirsReplacesExistingList) { + fs::path d1 = temp_dir->path() / "g1"; + fs::path d2 = temp_dir->path() / "g2"; + fs::create_directories(d1); + fs::create_directories(d2); + temp_settings->AddGameInstallDir(d1); + + temp_settings->SetAllGameInstallDirs({{d2, true}}); + ASSERT_EQ(temp_settings->GetGameInstallDirs().size(), 1u); + EXPECT_EQ(temp_settings->GetGameInstallDirs()[0], d2); +} + +TEST_F(EmulatorSettingsTest, GameInstallDirsFullRoundTripWithEnabledFlags) { + fs::path d1 = temp_dir->path() / "g1"; + fs::path d2 = temp_dir->path() / "g2"; + fs::create_directories(d1); + fs::create_directories(d2); + temp_settings->AddGameInstallDir(d1, true); + temp_settings->AddGameInstallDir(d2, false); + temp_settings->Save(); + + auto f = std::make_shared(); + EmulatorSettingsImpl::SetInstance(f); + f->Load(); + + const auto& all = f->GetAllGameInstallDirs(); + ASSERT_EQ(all.size(), 2u); + EXPECT_EQ(all[0].path, d1); + EXPECT_TRUE(all[0].enabled); + EXPECT_EQ(all[1].path, d2); + EXPECT_FALSE(all[1].enabled); +} + +TEST_F(EmulatorSettingsTest, GetGameInstallDirsEnabledReflectsState) { + fs::path d1 = temp_dir->path() / "g1"; + fs::path d2 = temp_dir->path() / "g2"; + fs::create_directories(d1); + fs::create_directories(d2); + temp_settings->AddGameInstallDir(d1, true); + temp_settings->AddGameInstallDir(d2, false); + + auto enabled = temp_settings->GetGameInstallDirsEnabled(); + ASSERT_EQ(enabled.size(), 2u); + EXPECT_TRUE(enabled[0]); + EXPECT_FALSE(enabled[1]); +} + +// GetAllOverrideableKeys tests + +TEST_F(EmulatorSettingsTest, GetAllOverrideableKeysIsNonEmpty) { + EXPECT_FALSE(temp_settings->GetAllOverrideableKeys().empty()); +} + +TEST_F(EmulatorSettingsTest, GetAllOverrideableKeysContainsRepresentativeKeys) { + auto keys = temp_settings->GetAllOverrideableKeys(); + auto has = [&](const char* k) { return std::find(keys.begin(), keys.end(), k) != keys.end(); }; + // General + EXPECT_TRUE(has("neo_mode")); + EXPECT_TRUE(has("volume_slider")); + // GPU + EXPECT_TRUE(has("window_width")); + EXPECT_TRUE(has("null_gpu")); + EXPECT_TRUE(has("vblank_frequency")); + // Vulkan + EXPECT_TRUE(has("gpu_id")); + EXPECT_TRUE(has("pipeline_cache_enabled")); + // Debug + EXPECT_TRUE(has("debug_dump")); + EXPECT_TRUE(has("log_enabled")); + // Input + EXPECT_TRUE(has("cursor_state")); + // Audio + EXPECT_TRUE(has("audio_backend")); +} + +TEST_F(EmulatorSettingsTest, GetAllOverrideableKeysNoDuplicates) { + auto keys = temp_settings->GetAllOverrideableKeys(); + std::vector sorted = keys; + std::sort(sorted.begin(), sorted.end()); + auto it = std::unique(sorted.begin(), sorted.end()); + EXPECT_EQ(it, sorted.end()) << "Duplicate key found in overrideable keys list"; +} + +// Per-group GetOverrideableFields tests + +TEST_F(EmulatorSettingsTest, GetGeneralOverrideableFieldsNonEmpty) { + EXPECT_FALSE(temp_settings->GetGeneralOverrideableFields().empty()); +} + +TEST_F(EmulatorSettingsTest, GetGPUOverrideableFieldsContainsWindowAndFullscreen) { + auto fields = temp_settings->GetGPUOverrideableFields(); + auto has = [&](const char* k) { + return std::any_of(fields.begin(), fields.end(), + [k](const OverrideItem& f) { return std::string(f.key) == k; }); + }; + EXPECT_TRUE(has("window_width")); + EXPECT_TRUE(has("window_height")); + EXPECT_TRUE(has("full_screen")); + EXPECT_TRUE(has("vblank_frequency")); +} + +TEST_F(EmulatorSettingsTest, GetVulkanOverrideableFieldsContainsGpuId) { + auto fields = temp_settings->GetVulkanOverrideableFields(); + bool found = std::any_of(fields.begin(), fields.end(), + [](const OverrideItem& f) { return std::string(f.key) == "gpu_id"; }); + EXPECT_TRUE(found); +} + +// Path accessors tests +TEST_F(EmulatorSettingsTest, GetHomeDirReturnsCustomWhenSet) { + fs::path dir = temp_dir->path() / "custom_home"; + fs::create_directories(dir); + temp_settings->SetHomeDir(dir); + EXPECT_EQ(temp_settings->GetHomeDir(), dir); +} +TEST_F(EmulatorSettingsTest, GetSysModulesDirFallsBackToPathUtilWhenEmpty) { + // default_value is empty; GetSysModulesDir falls back to GetUserPath(SysModuleDir) + auto result = temp_settings->GetSysModulesDir(); + EXPECT_FALSE(result.empty()); +} +TEST_F(EmulatorSettingsTest, GetFontsDirFallsBackToPathUtilWhenEmpty) { + auto result = temp_settings->GetFontsDir(); + EXPECT_FALSE(result.empty()); +} + +// edge cases tests + +TEST_F(EmulatorSettingsTest, VersionMismatchPreservesSettings) { + temp_settings->SetNeo(true); + temp_settings->SetWindowWidth(2560u); + temp_settings->Save(); + + // Force a stale version string so the mismatch branch fires + json j = ReadJson(ConfigJson()); + j["Debug"]["config_version"] = "old_hash_0000"; + WriteJson(ConfigJson(), j); + + auto f = std::make_shared(); + EmulatorSettingsImpl::SetInstance(f); + f->Load(); // triggers version-bump Save() internally + + EXPECT_TRUE(f->IsNeo()); + EXPECT_EQ(f->GetWindowWidth(), 2560u); +} + +TEST_F(EmulatorSettingsTest, DoubleGlobalLoadIsIdempotent) { + temp_settings->SetNeo(true); + temp_settings->SetWindowWidth(2560u); + temp_settings->Save(); + + auto f = std::make_shared(); + EmulatorSettingsImpl::SetInstance(f); + f->Load(""); // first — loads from disk + f->Load(""); // second — must not reset anything + + EXPECT_TRUE(f->IsNeo()); + EXPECT_EQ(f->GetWindowWidth(), 2560u); +} + +TEST_F(EmulatorSettingsTest, ConfigUsedFlagTrueWhenFileExists) { + json game; + game["General"]["neo_mode"] = true; + WriteJson(GameConfig("CUSA01234"), game); + temp_settings->Load("CUSA01234"); + EXPECT_TRUE(EmulatorState::GetInstance()->IsGameSpecifigConfigUsed()); +} + +TEST_F(EmulatorSettingsTest, ConfigUsedFlagFalseWhenFileAbsent) { + temp_settings->Load("CUSA99999"); + EXPECT_FALSE(EmulatorState::GetInstance()->IsGameSpecifigConfigUsed()); +} + +TEST_F(EmulatorSettingsTest, DestructorNoSaveIfLoadNeverCalled) { + temp_settings->SetNeo(true); + temp_settings->Save(); + auto t0 = fs::last_write_time(ConfigJson()); + + { + // Create and immediately destroy without calling Load() + auto untouched = std::make_shared(); + // destructor fires here + } + + auto t1 = fs::last_write_time(ConfigJson()); + EXPECT_EQ(t0, t1) << "Destructor wrote config.json without a prior Load()"; +} + +TEST_F(EmulatorSettingsTest, DestructorSavesAfterSuccessfulLoad) { + temp_settings->SetNeo(true); + temp_settings->Save(); + + { + auto s = std::make_shared(); + EmulatorSettingsImpl::SetInstance(s); + s->Load(); + s->SetWindowWidth(2560u); // mutate after successful load + // destructor should write this change + } + + auto verify = std::make_shared(); + EmulatorSettingsImpl::SetInstance(verify); + verify->Load(); + EXPECT_EQ(verify->GetWindowWidth(), 2560); +} From e0a86dc8f926df913b8b478b4ba897ddb8d27c79 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Thu, 26 Mar 2026 22:33:31 +0200 Subject: [PATCH 05/14] added transfer of sysmodules path and fonts path (#4175) --- src/core/emulator_settings.cpp | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/core/emulator_settings.cpp b/src/core/emulator_settings.cpp index 79d9a86f8..066c23af8 100644 --- a/src/core/emulator_settings.cpp +++ b/src/core/emulator_settings.cpp @@ -633,6 +633,41 @@ bool EmulatorSettingsImpl::TransferSettings() { LOG_WARNING(Config, "Failed to transfer addon install directory: {}", e.what()); } } + if (og_data.contains("General")) { + const toml::value& general = og_data.at("General"); + auto& s = m_general; + // Transfer sysmodules install directory + try { + std::string sysmodules_install_dir_str; + if (general.contains("sysModulesPath")) { + const auto& sysmodule_value = general.at("sysModulesPath"); + if (sysmodule_value.is_string()) { + sysmodules_install_dir_str = toml::get(sysmodule_value); + if (!sysmodules_install_dir_str.empty()) { + s.sys_modules_dir.value = std::filesystem::path{sysmodules_install_dir_str}; + } + } + } + } catch (const std::exception& e) { + LOG_WARNING(Config, "Failed to transfer sysmodules install directory: {}", e.what()); + } + + // Transfer font install directory + try { + std::string font_install_dir_str; + if (general.contains("fontsPath")) { + const auto& font_value = general.at("fontsPath"); + if (font_value.is_string()) { + font_install_dir_str = toml::get(font_value); + if (!font_install_dir_str.empty()) { + s.font_dir.value = std::filesystem::path{font_install_dir_str}; + } + } + } + } catch (const std::exception& e) { + LOG_WARNING(Config, "Failed to transfer font install directory: {}", e.what()); + } + } return true; } From 8a1500d7ad31fa0b3da45b5883c3ac4dd456c287 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Fri, 27 Mar 2026 12:37:25 +0100 Subject: [PATCH 06/14] Local multiplayer support (#4169) * multiple controllers v2 * the c language formatter tool * review comments (the easy ones) * c++ copy semantics * correct error return for remotes without the system user id * update pad handle handling logic * controller override colour --- src/core/ipc/ipc.cpp | 7 - src/core/libraries/pad/pad.cpp | 258 ++++++++---- src/core/libraries/pad/pad.h | 4 +- src/core/libraries/system/userservice.cpp | 65 ++- src/emulator.cpp | 4 +- src/emulator.h | 4 +- src/imgui/renderer/imgui_impl_sdl3.cpp | 5 +- src/input/controller.cpp | 294 +++++++++---- src/input/controller.h | 125 ++++-- src/input/input_handler.cpp | 475 +++++++++++++++------- src/input/input_handler.h | 118 +++++- src/input/input_mouse.cpp | 14 +- src/sdl_window.cpp | 335 ++++----------- src/sdl_window.h | 25 +- 14 files changed, 1037 insertions(+), 696 deletions(-) diff --git a/src/core/ipc/ipc.cpp b/src/core/ipc/ipc.cpp index 489c34646..70180d3bf 100644 --- a/src/core/ipc/ipc.cpp +++ b/src/core/ipc/ipc.cpp @@ -211,13 +211,6 @@ void IPC::InputLoop() { } else if (cmd == "RELOAD_INPUTS") { std::string config = next_str(); Input::ParseInputConfig(config); - } else if (cmd == "SET_ACTIVE_CONTROLLER") { - std::string active_controller = next_str(); - GamepadSelect::SetSelectedGamepad(active_controller); - SDL_Event checkGamepad; - SDL_memset(&checkGamepad, 0, sizeof(checkGamepad)); - checkGamepad.type = SDL_EVENT_CHANGE_CONTROLLER; - SDL_PushEvent(&checkGamepad); } else { std::cerr << ";UNKNOWN CMD: " << cmd << std::endl; } diff --git a/src/core/libraries/pad/pad.cpp b/src/core/libraries/pad/pad.cpp index b31ed1f0b..f38621191 100644 --- a/src/core/libraries/pad/pad.cpp +++ b/src/core/libraries/pad/pad.cpp @@ -1,20 +1,24 @@ // SPDX-FileCopyrightText: Copyright 2024-2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include "common/config.h" #include "common/logging/log.h" #include "common/singleton.h" +#include "core/emulator_settings.h" #include "core/libraries/libs.h" #include "core/libraries/pad/pad_errors.h" +#include "core/user_settings.h" #include "input/controller.h" #include "pad.h" namespace Libraries::Pad { using Input::GameController; +using Input::GameControllers; +using namespace Libraries::UserService; static bool g_initialized = false; -static bool g_opened = false; +static std::unordered_map user_id_pad_handle_map{}; +static constexpr s32 tv_remote_handle = 5; int PS4_SYSV_ABI scePadClose(s32 handle) { LOG_ERROR(Lib_Pad, "(STUBBED) called"); @@ -30,8 +34,8 @@ int PS4_SYSV_ABI scePadDeviceClassGetExtendedInformation( s32 handle, OrbisPadDeviceClassExtendedInformation* pExtInfo) { LOG_ERROR(Lib_Pad, "(STUBBED) called"); std::memset(pExtInfo, 0, sizeof(OrbisPadDeviceClassExtendedInformation)); - if (Config::getUseSpecialPad()) { - pExtInfo->deviceClass = (OrbisPadDeviceClass)Config::getSpecialPadClass(); + if (EmulatorSettings.IsUsingSpecialPad()) { + pExtInfo->deviceClass = (OrbisPadDeviceClass)EmulatorSettings.GetSpecialPadClass(); } return ORBIS_OK; } @@ -107,9 +111,9 @@ int PS4_SYSV_ABI scePadGetControllerInformation(s32 handle, OrbisPadControllerIn return ORBIS_OK; } pInfo->connected = true; - if (Config::getUseSpecialPad()) { + if (EmulatorSettings.IsUsingSpecialPad()) { pInfo->connectionType = ORBIS_PAD_PORT_TYPE_SPECIAL; - pInfo->deviceClass = (OrbisPadDeviceClass)Config::getSpecialPadClass(); + pInfo->deviceClass = (OrbisPadDeviceClass)EmulatorSettings.GetSpecialPadClass(); } return ORBIS_OK; } @@ -156,11 +160,16 @@ int PS4_SYSV_ABI scePadGetHandle(Libraries::UserService::OrbisUserServiceUserId if (!g_initialized) { return ORBIS_PAD_ERROR_NOT_INITIALIZED; } - if (userId == -1 || !g_opened) { + if (userId == -1) { return ORBIS_PAD_ERROR_DEVICE_NO_HANDLE; } - LOG_DEBUG(Lib_Pad, "(DUMMY) called"); - return 1; + auto it = user_id_pad_handle_map.find(userId); + if (it == user_id_pad_handle_map.end()) { + return ORBIS_PAD_ERROR_DEVICE_NO_HANDLE; + } + s32 pad_handle = it->second; + LOG_DEBUG(Lib_Pad, "called, userid: {}, out pad handle: {}", userId, pad_handle); + return pad_handle; } int PS4_SYSV_ABI scePadGetIdleCount() { @@ -168,8 +177,19 @@ int PS4_SYSV_ABI scePadGetIdleCount() { return ORBIS_OK; } -int PS4_SYSV_ABI scePadGetInfo() { - LOG_ERROR(Lib_Pad, "(STUBBED) called"); +int PS4_SYSV_ABI scePadGetInfo(u32* data) { + LOG_WARNING(Lib_Pad, "(DUMMY) called"); + if (!data) { + return ORBIS_PAD_ERROR_INVALID_ARG; + } + data[0] = 0x1; // index but starting from one? + data[1] = 0x0; // index? + data[2] = 1; // pad handle + data[3] = 0x0101; // ??? + data[4] = 0x0; // ? + data[5] = 0x0; // ? + data[6] = 0x00ff0000; // colour(?) + data[7] = 0x0; // ? return ORBIS_OK; } @@ -254,34 +274,61 @@ int PS4_SYSV_ABI scePadOpen(Libraries::UserService::OrbisUserServiceUserId userI if (!g_initialized) { return ORBIS_PAD_ERROR_NOT_INITIALIZED; } - if (userId == -1) { - return ORBIS_PAD_ERROR_DEVICE_NO_HANDLE; + if (userId < 0) { + return ORBIS_DEVICE_SERVICE_ERROR_INVALID_USER; } - if (Config::getUseSpecialPad()) { + if (userId == ORBIS_USER_SERVICE_USER_ID_SYSTEM) { + if (type == ORBIS_PAD_PORT_TYPE_REMOTE_CONTROL) { + LOG_INFO(Lib_Pad, "Opened a TV remote device"); + user_id_pad_handle_map[ORBIS_USER_SERVICE_USER_ID_SYSTEM] = tv_remote_handle; + return tv_remote_handle; + } + return ORBIS_DEVICE_SERVICE_ERROR_INVALID_USER; + } + if (type == ORBIS_PAD_PORT_TYPE_REMOTE_CONTROL) { + return ORBIS_PAD_ERROR_INVALID_ARG; + } + if (EmulatorSettings.IsUsingSpecialPad()) { if (type != ORBIS_PAD_PORT_TYPE_SPECIAL) return ORBIS_PAD_ERROR_DEVICE_NOT_CONNECTED; } else { - if (type != ORBIS_PAD_PORT_TYPE_STANDARD && type != ORBIS_PAD_PORT_TYPE_REMOTE_CONTROL) + if (type != ORBIS_PAD_PORT_TYPE_STANDARD) return ORBIS_PAD_ERROR_DEVICE_NOT_CONNECTED; } - LOG_INFO(Lib_Pad, "(DUMMY) called user_id = {} type = {} index = {}", userId, type, index); - g_opened = true; - scePadResetLightBar(userId); - scePadResetOrientation(userId); - return 1; // dummy + auto u = UserManagement.GetUserByID(userId); + if (!u) { + return ORBIS_DEVICE_SERVICE_ERROR_USER_NOT_LOGIN; + } + s32 pad_handle = u->player_index; + LOG_INFO(Lib_Pad, "called user_id = {} type = {} index = {}, pad_handle = {}", userId, type, + index, pad_handle); + scePadResetLightBar(pad_handle); + scePadResetOrientation(pad_handle); + user_id_pad_handle_map[userId] = pad_handle; + return pad_handle; } int PS4_SYSV_ABI scePadOpenExt(Libraries::UserService::OrbisUserServiceUserId userId, s32 type, s32 index, const OrbisPadOpenExtParam* pParam) { LOG_ERROR(Lib_Pad, "(STUBBED) called"); - if (Config::getUseSpecialPad()) { + if (EmulatorSettings.IsUsingSpecialPad()) { if (type != ORBIS_PAD_PORT_TYPE_SPECIAL) return ORBIS_PAD_ERROR_DEVICE_NOT_CONNECTED; } else { if (type != ORBIS_PAD_PORT_TYPE_STANDARD && type != ORBIS_PAD_PORT_TYPE_REMOTE_CONTROL) return ORBIS_PAD_ERROR_DEVICE_NOT_CONNECTED; } - return 1; // dummy + auto u = UserManagement.GetUserByID(userId); + if (!u) { + return ORBIS_DEVICE_SERVICE_ERROR_USER_NOT_LOGIN; + } + s32 pad_handle = u->player_index; + LOG_INFO(Lib_Pad, "called user_id = {} type = {} index = {}, pad_handle = {}", userId, type, + index, pad_handle); + scePadResetLightBar(pad_handle); + scePadResetOrientation(pad_handle); + user_id_pad_handle_map[userId] = pad_handle; + return pad_handle; } int PS4_SYSV_ABI scePadOpenExt2() { @@ -294,8 +341,8 @@ int PS4_SYSV_ABI scePadOutputReport() { return ORBIS_OK; } -int ProcessStates(s32 handle, OrbisPadData* pData, Input::State* states, s32 num, bool connected, - u32 connected_count) { +int ProcessStates(s32 handle, OrbisPadData* pData, Input::GameController& controller, + Input::State* states, s32 num, bool connected, u32 connected_count) { if (!connected) { pData[0] = {}; pData[0].orientation = {0.0f, 0.0f, 0.0f, 1.0f}; @@ -319,61 +366,57 @@ int ProcessStates(s32 handle, OrbisPadData* pData, Input::State* states, s32 num pData[i].angularVelocity.z = states[i].angularVelocity.z; pData[i].orientation = {0.0f, 0.0f, 0.0f, 1.0f}; - auto* controller = Common::Singleton::Instance(); - const auto* engine = controller->GetEngine(); - if (engine && handle == 1) { - const auto gyro_poll_rate = engine->GetAccelPollRate(); - if (gyro_poll_rate != 0.0f) { - auto now = std::chrono::steady_clock::now(); - float deltaTime = std::chrono::duration_cast( - now - controller->GetLastUpdate()) - .count() / - 1000000.0f; - controller->SetLastUpdate(now); - Libraries::Pad::OrbisFQuaternion lastOrientation = controller->GetLastOrientation(); - Libraries::Pad::OrbisFQuaternion outputOrientation = {0.0f, 0.0f, 0.0f, 1.0f}; - GameController::CalculateOrientation(pData->acceleration, pData->angularVelocity, - deltaTime, lastOrientation, outputOrientation); - pData[i].orientation = outputOrientation; - controller->SetLastOrientation(outputOrientation); - } + const auto gyro_poll_rate = controller.accel_poll_rate; + if (gyro_poll_rate != 0.0f) { + auto now = std::chrono::steady_clock::now(); + float deltaTime = std::chrono::duration_cast( + now - controller.GetLastUpdate()) + .count() / + 1000000.0f; + controller.SetLastUpdate(now); + Libraries::Pad::OrbisFQuaternion lastOrientation = controller.GetLastOrientation(); + Libraries::Pad::OrbisFQuaternion outputOrientation = {0.0f, 0.0f, 0.0f, 1.0f}; + GameControllers::CalculateOrientation(pData->acceleration, pData->angularVelocity, + deltaTime, lastOrientation, outputOrientation); + pData[i].orientation = outputOrientation; + controller.SetLastOrientation(outputOrientation); } pData[i].touchData.touchNum = (states[i].touchpad[0].state ? 1 : 0) + (states[i].touchpad[1].state ? 1 : 0); if (handle == 1) { - if (controller->GetTouchCount() >= 127) { - controller->SetTouchCount(0); + if (controller.GetTouchCount() >= 127) { + controller.SetTouchCount(0); } - if (controller->GetSecondaryTouchCount() >= 127) { - controller->SetSecondaryTouchCount(0); + if (controller.GetSecondaryTouchCount() >= 127) { + controller.SetSecondaryTouchCount(0); } - if (pData->touchData.touchNum == 1 && controller->GetPreviousTouchNum() == 0) { - controller->SetTouchCount(controller->GetTouchCount() + 1); - controller->SetSecondaryTouchCount(controller->GetTouchCount()); - } else if (pData->touchData.touchNum == 2 && controller->GetPreviousTouchNum() == 1) { - controller->SetSecondaryTouchCount(controller->GetSecondaryTouchCount() + 1); - } else if (pData->touchData.touchNum == 0 && controller->GetPreviousTouchNum() > 0) { - if (controller->GetTouchCount() < controller->GetSecondaryTouchCount()) { - controller->SetTouchCount(controller->GetSecondaryTouchCount()); + if (pData->touchData.touchNum == 1 && controller.GetPreviousTouchNum() == 0) { + controller.SetTouchCount(controller.GetTouchCount() + 1); + controller.SetSecondaryTouchCount(controller.GetTouchCount()); + } else if (pData->touchData.touchNum == 2 && controller.GetPreviousTouchNum() == 1) { + controller.SetSecondaryTouchCount(controller.GetSecondaryTouchCount() + 1); + } else if (pData->touchData.touchNum == 0 && controller.GetPreviousTouchNum() > 0) { + if (controller.GetTouchCount() < controller.GetSecondaryTouchCount()) { + controller.SetTouchCount(controller.GetSecondaryTouchCount()); } else { - if (controller->WasSecondaryTouchReset()) { - controller->SetTouchCount(controller->GetSecondaryTouchCount()); - controller->UnsetSecondaryTouchResetBool(); + if (controller.WasSecondaryTouchReset()) { + controller.SetTouchCount(controller.GetSecondaryTouchCount()); + controller.UnsetSecondaryTouchResetBool(); } } } - controller->SetPreviousTouchNum(pData->touchData.touchNum); + controller.SetPreviousTouchNum(pData->touchData.touchNum); if (pData->touchData.touchNum == 1) { - states[i].touchpad[0].ID = controller->GetTouchCount(); + states[i].touchpad[0].ID = controller.GetTouchCount(); states[i].touchpad[1].ID = 0; } else if (pData->touchData.touchNum == 2) { - states[i].touchpad[0].ID = controller->GetTouchCount(); - states[i].touchpad[1].ID = controller->GetSecondaryTouchCount(); + states[i].touchpad[0].ID = controller.GetTouchCount(); + states[i].touchpad[1].ID = controller.GetSecondaryTouchCount(); } } else { states[i].touchpad[0].ID = 1; @@ -397,16 +440,18 @@ int ProcessStates(s32 handle, OrbisPadData* pData, Input::State* states, s32 num int PS4_SYSV_ABI scePadRead(s32 handle, OrbisPadData* pData, s32 num) { LOG_TRACE(Lib_Pad, "called"); - if (handle < 1) { - return ORBIS_PAD_ERROR_INVALID_HANDLE; - } int connected_count = 0; bool connected = false; std::vector states(64); - auto* controller = Common::Singleton::Instance(); - const auto* engine = controller->GetEngine(); - int ret_num = controller->ReadStates(states.data(), num, &connected, &connected_count); - return ProcessStates(handle, pData, states.data(), ret_num, connected, connected_count); + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { + return ORBIS_PAD_ERROR_INVALID_HANDLE; + } + auto& controllers = *Common::Singleton::Instance(); + auto& controller = *controllers[*controller_id]; + int ret_num = controller.ReadStates(states.data(), num, &connected, &connected_count); + return ProcessStates(handle, pData, controller, states.data(), ret_num, connected, + connected_count); } int PS4_SYSV_ABI scePadReadBlasterForTracker() { @@ -430,17 +475,18 @@ int PS4_SYSV_ABI scePadReadHistory() { } int PS4_SYSV_ABI scePadReadState(s32 handle, OrbisPadData* pData) { - LOG_TRACE(Lib_Pad, "called"); - if (handle < 1) { + LOG_TRACE(Lib_Pad, "handle: {}", handle); + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { return ORBIS_PAD_ERROR_INVALID_HANDLE; } - auto* controller = Common::Singleton::Instance(); - const auto* engine = controller->GetEngine(); + auto& controllers = *Common::Singleton::Instance(); + auto& controller = *controllers[*controller_id]; int connected_count = 0; bool connected = false; Input::State state; - controller->ReadState(&state, &connected, &connected_count); - ProcessStates(handle, pData, &state, 1, connected, connected_count); + controller.ReadState(&state, &connected, &connected_count); + ProcessStates(handle, pData, controller, &state, 1, connected, connected_count); return ORBIS_OK; } @@ -450,13 +496,30 @@ int PS4_SYSV_ABI scePadReadStateExt() { } int PS4_SYSV_ABI scePadResetLightBar(s32 handle) { - LOG_INFO(Lib_Pad, "(DUMMY) called"); - if (handle != 1) { + LOG_DEBUG(Lib_Pad, "called, handle: {}", handle); + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { return ORBIS_PAD_ERROR_INVALID_HANDLE; } - auto* controller = Common::Singleton::Instance(); - int* rgb = Config::GetControllerCustomColor(); - controller->SetLightBarRGB(rgb[0], rgb[1], rgb[2]); + auto& controllers = *Common::Singleton::Instance(); + s32 colour_index = UserManagement.GetUserByPlayerIndex(handle)->user_color - 1; + Input::Colour colour{255, 0, 0}; + if (colour_index >= 0 && colour_index <= 3) { + static constexpr Input::Colour colours[4]{ + {0, 0, 255}, // blue + {255, 0, 0}, // red + {0, 255, 0}, // green + {255, 0, 255}, // pink + }; + colour = colours[colour_index]; + } else { + LOG_ERROR(Lib_Pad, "Invalid user colour value {} for controller {}, falling back to blue", + colour_index, handle); + } + if (auto oc = GameControllers::GetControllerCustomColor(*controller_id)) { + colour = *oc; + } + controllers[*controller_id]->SetLightBarRGB(colour.r, colour.g, colour.b); return ORBIS_OK; } @@ -473,14 +536,15 @@ int PS4_SYSV_ABI scePadResetLightBarAllByPortType() { int PS4_SYSV_ABI scePadResetOrientation(s32 handle) { LOG_INFO(Lib_Pad, "scePadResetOrientation called handle = {}", handle); - if (handle != 1) { + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { return ORBIS_PAD_ERROR_INVALID_HANDLE; } - auto* controller = Common::Singleton::Instance(); + auto& controllers = *Common::Singleton::Instance(); Libraries::Pad::OrbisFQuaternion defaultOrientation = {0.0f, 0.0f, 0.0f, 1.0f}; - controller->SetLastOrientation(defaultOrientation); - controller->SetLastUpdate(std::chrono::steady_clock::now()); + controllers[*controller_id]->SetLastOrientation(defaultOrientation); + controllers[*controller_id]->SetLastUpdate(std::chrono::steady_clock::now()); return ORBIS_OK; } @@ -526,7 +590,11 @@ int PS4_SYSV_ABI scePadSetForceIntercepted() { } int PS4_SYSV_ABI scePadSetLightBar(s32 handle, const OrbisPadLightBarParam* pParam) { - if (Config::GetOverrideControllerColor()) { + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { + return ORBIS_PAD_ERROR_INVALID_HANDLE; + } + if (GameControllers::GetControllerCustomColor(*controller_id)) { return ORBIS_OK; } if (pParam != nullptr) { @@ -538,8 +606,8 @@ int PS4_SYSV_ABI scePadSetLightBar(s32 handle, const OrbisPadLightBarParam* pPar return ORBIS_PAD_ERROR_INVALID_LIGHTBAR_SETTING; } - auto* controller = Common::Singleton::Instance(); - controller->SetLightBarRGB(pParam->r, pParam->g, pParam->b); + auto& controllers = *Common::Singleton::Instance(); + controllers[*controller_id]->SetLightBarRGB(pParam->r, pParam->g, pParam->b); return ORBIS_OK; } return ORBIS_PAD_ERROR_INVALID_ARG; @@ -555,8 +623,14 @@ int PS4_SYSV_ABI scePadSetLightBarBlinking() { return ORBIS_OK; } -int PS4_SYSV_ABI scePadSetLightBarForTracker() { - LOG_ERROR(Lib_Pad, "(STUBBED) called"); +int PS4_SYSV_ABI scePadSetLightBarForTracker(s32 handle, const OrbisPadLightBarParam* pParam) { + LOG_INFO(Lib_Pad, "called, r: {} g: {} b: {}", pParam->r, pParam->g, pParam->b); + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { + return ORBIS_PAD_ERROR_INVALID_HANDLE; + } + auto& controllers = *Common::Singleton::Instance(); + controllers[*controller_id]->SetLightBarRGB(pParam->r, pParam->g, pParam->b); return ORBIS_OK; } @@ -603,11 +677,15 @@ int PS4_SYSV_ABI scePadSetUserColor() { } int PS4_SYSV_ABI scePadSetVibration(s32 handle, const OrbisPadVibrationParam* pParam) { + auto controller_id = GameControllers::GetControllerIndexFromControllerID(handle); + if (!controller_id) { + return ORBIS_PAD_ERROR_INVALID_HANDLE; + } if (pParam != nullptr) { LOG_DEBUG(Lib_Pad, "scePadSetVibration called handle = {} data = {} , {}", handle, pParam->smallMotor, pParam->largeMotor); - auto* controller = Common::Singleton::Instance(); - controller->SetVibration(pParam->smallMotor, pParam->largeMotor); + auto& controllers = *Common::Singleton::Instance(); + controllers[*controller_id]->SetVibration(pParam->smallMotor, pParam->largeMotor); return ORBIS_OK; } return ORBIS_PAD_ERROR_INVALID_ARG; diff --git a/src/core/libraries/pad/pad.h b/src/core/libraries/pad/pad.h index 02ceaf3d9..2f4cbcc6a 100644 --- a/src/core/libraries/pad/pad.h +++ b/src/core/libraries/pad/pad.h @@ -280,7 +280,7 @@ int PS4_SYSV_ABI scePadGetFeatureReport(); int PS4_SYSV_ABI scePadGetHandle(Libraries::UserService::OrbisUserServiceUserId userId, s32 type, s32 index); int PS4_SYSV_ABI scePadGetIdleCount(); -int PS4_SYSV_ABI scePadGetInfo(); +int PS4_SYSV_ABI scePadGetInfo(u32* data); int PS4_SYSV_ABI scePadGetInfoByPortType(); int PS4_SYSV_ABI scePadGetLicenseControllerInformation(); int PS4_SYSV_ABI scePadGetMotionSensorPosition(); @@ -324,7 +324,7 @@ int PS4_SYSV_ABI scePadSetForceIntercepted(); int PS4_SYSV_ABI scePadSetLightBar(s32 handle, const OrbisPadLightBarParam* pParam); int PS4_SYSV_ABI scePadSetLightBarBaseBrightness(); int PS4_SYSV_ABI scePadSetLightBarBlinking(); -int PS4_SYSV_ABI scePadSetLightBarForTracker(); +int PS4_SYSV_ABI scePadSetLightBarForTracker(s32 handle, const OrbisPadLightBarParam* pParam); int PS4_SYSV_ABI scePadSetLoginUserNumber(); int PS4_SYSV_ABI scePadSetMotionSensorState(s32 handle, bool bEnable); int PS4_SYSV_ABI scePadSetProcessFocus(); diff --git a/src/core/libraries/system/userservice.cpp b/src/core/libraries/system/userservice.cpp index b82549c27..029868eb4 100644 --- a/src/core/libraries/system/userservice.cpp +++ b/src/core/libraries/system/userservice.cpp @@ -1,13 +1,19 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include "common/config.h" +#include + #include "common/logging/log.h" +#include #include +#include "common/singleton.h" +#include "core/emulator_settings.h" #include "core/libraries/libs.h" #include "core/libraries/system/userservice.h" #include "core/libraries/system/userservice_error.h" +#include "core/tls.h" +#include "input/controller.h" namespace Libraries::UserService { @@ -114,14 +120,15 @@ void AddUserServiceEvent(const OrbisUserServiceEvent e) { } s32 PS4_SYSV_ABI sceUserServiceGetEvent(OrbisUserServiceEvent* event) { - LOG_TRACE(Lib_UserService, "(DUMMY) called"); - // fake a loggin event - static bool logged_in = false; + LOG_TRACE(Lib_UserService, "called"); - if (!logged_in) { - logged_in = true; - event->event = OrbisUserServiceEventType::Login; - event->userId = 1; + if (!user_service_event_queue.empty()) { + OrbisUserServiceEvent& temp = user_service_event_queue.front(); + event->event = temp.event; + event->userId = temp.userId; + user_service_event_queue.pop(); + LOG_INFO(Lib_UserService, "Event processed by the game: {} {}", (u8)temp.event, + temp.userId); return ORBIS_OK; } @@ -504,8 +511,7 @@ s32 PS4_SYSV_ABI sceUserServiceGetInitialUser(int* user_id) { LOG_ERROR(Lib_UserService, "user_id is null"); return ORBIS_USER_SERVICE_ERROR_INVALID_ARGUMENT; } - // select first user (TODO add more) - *user_id = 1; + *user_id = UserManagement.GetDefaultUser().user_id; return ORBIS_OK; } @@ -575,20 +581,29 @@ int PS4_SYSV_ABI sceUserServiceGetLoginFlag() { } s32 PS4_SYSV_ABI sceUserServiceGetLoginUserIdList(OrbisUserServiceLoginUserIdList* userIdList) { - LOG_DEBUG(Lib_UserService, "called"); if (userIdList == nullptr) { - LOG_ERROR(Lib_UserService, "user_id is null"); + LOG_ERROR(Lib_UserService, "userIdList is null"); return ORBIS_USER_SERVICE_ERROR_INVALID_ARGUMENT; } - // TODO only first user, do the others as well - userIdList->user_id[0] = 1; - userIdList->user_id[1] = ORBIS_USER_SERVICE_USER_ID_INVALID; - userIdList->user_id[2] = ORBIS_USER_SERVICE_USER_ID_INVALID; - userIdList->user_id[3] = ORBIS_USER_SERVICE_USER_ID_INVALID; + // Initialize all slots to invalid (-1) + for (int i = 0; i < ORBIS_USER_SERVICE_MAX_LOGIN_USERS; i++) { + userIdList->user_id[i] = ORBIS_USER_SERVICE_USER_ID_INVALID; + } + + auto& user_manager = UserManagement; + + auto logged_in_users = user_manager.GetLoggedInUsers(); + + for (int i = 0; i < ORBIS_USER_SERVICE_MAX_LOGIN_USERS; i++) { + s32 id = + logged_in_users[i] ? logged_in_users[i]->user_id : ORBIS_USER_SERVICE_USER_ID_INVALID; + userIdList->user_id[i] = id; + LOG_DEBUG(Lib_UserService, "Slot {}: User ID {} (port {})", i, id, + logged_in_users[i] ? logged_in_users[i]->player_index : -1); + } return ORBIS_OK; } - int PS4_SYSV_ABI sceUserServiceGetMicLevel() { LOG_ERROR(Lib_UserService, "(STUBBED) called"); return ORBIS_OK; @@ -1056,7 +1071,7 @@ s32 PS4_SYSV_ABI sceUserServiceGetUserColor(int user_id, OrbisUserServiceUserCol LOG_ERROR(Lib_UserService, "color is null"); return ORBIS_USER_SERVICE_ERROR_INVALID_ARGUMENT; } - *color = OrbisUserServiceUserColor::Blue; + *color = (OrbisUserServiceUserColor)UserManagement.GetUserByID(user_id)->user_color; return ORBIS_OK; } @@ -1076,12 +1091,18 @@ int PS4_SYSV_ABI sceUserServiceGetUserGroupNum() { } s32 PS4_SYSV_ABI sceUserServiceGetUserName(int user_id, char* user_name, std::size_t size) { - LOG_DEBUG(Lib_UserService, "called user_id = {} ,size = {} ", user_id, size); + LOG_DEBUG(Lib_UserService, "called user_id = {}, size = {} ", user_id, size); if (user_name == nullptr) { LOG_ERROR(Lib_UserService, "user_name is null"); return ORBIS_USER_SERVICE_ERROR_INVALID_ARGUMENT; } - std::string name = Config::getUserName(); + std::string name = "shadPS4"; + auto const* u = UserManagement.GetUserByID(user_id); + if (u != nullptr) { + name = u->user_name; + } else { + LOG_ERROR(Lib_UserService, "No user found"); + } if (size < name.length()) { LOG_ERROR(Lib_UserService, "buffer is too short"); return ORBIS_USER_SERVICE_ERROR_BUFFER_TOO_SHORT; diff --git a/src/emulator.cpp b/src/emulator.cpp index 5206a309d..447c72391 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -289,7 +289,7 @@ void Emulator::Run(std::filesystem::path file, std::vector args, // Initialize components memory = Core::Memory::Instance(); - controller = Common::Singleton::Instance(); + controllers = Common::Singleton::Instance(); linker = Common::Singleton::Instance(); // Load renderdoc module @@ -331,7 +331,7 @@ void Emulator::Run(std::filesystem::path file, std::vector args, } } window = std::make_unique(EmulatorSettings.GetWindowWidth(), - EmulatorSettings.GetWindowHeight(), controller, + EmulatorSettings.GetWindowHeight(), controllers, window_title); g_window = window.get(); diff --git a/src/emulator.h b/src/emulator.h index f4dd32c20..d350ce16c 100644 --- a/src/emulator.h +++ b/src/emulator.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #pragma once @@ -43,7 +43,7 @@ private: void LoadSystemModules(const std::string& game_serial); Core::MemoryManager* memory; - Input::GameController* controller; + Input::GameControllers* controllers; Core::Linker* linker; std::unique_ptr window; std::chrono::steady_clock::time_point start_time; diff --git a/src/imgui/renderer/imgui_impl_sdl3.cpp b/src/imgui/renderer/imgui_impl_sdl3.cpp index 388c23b03..679aeb8c0 100644 --- a/src/imgui/renderer/imgui_impl_sdl3.cpp +++ b/src/imgui/renderer/imgui_impl_sdl3.cpp @@ -737,9 +737,8 @@ static void UpdateGamepads() { ImGuiIO& io = ImGui::GetIO(); SdlData* bd = GetBackendData(); - auto controller = Common::Singleton::Instance(); - auto engine = controller->GetEngine(); - SDL_Gamepad* SDLGamepad = engine->m_gamepad; + auto& controllers = *Common::Singleton::Instance(); + SDL_Gamepad* SDLGamepad = controllers[0]->m_sdl_gamepad; // Update list of gamepads to use if (bd->want_update_gamepads_list && bd->gamepad_mode != ImGui_ImplSDL3_GamepadMode_Manual) { if (SDLGamepad) { diff --git a/src/input/controller.cpp b/src/input/controller.cpp index 3606ad5d2..c1ba584e3 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -1,15 +1,20 @@ // SPDX-FileCopyrightText: Copyright 2024-2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include +#include #include -#include "common/config.h" +#include +#include #include "common/logging/log.h" +#include "controller.h" +#include "core/emulator_settings.h" #include "core/libraries/kernel/time.h" #include "core/libraries/pad/pad.h" +#include "core/libraries/system/userservice.h" +#include "core/user_settings.h" #include "input/controller.h" -static std::string SelectedGamepad = ""; - namespace Input { using Libraries::Pad::OrbisPadButtonDataOffset; @@ -22,7 +27,15 @@ void State::OnButton(OrbisPadButtonDataOffset button, bool isPressed) { } } -void State::OnAxis(Axis axis, int value) { +void State::OnAxis(Axis axis, int value, bool smooth) { + auto const i = std::to_underlying(axis); + // forcibly finish the previous smoothing task by jumping to the end + axes[i] = axis_smoothing_end_values[i]; + + axis_smoothing_start_times[i] = time; + axis_smoothing_start_values[i] = axes[i]; + axis_smoothing_end_values[i] = value; + axis_smoothing_flags[i] = smooth; const auto toggle = [&](const auto button) { if (value > 0) { buttonsState |= button; @@ -40,7 +53,6 @@ void State::OnAxis(Axis axis, int value) { default: break; } - axes[static_cast(axis)] = value; } void State::OnTouchpad(int touchIndex, bool isDown, float x, float y) { @@ -61,6 +73,22 @@ void State::OnAccel(const float accel[3]) { acceleration.z = accel[2]; } +void State::UpdateAxisSmoothing() { + for (int i = 0; i < std::to_underlying(Axis::AxisMax); i++) { + // if it's not to be smoothed or close enough, just jump to the end + if (!axis_smoothing_flags[i] || std::abs(axes[i] - axis_smoothing_end_values[i]) < 16) { + if (axes[i] != axis_smoothing_end_values[i]) { + axes[i] = axis_smoothing_end_values[i]; + } + continue; + } + auto now = Libraries::Kernel::sceKernelGetProcessTime(); + f32 t = + std::clamp((now - axis_smoothing_start_times[i]) / f32{axis_smoothing_time}, 0.f, 1.f); + axes[i] = s32(axis_smoothing_start_values[i] * (1 - t) + axis_smoothing_end_values[i] * t); + } +} + GameController::GameController() : m_states_queue(64) {} void GameController::ReadState(State* state, bool* isConnected, int* connectedCount) { @@ -88,31 +116,82 @@ int GameController::ReadStates(State* states, int states_num, bool* isConnected, return ret_num; } -void GameController::Button(int id, OrbisPadButtonDataOffset button, bool is_pressed) { +void GameController::Button(OrbisPadButtonDataOffset button, bool is_pressed) { m_state.OnButton(button, is_pressed); PushState(); } -void GameController::Axis(int id, Input::Axis axis, int value) { - m_state.OnAxis(axis, value); +void GameController::Axis(Input::Axis axis, int value, bool smooth) { + m_state.OnAxis(axis, value, smooth); PushState(); } -void GameController::Gyro(int id, const float gyro[3]) { - m_state.OnGyro(gyro); +void GameController::Gyro(int id) { + m_state.OnGyro(gyro_buf); PushState(); } -void GameController::Acceleration(int id, const float acceleration[3]) { - m_state.OnAccel(acceleration); +void GameController::Acceleration(int id) { + m_state.OnAccel(accel_buf); PushState(); } -void GameController::CalculateOrientation(Libraries::Pad::OrbisFVector3& acceleration, - Libraries::Pad::OrbisFVector3& angularVelocity, - float deltaTime, - Libraries::Pad::OrbisFQuaternion& lastOrientation, - Libraries::Pad::OrbisFQuaternion& orientation) { +void GameController::UpdateGyro(const float gyro[3]) { + std::scoped_lock l(m_states_queue_mutex); + std::memcpy(gyro_buf, gyro, sizeof(gyro_buf)); +} + +void GameController::UpdateAcceleration(const float acceleration[3]) { + std::scoped_lock l(m_states_queue_mutex); + std::memcpy(accel_buf, acceleration, sizeof(accel_buf)); +} + +void GameController::UpdateAxisSmoothing() { + m_state.UpdateAxisSmoothing(); +} + +void GameController::SetLightBarRGB(u8 r, u8 g, u8 b) { + colour = {r, g, b}; + if (m_sdl_gamepad != nullptr) { + SDL_SetGamepadLED(m_sdl_gamepad, r, g, b); + } +} + +void GameController::PollLightColour() { + if (m_sdl_gamepad != nullptr) { + SDL_SetGamepadLED(m_sdl_gamepad, colour.r, colour.g, colour.b); + } +} + +bool GameController::SetVibration(u8 smallMotor, u8 largeMotor) { + if (m_sdl_gamepad != nullptr) { + return SDL_RumbleGamepad(m_sdl_gamepad, (smallMotor / 255.0f) * 0xFFFF, + (largeMotor / 255.0f) * 0xFFFF, -1); + } + return true; +} + +void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, float y) { + if (touchIndex < 2) { + m_state.OnTouchpad(touchIndex, touchDown, x, y); + PushState(); + } +} + +std::array, 4> GameControllers::controller_override_colors{ + std::nullopt, std::nullopt, std::nullopt, std::nullopt}; + +void GameControllers::CalculateOrientation(Libraries::Pad::OrbisFVector3& acceleration, + Libraries::Pad::OrbisFVector3& angularVelocity, + float deltaTime, + Libraries::Pad::OrbisFQuaternion& lastOrientation, + Libraries::Pad::OrbisFQuaternion& orientation) { + // avoid wildly off values coming from elapsed time between two samples + // being too high, such as on the first time the controller is polled + if (deltaTime > 1.0f) { + orientation = lastOrientation; + return; + } Libraries::Pad::OrbisFQuaternion q = lastOrientation; Libraries::Pad::OrbisFQuaternion ω = {angularVelocity.x, angularVelocity.y, angularVelocity.z, 0.0f}; @@ -143,27 +222,100 @@ void GameController::CalculateOrientation(Libraries::Pad::OrbisFVector3& acceler orientation.y, orientation.z, orientation.w); } -void GameController::SetLightBarRGB(u8 r, u8 g, u8 b) { - if (!m_engine) { - return; - } - m_engine->SetLightBarRGB(r, g, b); -} +bool is_first_check = true; -void GameController::SetVibration(u8 smallMotor, u8 largeMotor) { - if (!m_engine) { - return; - } - m_engine->SetVibration(smallMotor, largeMotor); -} +void GameControllers::TryOpenSDLControllers() { + using namespace Libraries::UserService; + int controller_count; + s32 move_count = 0; + SDL_JoystickID* new_joysticks = SDL_GetGamepads(&controller_count); + LOG_INFO(Input, "{} controllers are currently connected", controller_count); -void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, float y) { - if (touchIndex < 2) { - m_state.OnTouchpad(touchIndex, touchDown, x, y); - PushState(); - } -} + std::unordered_set assigned_ids; + std::array slot_taken{false, false, false, false}; + for (int i = 0; i < 4; i++) { + SDL_Gamepad* pad = controllers[i]->m_sdl_gamepad; + if (pad) { + SDL_JoystickID id = SDL_GetGamepadID(pad); + bool still_connected = false; + ControllerType type = ControllerType::Standard; + for (int j = 0; j < controller_count; j++) { + if (new_joysticks[j] == id) { + still_connected = true; + assigned_ids.insert(id); + slot_taken[i] = true; + break; + } + } + if (!still_connected) { + auto u = UserManagement.GetUserByID(controllers[i]->user_id); + UserManagement.LogoutUser(u); + SDL_CloseGamepad(pad); + controllers[i]->m_sdl_gamepad = nullptr; + controllers[i]->user_id = -1; + slot_taken[i] = false; + } + } + } + + for (int j = 0; j < controller_count; j++) { + SDL_JoystickID id = new_joysticks[j]; + if (assigned_ids.contains(id)) + continue; + + SDL_Gamepad* pad = SDL_OpenGamepad(id); + if (!pad) { + continue; + } + + for (int i = 0; i < 4; i++) { + if (!slot_taken[i]) { + auto u = UserManagement.GetUserByPlayerIndex(i + 1); + if (!u) { + LOG_INFO(Input, "User {} not found", i + 1); + continue; // for now, if you don't specify who Player N is in the config, + // Player N won't be registered at all + } + auto* c = controllers[i]; + c->m_sdl_gamepad = pad; + LOG_INFO(Input, "Gamepad registered for slot {}! Handle: {}", i, + SDL_GetGamepadID(pad)); + c->user_id = u->user_id; + slot_taken[i] = true; + UserManagement.LoginUser(u, i + 1); + if (EmulatorSettings.IsMotionControlsEnabled()) { + if (SDL_SetGamepadSensorEnabled(c->m_sdl_gamepad, SDL_SENSOR_GYRO, true)) { + c->gyro_poll_rate = + SDL_GetGamepadSensorDataRate(c->m_sdl_gamepad, SDL_SENSOR_GYRO); + LOG_INFO(Input, "Gyro initialized, poll rate: {}", c->gyro_poll_rate); + } else { + LOG_ERROR(Input, "Failed to initialize gyro controls for gamepad {}", + c->user_id); + } + if (SDL_SetGamepadSensorEnabled(c->m_sdl_gamepad, SDL_SENSOR_ACCEL, true)) { + c->accel_poll_rate = + SDL_GetGamepadSensorDataRate(c->m_sdl_gamepad, SDL_SENSOR_ACCEL); + LOG_INFO(Input, "Accel initialized, poll rate: {}", c->accel_poll_rate); + } else { + LOG_ERROR(Input, "Failed to initialize accel controls for gamepad {}", + c->user_id); + } + } + break; + } + } + } + if (is_first_check) [[unlikely]] { + is_first_check = false; + if (controller_count - move_count == 0) { + auto u = UserManagement.GetUserByPlayerIndex(1); + controllers[0]->user_id = u->user_id; + UserManagement.LoginUser(u, 1); + } + } + SDL_free(new_joysticks); +} u8 GameController::GetTouchCount() { return m_touch_count; } @@ -215,73 +367,37 @@ void GameController::SetLastUpdate(std::chrono::steady_clock::time_point lastUpd m_last_update = lastUpdate; } -void GameController::SetEngine(std::unique_ptr engine) { - m_engine = std::move(engine); - if (m_engine) { - m_engine->Init(); - } -} - -Engine* GameController::GetEngine() { - return m_engine.get(); -} - void GameController::PushState() { std::lock_guard lg(m_states_queue_mutex); m_state.time = Libraries::Kernel::sceKernelGetProcessTime(); m_states_queue.Push(m_state); } -u32 GameController::Poll() { - if (m_connected) { - PushState(); - } - return 33; -} - -} // namespace Input - -namespace GamepadSelect { - -int GetDefaultGamepad(SDL_JoystickID* gamepadIDs, int gamepadCount) { - char GUIDbuf[33]; - if (Config::getDefaultControllerID() != "") { - for (int i = 0; i < gamepadCount; i++) { - SDL_GUIDToString(SDL_GetGamepadGUIDForID(gamepadIDs[i]), GUIDbuf, 33); - std::string currentGUID = std::string(GUIDbuf); - if (currentGUID == Config::getDefaultControllerID()) { - return i; - } - } - } - return -1; -} - -int GetIndexfromGUID(SDL_JoystickID* gamepadIDs, int gamepadCount, std::string GUID) { - char GUIDbuf[33]; - for (int i = 0; i < gamepadCount; i++) { - SDL_GUIDToString(SDL_GetGamepadGUIDForID(gamepadIDs[i]), GUIDbuf, 33); - std::string currentGUID = std::string(GUIDbuf); - if (currentGUID == GUID) { +u8 GameControllers::GetGamepadIndexFromJoystickId(SDL_JoystickID id) { + auto g = SDL_GetGamepadFromID(id); + ASSERT(g != nullptr); + for (int i = 0; i < 5; i++) { + if (controllers[i]->m_sdl_gamepad == g) { return i; } } + // LOG_TRACE(Input, "Gamepad index: {}", index); return -1; } -std::string GetGUIDString(SDL_JoystickID* gamepadIDs, int index) { - char GUIDbuf[33]; - SDL_GUIDToString(SDL_GetGamepadGUIDForID(gamepadIDs[index]), GUIDbuf, 33); - std::string GUID = std::string(GUIDbuf); - return GUID; +std::optional GameControllers::GetControllerIndexFromUserID(s32 user_id) { + auto const u = UserManagement.GetUserByID(user_id); + if (!u) { + return std::nullopt; + } + return u->player_index - 1; } -std::string GetSelectedGamepad() { - return SelectedGamepad; +std::optional GameControllers::GetControllerIndexFromControllerID(s32 controller_id) { + if (controller_id < 1 || controller_id > 5) { + return std::nullopt; + } + return controller_id - 1; } -void SetSelectedGamepad(std::string GUID) { - SelectedGamepad = GUID; -} - -} // namespace GamepadSelect +} // namespace Input diff --git a/src/input/controller.h b/src/input/controller.h index 6c13fdf99..1c711c488 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -3,18 +3,25 @@ #pragma once -#include -#include #include +#include #include #include - +#include "SDL3/SDL_joystick.h" +#include "common/assert.h" #include "common/types.h" #include "core/libraries/pad/pad.h" +#include "core/libraries/system/userservice.h" + +struct SDL_Gamepad; namespace Input { +enum class ControllerType { + Standard, +}; + enum class Axis { LeftX = 0, LeftY = 1, @@ -33,37 +40,41 @@ struct TouchpadEntry { u16 y{}; }; -class State { +struct Colour { + u8 r, g, b; +}; + +struct State { +private: + template + using AxisArray = std::array; + static constexpr AxisArray axis_defaults{128, 128, 128, 128, 0, 0}; + static constexpr u64 axis_smoothing_time{33000}; + AxisArray axis_smoothing_flags{true}; + AxisArray axis_smoothing_start_times{0}; + AxisArray axis_smoothing_start_values{axis_defaults}; + AxisArray axis_smoothing_end_values{axis_defaults}; + public: void OnButton(Libraries::Pad::OrbisPadButtonDataOffset, bool); - void OnAxis(Axis, int); + void OnAxis(Axis, int, bool smooth = true); void OnTouchpad(int touchIndex, bool isDown, float x, float y); void OnGyro(const float[3]); void OnAccel(const float[3]); + void UpdateAxisSmoothing(); Libraries::Pad::OrbisPadButtonDataOffset buttonsState{}; u64 time = 0; - int axes[static_cast(Axis::AxisMax)] = {128, 128, 128, 128, 0, 0}; + AxisArray axes{axis_defaults}; TouchpadEntry touchpad[2] = {{false, 0, 0}, {false, 0, 0}}; - Libraries::Pad::OrbisFVector3 acceleration = {0.0f, 0.0f, 0.0f}; + Libraries::Pad::OrbisFVector3 acceleration = {0.0f, -9.81f, 0.0f}; Libraries::Pad::OrbisFVector3 angularVelocity = {0.0f, 0.0f, 0.0f}; Libraries::Pad::OrbisFQuaternion orientation = {0.0f, 0.0f, 0.0f, 1.0f}; }; -class Engine { -public: - virtual ~Engine() = default; - virtual void Init() = 0; - virtual void SetLightBarRGB(u8 r, u8 g, u8 b) = 0; - virtual void SetVibration(u8 smallMotor, u8 largeMotor) = 0; - virtual State ReadState() = 0; - virtual float GetAccelPollRate() const = 0; - virtual float GetGyroPollRate() const = 0; - SDL_Gamepad* m_gamepad; -}; - inline int GetAxis(int min, int max, int value) { - return std::clamp((255 * (value - min)) / (max - min), 0, 255); + int v = (255 * (value - min)) / (max - min); + return (v < 0 ? 0 : (v > 255 ? 255 : v)); } template @@ -98,6 +109,8 @@ private: }; class GameController { + friend class GameControllers; + public: GameController(); virtual ~GameController() = default; @@ -105,16 +118,17 @@ public: void ReadState(State* state, bool* isConnected, int* connectedCount); int ReadStates(State* states, int states_num, bool* isConnected, int* connectedCount); - void Button(int id, Libraries::Pad::OrbisPadButtonDataOffset button, bool isPressed); - void Axis(int id, Input::Axis axis, int value); - void Gyro(int id, const float gyro[3]); - void Acceleration(int id, const float acceleration[3]); + void Button(Libraries::Pad::OrbisPadButtonDataOffset button, bool isPressed); + void Axis(Input::Axis axis, int value, bool smooth = true); + void Gyro(int id); + void Acceleration(int id); + void UpdateGyro(const float gyro[3]); + void UpdateAcceleration(const float acceleration[3]); + void UpdateAxisSmoothing(); void SetLightBarRGB(u8 r, u8 g, u8 b); - void SetVibration(u8 smallMotor, u8 largeMotor); + void PollLightColour(); + bool SetVibration(u8 smallMotor, u8 largeMotor); void SetTouchpadState(int touchIndex, bool touchDown, float x, float y); - void SetEngine(std::unique_ptr); - Engine* GetEngine(); - u32 Poll(); u8 GetTouchCount(); void SetTouchCount(u8 touchCount); @@ -129,11 +143,12 @@ public: Libraries::Pad::OrbisFQuaternion GetLastOrientation(); std::chrono::steady_clock::time_point GetLastUpdate(); void SetLastUpdate(std::chrono::steady_clock::time_point lastUpdate); - static void CalculateOrientation(Libraries::Pad::OrbisFVector3& acceleration, - Libraries::Pad::OrbisFVector3& angularVelocity, - float deltaTime, - Libraries::Pad::OrbisFQuaternion& lastOrientation, - Libraries::Pad::OrbisFQuaternion& orientation); + + float gyro_poll_rate; + float accel_poll_rate; + float gyro_buf[3] = {0.0f, 0.0f, 0.0f}, accel_buf[3] = {0.0f, 9.81f, 0.0f}; + s32 user_id = Libraries::UserService::ORBIS_USER_SERVICE_USER_ID_INVALID; + SDL_Gamepad* m_sdl_gamepad = nullptr; private: void PushState(); @@ -146,22 +161,46 @@ private: bool m_was_secondary_reset = false; std::chrono::steady_clock::time_point m_last_update = {}; Libraries::Pad::OrbisFQuaternion m_orientation = {0.0f, 0.0f, 0.0f, 1.0f}; + Colour colour; State m_state; std::mutex m_states_queue_mutex; RingBufferQueue m_states_queue; +}; - std::unique_ptr m_engine = nullptr; +class GameControllers { + std::array controllers; + + static std::array, 4> controller_override_colors; + +public: + GameControllers() + : controllers({new GameController(), new GameController(), new GameController(), + new GameController(), new GameController()}) {}; + virtual ~GameControllers() = default; + GameController* operator[](const size_t& i) const { + if (i > 4) { + UNREACHABLE_MSG("Index {} is out of bounds for GameControllers!", i); + } + return controllers[i]; + } + void TryOpenSDLControllers(); + u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id); + static std::optional GetControllerIndexFromUserID(s32 user_id); + static std::optional GetControllerIndexFromControllerID(s32 controller_id); + + static void CalculateOrientation(Libraries::Pad::OrbisFVector3& acceleration, + Libraries::Pad::OrbisFVector3& angularVelocity, + float deltaTime, + Libraries::Pad::OrbisFQuaternion& lastOrientation, + Libraries::Pad::OrbisFQuaternion& orientation); + static void SetControllerCustomColor(s32 i, u8 r, u8 g, u8 b) { + controller_override_colors[i] = {r, g, b}; + } + static std::optional GetControllerCustomColor(s32 i) { + return controller_override_colors[i]; + } }; } // namespace Input - -namespace GamepadSelect { - -int GetIndexfromGUID(SDL_JoystickID* gamepadIDs, int gamepadCount, std::string GUID); -std::string GetGUIDString(SDL_JoystickID* gamepadIDs, int index); -std::string GetSelectedGamepad(); -void SetSelectedGamepad(std::string GUID); - -} // namespace GamepadSelect diff --git a/src/input/input_handler.cpp b/src/input/input_handler.cpp index fbda6e394..cf258235d 100644 --- a/src/input/input_handler.cpp +++ b/src/input/input_handler.cpp @@ -18,10 +18,10 @@ #include "SDL3/SDL_events.h" #include "SDL3/SDL_timer.h" -#include "common/config.h" #include "common/elf_info.h" #include "common/io_file.h" #include "common/path_util.h" +#include "common/singleton.h" #include "core/devtools/layer.h" #include "core/emulator_settings.h" #include "core/emulator_state.h" @@ -43,78 +43,185 @@ What structs are needed? InputBinding(key1, key2, key3) ControllerOutput(button, axis) - we only need a const array of these, and one of the attr-s is always 0 BindingConnection(inputBinding (member), controllerOutput (ref to the array element)) - -Things to always test before pushing like a dumbass: -Button outputs -Axis outputs -Input hierarchy -Multi key inputs -Mouse to joystick -Key toggle -Joystick halfmode - -Don't be an idiot and test only the changed part expecting everything else to not be broken */ +constexpr std::string_view GetDefaultGlobalConfig() { + return R"(# Anything put here will be loaded for all games, +# alongside the game's config or default.ini depending on your preference. +)"; +} + +constexpr std::string_view GetDefaultInputConfig() { + return R"(#Feeling lost? Check out the Help section! + +# Keyboard bindings + +triangle = kp8 +circle = kp6 +cross = kp2 +square = kp4 +# Alternatives for users without a keypad +triangle = c +circle = b +cross = n +square = v + +l1 = q +r1 = u +l2 = e +r2 = o +l3 = x +r3 = m + +options = enter +touchpad_center = space + +pad_up = up +pad_down = down +pad_left = left +pad_right = right + +axis_left_x_minus = a +axis_left_x_plus = d +axis_left_y_minus = w +axis_left_y_plus = s + +axis_right_x_minus = j +axis_right_x_plus = l +axis_right_y_minus = i +axis_right_y_plus = k + +# Controller bindings + +triangle = triangle +cross = cross +square = square +circle = circle + +l1 = l1 +l2 = l2 +l3 = l3 +r1 = r1 +r2 = r2 +r3 = r3 + +options = options +touchpad_center = back + +pad_up = pad_up +pad_down = pad_down +pad_left = pad_left +pad_right = pad_right + +axis_left_x = axis_left_x +axis_left_y = axis_left_y +axis_right_x = axis_right_x +axis_right_y = axis_right_y + +# Range of deadzones: 1 (almost none) to 127 (max) +analog_deadzone = leftjoystick, 2, 127 +analog_deadzone = rightjoystick, 2, 127 + +override_controller_color = false, 0, 0, 255 +)"; +} +std::filesystem::path GetInputConfigFile(const std::string& game_id) { + // Read configuration file of the game, and if it doesn't exist, generate it from default + // If that doesn't exist either, generate that from getDefaultConfig() and try again + // If even the folder is missing, we start with that. + + const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "input_config"; + const auto config_file = config_dir / (game_id + ".ini"); + const auto default_config_file = config_dir / "default.ini"; + + // Ensure the config directory exists + if (!std::filesystem::exists(config_dir)) { + std::filesystem::create_directories(config_dir); + } + + // Check if the default config exists + if (!std::filesystem::exists(default_config_file)) { + // If the default config is also missing, create it from getDefaultConfig() + const auto default_config = GetDefaultInputConfig(); + std::ofstream default_config_stream(default_config_file); + if (default_config_stream) { + default_config_stream << default_config; + } + } + + // if empty, we only need to execute the function up until this point + if (game_id.empty()) { + return default_config_file; + } + + // Create global config if it doesn't exist yet + if (game_id == "global" && !std::filesystem::exists(config_file)) { + if (!std::filesystem::exists(config_file)) { + const auto global_config = GetDefaultGlobalConfig(); + std::ofstream global_config_stream(config_file); + if (global_config_stream) { + global_config_stream << global_config; + } + } + } + if (game_id == "global") { + std::map default_bindings_to_add = { + {"hotkey_renderdoc_capture", "f12"}, + {"hotkey_fullscreen", "f11"}, + {"hotkey_show_fps", "f10"}, + {"hotkey_pause", "f9"}, + {"hotkey_reload_inputs", "f8"}, + {"hotkey_toggle_mouse_to_joystick", "f7"}, + {"hotkey_toggle_mouse_to_gyro", "f6"}, + {"hotkey_add_virtual_user", "f5"}, + {"hotkey_remove_virtual_user", "f4"}, + {"hotkey_toggle_mouse_to_touchpad", "delete"}, + {"hotkey_quit", "lctrl, lshift, end"}, + {"hotkey_volume_up", "kpplus"}, + {"hotkey_volume_down", "kpminus"}, + }; + std::ifstream global_in(config_file); + std::string line; + while (std::getline(global_in, line)) { + line.erase(std::remove_if(line.begin(), line.end(), + [](unsigned char c) { return std::isspace(c); }), + line.end()); + std::size_t equal_pos = line.find('='); + if (equal_pos == std::string::npos) { + continue; + } + std::string output_string = line.substr(0, equal_pos); + default_bindings_to_add.erase(output_string); + } + global_in.close(); + std::ofstream global_out(config_file, std::ios::app); + for (auto const& b : default_bindings_to_add) { + global_out << b.first << " = " << b.second << "\n"; + } + } + + // If game-specific config doesn't exist, create it from the default config + if (!std::filesystem::exists(config_file)) { + std::filesystem::copy(default_config_file, config_file); + } + return config_file; +} + bool leftjoystick_halfmode = false, rightjoystick_halfmode = false; -std::pair leftjoystick_deadzone, rightjoystick_deadzone, lefttrigger_deadzone, - righttrigger_deadzone; +std::array, 4> leftjoystick_deadzone, rightjoystick_deadzone, + lefttrigger_deadzone, righttrigger_deadzone; std::list> pressed_keys; std::list toggled_keys; static std::vector connections; -auto output_array = std::array{ - // Important: these have to be the first, or else they will update in the wrong order - ControllerOutput(LEFTJOYSTICK_HALFMODE), - ControllerOutput(RIGHTJOYSTICK_HALFMODE), - ControllerOutput(KEY_TOGGLE), - ControllerOutput(MOUSE_GYRO_ROLL_MODE), +GameControllers ControllerOutput::controllers = + *Common::Singleton::Instance(); - // Button mappings - ControllerOutput(SDL_GAMEPAD_BUTTON_NORTH), // Triangle - ControllerOutput(SDL_GAMEPAD_BUTTON_EAST), // Circle - ControllerOutput(SDL_GAMEPAD_BUTTON_SOUTH), // Cross - ControllerOutput(SDL_GAMEPAD_BUTTON_WEST), // Square - ControllerOutput(SDL_GAMEPAD_BUTTON_LEFT_SHOULDER), // L1 - ControllerOutput(SDL_GAMEPAD_BUTTON_LEFT_STICK), // L3 - ControllerOutput(SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER), // R1 - ControllerOutput(SDL_GAMEPAD_BUTTON_RIGHT_STICK), // R3 - ControllerOutput(SDL_GAMEPAD_BUTTON_START), // Options - ControllerOutput(SDL_GAMEPAD_BUTTON_TOUCHPAD_LEFT), // TouchPad - ControllerOutput(SDL_GAMEPAD_BUTTON_TOUCHPAD_CENTER), // TouchPad - ControllerOutput(SDL_GAMEPAD_BUTTON_TOUCHPAD_RIGHT), // TouchPad - ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_UP), // Up - ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_DOWN), // Down - ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_LEFT), // Left - ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_RIGHT), // Right - - // Axis mappings - // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX, false), - // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY, false), - // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX, false), - // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY, false), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY), - - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFT_TRIGGER), - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER), - - ControllerOutput(HOTKEY_FULLSCREEN), - ControllerOutput(HOTKEY_PAUSE), - ControllerOutput(HOTKEY_SIMPLE_FPS), - ControllerOutput(HOTKEY_QUIT), - ControllerOutput(HOTKEY_RELOAD_INPUTS), - ControllerOutput(HOTKEY_TOGGLE_MOUSE_TO_JOYSTICK), - ControllerOutput(HOTKEY_TOGGLE_MOUSE_TO_GYRO), - ControllerOutput(HOTKEY_TOGGLE_MOUSE_TO_TOUCHPAD), - ControllerOutput(HOTKEY_RENDERDOC), - ControllerOutput(HOTKEY_VOLUME_UP), - ControllerOutput(HOTKEY_VOLUME_DOWN), - - ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_INVALID), +std::array output_arrays = { + ControllerAllOutputs(0), ControllerAllOutputs(1), ControllerAllOutputs(2), + ControllerAllOutputs(3), ControllerAllOutputs(4), ControllerAllOutputs(5), + ControllerAllOutputs(6), ControllerAllOutputs(7), ControllerAllOutputs(8), }; void ControllerOutput::LinkJoystickAxes() { @@ -158,6 +265,8 @@ static OrbisPadButtonDataOffset SDLGamepadToOrbisButton(u8 button) { return OPBDO::TouchPad; case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: return OPBDO::L1; + case SDL_GAMEPAD_BUTTON_MISC1: // Move + return OPBDO::L1; case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: return OPBDO::R1; case SDL_GAMEPAD_BUTTON_LEFT_STICK: @@ -223,10 +332,19 @@ InputBinding GetBindingFromString(std::string& line) { return InputBinding(keys[0], keys[1], keys[2]); } +std::optional parseInt(const std::string& s) { + try { + return std::stoi(s); + } catch (...) { + return std::nullopt; + } +}; + void ParseInputConfig(const std::string game_id = "") { - std::string game_id_or_default = Config::GetUseUnifiedInputConfig() ? "default" : game_id; - const auto config_file = Config::GetInputConfigFile(game_id_or_default); - const auto global_config_file = Config::GetInputConfigFile("global"); + std::string game_id_or_default = + EmulatorSettings.IsUseUnifiedInputConfig() ? "default" : game_id; + const auto config_file = GetInputConfigFile(game_id_or_default); + const auto global_config_file = GetInputConfigFile("global"); // we reset these here so in case the user fucks up or doesn't include some of these, // we can fall back to default @@ -235,13 +353,14 @@ void ParseInputConfig(const std::string game_id = "") { float mouse_speed = 1; float mouse_speed_offset = 0.125; - leftjoystick_deadzone = {1, 127}; - rightjoystick_deadzone = {1, 127}; - lefttrigger_deadzone = {1, 127}; - righttrigger_deadzone = {1, 127}; + // me when I'm in a type deduction tournament and my opponent is clang + constexpr std::array, 4> default_deadzone = { + std::pair{1, 127}, {1, 127}, {1, 127}, {1, 127}}; - Config::SetOverrideControllerColor(false); - Config::SetControllerCustomColor(0, 0, 255); + leftjoystick_deadzone = default_deadzone; + rightjoystick_deadzone = default_deadzone; + lefttrigger_deadzone = default_deadzone; + righttrigger_deadzone = default_deadzone; int lineCount = 0; @@ -278,21 +397,37 @@ void ParseInputConfig(const std::string game_id = "") { std::string output_string = line.substr(0, equal_pos); std::string input_string = line.substr(equal_pos + 1); - // Remove trailing semicolon from input_string - if (!input_string.empty() && input_string[input_string.length() - 1] == ';' && - input_string != ";") { - line = line.substr(0, line.length() - 1); + s8 input_gamepad_id = -1, output_gamepad_id = -1; // -1 means it's not specified + + // input gamepad id is only for controllers, it's discarded otherwise + std::size_t input_colon_pos = input_string.find(':'); + if (input_colon_pos != std::string::npos) { + auto temp = parseInt(input_string.substr(input_colon_pos + 1)); + if (!temp) { + LOG_WARNING(Input, "Invalid gamepad ID value at line {}: \"{}\"", lineCount, line); + } else { + input_gamepad_id = *temp; + } + input_string = input_string.substr(0, input_colon_pos); + } + + // if not provided, assume it's for all gamepads, if the input is a controller and that also + // doesn't have an ID, and for the first otherwise + std::size_t output_colon_pos = output_string.find(':'); + if (output_colon_pos != std::string::npos) { + auto temp = parseInt(output_string.substr(output_colon_pos + 1)); + if (!temp) { + LOG_WARNING(Input, "Invalid gamepad ID value at line {}: \"{}\"", lineCount, line); + } else { + output_gamepad_id = *temp; + } + output_string = output_string.substr(0, output_colon_pos); } std::size_t comma_pos = input_string.find(','); - auto parseInt = [](const std::string& s) -> std::optional { - try { - return std::stoi(s); - } catch (...) { - return std::nullopt; - } - }; + // todo make override_controller_color and analog_deadzone be controller specific + // instead of global if (output_string == "mouse_to_joystick") { if (input_string == "left") { SetMouseToJoystick(1); @@ -315,7 +450,7 @@ void ParseInputConfig(const std::string game_id = "") { return; } ControllerOutput* toggle_out = - &*std::ranges::find(output_array, ControllerOutput(KEY_TOGGLE)); + &*std::ranges::find(output_arrays[0].data, ControllerOutput(KEY_TOGGLE)); BindingConnection toggle_connection = BindingConnection( InputBinding(toggle_keys.keys[0]), toggle_out, 0, toggle_keys.keys[1]); connections.insert(connections.end(), toggle_connection); @@ -356,15 +491,17 @@ void ParseInputConfig(const std::string game_id = "") { std::pair deadzone = {*inner_deadzone, *outer_deadzone}; - static std::unordered_map&> deadzone_map = { - {"leftjoystick", leftjoystick_deadzone}, - {"rightjoystick", rightjoystick_deadzone}, - {"l2", lefttrigger_deadzone}, - {"r2", righttrigger_deadzone}, - }; + static std::unordered_map, 4>&> + deadzone_map = { + {"leftjoystick", leftjoystick_deadzone}, + {"rightjoystick", rightjoystick_deadzone}, + {"l2", lefttrigger_deadzone}, + {"r2", righttrigger_deadzone}, + }; + output_gamepad_id = output_gamepad_id == -1 ? 1 : output_gamepad_id; if (auto it = deadzone_map.find(device); it != deadzone_map.end()) { - it->second = deadzone; + it->second[output_gamepad_id - 1] = deadzone; LOG_DEBUG(Input, "Parsed deadzone: {} {} {}", device, inner_deadzone_str, outer_deadzone_str); } else { @@ -390,10 +527,12 @@ void ParseInputConfig(const std::string game_id = "") { lineCount, line); return; } - Config::SetOverrideControllerColor(enable == "true"); - Config::SetControllerCustomColor(*r, *g, *b); - LOG_DEBUG(Input, "Parsed color settings: {} {} {} {}", - enable == "true" ? "override" : "no override", *r, *b, *g); + output_gamepad_id = output_gamepad_id == -1 ? 1 : output_gamepad_id; + if (enable == "true") { + GameControllers::SetControllerCustomColor(output_gamepad_id - 1, *r, *g, *b); + } + LOG_DEBUG(Input, "Parsed color settings: {} {} - {} {} {}", + enable == "true" ? "override" : "no override", output_gamepad_id, *r, *b, *g); return; } @@ -410,31 +549,46 @@ void ParseInputConfig(const std::string game_id = "") { auto axis_it = string_to_axis_map.find(output_string); if (button_it != string_to_cbutton_map.end()) { connection = BindingConnection( - binding, &*std::ranges::find(output_array, ControllerOutput(button_it->second))); - connections.insert(connections.end(), connection); + binding, + &*std::ranges::find(output_arrays[std::clamp(output_gamepad_id - 1, 0, 3)].data, + ControllerOutput(button_it->second))); } else if (hotkey_it != string_to_hotkey_map.end()) { connection = BindingConnection( - binding, &*std::ranges::find(output_array, ControllerOutput(hotkey_it->second))); - connections.insert(connections.end(), connection); + binding, + &*std::ranges::find(output_arrays[std::clamp(output_gamepad_id - 1, 0, 3)].data, + ControllerOutput(hotkey_it->second))); } else if (axis_it != string_to_axis_map.end()) { int value_to_set = binding.keys[2].type == InputType::Axis ? 0 : axis_it->second.value; connection = BindingConnection( binding, - &*std::ranges::find(output_array, ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, - axis_it->second.axis, - axis_it->second.value >= 0)), + &*std::ranges::find(output_arrays[std::clamp(output_gamepad_id - 1, 0, 3)].data, + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, + axis_it->second.axis, + axis_it->second.value >= 0)), value_to_set); - connections.insert(connections.end(), connection); } else { LOG_WARNING(Input, "Invalid format at line: {}, data: \"{}\", skipping line.", lineCount, line); return; } + // if the input binding contains a controller input, and gamepad ID + // isn't specified for either inputs or output (both are -1), then multiply the binding and + // add it to all 4 controllers + if (connection.HasGamepadInput() && input_gamepad_id == -1 && output_gamepad_id == -1) { + for (int i = 0; i < output_arrays.size(); i++) { + BindingConnection copy = connection.CopyWithChangedGamepadId(i + 1); + copy.output = &*std::ranges::find(output_arrays[i].data, *connection.output); + connections.push_back(copy); + } + } else { + connections.push_back(connection); + } LOG_DEBUG(Input, "Succesfully parsed line {}", lineCount); }; while (std::getline(global_config_stream, line)) { ProcessLine(); } + lineCount = 0; while (std::getline(config_stream, line)) { ProcessLine(); } @@ -446,6 +600,16 @@ void ParseInputConfig(const std::string game_id = "") { LOG_DEBUG(Input, "Done parsing the input config!"); } +BindingConnection BindingConnection::CopyWithChangedGamepadId(u8 gamepad) { + BindingConnection copy = *this; + for (auto& key : copy.binding.keys) { + if (key.type == InputType::Controller || key.type == InputType::Axis) { + key.gamepad_id = gamepad; + } + } + return copy; +} + u32 GetMouseWheelEvent(const SDL_Event& event) { if (event.type != SDL_EVENT_MOUSE_WHEEL && event.type != SDL_EVENT_MOUSE_WHEEL_OFF) { LOG_WARNING(Input, "Something went wrong with wheel input parsing!"); @@ -464,6 +628,7 @@ u32 GetMouseWheelEvent(const SDL_Event& event) { } InputEvent InputBinding::GetInputEventFromSDLEvent(const SDL_Event& e) { + u8 gamepad = 1; switch (e.type) { case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_UP: @@ -478,21 +643,17 @@ InputEvent InputBinding::GetInputEventFromSDLEvent(const SDL_Event& e) { e.type == SDL_EVENT_MOUSE_WHEEL, 0); case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_UP: - return InputEvent(InputType::Controller, static_cast(e.gbutton.button), e.gbutton.down, - 0); // clang made me do it + gamepad = ControllerOutput::controllers.GetGamepadIndexFromJoystickId(e.gbutton.which) + 1; + return InputEvent({InputType::Controller, (u32)e.gbutton.button, gamepad}, e.gbutton.down, + 0); case SDL_EVENT_GAMEPAD_AXIS_MOTION: - return InputEvent(InputType::Axis, static_cast(e.gaxis.axis), true, - e.gaxis.value / 256); // this too + gamepad = ControllerOutput::controllers.GetGamepadIndexFromJoystickId(e.gaxis.which) + 1; + return InputEvent({InputType::Axis, (u32)e.gaxis.axis, gamepad}, true, e.gaxis.value / 256); default: return InputEvent(); } } -GameController* ControllerOutput::controller = nullptr; -void ControllerOutput::SetControllerOutputController(GameController* c) { - ControllerOutput::controller = c; -} - void ToggleKeyInList(InputID input) { if (input.type == InputType::Axis) { LOG_ERROR(Input, "Toggling analog inputs is not supported!"); @@ -538,7 +699,7 @@ void ControllerOutput::AddUpdate(InputEvent event) { *new_param = (event.active ? event.axis_value : 0) + *new_param; } } -void ControllerOutput::FinalizeUpdate() { +void ControllerOutput::FinalizeUpdate(u8 gamepad_index) { auto PushSDLEvent = [&](u32 event_type) { if (new_button_state) { SDL_Event e; @@ -553,20 +714,24 @@ void ControllerOutput::FinalizeUpdate() { } old_button_state = new_button_state; old_param = *new_param; - bool is_game_specific = EmulatorState::GetInstance()->IsGameSpecifigConfigUsed(); + GameController* controller; + if (gamepad_index < 5) + controller = controllers[gamepad_index]; + else + UNREACHABLE(); if (button != SDL_GAMEPAD_BUTTON_INVALID) { switch (button) { case SDL_GAMEPAD_BUTTON_TOUCHPAD_LEFT: controller->SetTouchpadState(0, new_button_state, 0.25f, 0.5f); - controller->Button(0, SDLGamepadToOrbisButton(button), new_button_state); + controller->Button(SDLGamepadToOrbisButton(button), new_button_state); break; case SDL_GAMEPAD_BUTTON_TOUCHPAD_CENTER: controller->SetTouchpadState(0, new_button_state, 0.50f, 0.5f); - controller->Button(0, SDLGamepadToOrbisButton(button), new_button_state); + controller->Button(SDLGamepadToOrbisButton(button), new_button_state); break; case SDL_GAMEPAD_BUTTON_TOUCHPAD_RIGHT: controller->SetTouchpadState(0, new_button_state, 0.75f, 0.5f); - controller->Button(0, SDLGamepadToOrbisButton(button), new_button_state); + controller->Button(SDLGamepadToOrbisButton(button), new_button_state); break; case LEFTJOYSTICK_HALFMODE: leftjoystick_halfmode = new_button_state; @@ -598,6 +763,12 @@ void ControllerOutput::FinalizeUpdate() { case HOTKEY_RENDERDOC: PushSDLEvent(SDL_EVENT_RDOC_CAPTURE); break; + case HOTKEY_ADD_VIRTUAL_USER: + PushSDLEvent(SDL_EVENT_ADD_VIRTUAL_USER); + break; + case HOTKEY_REMOVE_VIRTUAL_USER: + PushSDLEvent(SDL_EVENT_REMOVE_VIRTUAL_USER); + break; case HOTKEY_VOLUME_UP: EmulatorSettings.SetVolumeSlider( std::clamp(EmulatorSettings.GetVolumeSlider() + 10, 0, 500)); @@ -618,7 +789,7 @@ void ControllerOutput::FinalizeUpdate() { SetMouseGyroRollMode(new_button_state); break; default: // is a normal key (hopefully) - controller->Button(0, SDLGamepadToOrbisButton(button), new_button_state); + controller->Button(SDLGamepadToOrbisButton(button), new_button_state); break; } } else if (axis != SDL_GAMEPAD_AXIS_INVALID && positive_axis) { @@ -638,28 +809,28 @@ void ControllerOutput::FinalizeUpdate() { switch (c_axis) { case Axis::LeftX: case Axis::LeftY: - ApplyDeadzone(new_param, leftjoystick_deadzone); + ApplyDeadzone(new_param, leftjoystick_deadzone[gamepad_index]); multiplier = leftjoystick_halfmode ? 0.5 : 1.0; break; case Axis::RightX: case Axis::RightY: - ApplyDeadzone(new_param, rightjoystick_deadzone); + ApplyDeadzone(new_param, rightjoystick_deadzone[gamepad_index]); multiplier = rightjoystick_halfmode ? 0.5 : 1.0; break; case Axis::TriggerLeft: - ApplyDeadzone(new_param, lefttrigger_deadzone); - controller->Axis(0, c_axis, GetAxis(0x0, 0x7f, *new_param)); - controller->Button(0, OrbisPadButtonDataOffset::L2, *new_param > 0x20); + ApplyDeadzone(new_param, lefttrigger_deadzone[gamepad_index]); + controller->Axis(c_axis, GetAxis(0x0, 0x7f, *new_param)); + controller->Button(OrbisPadButtonDataOffset::L2, *new_param > 0x20); return; case Axis::TriggerRight: - ApplyDeadzone(new_param, righttrigger_deadzone); - controller->Axis(0, c_axis, GetAxis(0x0, 0x7f, *new_param)); - controller->Button(0, OrbisPadButtonDataOffset::R2, *new_param > 0x20); + ApplyDeadzone(new_param, righttrigger_deadzone[gamepad_index]); + controller->Axis(c_axis, GetAxis(0x0, 0x7f, *new_param)); + controller->Button(OrbisPadButtonDataOffset::R2, *new_param > 0x20); return; default: break; } - controller->Axis(0, c_axis, GetAxis(-0x80, 0x7f, *new_param * multiplier)); + controller->Axis(c_axis, GetAxis(-0x80, 0x7f, *new_param * multiplier)); } } @@ -674,11 +845,9 @@ bool UpdatePressedKeys(InputEvent event) { if (input.type == InputType::Axis) { // analog input, it gets added when it first sends an event, // and from there, it only changes the parameter - auto it = std::lower_bound(pressed_keys.begin(), pressed_keys.end(), input, - [](const std::pair& e, InputID i) { - return std::tie(e.first.input.type, e.first.input.sdl_id) < - std::tie(i.type, i.sdl_id); - }); + auto it = std::lower_bound( + pressed_keys.begin(), pressed_keys.end(), input, + [](const std::pair& e, InputID i) { return e.first.input < i; }); if (it == pressed_keys.end() || it->first.input != input) { pressed_keys.insert(it, {event, false}); LOG_DEBUG(Input, "Added axis {} to the input list", event.input.sdl_id); @@ -791,25 +960,33 @@ InputEvent BindingConnection::ProcessBinding() { } void ActivateOutputsFromInputs() { - // Reset values and flags - for (auto& it : pressed_keys) { - it.second = false; - } - for (auto& it : output_array) { - it.ResetUpdate(); - } - // Check for input blockers - ApplyMouseInputBlockers(); + // todo find a better solution + for (int i = 0; i < output_arrays.size(); i++) { - // Iterate over all inputs, and update their respecive outputs accordingly - for (auto& it : connections) { - it.output->AddUpdate(it.ProcessBinding()); - } + // Reset values and flags + for (auto& it : pressed_keys) { + it.second = false; + } + for (auto& it : output_arrays[i].data) { + it.ResetUpdate(); + } - // Update all outputs - for (auto& it : output_array) { - it.FinalizeUpdate(); + // Check for input blockers + ApplyMouseInputBlockers(); + + // Iterate over all inputs, and update their respecive outputs accordingly + for (auto& it : connections) { + // only update this when it's the correct pass + if (it.output->gamepad_id == i) { + it.output->AddUpdate(it.ProcessBinding()); + } + } + + // Update all outputs + for (auto& it : output_arrays[i].data) { + it.FinalizeUpdate(i); + } } } diff --git a/src/input/input_handler.h b/src/input/input_handler.h index 844870b5d..22ae0f4e0 100644 --- a/src/input/input_handler.h +++ b/src/input/input_handler.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "SDL3/SDL_events.h" #include "SDL3/SDL_timer.h" @@ -35,9 +36,11 @@ #define SDL_EVENT_MOUSE_TO_JOYSTICK SDL_EVENT_USER + 6 #define SDL_EVENT_MOUSE_TO_GYRO SDL_EVENT_USER + 7 #define SDL_EVENT_MOUSE_TO_TOUCHPAD SDL_EVENT_USER + 8 -#define SDL_EVENT_RDOC_CAPTURE SDL_EVENT_USER + 9 -#define SDL_EVENT_QUIT_DIALOG SDL_EVENT_USER + 10 -#define SDL_EVENT_MOUSE_WHEEL_OFF SDL_EVENT_USER + 11 +#define SDL_EVENT_QUIT_DIALOG SDL_EVENT_USER + 9 +#define SDL_EVENT_MOUSE_WHEEL_OFF SDL_EVENT_USER + 10 +#define SDL_EVENT_ADD_VIRTUAL_USER SDL_EVENT_USER + 11 +#define SDL_EVENT_REMOVE_VIRTUAL_USER SDL_EVENT_USER + 12 +#define SDL_EVENT_RDOC_CAPTURE SDL_EVENT_USER + 13 #define LEFTJOYSTICK_HALFMODE 0x00010000 #define RIGHTJOYSTICK_HALFMODE 0x00020000 @@ -57,6 +60,8 @@ #define HOTKEY_RENDERDOC 0xf0000009 #define HOTKEY_VOLUME_UP 0xf000000a #define HOTKEY_VOLUME_DOWN 0xf000000b +#define HOTKEY_ADD_VIRTUAL_USER 0xf000000c +#define HOTKEY_REMOVE_VIRTUAL_USER 0xf000000d #define SDL_UNMAPPED UINT32_MAX - 1 @@ -77,21 +82,24 @@ class InputID { public: InputType type; u32 sdl_id; - InputID(InputType d = InputType::Count, u32 i = SDL_UNMAPPED) : type(d), sdl_id(i) {} + u8 gamepad_id; + InputID(InputType d = InputType::Count, u32 i = (u32)-1, u8 g = 1) + : type(d), sdl_id(i), gamepad_id(g) {} bool operator==(const InputID& o) const { - return type == o.type && sdl_id == o.sdl_id; + return type == o.type && sdl_id == o.sdl_id && gamepad_id == o.gamepad_id; } bool operator!=(const InputID& o) const { - return type != o.type || sdl_id != o.sdl_id; + return type != o.type || sdl_id != o.sdl_id || gamepad_id != o.gamepad_id; } - bool operator<=(const InputID& o) const { - return type <= o.type && sdl_id <= o.sdl_id; + auto operator<=>(const InputID& o) const { + return std::tie(gamepad_id, type, sdl_id, gamepad_id) <=> + std::tie(o.gamepad_id, o.type, o.sdl_id, o.gamepad_id); } bool IsValid() const { return *this != InputID(); } std::string ToString() { - return fmt::format("({}: {:x})", input_type_names[static_cast(type)], sdl_id); + return fmt::format("({}. {}: {:x})", gamepad_id, input_type_names[(u8)type], sdl_id); } }; @@ -149,6 +157,8 @@ const std::map string_to_hotkey_map = { {"hotkey_toggle_mouse_to_gyro", HOTKEY_TOGGLE_MOUSE_TO_GYRO}, {"hotkey_toggle_mouse_to_touchpad", HOTKEY_TOGGLE_MOUSE_TO_TOUCHPAD}, {"hotkey_renderdoc_capture", HOTKEY_RENDERDOC}, + {"hotkey_add_virtual_user", HOTKEY_ADD_VIRTUAL_USER}, + {"hotkey_remove_virtual_user", HOTKEY_REMOVE_VIRTUAL_USER}, {"hotkey_volume_up", HOTKEY_VOLUME_UP}, {"hotkey_volume_down", HOTKEY_VOLUME_DOWN}, }; @@ -401,7 +411,7 @@ public: inline bool IsEmpty() { return !(keys[0].IsValid() || keys[1].IsValid() || keys[2].IsValid()); } - std::string ToString() { // todo add device type + std::string ToString() { switch (KeyCount()) { case 1: return fmt::format("({})", keys[0].ToString()); @@ -420,14 +430,14 @@ public: }; class ControllerOutput { - static GameController* controller; - public: - static void SetControllerOutputController(GameController* c); + static GameControllers controllers; + static void GetGetGamepadIndexFromSDLJoystickID(const SDL_JoystickID id) {} static void LinkJoystickAxes(); u32 button; u32 axis; + u8 gamepad_id; // these are only used as s8, // but I added some padding to avoid overflow if it's activated by multiple inputs // axis_plus and axis_minus pairs share a common new_param, the other outputs have their own @@ -441,6 +451,7 @@ public: new_param = new s16(0); old_param = 0; positive_axis = p; + gamepad_id = 0; } ControllerOutput(const ControllerOutput& o) : button(o.button), axis(o.axis) { new_param = new s16(*o.new_param); @@ -466,7 +477,7 @@ public: void ResetUpdate(); void AddUpdate(InputEvent event); - void FinalizeUpdate(); + void FinalizeUpdate(u8 gamepad_index); }; class BindingConnection { public: @@ -481,6 +492,13 @@ public: output = out; toggle = t; } + BindingConnection& operator=(const BindingConnection& o) { + binding = o.binding; + output = o.output; + axis_param = o.axis_param; + toggle = o.toggle; + return *this; + } bool operator<(const BindingConnection& other) const { // a button is a higher priority than an axis, as buttons can influence axes // (e.g. joystick_halfmode) @@ -494,9 +512,81 @@ public: } return false; } + bool HasGamepadInput() { + for (auto& key : binding.keys) { + if (key.type == InputType::Controller || key.type == InputType::Axis) { + return true; + } + } + return false; + } + BindingConnection CopyWithChangedGamepadId(u8 gamepad); InputEvent ProcessBinding(); }; +class ControllerAllOutputs { +public: + static constexpr u64 output_count = 39; + std::array data = { + // Important: these have to be the first, or else they will update in the wrong order + ControllerOutput(LEFTJOYSTICK_HALFMODE), + ControllerOutput(RIGHTJOYSTICK_HALFMODE), + ControllerOutput(KEY_TOGGLE), + ControllerOutput(MOUSE_GYRO_ROLL_MODE), + + // Button mappings + ControllerOutput(SDL_GAMEPAD_BUTTON_NORTH), // Triangle + ControllerOutput(SDL_GAMEPAD_BUTTON_EAST), // Circle + ControllerOutput(SDL_GAMEPAD_BUTTON_SOUTH), // Cross + ControllerOutput(SDL_GAMEPAD_BUTTON_WEST), // Square + ControllerOutput(SDL_GAMEPAD_BUTTON_LEFT_SHOULDER), // L1 + ControllerOutput(SDL_GAMEPAD_BUTTON_LEFT_STICK), // L3 + ControllerOutput(SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER), // R1 + ControllerOutput(SDL_GAMEPAD_BUTTON_RIGHT_STICK), // R3 + ControllerOutput(SDL_GAMEPAD_BUTTON_START), // Options + ControllerOutput(SDL_GAMEPAD_BUTTON_TOUCHPAD_LEFT), // TouchPad + ControllerOutput(SDL_GAMEPAD_BUTTON_TOUCHPAD_CENTER), // TouchPad + ControllerOutput(SDL_GAMEPAD_BUTTON_TOUCHPAD_RIGHT), // TouchPad + ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_UP), // Up + ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_DOWN), // Down + ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_LEFT), // Left + ControllerOutput(SDL_GAMEPAD_BUTTON_DPAD_RIGHT), // Right + + // Axis mappings + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX, false), + // ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY, false), + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTX), + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFTY), + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTX), + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY), + + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFT_TRIGGER), + + ControllerOutput(HOTKEY_FULLSCREEN), + ControllerOutput(HOTKEY_PAUSE), + ControllerOutput(HOTKEY_SIMPLE_FPS), + ControllerOutput(HOTKEY_QUIT), + ControllerOutput(HOTKEY_RELOAD_INPUTS), + ControllerOutput(HOTKEY_TOGGLE_MOUSE_TO_JOYSTICK), + ControllerOutput(HOTKEY_TOGGLE_MOUSE_TO_GYRO), + ControllerOutput(HOTKEY_TOGGLE_MOUSE_TO_TOUCHPAD), + ControllerOutput(HOTKEY_RENDERDOC), + ControllerOutput(HOTKEY_ADD_VIRTUAL_USER), + ControllerOutput(HOTKEY_REMOVE_VIRTUAL_USER), + ControllerOutput(HOTKEY_VOLUME_UP), + ControllerOutput(HOTKEY_VOLUME_DOWN), + + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_INVALID), + }; + ControllerAllOutputs(u8 g) { + for (int i = 0; i < output_count; i++) { + data[i].gamepad_id = g; + } + } +}; + // Updates the list of pressed keys with the given input. // Returns whether the list was updated or not. bool UpdatePressedKeys(InputEvent event); diff --git a/src/input/input_mouse.cpp b/src/input/input_mouse.cpp index 19daab3d6..f90c20484 100644 --- a/src/input/input_mouse.cpp +++ b/src/input/input_mouse.cpp @@ -77,11 +77,11 @@ void EmulateJoystick(GameController* controller, u32 interval) { float a_x = cos(angle) * output_speed, a_y = sin(angle) * output_speed; if (d_x != 0 || d_y != 0) { - controller->Axis(0, axis_x, GetAxis(-0x80, 0x7f, a_x)); - controller->Axis(0, axis_y, GetAxis(-0x80, 0x7f, a_y)); + controller->Axis(axis_x, GetAxis(-0x80, 0x7f, a_x), false); + controller->Axis(axis_y, GetAxis(-0x80, 0x7f, a_y), false); } else { - controller->Axis(0, axis_x, GetAxis(-0x80, 0x7f, 0)); - controller->Axis(0, axis_y, GetAxis(-0x80, 0x7f, 0)); + controller->Axis(axis_x, GetAxis(-0x80, 0x7f, 0), false); + controller->Axis(axis_y, GetAxis(-0x80, 0x7f, 0), false); } } @@ -89,13 +89,13 @@ constexpr float constant_down_accel[3] = {0.0f, 9.81f, 0.0f}; void EmulateGyro(GameController* controller, u32 interval) { float d_x = 0, d_y = 0; SDL_GetRelativeMouseState(&d_x, &d_y); - controller->Acceleration(1, constant_down_accel); + controller->UpdateAcceleration(constant_down_accel); float gyro_from_mouse[3] = {-d_y / 100, -d_x / 100, 0.0f}; if (mouse_gyro_roll_mode) { gyro_from_mouse[1] = 0.0f; gyro_from_mouse[2] = -d_x / 100; } - controller->Gyro(1, gyro_from_mouse); + controller->UpdateGyro(gyro_from_mouse); } void EmulateTouchpad(GameController* controller, u32 interval) { @@ -104,7 +104,7 @@ void EmulateTouchpad(GameController* controller, u32 interval) { controller->SetTouchpadState(0, (mouse_buttons & SDL_BUTTON_LMASK) != 0, std::clamp(x / g_window->GetWidth(), 0.0f, 1.0f), std::clamp(y / g_window->GetHeight(), 0.0f, 1.0f)); - controller->Button(0, Libraries::Pad::OrbisPadButtonDataOffset::TouchPad, + controller->Button(Libraries::Pad::OrbisPadButtonDataOffset::TouchPad, (mouse_buttons & SDL_BUTTON_RMASK) != 0); } diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index 89b65f3dc..766a336c2 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -8,12 +8,14 @@ #include "SDL3/SDL_timer.h" #include "SDL3/SDL_video.h" #include "common/assert.h" -#include "common/config.h" #include "common/elf_info.h" #include "core/debug_state.h" #include "core/devtools/layer.h" +#include "core/emulator_settings.h" #include "core/libraries/kernel/time.h" #include "core/libraries/pad/pad.h" +#include "core/libraries/system/userservice.h" +#include "core/user_settings.h" #include "imgui/renderer/imgui_core.h" #include "input/controller.h" #include "input/input_handler.h" @@ -26,9 +28,9 @@ #endif #include -namespace Input { +namespace Frontend { -using Libraries::Pad::OrbisPadButtonDataOffset; +using namespace Libraries::Pad; static OrbisPadButtonDataOffset SDLGamepadToOrbisButton(u8 button) { using OPBDO = OrbisPadButtonDataOffset; @@ -69,220 +71,24 @@ static OrbisPadButtonDataOffset SDLGamepadToOrbisButton(u8 button) { } } -static SDL_GamepadAxis InputAxisToSDL(Axis axis) { - switch (axis) { - case Axis::LeftX: - return SDL_GAMEPAD_AXIS_LEFTX; - case Axis::LeftY: - return SDL_GAMEPAD_AXIS_LEFTY; - case Axis::RightX: - return SDL_GAMEPAD_AXIS_RIGHTX; - case Axis::RightY: - return SDL_GAMEPAD_AXIS_RIGHTY; - case Axis::TriggerLeft: - return SDL_GAMEPAD_AXIS_LEFT_TRIGGER; - case Axis::TriggerRight: - return SDL_GAMEPAD_AXIS_RIGHT_TRIGGER; - default: - UNREACHABLE(); - } -} - -SDLInputEngine::~SDLInputEngine() { - if (m_gamepad) { - SDL_CloseGamepad(m_gamepad); - } -} - -void SDLInputEngine::Init() { - if (m_gamepad) { - SDL_CloseGamepad(m_gamepad); - m_gamepad = nullptr; - } - - int gamepad_count; - SDL_JoystickID* gamepads = SDL_GetGamepads(&gamepad_count); - if (!gamepads) { - LOG_ERROR(Input, "Cannot get gamepad list: {}", SDL_GetError()); - return; - } - if (gamepad_count == 0) { - LOG_INFO(Input, "No gamepad found!"); - SDL_free(gamepads); - return; - } - - int selectedIndex = GamepadSelect::GetIndexfromGUID(gamepads, gamepad_count, - GamepadSelect::GetSelectedGamepad()); - int defaultIndex = - GamepadSelect::GetIndexfromGUID(gamepads, gamepad_count, Config::getDefaultControllerID()); - - // If user selects a gamepad in the GUI, use that, otherwise try the default - if (!m_gamepad) { - if (selectedIndex != -1) { - m_gamepad = SDL_OpenGamepad(gamepads[selectedIndex]); - LOG_INFO(Input, "Opening gamepad selected in GUI."); - } else if (defaultIndex != -1) { - m_gamepad = SDL_OpenGamepad(gamepads[defaultIndex]); - LOG_INFO(Input, "Opening default gamepad."); - } else { - m_gamepad = SDL_OpenGamepad(gamepads[0]); - LOG_INFO(Input, "Got {} gamepads. Opening the first one.", gamepad_count); - } - } - - if (!m_gamepad) { - if (!m_gamepad) { - LOG_ERROR(Input, "Failed to open gamepad: {}", SDL_GetError()); - SDL_free(gamepads); - return; - } - } - - SDL_Joystick* joystick = SDL_GetGamepadJoystick(m_gamepad); - Uint16 vendor = SDL_GetJoystickVendor(joystick); - Uint16 product = SDL_GetJoystickProduct(joystick); - - bool isDualSense = (vendor == 0x054C && product == 0x0CE6); - - LOG_INFO(Input, "Gamepad Vendor: {:04X}, Product: {:04X}", vendor, product); - if (isDualSense) { - LOG_INFO(Input, "Detected DualSense Controller"); - } - - if (Config::getIsMotionControlsEnabled()) { - if (SDL_SetGamepadSensorEnabled(m_gamepad, SDL_SENSOR_GYRO, true)) { - m_gyro_poll_rate = SDL_GetGamepadSensorDataRate(m_gamepad, SDL_SENSOR_GYRO); - LOG_INFO(Input, "Gyro initialized, poll rate: {}", m_gyro_poll_rate); - } else { - LOG_ERROR(Input, "Failed to initialize gyro controls for gamepad, error: {}", - SDL_GetError()); - SDL_SetGamepadSensorEnabled(m_gamepad, SDL_SENSOR_GYRO, false); - } - if (SDL_SetGamepadSensorEnabled(m_gamepad, SDL_SENSOR_ACCEL, true)) { - m_accel_poll_rate = SDL_GetGamepadSensorDataRate(m_gamepad, SDL_SENSOR_ACCEL); - LOG_INFO(Input, "Accel initialized, poll rate: {}", m_accel_poll_rate); - } else { - LOG_ERROR(Input, "Failed to initialize accel controls for gamepad, error: {}", - SDL_GetError()); - SDL_SetGamepadSensorEnabled(m_gamepad, SDL_SENSOR_ACCEL, false); - } - } - - SDL_free(gamepads); - - int* rgb = Config::GetControllerCustomColor(); - - if (isDualSense) { - if (SDL_SetJoystickLED(joystick, rgb[0], rgb[1], rgb[2]) == 0) { - LOG_INFO(Input, "Set DualSense LED to R:{} G:{} B:{}", rgb[0], rgb[1], rgb[2]); - } else { - LOG_ERROR(Input, "Failed to set DualSense LED: {}", SDL_GetError()); - } - } else { - SetLightBarRGB(rgb[0], rgb[1], rgb[2]); - } -} - -void SDLInputEngine::SetLightBarRGB(u8 r, u8 g, u8 b) { - if (m_gamepad) { - SDL_SetGamepadLED(m_gamepad, r, g, b); - } -} - -void SDLInputEngine::SetVibration(u8 smallMotor, u8 largeMotor) { - if (m_gamepad) { - const auto low_freq = (smallMotor / 255.0f) * 0xFFFF; - const auto high_freq = (largeMotor / 255.0f) * 0xFFFF; - SDL_RumbleGamepad(m_gamepad, low_freq, high_freq, -1); - } -} - -State SDLInputEngine::ReadState() { - State state{}; - state.time = Libraries::Kernel::sceKernelGetProcessTime(); - - // Buttons - for (u8 i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) { - auto orbisButton = SDLGamepadToOrbisButton(i); - if (orbisButton == OrbisPadButtonDataOffset::None) { - continue; - } - state.OnButton(orbisButton, SDL_GetGamepadButton(m_gamepad, (SDL_GamepadButton)i)); - } - - // Axes - for (int i = 0; i < static_cast(Axis::AxisMax); ++i) { - const auto axis = static_cast(i); - const auto value = SDL_GetGamepadAxis(m_gamepad, InputAxisToSDL(axis)); - switch (axis) { - case Axis::TriggerLeft: - case Axis::TriggerRight: - state.OnAxis(axis, GetAxis(0, 0x8000, value)); - break; - default: - state.OnAxis(axis, GetAxis(-0x8000, 0x8000, value)); - break; - } - } - - // Touchpad - if (SDL_GetNumGamepadTouchpads(m_gamepad) > 0) { - for (int finger = 0; finger < 2; ++finger) { - bool down; - float x, y; - if (SDL_GetGamepadTouchpadFinger(m_gamepad, 0, finger, &down, &x, &y, NULL)) { - state.OnTouchpad(finger, down, x, y); - } - } - } - - // Gyro - if (SDL_GamepadHasSensor(m_gamepad, SDL_SENSOR_GYRO)) { - float gyro[3]; - if (SDL_GetGamepadSensorData(m_gamepad, SDL_SENSOR_GYRO, gyro, 3)) { - state.OnGyro(gyro); - } - } - - // Accel - if (SDL_GamepadHasSensor(m_gamepad, SDL_SENSOR_ACCEL)) { - float accel[3]; - if (SDL_GetGamepadSensorData(m_gamepad, SDL_SENSOR_ACCEL, accel, 3)) { - state.OnAccel(accel); - } - } - - return state; -} - -float SDLInputEngine::GetGyroPollRate() const { - return m_gyro_poll_rate; -} - -float SDLInputEngine::GetAccelPollRate() const { - return m_accel_poll_rate; -} - -} // namespace Input - -namespace Frontend { - -using namespace Libraries::Pad; - -std::mutex motion_control_mutex; -float gyro_buf[3] = {0.0f, 0.0f, 0.0f}, accel_buf[3] = {0.0f, 9.81f, 0.0f}; -static Uint32 SDLCALL PollGyroAndAccel(void* userdata, SDL_TimerID timer_id, Uint32 interval) { +static Uint32 SDLCALL PollController(void* userdata, SDL_TimerID timer_id, Uint32 interval) { auto* controller = reinterpret_cast(userdata); - std::scoped_lock l{motion_control_mutex}; - controller->Gyro(0, gyro_buf); - controller->Acceleration(0, accel_buf); - return 4; + controller->UpdateAxisSmoothing(); + controller->Gyro(0); + controller->Acceleration(0); + return interval; } -WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_, +static Uint32 SDLCALL PollControllerLightColour(void* userdata, SDL_TimerID timer_id, + Uint32 interval) { + auto* controller = reinterpret_cast(userdata); + controller->PollLightColour(); + return interval; +} + +WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameControllers* controllers_, std::string_view window_title) - : width{width_}, height{height_}, controller{controller_} { + : width{width_}, height{height_}, controllers{*controllers_} { if (!SDL_SetHint(SDL_HINT_APP_NAME, "shadPS4")) { UNREACHABLE_MSG("Failed to set SDL window hint: {}", SDL_GetError()); } @@ -330,7 +136,6 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_ SDL_SyncWindow(window); SDL_InitSubSystem(SDL_INIT_GAMEPAD); - controller->SetEngine(std::make_unique()); #if defined(SDL_PLATFORM_WIN32) window_info.type = WindowSystemType::Windows; @@ -355,11 +160,11 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_ window_info.render_surface = SDL_Metal_GetLayer(SDL_Metal_CreateView(window)); #endif // input handler init-s - Input::ControllerOutput::SetControllerOutputController(controller); Input::ControllerOutput::LinkJoystickAxes(); Input::ParseInputConfig(std::string(Common::ElfInfo::Instance().GameSerial())); + controllers.TryOpenSDLControllers(); - if (Config::getBackgroundControllerInput()) { + if (EmulatorSettings.IsBackgroundControllerInput()) { SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); } } @@ -399,37 +204,16 @@ void WindowSDL::WaitEvent() { break; case SDL_EVENT_GAMEPAD_ADDED: case SDL_EVENT_GAMEPAD_REMOVED: - controller->SetEngine(std::make_unique()); - break; - case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: - case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: - case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION: - controller->SetTouchpadState(event.gtouchpad.finger, - event.type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP, event.gtouchpad.x, - event.gtouchpad.y); + controllers.TryOpenSDLControllers(); break; case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_AXIS_MOTION: - OnGamepadEvent(&event); - break; - // i really would have appreciated ANY KIND OF DOCUMENTATION ON THIS - // AND IT DOESN'T EVEN USE PROPER ENUMS + case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: + case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: + case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION: case SDL_EVENT_GAMEPAD_SENSOR_UPDATE: - switch ((SDL_SensorType)event.gsensor.sensor) { - case SDL_SENSOR_GYRO: { - std::scoped_lock l{motion_control_mutex}; - memcpy(gyro_buf, event.gsensor.data, sizeof(gyro_buf)); - break; - } - case SDL_SENSOR_ACCEL: { - std::scoped_lock l{motion_control_mutex}; - memcpy(accel_buf, event.gsensor.data, sizeof(accel_buf)); - break; - } - default: - break; - } + OnGamepadEvent(&event); break; case SDL_EVENT_QUIT: is_open = false; @@ -455,7 +239,7 @@ void WindowSDL::WaitEvent() { } break; case SDL_EVENT_CHANGE_CONTROLLER: - controller->GetEngine()->Init(); + UNREACHABLE_MSG("todo"); break; case SDL_EVENT_TOGGLE_SIMPLE_FPS: Overlay::ToggleSimpleFps(); @@ -476,6 +260,29 @@ void WindowSDL::WaitEvent() { Input::ToggleMouseModeTo(Input::MouseMode::Touchpad)); SDL_SetWindowRelativeMouseMode(this->GetSDLWindow(), false); break; + case SDL_EVENT_ADD_VIRTUAL_USER: + for (int i = 0; i < 4; i++) { + if (controllers[i]->user_id == -1) { + auto u = UserManagement.GetUserByPlayerIndex(i + 1); + if (!u) { + break; + } + controllers[i]->user_id = u->user_id; + UserManagement.LoginUser(u, i + 1); + break; + } + } + break; + case SDL_EVENT_REMOVE_VIRTUAL_USER: + LOG_INFO(Input, "Remove user"); + for (int i = 3; i >= 0; i--) { + if (controllers[i]->user_id != -1) { + UserManagement.LogoutUser(UserManagement.GetUserByID(controllers[i]->user_id)); + controllers[i]->user_id = -1; + break; + } + } + break; case SDL_EVENT_RDOC_CAPTURE: VideoCore::TriggerCapture(); break; @@ -485,8 +292,10 @@ void WindowSDL::WaitEvent() { } void WindowSDL::InitTimers() { - SDL_AddTimer(4, &PollGyroAndAccel, controller); - SDL_AddTimer(33, Input::MousePolling, (void*)controller); + for (int i = 0; i < 4; ++i) { + SDL_AddTimer(4, &PollController, controllers[i]); + } + SDL_AddTimer(33, Input::MousePolling, (void*)controllers[0]); } void WindowSDL::RequestKeyboard() { @@ -554,10 +363,44 @@ void WindowSDL::OnGamepadEvent(const SDL_Event* event) { // as it would break the entire touchpad handling // You can still bind other things to it though if (event->gbutton.button == SDL_GAMEPAD_BUTTON_TOUCHPAD) { - controller->Button(0, OrbisPadButtonDataOffset::TouchPad, input_down); + controllers[controllers.GetGamepadIndexFromJoystickId(event->gbutton.which)]->Button( + OrbisPadButtonDataOffset::TouchPad, input_down); return; } + u8 gamepad; + + switch (event->type) { + case SDL_EVENT_GAMEPAD_SENSOR_UPDATE: + switch ((SDL_SensorType)event->gsensor.sensor) { + case SDL_SENSOR_GYRO: + gamepad = controllers.GetGamepadIndexFromJoystickId(event->gsensor.which); + if (gamepad < 5) { + controllers[gamepad]->UpdateGyro(event->gsensor.data); + } + break; + case SDL_SENSOR_ACCEL: + gamepad = controllers.GetGamepadIndexFromJoystickId(event->gsensor.which); + if (gamepad < 5) { + controllers[gamepad]->UpdateAcceleration(event->gsensor.data); + } + break; + default: + break; + } + return; + case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: + case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: + case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION: + controllers[controllers.GetGamepadIndexFromJoystickId(event->gtouchpad.which)] + ->SetTouchpadState(event->gtouchpad.finger, + event->type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP, event->gtouchpad.x, + event->gtouchpad.y); + return; + default: + break; + } + // add/remove it from the list bool inputs_changed = Input::UpdatePressedKeys(input_event); diff --git a/src/sdl_window.h b/src/sdl_window.h index 3a4341de5..4fc750bbc 100644 --- a/src/sdl_window.h +++ b/src/sdl_window.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #pragma once @@ -14,23 +14,8 @@ struct SDL_Gamepad; union SDL_Event; namespace Input { - -class SDLInputEngine : public Engine { -public: - ~SDLInputEngine() override; - void Init() override; - void SetLightBarRGB(u8 r, u8 g, u8 b) override; - void SetVibration(u8 smallMotor, u8 largeMotor) override; - float GetGyroPollRate() const override; - float GetAccelPollRate() const override; - State ReadState() override; - -private: - float m_gyro_poll_rate = 0.0f; - float m_accel_poll_rate = 0.0f; -}; - -} // namespace Input +class GameController; +} namespace Frontend { @@ -62,7 +47,7 @@ class WindowSDL { int keyboard_grab = 0; public: - explicit WindowSDL(s32 width, s32 height, Input::GameController* controller, + explicit WindowSDL(s32 width, s32 height, Input::GameControllers* controllers, std::string_view window_title); ~WindowSDL(); @@ -100,7 +85,7 @@ private: private: s32 width; s32 height; - Input::GameController* controller; + Input::GameControllers controllers{}; WindowSystemInfo window_info{}; SDL_Window* window{}; bool is_shown{}; From 5b60b73e9a6fa1a1b6eac5487085e35109114965 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Fri, 27 Mar 2026 17:58:54 +0200 Subject: [PATCH 07/14] added new trophies and saves dirs (#4177) --- src/core/file_format/trp.cpp | 150 ++-- src/core/file_format/trp.h | 8 +- src/core/libraries/np/np_manager.cpp | 12 +- src/core/libraries/np/np_trophy.cpp | 771 ++++++++++++------ src/core/libraries/np/np_trophy.h | 2 - .../libraries/save_data/save_instance.cpp | 12 +- src/core/libraries/save_data/savedata.cpp | 12 +- src/emulator.cpp | 53 +- 8 files changed, 635 insertions(+), 385 deletions(-) diff --git a/src/core/file_format/trp.cpp b/src/core/file_format/trp.cpp index f0a258c12..6269fc6c7 100644 --- a/src/core/file_format/trp.cpp +++ b/src/core/file_format/trp.cpp @@ -5,7 +5,6 @@ #include "common/key_manager.h" #include "common/logging/log.h" #include "common/path_util.h" -#include "core/file_format/npbind.h" #include "core/file_format/trp.h" static void DecryptEFSM(std::span trophyKey, std::span NPcommID, @@ -43,8 +42,10 @@ static void hexToBytes(const char* hex, unsigned char* dst) { } } -bool TRP::Extract(const std::filesystem::path& trophyPath, const std::string titleId) { - std::filesystem::path gameSysDir = trophyPath / "sce_sys/trophy/"; +bool TRP::Extract(const std::filesystem::path& trophyPath, int index, std::string npCommId, + const std::filesystem::path& outputPath) { + std::filesystem::path gameSysDir = + trophyPath / "sce_sys/trophy/" / std::format("trophy{:02d}.trp", index); if (!std::filesystem::exists(gameSysDir)) { LOG_WARNING(Common_Filesystem, "Game trophy directory doesn't exist"); return false; @@ -61,117 +62,82 @@ bool TRP::Extract(const std::filesystem::path& trophyPath, const std::string tit std::array user_key{}; std::copy(user_key_vec.begin(), user_key_vec.end(), user_key.begin()); - // Load npbind.dat using the new class - std::filesystem::path npbindPath = trophyPath / "sce_sys/npbind.dat"; - NPBindFile npbind; - if (!npbind.Load(npbindPath.string())) { - LOG_WARNING(Common_Filesystem, "Failed to load npbind.dat file"); - } - - auto npCommIds = npbind.GetNpCommIds(); - if (npCommIds.empty()) { - LOG_WARNING(Common_Filesystem, "No NPComm IDs found in npbind.dat"); - } - bool success = true; int trpFileIndex = 0; try { - // Process each TRP file in the trophy directory - for (const auto& it : std::filesystem::directory_iterator(gameSysDir)) { - if (!it.is_regular_file() || it.path().extension() != ".trp") { - continue; // Skip non-TRP files - } + const auto& it = gameSysDir; + if (it.extension() != ".trp") { + return false; + } + Common::FS::IOFile file(it, Common::FS::FileAccessMode::Read); + if (!file.IsOpen()) { + LOG_ERROR(Common_Filesystem, "Unable to open trophy file: {}", it.string()); + return false; + } - // Get NPCommID for this TRP file (if available) - std::string npCommId; - if (trpFileIndex < static_cast(npCommIds.size())) { - npCommId = npCommIds[trpFileIndex]; - LOG_DEBUG(Common_Filesystem, "Using NPCommID: {} for {}", npCommId, - it.path().filename().string()); - } else { - LOG_WARNING(Common_Filesystem, "No NPCommID found for TRP file index {}", - trpFileIndex); - } + TrpHeader header; + if (!file.Read(header)) { + LOG_ERROR(Common_Filesystem, "Failed to read TRP header from {}", it.string()); + return false; + } - Common::FS::IOFile file(it.path(), Common::FS::FileAccessMode::Read); - if (!file.IsOpen()) { - LOG_ERROR(Common_Filesystem, "Unable to open trophy file: {}", it.path().string()); + if (header.magic != TRP_MAGIC) { + LOG_ERROR(Common_Filesystem, "Wrong trophy magic number in {}", it.string()); + return false; + } + + s64 seekPos = sizeof(TrpHeader); + // Create output directories + if (!std::filesystem::create_directories(outputPath / "Icons") || + !std::filesystem::create_directories(outputPath / "Xml")) { + LOG_ERROR(Common_Filesystem, "Failed to create output directories for {}", npCommId); + return false; + } + + // Process each entry in the TRP file + for (int i = 0; i < header.entry_num; i++) { + if (!file.Seek(seekPos)) { + LOG_ERROR(Common_Filesystem, "Failed to seek to TRP entry offset"); success = false; - continue; + break; } + seekPos += static_cast(header.entry_size); - TrpHeader header; - if (!file.Read(header)) { - LOG_ERROR(Common_Filesystem, "Failed to read TRP header from {}", - it.path().string()); + TrpEntry entry; + if (!file.Read(entry)) { + LOG_ERROR(Common_Filesystem, "Failed to read TRP entry"); success = false; - continue; + break; } - if (header.magic != TRP_MAGIC) { - LOG_ERROR(Common_Filesystem, "Wrong trophy magic number in {}", it.path().string()); - success = false; - continue; - } + std::string_view name(entry.entry_name); - s64 seekPos = sizeof(TrpHeader); - std::filesystem::path trpFilesPath( - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / titleId / - "TrophyFiles" / it.path().stem()); - - // Create output directories - if (!std::filesystem::create_directories(trpFilesPath / "Icons") || - !std::filesystem::create_directories(trpFilesPath / "Xml")) { - LOG_ERROR(Common_Filesystem, "Failed to create output directories for {}", titleId); - success = false; - continue; - } - - // Process each entry in the TRP file - for (int i = 0; i < header.entry_num; i++) { - if (!file.Seek(seekPos)) { - LOG_ERROR(Common_Filesystem, "Failed to seek to TRP entry offset"); + if (entry.flag == ENTRY_FLAG_PNG) { + if (!ProcessPngEntry(file, entry, outputPath, name)) { success = false; - break; + // Continue with next entry } - seekPos += static_cast(header.entry_size); - - TrpEntry entry; - if (!file.Read(entry)) { - LOG_ERROR(Common_Filesystem, "Failed to read TRP entry"); - success = false; - break; - } - - std::string_view name(entry.entry_name); - - if (entry.flag == ENTRY_FLAG_PNG) { - if (!ProcessPngEntry(file, entry, trpFilesPath, name)) { + } else if (entry.flag == ENTRY_FLAG_ENCRYPTED_XML) { + // Check if we have a valid NPCommID for decryption + if (npCommId.size() >= 12 && npCommId[0] == 'N' && npCommId[1] == 'P') { + if (!ProcessEncryptedXmlEntry(file, entry, outputPath, name, user_key, + npCommId)) { success = false; // Continue with next entry } - } else if (entry.flag == ENTRY_FLAG_ENCRYPTED_XML) { - // Check if we have a valid NPCommID for decryption - if (npCommId.size() >= 12 && npCommId[0] == 'N' && npCommId[1] == 'P') { - if (!ProcessEncryptedXmlEntry(file, entry, trpFilesPath, name, user_key, - npCommId)) { - success = false; - // Continue with next entry - } - } else { - LOG_WARNING(Common_Filesystem, - "Skipping encrypted XML entry - invalid NPCommID"); - // Skip this entry but continue - } } else { - LOG_DEBUG(Common_Filesystem, "Unknown entry flag: {} for {}", - static_cast(entry.flag), name); + LOG_WARNING(Common_Filesystem, + "Skipping encrypted XML entry - invalid NPCommID"); + // Skip this entry but continue } + } else { + LOG_DEBUG(Common_Filesystem, "Unknown entry flag: {} for {}", + static_cast(entry.flag), name); } - trpFileIndex++; } + } catch (const std::filesystem::filesystem_error& e) { LOG_CRITICAL(Common_Filesystem, "Filesystem error during trophy extraction: {}", e.what()); return false; @@ -182,7 +148,7 @@ bool TRP::Extract(const std::filesystem::path& trophyPath, const std::string tit if (success) { LOG_INFO(Common_Filesystem, "Successfully extracted {} trophy files for {}", trpFileIndex, - titleId); + npCommId); } return success; diff --git a/src/core/file_format/trp.h b/src/core/file_format/trp.h index 2b52a4d57..df0ea6eaf 100644 --- a/src/core/file_format/trp.h +++ b/src/core/file_format/trp.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2024-2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #pragma once @@ -36,7 +36,8 @@ class TRP { public: TRP(); ~TRP(); - bool Extract(const std::filesystem::path& trophyPath, const std::string titleId); + bool Extract(const std::filesystem::path& trophyPath, int index, std::string npCommId, + const std::filesystem::path& outputPath); private: bool ProcessPngEntry(Common::FS::IOFile& file, const TrpEntry& entry, @@ -45,9 +46,6 @@ private: const std::filesystem::path& outputPath, std::string_view name, const std::array& user_key, const std::string& npCommId); - std::vector NPcommID = std::vector(12); - std::array np_comm_id{}; std::array esfmIv{}; - std::filesystem::path trpFilesPath; static constexpr int iv_len = 16; }; diff --git a/src/core/libraries/np/np_manager.cpp b/src/core/libraries/np/np_manager.cpp index 62cad455b..0ffbb682a 100644 --- a/src/core/libraries/np/np_manager.cpp +++ b/src/core/libraries/np/np_manager.cpp @@ -1,13 +1,13 @@ -// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include #include #include -#include -#include "common/config.h" +#include #include "common/logging/log.h" +#include "core/emulator_settings.h" #include "core/libraries/error_codes.h" #include "core/libraries/libs.h" #include "core/libraries/np/np_error.h" @@ -632,7 +632,8 @@ s32 PS4_SYSV_ABI sceNpGetNpId(Libraries::UserService::OrbisUserServiceUserId use return ORBIS_NP_ERROR_SIGNED_OUT; } memset(np_id, 0, sizeof(OrbisNpId)); - strncpy(np_id->handle.data, Config::getUserName().c_str(), sizeof(np_id->handle.data)); + strncpy(np_id->handle.data, UserManagement.GetDefaultUser().user_name.c_str(), + sizeof(np_id->handle.data)); return ORBIS_OK; } @@ -646,7 +647,8 @@ s32 PS4_SYSV_ABI sceNpGetOnlineId(Libraries::UserService::OrbisUserServiceUserId return ORBIS_NP_ERROR_SIGNED_OUT; } memset(online_id, 0, sizeof(OrbisNpOnlineId)); - strncpy(online_id->data, Config::getUserName().c_str(), sizeof(online_id->data)); + strncpy(online_id->data, UserManagement.GetDefaultUser().user_name.c_str(), + sizeof(online_id->data)); return ORBIS_OK; } diff --git a/src/core/libraries/np/np_trophy.cpp b/src/core/libraries/np/np_trophy.cpp index 976d614c0..287d8d295 100644 --- a/src/core/libraries/np/np_trophy.cpp +++ b/src/core/libraries/np/np_trophy.cpp @@ -1,22 +1,118 @@ -// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2025-2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include #include +#include "common/elf_info.h" #include "common/logging/log.h" #include "common/path_util.h" #include "common/slot_vector.h" +#include "core/emulator_settings.h" #include "core/libraries/libs.h" #include "core/libraries/np/np_error.h" #include "core/libraries/np/np_trophy.h" #include "core/libraries/np/np_trophy_error.h" #include "core/libraries/np/trophy_ui.h" +#include "core/libraries/system/userservice.h" #include "core/memory.h" namespace Libraries::Np::NpTrophy { -std::string game_serial; +// PS4 system language IDs map directly to TROP00.XML .. TROP30.XML. +// Index = OrbisSystemServiceParamId language value reported by the system. +// clang-format off +static constexpr std::array s_language_xml_names = { + "TROP_00.XML", // 00 Japanese + "TROP_01.XML", // 01 English (US) + "TROP_02.XML", // 02 French + "TROP_03.XML", // 03 Spanish (ES) + "TROP_04.XML", // 04 German + "TROP_05.XML", // 05 Italian + "TROP_06.XML", // 06 Dutch + "TROP_07.XML", // 07 Portuguese (PT) + "TROP_08.XML", // 08 Russian + "TROP_09.XML", // 09 Korean + "TROP_10.XML", // 10 Traditional Chinese + "TROP_11.XML", // 11 Simplified Chinese + "TROP_12.XML", // 12 Finnish + "TROP_13.XML", // 13 Swedish + "TROP_14.XML", // 14 Danish + "TROP_15.XML", // 15 Norwegian + "TROP_16.XML", // 16 Polish + "TROP_17.XML", // 17 Portuguese (BR) + "TROP_18.XML", // 18 English (GB) + "TROP_19.XML", // 19 Turkish + "TROP_20.XML", // 20 Spanish (LA) + "TROP_21.XML", // 21 Arabic + "TROP_22.XML", // 22 French (CA) + "TROP_23.XML", // 23 Czech + "TROP_24.XML", // 24 Hungarian + "TROP_25.XML", // 25 Greek + "TROP_26.XML", // 26 Romanian + "TROP_27.XML", // 27 Thai + "TROP_28.XML", // 28 Vietnamese + "TROP_29.XML", // 29 Indonesian + "TROP_30.XML", // 30 Unkrainian +}; +// clang-format on + +// Returns the best available trophy XML path for the current system language. +// Resolution order: +// 1. TROP_XX.XML for the active system language (e.g. TROP01.XML for English) +// 2. TROP.XML (master / language-neutral fallback) +static std::filesystem::path GetTrophyXmlPath(const std::filesystem::path& xml_dir, + int system_language) { + // Try the exact language file first. + if (system_language >= 0 && system_language < static_cast(s_language_xml_names.size())) { + auto lang_path = xml_dir / s_language_xml_names[system_language]; + if (std::filesystem::exists(lang_path)) { + return lang_path; + } + } + // Final fallback: master TROP.XML (always present). + return xml_dir / "TROP.XML"; +} + +static void ApplyUnlockToXmlFile(const std::filesystem::path& xml_path, OrbisNpTrophyId trophyId, + u64 trophyTimestamp, bool unlock_platinum, + OrbisNpTrophyId platinumId, u64 platinumTimestamp) { + pugi::xml_document doc; + if (!doc.load_file(xml_path.native().c_str())) { + LOG_WARNING(Lib_NpTrophy, "ApplyUnlock: failed to load {}", xml_path.string()); + return; + } + + auto trophyconf = doc.child("trophyconf"); + for (pugi::xml_node& node : trophyconf.children()) { + if (std::string_view(node.name()) != "trophy") { + continue; + } + int id = node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); + + auto set_unlock = [&](u64 ts) { + if (node.attribute("unlockstate").empty()) { + node.append_attribute("unlockstate") = "true"; + } else { + node.attribute("unlockstate").set_value("true"); + } + const auto ts_str = std::to_string(ts); + if (node.attribute("timestamp").empty()) { + node.append_attribute("timestamp") = ts_str.c_str(); + } else { + node.attribute("timestamp").set_value(ts_str.c_str()); + } + }; + + if (id == trophyId) { + set_unlock(trophyTimestamp); + } else if (unlock_platinum && id == platinumId) { + set_unlock(platinumTimestamp); + } + } + + doc.save_file(xml_path.native().c_str()); +} static constexpr auto MaxTrophyHandles = 4u; static constexpr auto MaxTrophyContexts = 8u; @@ -30,6 +126,11 @@ struct ContextKeyHash { struct TrophyContext { u32 context_id; + bool registered = false; + std::filesystem::path trophy_xml_path; // resolved once at CreateContext + std::filesystem::path xml_dir; // .../Xml/ + std::filesystem::path xml_save_file; // The actual file for tracking progress per-user. + std::filesystem::path icons_dir; // .../Icons/ }; static Common::SlotVector trophy_handles{}; static Common::SlotVector trophy_contexts{}; @@ -94,66 +195,10 @@ OrbisNpTrophyGrade GetTrophyGradeFromChar(char trophyType) { } } -int PS4_SYSV_ABI sceNpTrophyAbortHandle(OrbisNpTrophyHandle handle) { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyCaptureScreenshot() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyDetails() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyFlagArray() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyGroupArray() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyGroupDetails() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophySetInfo() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophySetInfoInGroup() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophySetVersion() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyTitleDetails() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - -int PS4_SYSV_ABI sceNpTrophyConfigHasGroupFeature() { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - return ORBIS_OK; -} - s32 PS4_SYSV_ABI sceNpTrophyCreateContext(OrbisNpTrophyContext* context, Libraries::UserService::OrbisUserServiceUserId user_id, uint32_t service_label, u64 options) { - ASSERT(options == 0ull); - if (!context) { + if (!context || options != 0ull) { return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; } @@ -169,7 +214,20 @@ s32 PS4_SYSV_ABI sceNpTrophyCreateContext(OrbisNpTrophyContext* context, const auto ctx_id = trophy_contexts.insert(user_id, service_label); *context = ctx_id.index + 1; - contexts_internal[key].context_id = *context; + + auto& ctx = contexts_internal[key]; + ctx.context_id = *context; + + // Resolve and cache all paths once so callers never recompute them. + const std::string np_comm_id = Common::ElfInfo::Instance().GetNpCommIds()[service_label]; + const auto trophy_base = + Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "trophy" / np_comm_id; + ctx.xml_save_file = Common::FS::GetUserPath(Common::FS::PathType::HomeDir) / + std::to_string(user_id) / "trophy" / (np_comm_id + ".xml"); + ctx.xml_dir = trophy_base / "Xml"; + ctx.icons_dir = trophy_base / "Icons"; + ctx.trophy_xml_path = GetTrophyXmlPath(ctx.xml_dir, EmulatorSettings.GetConsoleLanguage()); + LOG_INFO(Lib_NpTrophy, "New context = {}, user_id = {} service label = {}", *context, user_id, service_label); @@ -206,6 +264,10 @@ int PS4_SYSV_ABI sceNpTrophyDestroyContext(OrbisNpTrophyContext context) { return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; } + if (!trophy_contexts.is_allocated(contextId)) { + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + } + ContextKey contextkey = trophy_contexts[contextId]; trophy_contexts.erase(contextId); contexts_internal.erase(contextkey); @@ -251,12 +313,10 @@ int PS4_SYSV_ABI sceNpTrophyGetGameIcon(OrbisNpTrophyContext context, OrbisNpTro return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; } ContextKey contextkey = trophy_contexts[contextId]; - char trophy_folder[9]; - snprintf(trophy_folder, sizeof(trophy_folder), "trophy%02d", contextkey.second); - const auto trophy_dir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / game_serial / "TrophyFiles"; - auto icon_file = trophy_dir / trophy_folder / "Icons" / "ICON0.PNG"; + const auto& ctx = contexts_internal[contextkey]; + + auto icon_file = ctx.icons_dir / "ICON0.PNG"; Common::FS::IOFile icon(icon_file, Common::FS::FileAccessMode::Read); if (!icon.IsOpen()) { @@ -304,12 +364,11 @@ int PS4_SYSV_ABI sceNpTrophyGetGameInfo(OrbisNpTrophyContext context, OrbisNpTro return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; } ContextKey contextkey = trophy_contexts[contextId]; - char trophy_folder[9]; - snprintf(trophy_folder, sizeof(trophy_folder), "trophy%02d", contextkey.second); - - const auto trophy_dir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / game_serial / "TrophyFiles"; - auto trophy_file = trophy_dir / trophy_folder / "Xml" / "TROP.XML"; + const auto& ctx = contexts_internal[contextkey]; + if (!ctx.registered) + return ORBIS_NP_TROPHY_ERROR_NOT_REGISTERED; + const auto& trophy_file = ctx.trophy_xml_path; + const auto& trophy_save_file = ctx.xml_save_file; pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(trophy_file.native().c_str()); @@ -336,7 +395,18 @@ int PS4_SYSV_ABI sceNpTrophyGetGameInfo(OrbisNpTrophyContext context, OrbisNpTro if (node_name == "group") game_info.num_groups++; + } + pugi::xml_document save_doc; + pugi::xml_parse_result save_result = save_doc.load_file(ctx.xml_save_file.native().c_str()); + + if (!save_result) { + LOG_ERROR(Lib_NpTrophy, "Failed to parse user trophy xml : {}", result.description()); + return ORBIS_OK; + } + auto save_trophyconf = save_doc.child("trophyconf"); + for (const pugi::xml_node& node : save_trophyconf.children()) { + std::string_view node_name = node.name(); if (node_name == "trophy") { bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); std::string_view current_trophy_grade = node.attribute("ttype").value(); @@ -368,8 +438,9 @@ int PS4_SYSV_ABI sceNpTrophyGetGameInfo(OrbisNpTrophyContext context, OrbisNpTro data->unlocked_silver = game_info.unlocked_trophies_by_rarity[ORBIS_NP_TROPHY_GRADE_SILVER]; data->unlocked_bronze = game_info.unlocked_trophies_by_rarity[ORBIS_NP_TROPHY_GRADE_BRONZE]; - // maybe this should be 1 instead of 100? - data->progress_percentage = 100; + data->progress_percentage = (game_info.num_trophies > 0) + ? (game_info.unlocked_trophies * 100u) / game_info.num_trophies + : 0; return ORBIS_OK; } @@ -411,12 +482,10 @@ int PS4_SYSV_ABI sceNpTrophyGetGroupInfo(OrbisNpTrophyContext context, OrbisNpTr return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; } ContextKey contextkey = trophy_contexts[contextId]; - char trophy_folder[9]; - snprintf(trophy_folder, sizeof(trophy_folder), "trophy%02d", contextkey.second); - - const auto trophy_dir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / game_serial / "TrophyFiles"; - auto trophy_file = trophy_dir / trophy_folder / "Xml" / "TROP.XML"; + const auto& ctx = contexts_internal[contextkey]; + if (!ctx.registered) + return ORBIS_NP_TROPHY_ERROR_NOT_REGISTERED; + const auto& trophy_file = ctx.trophy_xml_path; pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(trophy_file.native().c_str()); @@ -450,7 +519,18 @@ int PS4_SYSV_ABI sceNpTrophyGetGroupInfo(OrbisNpTrophyContext context, OrbisNpTr details->group_id = groupId; data->group_id = groupId; + } + pugi::xml_document save_doc; + pugi::xml_parse_result save_result = save_doc.load_file(ctx.xml_save_file.native().c_str()); + + if (!save_result) { + LOG_ERROR(Lib_NpTrophy, "Failed to parse user trophy xml : {}", result.description()); + return ORBIS_OK; + } + auto save_trophyconf = save_doc.child("trophyconf"); + for (const pugi::xml_node& node : save_trophyconf.children()) { + std::string_view node_name = node.name(); if (node_name == "trophy") { bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); std::string_view current_trophy_grade = node.attribute("ttype").value(); @@ -484,15 +564,84 @@ int PS4_SYSV_ABI sceNpTrophyGetGroupInfo(OrbisNpTrophyContext context, OrbisNpTr data->unlocked_silver = group_info.unlocked_trophies_by_rarity[ORBIS_NP_TROPHY_GRADE_SILVER]; data->unlocked_bronze = group_info.unlocked_trophies_by_rarity[ORBIS_NP_TROPHY_GRADE_BRONZE]; - // maybe this should be 1 instead of 100? - data->progress_percentage = 100; + data->progress_percentage = + (group_info.num_trophies > 0) + ? (group_info.unlocked_trophies * 100u) / group_info.num_trophies + : 0; return ORBIS_OK; } int PS4_SYSV_ABI sceNpTrophyGetTrophyIcon(OrbisNpTrophyContext context, OrbisNpTrophyHandle handle, OrbisNpTrophyId trophyId, void* buffer, u64* size) { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + if (size == nullptr) + return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; + + if (trophyId < 0 || trophyId >= ORBIS_NP_TROPHY_NUM_MAX) + return ORBIS_NP_TROPHY_ERROR_INVALID_TROPHY_ID; + + if (context == ORBIS_NP_TROPHY_INVALID_CONTEXT) + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + + if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + + Common::SlotId contextId; + contextId.index = context - 1; + if (contextId.index >= trophy_contexts.size() || !trophy_contexts.is_allocated(contextId)) { + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + } + + s32 handle_index = handle - 1; + if (handle_index >= trophy_handles.size() || + !trophy_handles.is_allocated({static_cast(handle_index)})) { + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + } + + ContextKey contextkey = trophy_contexts[contextId]; + const auto& ctx = contexts_internal[contextkey]; + if (!ctx.registered) + return ORBIS_NP_TROPHY_ERROR_NOT_REGISTERED; + + // Check that the trophy is unlocked and icons are only available for earned trophies. + pugi::xml_document doc; + if (!doc.load_file(ctx.xml_save_file.native().c_str())) { + LOG_ERROR(Lib_NpTrophy, "Failed to open trophy XML: {}", ctx.xml_save_file.string()); + return ORBIS_NP_TROPHY_ERROR_ICON_FILE_NOT_FOUND; + } + + bool unlocked = false; + bool found = false; + for (const pugi::xml_node& node : doc.child("trophyconf").children()) { + if (std::string_view(node.name()) != "trophy") + continue; + if (node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID) == trophyId) { + found = true; + unlocked = node.attribute("unlockstate").as_bool(); + break; + } + } + + if (!found) + return ORBIS_NP_TROPHY_ERROR_INVALID_TROPHY_ID; + + if (!unlocked) + return ORBIS_NP_TROPHY_ERROR_TROPHY_NOT_UNLOCKED; + + const std::string icon_name = fmt::format("TROP{:03d}.PNG", trophyId); + const auto icon_path = ctx.icons_dir / icon_name; + + Common::FS::IOFile icon(icon_path, Common::FS::FileAccessMode::Read); + if (!icon.IsOpen()) { + LOG_ERROR(Lib_NpTrophy, "Failed to open trophy icon: {}", icon_path.string()); + return ORBIS_NP_TROPHY_ERROR_ICON_FILE_NOT_FOUND; + } + + if (buffer != nullptr) { + ReadFile(icon, buffer, *size); + } else { + *size = icon.GetSize(); + } return ORBIS_OK; } @@ -507,7 +656,7 @@ int PS4_SYSV_ABI sceNpTrophyGetTrophyInfo(OrbisNpTrophyContext context, OrbisNpT if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; - if (trophyId >= 127) + if (trophyId >= ORBIS_NP_TROPHY_NUM_MAX) return ORBIS_NP_TROPHY_ERROR_INVALID_TROPHY_ID; if (details == nullptr || data == nullptr) @@ -522,12 +671,10 @@ int PS4_SYSV_ABI sceNpTrophyGetTrophyInfo(OrbisNpTrophyContext context, OrbisNpT return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; } ContextKey contextkey = trophy_contexts[contextId]; - char trophy_folder[9]; - snprintf(trophy_folder, sizeof(trophy_folder), "trophy%02d", contextkey.second); - - const auto trophy_dir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / game_serial / "TrophyFiles"; - auto trophy_file = trophy_dir / trophy_folder / "Xml" / "TROP.XML"; + const auto& ctx = contexts_internal[contextkey]; + if (!ctx.registered) + return ORBIS_NP_TROPHY_ERROR_NOT_REGISTERED; + const auto& trophy_file = ctx.trophy_xml_path; pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(trophy_file.native().c_str()); @@ -545,12 +692,34 @@ int PS4_SYSV_ABI sceNpTrophyGetTrophyInfo(OrbisNpTrophyContext context, OrbisNpT if (node_name == "trophy") { int current_trophy_id = node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); if (current_trophy_id == trophyId) { - bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); - std::string_view current_trophy_grade = node.attribute("ttype").value(); std::string_view current_trophy_name = node.child("name").text().as_string(); std::string_view current_trophy_description = node.child("detail").text().as_string(); + strncpy(details->name, current_trophy_name.data(), ORBIS_NP_TROPHY_NAME_MAX_SIZE); + strncpy(details->description, current_trophy_description.data(), + ORBIS_NP_TROPHY_DESCR_MAX_SIZE); + } + } + } + + pugi::xml_document save_doc; + pugi::xml_parse_result save_result = save_doc.load_file(ctx.xml_save_file.native().c_str()); + + if (!save_result) { + LOG_ERROR(Lib_NpTrophy, "Failed to parse user trophy xml : {}", result.description()); + return ORBIS_OK; + } + auto save_trophyconf = save_doc.child("trophyconf"); + for (const pugi::xml_node& node : save_trophyconf.children()) { + std::string_view node_name = node.name(); + + if (node_name == "trophy") { + int current_trophy_id = node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); + if (current_trophy_id == trophyId) { + bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); + std::string_view current_trophy_grade = node.attribute("ttype").value(); + uint64_t current_trophy_timestamp = node.attribute("timestamp").as_ullong(); int current_trophy_groupid = node.attribute("gid").as_int(-1); bool current_trophy_hidden = node.attribute("hidden").as_bool(); @@ -560,10 +729,6 @@ int PS4_SYSV_ABI sceNpTrophyGetTrophyInfo(OrbisNpTrophyContext context, OrbisNpT details->group_id = current_trophy_groupid; details->hidden = current_trophy_hidden; - strncpy(details->name, current_trophy_name.data(), ORBIS_NP_TROPHY_NAME_MAX_SIZE); - strncpy(details->description, current_trophy_description.data(), - ORBIS_NP_TROPHY_DESCR_MAX_SIZE); - data->trophy_id = trophyId; data->unlocked = current_trophy_unlockstate; data->timestamp.tick = current_trophy_timestamp; @@ -579,29 +744,34 @@ s32 PS4_SYSV_ABI sceNpTrophyGetTrophyUnlockState(OrbisNpTrophyContext context, OrbisNpTrophyFlagArray* flags, u32* count) { LOG_INFO(Lib_NpTrophy, "called"); + if (flags == nullptr || count == nullptr) + return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; + if (context == ORBIS_NP_TROPHY_INVALID_CONTEXT) return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; - if (flags == nullptr || count == nullptr) - return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; - - ORBIS_NP_TROPHY_FLAG_ZERO(flags); - Common::SlotId contextId; contextId.index = context - 1; - if (contextId.index >= trophy_contexts.size()) { + if (contextId.index >= trophy_contexts.size() || !trophy_contexts.is_allocated(contextId)) { return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; } - ContextKey contextkey = trophy_contexts[contextId]; - char trophy_folder[9]; - snprintf(trophy_folder, sizeof(trophy_folder), "trophy%02d", contextkey.second); - const auto trophy_dir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / game_serial / "TrophyFiles"; - auto trophy_file = trophy_dir / trophy_folder / "Xml" / "TROP.XML"; + s32 handle_index = handle - 1; + if (handle_index >= trophy_handles.size() || + !trophy_handles.is_allocated({static_cast(handle_index)})) { + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + } + + ContextKey contextkey = trophy_contexts[contextId]; + const auto& ctx = contexts_internal[contextkey]; + if (!ctx.registered) + return ORBIS_NP_TROPHY_ERROR_NOT_REGISTERED; + const auto& trophy_file = ctx.xml_save_file; + + ORBIS_NP_TROPHY_FLAG_ZERO(flags); pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(trophy_file.native().c_str()); @@ -622,10 +792,9 @@ s32 PS4_SYSV_ABI sceNpTrophyGetTrophyUnlockState(OrbisNpTrophyContext context, if (node_name == "trophy") { num_trophies++; - } - - if (current_trophy_unlockstate) { - ORBIS_NP_TROPHY_FLAG_SET(current_trophy_id, flags); + if (current_trophy_unlockstate) { + ORBIS_NP_TROPHY_FLAG_SET(current_trophy_id, flags); + } } } @@ -633,6 +802,200 @@ s32 PS4_SYSV_ABI sceNpTrophyGetTrophyUnlockState(OrbisNpTrophyContext context, return ORBIS_OK; } +int PS4_SYSV_ABI sceNpTrophyRegisterContext(OrbisNpTrophyContext context, + OrbisNpTrophyHandle handle, uint64_t options) { + if (options != 0ull) + return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; + + if (context == ORBIS_NP_TROPHY_INVALID_CONTEXT) + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + + if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + + Common::SlotId contextId; + contextId.index = context - 1; + if (contextId.index >= trophy_contexts.size() || !trophy_contexts.is_allocated(contextId)) { + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + } + + s32 handle_index = handle - 1; + if (handle_index >= trophy_handles.size() || + !trophy_handles.is_allocated({static_cast(handle_index)})) { + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + } + + ContextKey contextkey = trophy_contexts[contextId]; + auto& ctx = contexts_internal[contextkey]; + + if (ctx.registered) + return ORBIS_NP_TROPHY_ERROR_ALREADY_REGISTERED; + + if (!std::filesystem::exists(ctx.trophy_xml_path)) + return ORBIS_NP_TROPHY_ERROR_TITLE_CONF_NOT_INSTALLED; + + ctx.registered = true; + LOG_INFO(Lib_NpTrophy, "Context {} registered", context); + + return ORBIS_OK; +} + +int PS4_SYSV_ABI sceNpTrophyUnlockTrophy(OrbisNpTrophyContext context, OrbisNpTrophyHandle handle, + OrbisNpTrophyId trophyId, OrbisNpTrophyId* platinumId) { + LOG_INFO(Lib_NpTrophy, "Unlocking trophy id {}", trophyId); + + if (context == ORBIS_NP_TROPHY_INVALID_CONTEXT) + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + + if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + + if (trophyId >= ORBIS_NP_TROPHY_NUM_MAX) + return ORBIS_NP_TROPHY_ERROR_INVALID_TROPHY_ID; + + if (platinumId == nullptr) + return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; + + Common::SlotId contextId; + contextId.index = context - 1; + if (contextId.index >= trophy_contexts.size() || !trophy_contexts.is_allocated(contextId)) { + return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; + } + + s32 handle_index = handle - 1; + if (handle_index >= trophy_handles.size() || + !trophy_handles.is_allocated({static_cast(handle_index)})) { + return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; + } + + ContextKey contextkey = trophy_contexts[contextId]; + const auto& ctx = contexts_internal[contextkey]; + if (!ctx.registered) + return ORBIS_NP_TROPHY_ERROR_NOT_REGISTERED; + const auto& xml_dir = ctx.xml_dir; + const auto& trophy_file = ctx.trophy_xml_path; + + pugi::xml_document save_doc; + pugi::xml_parse_result save_result = save_doc.load_file(ctx.xml_save_file.native().c_str()); + + if (!save_result) { + LOG_ERROR(Lib_NpTrophy, "Failed to parse user trophy xml : {}", save_result.description()); + return ORBIS_OK; + } + auto save_trophyconf = save_doc.child("trophyconf"); + for (const pugi::xml_node& node : save_trophyconf.children()) { + std::string_view node_name = node.name(); + if (std::string_view(node.name()) != "trophy") + continue; + + int current_trophy_id = node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); + bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); + + if (current_trophy_id == trophyId) { + if (current_trophy_unlockstate) { + LOG_INFO(Lib_NpTrophy, "Trophy already unlocked"); + return ORBIS_NP_TROPHY_ERROR_TROPHY_ALREADY_UNLOCKED; + } + } + } + + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_file(trophy_file.native().c_str()); + + if (!result) { + LOG_ERROR(Lib_NpTrophy, "Failed to parse trophy xml : {}", result.description()); + return ORBIS_NP_TROPHY_ERROR_TITLE_NOT_FOUND; + } + + *platinumId = ORBIS_NP_TROPHY_INVALID_TROPHY_ID; + + int num_trophies = 0; + int num_trophies_unlocked = 0; + pugi::xml_node platinum_node; + + // Outputs filled during the scan. + bool trophy_found = false; + const char* trophy_name = ""; + std::string_view trophy_type; + std::filesystem::path trophy_icon_path; + + auto trophyconf = doc.child("trophyconf"); + + for (pugi::xml_node& node : trophyconf.children()) { + if (std::string_view(node.name()) != "trophy") + continue; + + int current_trophy_id = node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); + bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); + std::string_view current_trophy_type = node.attribute("ttype").value(); + + if (current_trophy_type == "P") { + platinum_node = node; + if (trophyId == current_trophy_id) { + return ORBIS_NP_TROPHY_ERROR_PLATINUM_CANNOT_UNLOCK; + } + } + + if (node.attribute("pid").as_int(-1) != ORBIS_NP_TROPHY_INVALID_TROPHY_ID) { + num_trophies++; + if (current_trophy_unlockstate) { + num_trophies_unlocked++; + } + } + + if (current_trophy_id == trophyId) { + trophy_found = true; + trophy_name = node.child("name").text().as_string(); + trophy_type = current_trophy_type; + + const std::string icon_file = fmt::format("TROP{:03d}.PNG", current_trophy_id); + trophy_icon_path = ctx.icons_dir / icon_file; + } + } + + if (!trophy_found) + return ORBIS_NP_TROPHY_ERROR_INVALID_TROPHY_ID; + + // Capture timestamps once so every file gets the exact same value. + const auto now_secs = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + const u64 trophy_timestamp = static_cast(now_secs); + + // Decide platinum. + bool unlock_platinum = false; + OrbisNpTrophyId platinum_id = ORBIS_NP_TROPHY_INVALID_TROPHY_ID; + u64 platinum_timestamp = 0; + const char* platinum_name = ""; + std::filesystem::path platinum_icon_path; + + if (!platinum_node.attribute("unlockstate").as_bool()) { + if ((num_trophies - 1) == num_trophies_unlocked) { + unlock_platinum = true; + platinum_id = platinum_node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); + platinum_timestamp = trophy_timestamp; // same second is fine + platinum_name = platinum_node.child("name").text().as_string(); + + const std::string plat_icon_file = fmt::format("TROP{:03d}.PNG", platinum_id); + platinum_icon_path = ctx.icons_dir / plat_icon_file; + + *platinumId = platinum_id; + } + } + + // Queue UI notifications (only once, using the primary XML's strings). + AddTrophyToQueue(trophy_icon_path, trophy_name, trophy_type); + if (unlock_platinum) { + AddTrophyToQueue(platinum_icon_path, platinum_name, "P"); + } + + ApplyUnlockToXmlFile(ctx.xml_save_file, trophyId, trophy_timestamp, unlock_platinum, + platinum_id, platinum_timestamp); + LOG_INFO(Lib_NpTrophy, "Trophy {} successfully saved.", trophyId); + + return ORBIS_OK; +} + int PS4_SYSV_ABI sceNpTrophyGroupArrayGetNum() { LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); return ORBIS_OK; @@ -698,19 +1061,6 @@ int PS4_SYSV_ABI sceNpTrophyNumInfoGetTotal() { return ORBIS_OK; } -int PS4_SYSV_ABI sceNpTrophyRegisterContext(OrbisNpTrophyContext context, - OrbisNpTrophyHandle handle, uint64_t options) { - LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); - - if (context == ORBIS_NP_TROPHY_INVALID_CONTEXT) - return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; - - if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) - return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; - - return ORBIS_OK; -} - int PS4_SYSV_ABI sceNpTrophySetInfoGetTrophyFlagArray() { LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); return ORBIS_OK; @@ -942,147 +1292,58 @@ int PS4_SYSV_ABI sceNpTrophySystemSetDbgParamInt() { return ORBIS_OK; } -int PS4_SYSV_ABI sceNpTrophyUnlockTrophy(OrbisNpTrophyContext context, OrbisNpTrophyHandle handle, - OrbisNpTrophyId trophyId, OrbisNpTrophyId* platinumId) { - LOG_INFO(Lib_NpTrophy, "Unlocking trophy id {}", trophyId); +int PS4_SYSV_ABI sceNpTrophyAbortHandle(OrbisNpTrophyHandle handle) { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - if (context == ORBIS_NP_TROPHY_INVALID_CONTEXT) - return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; +int PS4_SYSV_ABI sceNpTrophyCaptureScreenshot() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - if (handle == ORBIS_NP_TROPHY_INVALID_HANDLE) - return ORBIS_NP_TROPHY_ERROR_INVALID_HANDLE; +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyDetails() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - if (trophyId >= 127) - return ORBIS_NP_TROPHY_ERROR_INVALID_TROPHY_ID; +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyFlagArray() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - if (platinumId == nullptr) - return ORBIS_NP_TROPHY_ERROR_INVALID_ARGUMENT; +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyGroupArray() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - Common::SlotId contextId; - contextId.index = context - 1; - if (contextId.index >= trophy_contexts.size()) { - return ORBIS_NP_TROPHY_ERROR_INVALID_CONTEXT; - } - ContextKey contextkey = trophy_contexts[contextId]; - char trophy_folder[9]; - snprintf(trophy_folder, sizeof(trophy_folder), "trophy%02d", contextkey.second); +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyGroupDetails() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - const auto trophy_dir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / game_serial / "TrophyFiles"; - auto trophy_file = trophy_dir / trophy_folder / "Xml" / "TROP.XML"; +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophySetInfo() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - pugi::xml_document doc; - pugi::xml_parse_result result = doc.load_file(trophy_file.native().c_str()); +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophySetInfoInGroup() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - if (!result) { - LOG_ERROR(Lib_NpTrophy, "Failed to parse trophy xml : {}", result.description()); - return ORBIS_OK; - } +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophySetVersion() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} - *platinumId = ORBIS_NP_TROPHY_INVALID_TROPHY_ID; - - int num_trophies = 0; - int num_trophies_unlocked = 0; - pugi::xml_node platinum_node; - - auto trophyconf = doc.child("trophyconf"); - - for (pugi::xml_node& node : trophyconf.children()) { - int current_trophy_id = node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); - bool current_trophy_unlockstate = node.attribute("unlockstate").as_bool(); - const char* current_trophy_name = node.child("name").text().as_string(); - std::string_view current_trophy_description = node.child("detail").text().as_string(); - std::string_view current_trophy_type = node.attribute("ttype").value(); - - if (current_trophy_type == "P") { - platinum_node = node; - if (trophyId == current_trophy_id) { - return ORBIS_NP_TROPHY_ERROR_PLATINUM_CANNOT_UNLOCK; - } - } - - if (std::string_view(node.name()) == "trophy") { - if (node.attribute("pid").as_int(-1) != ORBIS_NP_TROPHY_INVALID_TROPHY_ID) { - num_trophies++; - if (current_trophy_unlockstate) { - num_trophies_unlocked++; - } - } - - if (current_trophy_id == trophyId) { - if (current_trophy_unlockstate) { - LOG_INFO(Lib_NpTrophy, "Trophy already unlocked"); - return ORBIS_NP_TROPHY_ERROR_TROPHY_ALREADY_UNLOCKED; - } else { - if (node.attribute("unlockstate").empty()) { - node.append_attribute("unlockstate") = "true"; - } else { - node.attribute("unlockstate").set_value("true"); - } - - auto trophyTimestamp = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count(); - - if (node.attribute("timestamp").empty()) { - node.append_attribute("timestamp") = - std::to_string(trophyTimestamp).c_str(); - } else { - node.attribute("timestamp") - .set_value(std::to_string(trophyTimestamp).c_str()); - } - - std::string trophy_icon_file = "TROP"; - trophy_icon_file.append(node.attribute("id").value()); - trophy_icon_file.append(".PNG"); - - std::filesystem::path current_icon_path = - trophy_dir / trophy_folder / "Icons" / trophy_icon_file; - - AddTrophyToQueue(current_icon_path, current_trophy_name, current_trophy_type); - } - } - } - } - - if (!platinum_node.attribute("unlockstate").as_bool()) { - if ((num_trophies - 1) == num_trophies_unlocked) { - if (platinum_node.attribute("unlockstate").empty()) { - platinum_node.append_attribute("unlockstate") = "true"; - } else { - platinum_node.attribute("unlockstate").set_value("true"); - } - - auto trophyTimestamp = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count(); - - if (platinum_node.attribute("timestamp").empty()) { - platinum_node.append_attribute("timestamp") = - std::to_string(trophyTimestamp).c_str(); - } else { - platinum_node.attribute("timestamp") - .set_value(std::to_string(trophyTimestamp).c_str()); - } - - int platinum_trophy_id = - platinum_node.attribute("id").as_int(ORBIS_NP_TROPHY_INVALID_TROPHY_ID); - const char* platinum_trophy_name = platinum_node.child("name").text().as_string(); - - std::string platinum_icon_file = "TROP"; - platinum_icon_file.append(platinum_node.attribute("id").value()); - platinum_icon_file.append(".PNG"); - - std::filesystem::path platinum_icon_path = - trophy_dir / trophy_folder / "Icons" / platinum_icon_file; - - *platinumId = platinum_trophy_id; - AddTrophyToQueue(platinum_icon_path, platinum_trophy_name, "P"); - } - } - - doc.save_file((trophy_dir / trophy_folder / "Xml" / "TROP.XML").native().c_str()); +int PS4_SYSV_ABI sceNpTrophyConfigGetTrophyTitleDetails() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); + return ORBIS_OK; +} +int PS4_SYSV_ABI sceNpTrophyConfigHasGroupFeature() { + LOG_ERROR(Lib_NpTrophy, "(STUBBED) called"); return ORBIS_OK; } diff --git a/src/core/libraries/np/np_trophy.h b/src/core/libraries/np/np_trophy.h index ab187ae13..590e58c0d 100644 --- a/src/core/libraries/np/np_trophy.h +++ b/src/core/libraries/np/np_trophy.h @@ -13,8 +13,6 @@ class SymbolsResolver; namespace Libraries::Np::NpTrophy { -extern std::string game_serial; - constexpr int ORBIS_NP_TROPHY_FLAG_SETSIZE = 128; constexpr int ORBIS_NP_TROPHY_FLAG_BITS_SHIFT = 5; diff --git a/src/core/libraries/save_data/save_instance.cpp b/src/core/libraries/save_data/save_instance.cpp index 4ff682357..463baa50b 100644 --- a/src/core/libraries/save_data/save_instance.cpp +++ b/src/core/libraries/save_data/save_instance.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include @@ -6,7 +6,6 @@ #include #include "common/assert.h" -#include "common/config.h" #include "common/path_util.h" #include "common/singleton.h" #include "core/emulator_settings.h" @@ -49,12 +48,13 @@ namespace Libraries::SaveData { fs::path SaveInstance::MakeTitleSavePath(Libraries::UserService::OrbisUserServiceUserId user_id, std::string_view game_serial) { - return Config::GetSaveDataPath() / std::to_string(user_id) / game_serial; + return EmulatorSettings.GetHomeDir() / std::to_string(user_id) / "savedata" / game_serial; } -fs::path SaveInstance::MakeDirSavePath(Libraries::UserService::OrbisUserServiceUserId user_id, - std::string_view game_serial, std::string_view dir_name) { - return Config::GetSaveDataPath() / std::to_string(user_id) / game_serial / dir_name; +fs::path SaveInstance::MakeDirSavePath(OrbisUserServiceUserId user_id, std::string_view game_serial, + std::string_view dir_name) { + return EmulatorSettings.GetHomeDir() / std::to_string(user_id) / "savedata" / game_serial / + dir_name; } uint64_t SaveInstance::GetMaxBlockFromSFO(const PSF& psf) { diff --git a/src/core/libraries/save_data/savedata.cpp b/src/core/libraries/save_data/savedata.cpp index 48b086457..70c66e4cd 100644 --- a/src/core/libraries/save_data/savedata.cpp +++ b/src/core/libraries/save_data/savedata.cpp @@ -1,6 +1,7 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include #include #include #include @@ -8,13 +9,13 @@ #include #include "common/assert.h" -#include "common/config.h" #include "common/cstring.h" #include "common/elf_info.h" #include "common/enum.h" #include "common/logging/log.h" #include "common/path_util.h" #include "common/string_util.h" +#include "core/emulator_settings.h" #include "core/file_format/psf.h" #include "core/file_sys/fs.h" #include "core/libraries/error_codes.h" @@ -441,7 +442,8 @@ static Error saveDataMount(const OrbisSaveDataMount2* mount_info, LOG_INFO(Lib_SaveData, "called with invalid block size"); } - const auto root_save = Config::GetSaveDataPath(); + const auto root_save = + EmulatorSettings.GetHomeDir() / std::to_string(mount_info->userId) / "savedata"; fs::create_directories(root_save); const auto available = fs::space(root_save).available; @@ -489,7 +491,9 @@ static Error Umount(const OrbisSaveDataMountPoint* mountPoint, bool call_backup return Error::PARAMETER; } LOG_DEBUG(Lib_SaveData, "Umount mountPoint:{}", mountPoint->data.to_view()); - const std::string_view mount_point_str{mountPoint->data}; + + std::string mount_point_str = mountPoint->data.to_string(); + for (auto& instance : g_mount_slots) { if (instance.has_value()) { const auto& slot_name = instance->GetMountPoint(); diff --git a/src/emulator.cpp b/src/emulator.cpp index 447c72391..034d30ed8 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -28,6 +28,7 @@ #include "common/singleton.h" #include "core/debugger.h" #include "core/devtools/widget/module_list.h" +#include "core/emulator_settings.h" #include "core/emulator_state.h" #include "core/file_format/psf.h" #include "core/file_format/trp.h" @@ -38,6 +39,7 @@ #include "core/libraries/save_data/save_backup.h" #include "core/linker.h" #include "core/memory.h" +#include "core/user_settings.h" #include "emulator.h" #include "video_core/cache_storage.h" #include "video_core/renderdoc.h" @@ -50,6 +52,7 @@ #include #include #endif +#include Frontend::WindowSDL* g_window = nullptr; @@ -196,14 +199,20 @@ void Emulator::Run(std::filesystem::path file, std::vector args, } game_info.game_folder = game_folder; + std::filesystem::path npbindPath = game_folder / "sce_sys/npbind.dat"; + NPBindFile npbind; + if (!npbind.Load(npbindPath.string())) { + LOG_WARNING(Common_Filesystem, "Failed to load npbind.dat file"); + } else { + auto npCommIds = npbind.GetNpCommIds(); + if (npCommIds.empty()) { + LOG_WARNING(Common_Filesystem, "No NPComm IDs found in npbind.dat"); + } else { + game_info.npCommIds = std::move(npCommIds); + } + } EmulatorSettings.Load(id); - if (std::filesystem::exists(Common::FS::GetUserPath(Common::FS::PathType::CustomConfigs) / - (id + ".json"))) { - EmulatorState::GetInstance()->SetGameSpecifigConfigUsed(true); - } else { - EmulatorState::GetInstance()->SetGameSpecifigConfigUsed(false); - } // Initialize logging as soon as possible if (!id.empty() && EmulatorSettings.IsSeparateLoggingEnabled()) { @@ -224,9 +233,8 @@ void Emulator::Run(std::filesystem::path file, std::vector args, LOG_INFO(Loader, "Description {}", Common::g_scm_desc); LOG_INFO(Loader, "Remote {}", Common::g_scm_remote_url); - const bool has_game_config = std::filesystem::exists( - Common::FS::GetUserPath(Common::FS::PathType::CustomConfigs) / (id + ".json")); - LOG_INFO(Config, "Game-specific config exists: {}", has_game_config); + LOG_INFO(Config, "Game-specific config used: {}", + EmulatorState::GetInstance()->IsGameSpecifigConfigUsed()); LOG_INFO(Config, "General LogType: {}", EmulatorSettings.GetLogType()); LOG_INFO(Config, "General isIdenticalLogGrouped: {}", EmulatorSettings.IsIdenticalLogGrouped()); @@ -298,15 +306,28 @@ void Emulator::Run(std::filesystem::path file, std::vector args, // Initialize patcher and trophies if (!id.empty()) { MemoryPatcher::g_game_serial = id; - Libraries::Np::NpTrophy::game_serial = id; - const auto trophyDir = - Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) / id / "TrophyFiles"; - if (!std::filesystem::exists(trophyDir)) { - TRP trp; - if (!trp.Extract(game_folder, id)) { - LOG_ERROR(Loader, "Couldn't extract trophies"); + int index = 0; + for (std::string npCommId : game_info.npCommIds) { + const auto trophyDir = + Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "trophy" / npCommId; + if (!std::filesystem::exists(trophyDir)) { + TRP trp; + if (!trp.Extract(game_folder, index, npCommId, trophyDir)) { + LOG_ERROR(Loader, "Couldn't extract trophies"); + } } + for (User user : UserSettings.GetUserManager().GetValidUsers()) { + auto const user_trophy_file = + Common::FS::GetUserPath(Common::FS::PathType::HomeDir) / + std::to_string(user.user_id) / "trophy" / (npCommId + ".xml"); + if (!std::filesystem::exists(user_trophy_file)) { + std::error_code discard; + std::filesystem::copy_file(trophyDir / "Xml" / "TROPCONF.XML", user_trophy_file, + discard); + } + } + index++; } } From df32a2076b27a84660115f1b3872c89ac928be56 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Fri, 27 Mar 2026 17:20:26 +0100 Subject: [PATCH 08/14] readd missing line (#4180) Co-authored-by: dsprogrammingprojects --- src/input/input_handler.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/input/input_handler.h b/src/input/input_handler.h index 22ae0f4e0..ee286aea9 100644 --- a/src/input/input_handler.h +++ b/src/input/input_handler.h @@ -526,7 +526,7 @@ public: class ControllerAllOutputs { public: - static constexpr u64 output_count = 39; + static constexpr u64 output_count = 40; std::array data = { // Important: these have to be the first, or else they will update in the wrong order ControllerOutput(LEFTJOYSTICK_HALFMODE), @@ -563,6 +563,7 @@ public: ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHTY), ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_LEFT_TRIGGER), + ControllerOutput(SDL_GAMEPAD_BUTTON_INVALID, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER), ControllerOutput(HOTKEY_FULLSCREEN), ControllerOutput(HOTKEY_PAUSE), From 96411a17cb08e44598084eb89db2322de0fc8924 Mon Sep 17 00:00:00 2001 From: rainmakerv2 <30595646+rainmakerv3@users.noreply.github.com> Date: Sun, 29 Mar 2026 00:08:02 +0800 Subject: [PATCH 09/14] Imgui: translations (#4124) * WIP: imgui translations * fallback to original strings if tables are incomplete * reorder things a bit * construct tables as consts * Update imgui_translations.h --- CMakeLists.txt | 1 + src/imgui/imgui_translations.h | 184 +++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 src/imgui/imgui_translations.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cbf373e57..6bb22db33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1091,6 +1091,7 @@ set(IMGUI src/imgui/imgui_config.h src/imgui/imgui_layer.h src/imgui/imgui_std.h src/imgui/imgui_texture.h + src/imgui/imgui_translations.h src/imgui/renderer/imgui_core.cpp src/imgui/renderer/imgui_core.h src/imgui/renderer/imgui_impl_sdl3.cpp diff --git a/src/imgui/imgui_translations.h b/src/imgui/imgui_translations.h new file mode 100644 index 000000000..83f60f187 --- /dev/null +++ b/src/imgui/imgui_translations.h @@ -0,0 +1,184 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +#include "core/emulator_settings.h" + +///////////// ImGui Translation Tables + +// disable clang line limits for ease of translation +// clang-format off + +const std::map JapaneseMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map FrenchMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map FrenchCanadaMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map SpanishMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map SpanishLatinAmericanMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map GermanMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map ItalianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map DutchMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map PortugesePtMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map PortugeseBrMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map RussianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map KoreanMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map ChineseTraditionalMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map ChineseSimplifiedMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map FinnishMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map SwedishMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map DanishMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map NorwegianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map PolishMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map TurkishMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map ArabicMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map CzechMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map HungarianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map GreekMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map RomanianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map ThaiMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map VietnameseMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map IndonesianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +const std::map UkranianMap = { + {"Trophy Earned", "Trophy Earned"}, +}; + +// clang-format on + +///////////// End ImGui Translation Tables + +const std::map> langMap = { + {0, JapaneseMap}, + // {1, EnglishUsMap}, - not used + {2, FrenchMap}, + {3, SpanishMap}, + {4, GermanMap}, + {5, ItalianMap}, + {6, DutchMap}, + {7, PortugesePtMap}, + {8, RussianMap}, + {9, KoreanMap}, + {10, ChineseTraditionalMap}, + {11, ChineseSimplifiedMap}, + {12, FinnishMap}, + {13, SwedishMap}, + {14, DanishMap}, + {15, NorwegianMap}, + {16, PolishMap}, + {17, PortugeseBrMap}, + // {18, "English (UK)"}, - not used + {19, TurkishMap}, + {20, SpanishLatinAmericanMap}, + {21, ArabicMap}, + {22, FrenchCanadaMap}, + {23, CzechMap}, + {24, HungarianMap}, + {25, GreekMap}, + {26, RomanianMap}, + {27, ThaiMap}, + {28, VietnameseMap}, + {29, IndonesianMap}, + {30, UkranianMap}, +}; + +namespace ImguiTranslate { + +std::string tr(std::string input) { + // since we're coding in English + if (EmulatorSettings.GetConsoleLanguage() == 1 || EmulatorSettings.GetConsoleLanguage() == 18) + return input; + + const std::map translationTable = + langMap.at(EmulatorSettings.GetConsoleLanguage()); + + if (!translationTable.contains(input)) { + return input; + } + + return translationTable.at(input); +} + +} // namespace ImguiTranslate From 1018660ad70453b94fdb8ef58872de4e160d68a3 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Sat, 28 Mar 2026 18:34:30 +0100 Subject: [PATCH 10/14] Make sure user trophy folder exists before creating files in it (#4186) * Make sure user trophy folder exists before creating files in it * ??? --- src/emulator.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/emulator.cpp b/src/emulator.cpp index 034d30ed8..7e62680f6 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -322,6 +322,8 @@ void Emulator::Run(std::filesystem::path file, std::vector args, Common::FS::GetUserPath(Common::FS::PathType::HomeDir) / std::to_string(user.user_id) / "trophy" / (npCommId + ".xml"); if (!std::filesystem::exists(user_trophy_file)) { + auto temp = user_trophy_file.parent_path(); + std::filesystem::create_directories(temp); std::error_code discard; std::filesystem::copy_file(trophyDir / "Xml" / "TROPCONF.XML", user_trophy_file, discard); From 1012f84bf99700f3153f7d7d943eacd732becc06 Mon Sep 17 00:00:00 2001 From: rainmakerv2 <30595646+rainmakerv3@users.noreply.github.com> Date: Sun, 29 Mar 2026 14:58:48 +0800 Subject: [PATCH 11/14] use cpp file for function (#4187) --- CMakeLists.txt | 3 +- src/imgui/imgui_translations.cpp | 58 ++++++++++++++++++++++++++++++++ src/imgui/imgui_translations.h | 56 +++--------------------------- 3 files changed, 64 insertions(+), 53 deletions(-) create mode 100644 src/imgui/imgui_translations.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bb22db33..7e6349ff9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1091,6 +1091,7 @@ set(IMGUI src/imgui/imgui_config.h src/imgui/imgui_layer.h src/imgui/imgui_std.h src/imgui/imgui_texture.h + src/imgui/imgui_translations.cpp src/imgui/imgui_translations.h src/imgui/renderer/imgui_core.cpp src/imgui/renderer/imgui_core.h @@ -1275,4 +1276,4 @@ install(TARGETS shadps4 BUNDLE DESTINATION .) else() enable_testing() add_subdirectory(tests) -endif() \ No newline at end of file +endif() diff --git a/src/imgui/imgui_translations.cpp b/src/imgui/imgui_translations.cpp new file mode 100644 index 000000000..608f980c3 --- /dev/null +++ b/src/imgui/imgui_translations.cpp @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "core/emulator_settings.h" +#include "imgui_translations.h" + +namespace ImguiTranslate { + +const std::map> langMap = { + {0, JapaneseMap}, + // {1, EnglishUsMap}, - not used + {2, FrenchMap}, + {3, SpanishMap}, + {4, GermanMap}, + {5, ItalianMap}, + {6, DutchMap}, + {7, PortugesePtMap}, + {8, RussianMap}, + {9, KoreanMap}, + {10, ChineseTraditionalMap}, + {11, ChineseSimplifiedMap}, + {12, FinnishMap}, + {13, SwedishMap}, + {14, DanishMap}, + {15, NorwegianMap}, + {16, PolishMap}, + {17, PortugeseBrMap}, + // {18, "English (UK)"}, - not used + {19, TurkishMap}, + {20, SpanishLatinAmericanMap}, + {21, ArabicMap}, + {22, FrenchCanadaMap}, + {23, CzechMap}, + {24, HungarianMap}, + {25, GreekMap}, + {26, RomanianMap}, + {27, ThaiMap}, + {28, VietnameseMap}, + {29, IndonesianMap}, + {30, UkranianMap}, +}; + +std::string tr(std::string input) { + // since we're coding in English + if (EmulatorSettings.GetConsoleLanguage() == 1 || EmulatorSettings.GetConsoleLanguage() == 18) + return input; + + const std::map translationTable = + langMap.at(EmulatorSettings.GetConsoleLanguage()); + + if (!translationTable.contains(input)) { + return input; + } + + return translationTable.at(input); +} + +} // namespace ImguiTranslate diff --git a/src/imgui/imgui_translations.h b/src/imgui/imgui_translations.h index 83f60f187..d6844b759 100644 --- a/src/imgui/imgui_translations.h +++ b/src/imgui/imgui_translations.h @@ -2,8 +2,11 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include +#include -#include "core/emulator_settings.h" +namespace ImguiTranslate { + +std::string tr(std::string input); ///////////// ImGui Translation Tables @@ -130,55 +133,4 @@ const std::map UkranianMap = { ///////////// End ImGui Translation Tables -const std::map> langMap = { - {0, JapaneseMap}, - // {1, EnglishUsMap}, - not used - {2, FrenchMap}, - {3, SpanishMap}, - {4, GermanMap}, - {5, ItalianMap}, - {6, DutchMap}, - {7, PortugesePtMap}, - {8, RussianMap}, - {9, KoreanMap}, - {10, ChineseTraditionalMap}, - {11, ChineseSimplifiedMap}, - {12, FinnishMap}, - {13, SwedishMap}, - {14, DanishMap}, - {15, NorwegianMap}, - {16, PolishMap}, - {17, PortugeseBrMap}, - // {18, "English (UK)"}, - not used - {19, TurkishMap}, - {20, SpanishLatinAmericanMap}, - {21, ArabicMap}, - {22, FrenchCanadaMap}, - {23, CzechMap}, - {24, HungarianMap}, - {25, GreekMap}, - {26, RomanianMap}, - {27, ThaiMap}, - {28, VietnameseMap}, - {29, IndonesianMap}, - {30, UkranianMap}, -}; - -namespace ImguiTranslate { - -std::string tr(std::string input) { - // since we're coding in English - if (EmulatorSettings.GetConsoleLanguage() == 1 || EmulatorSettings.GetConsoleLanguage() == 18) - return input; - - const std::map translationTable = - langMap.at(EmulatorSettings.GetConsoleLanguage()); - - if (!translationTable.contains(input)) { - return input; - } - - return translationTable.at(input); -} - } // namespace ImguiTranslate From 2334981be80f8513acb352ce8c442077609e3beb Mon Sep 17 00:00:00 2001 From: rainmakerv2 <30595646+rainmakerv3@users.noreply.github.com> Date: Sun, 29 Mar 2026 15:21:21 +0800 Subject: [PATCH 12/14] respect emulator settings home directory for trophies (#4188) --- src/core/libraries/np/np_trophy.cpp | 4 ++-- src/emulator.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/libraries/np/np_trophy.cpp b/src/core/libraries/np/np_trophy.cpp index 287d8d295..449ee775a 100644 --- a/src/core/libraries/np/np_trophy.cpp +++ b/src/core/libraries/np/np_trophy.cpp @@ -222,8 +222,8 @@ s32 PS4_SYSV_ABI sceNpTrophyCreateContext(OrbisNpTrophyContext* context, const std::string np_comm_id = Common::ElfInfo::Instance().GetNpCommIds()[service_label]; const auto trophy_base = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "trophy" / np_comm_id; - ctx.xml_save_file = Common::FS::GetUserPath(Common::FS::PathType::HomeDir) / - std::to_string(user_id) / "trophy" / (np_comm_id + ".xml"); + ctx.xml_save_file = + EmulatorSettings.GetHomeDir() / std::to_string(user_id) / "trophy" / (np_comm_id + ".xml"); ctx.xml_dir = trophy_base / "Xml"; ctx.icons_dir = trophy_base / "Icons"; ctx.trophy_xml_path = GetTrophyXmlPath(ctx.xml_dir, EmulatorSettings.GetConsoleLanguage()); diff --git a/src/emulator.cpp b/src/emulator.cpp index 7e62680f6..a9496f5f0 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -318,9 +318,9 @@ void Emulator::Run(std::filesystem::path file, std::vector args, } } for (User user : UserSettings.GetUserManager().GetValidUsers()) { - auto const user_trophy_file = - Common::FS::GetUserPath(Common::FS::PathType::HomeDir) / - std::to_string(user.user_id) / "trophy" / (npCommId + ".xml"); + auto const user_trophy_file = EmulatorSettings.GetHomeDir() / + std::to_string(user.user_id) / "trophy" / + (npCommId + ".xml"); if (!std::filesystem::exists(user_trophy_file)) { auto temp = user_trophy_file.parent_path(); std::filesystem::create_directories(temp); From a87abee8e30f6b060f1dc70d137e7cc497d58b12 Mon Sep 17 00:00:00 2001 From: Ploo <239304139+xinitrcn1@users.noreply.github.com> Date: Mon, 30 Mar 2026 10:44:29 +0000 Subject: [PATCH 13/14] WIP: port: Add x64 FreeBSD (#3927) * port: Add x64 FreeBSD * clang formaa * fix epoll stuffs * date-tz for fbsd, force submodule zydis * fix filesystem hang + date-tz * fix * fix freebsd SIGBUS * madvise() ifdef * signal fix + camera fix * proper %gs tls for once * better tls? + clang format --------- Co-authored-by: lizzie --- CMakeLists.txt | 13 +++- externals/CMakeLists.txt | 24 ++++--- src/common/adaptive_mutex.h | 2 +- src/common/signal_context.cpp | 31 ++++++--- src/core/address_space.cpp | 17 ++++- src/core/debugger.cpp | 8 ++- src/core/file_sys/fs.cpp | 64 ++++++++++--------- .../libraries/kernel/threads/exception.cpp | 24 ++++++- src/core/libraries/kernel/threads/exception.h | 2 +- src/core/libraries/kernel/time.cpp | 4 +- src/core/libraries/network/net.cpp | 13 +++- src/core/libraries/network/net_epoll.cpp | 4 ++ src/core/libraries/network/net_epoll.h | 5 +- src/core/libraries/network/net_util.cpp | 9 ++- src/core/tls.cpp | 11 +++- src/emulator.cpp | 2 +- src/sdl_window.cpp | 5 +- src/video_core/buffer_cache/region_manager.h | 2 +- src/video_core/page_manager.cpp | 34 +++++----- src/video_core/page_manager.h | 10 +-- 20 files changed, 189 insertions(+), 95 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e6349ff9..147903a7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -245,7 +245,7 @@ find_package(VulkanMemoryAllocator 3.1.0 CONFIG) find_package(xbyak 7.07 CONFIG) find_package(xxHash 0.8.2 MODULE) find_package(ZLIB 1.3 MODULE) -find_package(Zydis 5.0.0 CONFIG) +find_package(Zydis 5.0.0 MODULE) find_package(pugixml 1.14 CONFIG) if (APPLE) find_package(date 3.0.1 CONFIG) @@ -1139,7 +1139,14 @@ create_target_directory_groups(shadps4) target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API FFmpeg::ffmpeg Dear_ImGui gcn half::half ZLIB::ZLIB PNG::PNG) target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator LibAtrac9 sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::glslang SDL3::SDL3 SDL3_mixer::SDL3_mixer pugixml::pugixml) -target_link_libraries(shadps4 PRIVATE stb::headers libusb::usb lfreist-hwinfo::hwinfo nlohmann_json::nlohmann_json miniz::miniz fdk-aac CLI11::CLI11 OpenAL::OpenAL Cpp_Httplib) +target_link_libraries(shadps4 PRIVATE stb::headers lfreist-hwinfo::hwinfo nlohmann_json::nlohmann_json miniz::miniz fdk-aac CLI11::CLI11 OpenAL::OpenAL Cpp_Httplib) + +if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") + target_link_libraries(shadps4 PRIVATE "/usr/lib/libusb.so") + target_link_libraries(shadps4 PRIVATE "/usr/local/lib/libuuid.so") +else() + target_link_libraries(shadps4 PRIVATE libusb::usb) +endif() target_compile_definitions(shadps4 PRIVATE IMGUI_USER_CONFIG="imgui/imgui_config.h") target_compile_definitions(Dear_ImGui PRIVATE IMGUI_USER_CONFIG="${PROJECT_SOURCE_DIR}/src/imgui/imgui_config.h") @@ -1185,6 +1192,8 @@ if (APPLE) # Replacement for std::chrono::time_zone target_link_libraries(shadps4 PRIVATE date::date-tz epoll-shim) +elseif (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") + target_link_libraries(shadps4 PRIVATE date::date-tz epoll-shim) endif() if (WIN32) diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 41a0f71c7..9e19e1404 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -210,8 +210,15 @@ endif() # libusb if (NOT TARGET libusb::usb) - add_subdirectory(ext-libusb) - add_library(libusb::usb ALIAS usb-1.0) + if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") + # YOU MUST USE NATIVE LIBUSB + # using anything else will crash instantly, also freebsd will NOT like it + # no you cant vendor this libusb, its builtin on freebsd + find_package(libusb) + else() + add_subdirectory(ext-libusb) + add_library(libusb::usb ALIAS usb-1.0) + endif() endif() # Discord RPC @@ -233,25 +240,26 @@ endif() set(HWINFO_STATIC ON) add_subdirectory(hwinfo) -# Apple-only dependencies -if (APPLE) +if (APPLE OR ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") # date if (NOT TARGET date::date-tz) option(BUILD_TZ_LIB "" ON) option(USE_SYSTEM_TZ_DB "" ON) add_subdirectory(date) endif() + if (NOT TARGET epoll-shim) + add_subdirectory(epoll-shim) + endif() +endif() +# Apple-only dependencies +if (APPLE) # MoltenVK if (NOT TARGET MoltenVK) set(MVK_EXCLUDE_SPIRV_TOOLS ON) set(MVK_USE_METAL_PRIVATE_API ON) add_subdirectory(MoltenVK) endif() - - if (NOT TARGET epoll-shim) - add_subdirectory(epoll-shim) - endif() endif() #windows only diff --git a/src/common/adaptive_mutex.h b/src/common/adaptive_mutex.h index 2ab385bdb..247d4c1ec 100644 --- a/src/common/adaptive_mutex.h +++ b/src/common/adaptive_mutex.h @@ -3,7 +3,7 @@ #pragma once -#ifdef __linux__ +#if __unix__ #include #endif diff --git a/src/common/signal_context.cpp b/src/common/signal_context.cpp index 112160bc8..b1ac8d96f 100644 --- a/src/common/signal_context.cpp +++ b/src/common/signal_context.cpp @@ -7,6 +7,9 @@ #ifdef _WIN32 #include +#elif defined(__FreeBSD__) +#include +#include #else #include #endif @@ -22,6 +25,16 @@ void* GetXmmPointer(void* ctx, u8 index) { #define CASE(index) \ case index: \ return (void*)(&((ucontext_t*)ctx)->uc_mcontext->__fs.__fpu_xmm##index); +#elif defined(__FreeBSD__) + // In mc_fpstate + // See for the internals of mc_fpstate[]. +#define CASE(index) \ + case index: { \ + auto& mctx = ((ucontext_t*)ctx)->uc_mcontext; \ + ASSERT(mctx.mc_fpformat == _MC_FPFMT_XMM); \ + auto* s_fpu = (struct savefpu*)(&mctx.mc_fpstate[0]); \ + return (void*)(&(s_fpu->sv_xmm[0])); \ + } #else #define CASE(index) \ case index: \ @@ -57,6 +70,8 @@ void* GetRip(void* ctx) { return (void*)((EXCEPTION_POINTERS*)ctx)->ContextRecord->Rip; #elif defined(__APPLE__) return (void*)((ucontext_t*)ctx)->uc_mcontext->__ss.__rip; +#elif defined(__FreeBSD__) + return (void*)((ucontext_t*)ctx)->uc_mcontext.mc_rip; #else return (void*)((ucontext_t*)ctx)->uc_mcontext.gregs[REG_RIP]; #endif @@ -67,6 +82,8 @@ void IncrementRip(void* ctx, u64 length) { ((EXCEPTION_POINTERS*)ctx)->ContextRecord->Rip += length; #elif defined(__APPLE__) ((ucontext_t*)ctx)->uc_mcontext->__ss.__rip += length; +#elif defined(__FreeBSD__) + ((ucontext_t*)ctx)->uc_mcontext.mc_rip += length; #else ((ucontext_t*)ctx)->uc_mcontext.gregs[REG_RIP] += length; #endif @@ -75,18 +92,16 @@ void IncrementRip(void* ctx, u64 length) { bool IsWriteError(void* ctx) { #if defined(_WIN32) return ((EXCEPTION_POINTERS*)ctx)->ExceptionRecord->ExceptionInformation[0] == 1; -#elif defined(__APPLE__) -#if defined(ARCH_X86_64) +#elif defined(__APPLE__) && defined(ARCH_X86_64) return ((ucontext_t*)ctx)->uc_mcontext->__es.__err & 0x2; -#elif defined(ARCH_ARM64) +#elif defined(__APPLE__) && defined(ARCH_ARM64) return ((ucontext_t*)ctx)->uc_mcontext->__es.__esr & 0x40; -#endif -#else -#if defined(ARCH_X86_64) +#elif defined(__FreeBSD__) && defined(ARCH_X86_64) + return ((ucontext_t*)ctx)->uc_mcontext.mc_err & 0x2; +#elif defined(ARCH_X86_64) return ((ucontext_t*)ctx)->uc_mcontext.gregs[REG_ERR] & 0x2; #else #error "Unsupported architecture" #endif -#endif } -} // namespace Common \ No newline at end of file +} // namespace Common diff --git a/src/core/address_space.cpp b/src/core/address_space.cpp index ca3d52042..4830f65a5 100644 --- a/src/core/address_space.cpp +++ b/src/core/address_space.cpp @@ -613,7 +613,11 @@ struct AddressSpace::Impl { user_size = UserSize; constexpr int protection_flags = PROT_READ | PROT_WRITE; - constexpr int map_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED; + int map_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED; // compiler knows its constexpr +#if !defined(__FreeBSD__) + map_flags |= MAP_NORESERVE; +#endif + #if defined(__APPLE__) && defined(ARCH_X86_64) // On ARM64 Macs, we run into limitations due to the commpage from 0xFC0000000 - 0xFFFFFFFFF // and the GPU carveout region from 0x1000000000 - 0x6FFFFFFFFF. Because this creates gaps @@ -628,7 +632,7 @@ struct AddressSpace::Impl { mmap(reinterpret_cast(USER_MIN), user_size, protection_flags, map_flags, -1, 0)); #else const auto virtual_size = system_managed_size + system_reserved_size + user_size; -#if defined(ARCH_X86_64) +#if defined(ARCH_X86_64) && !defined(__FreeBSD__) const auto virtual_base = reinterpret_cast(mmap(reinterpret_cast(SYSTEM_MANAGED_MIN), virtual_size, protection_flags, map_flags, -1, 0)); @@ -636,8 +640,10 @@ struct AddressSpace::Impl { system_reserved_base = reinterpret_cast(SYSTEM_RESERVED_MIN); user_base = reinterpret_cast(USER_MIN); #else + // FreeBSD can't stand MAP_FIXED or it may overwrite mmap() itself! // Map memory wherever possible and instruction translation can handle offsetting to the // base. + map_flags &= ~MAP_FIXED; const auto virtual_base = reinterpret_cast(mmap(nullptr, virtual_size, protection_flags, map_flags, -1, 0)); system_managed_base = virtual_base; @@ -676,8 +682,13 @@ struct AddressSpace::Impl { } shm_unlink(shm_path.c_str()); #else +#ifndef __FreeBSD__ madvise(virtual_base, virtual_size, MADV_HUGEPAGE); - +#endif + // NOTE: If you add MFD_HUGETLB or whatever, remember that FBSD will break (libc bug) + // so please, do not, add MFD_* whatever unless you ifdef it away (must be 0 for FBSD) + // using sized pages as well causes incessant vm_reclaim calls in kernel, do not use on FBSD + // under any circumstances. backing_fd = memfd_create("BackingDmem", 0); if (backing_fd < 0) { LOG_CRITICAL(Kernel_Vmm, "memfd_create failed: {}", strerror(errno)); diff --git a/src/core/debugger.cpp b/src/core/debugger.cpp index 16071ee69..b396a3ba5 100644 --- a/src/core/debugger.cpp +++ b/src/core/debugger.cpp @@ -12,7 +12,7 @@ #elif defined(__linux__) #include #include -#elif defined(__APPLE__) +#elif defined(__APPLE__) || defined(__FreeBSD__) #include #include #include @@ -48,6 +48,8 @@ bool Core::Debugger::IsDebuggerAttached() { return (info.kp_proc.p_flag & P_TRACED) != 0; } return false; +#elif defined(__FreeBSD__) + return false; #else #error "Unsupported platform" #endif @@ -66,7 +68,7 @@ void Core::Debugger::WaitForDebuggerAttach() { int Core::Debugger::GetCurrentPid() { #if defined(_WIN32) return GetCurrentProcessId(); -#elif defined(__APPLE__) || defined(__linux__) +#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) return getpid(); #else #error "Unsupported platform" @@ -88,7 +90,7 @@ void Core::Debugger::WaitForPid(int pid) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::cerr << "Waiting for process " << pid << " to exit..." << std::endl; } -#elif defined(__APPLE__) +#elif defined(__APPLE__) || defined(__FreeBSD__) while (kill(pid, 0) == 0) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::cerr << "Waiting for process " << pid << " to exit..." << std::endl; diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index 2fdf8c10b..aa474d20a 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -95,7 +95,7 @@ std::filesystem::path MntPoints::GetHostPath(std::string_view path, bool* is_rea std::scoped_lock lk{m_mutex}; path_parts.clear(); auto current_path = host_path; - while (!std::filesystem::exists(current_path)) { + while (!current_path.empty() && !std::filesystem::exists(current_path)) { // We have probably cached this if it's a folder. if (auto it = path_cache.find(current_path); it != path_cache.end()) { current_path = it->second; @@ -104,38 +104,40 @@ std::filesystem::path MntPoints::GetHostPath(std::string_view path, bool* is_rea path_parts.emplace_back(current_path.filename()); current_path = current_path.parent_path(); } - // We have found an anchor. Traverse parts we recoded and see if they - // exist in filesystem but in different case. - auto guest_path = current_path; - while (!path_parts.empty()) { - const auto part = path_parts.back(); - const auto add_match = [&](const auto& host_part) { - current_path /= host_part; - guest_path /= part; - path_cache[guest_path] = current_path; - path_parts.pop_back(); - }; - // Can happen when the mismatch is in upper folder. - if (std::filesystem::exists(current_path / part)) { - add_match(part); - continue; - } - const auto part_low = Common::ToLower(part.string()); - bool found_match = false; - for (const auto& path : std::filesystem::directory_iterator(current_path)) { - const auto candidate = path.path().filename(); - const auto filename = Common::ToLower(candidate.string()); - // Check if a filename matches in case insensitive manner. - if (filename != part_low) { + if (!current_path.empty()) { + // We have found an anchor. Traverse parts we recoded and see if they + // exist in filesystem but in different case. + auto guest_path = current_path; + while (!path_parts.empty()) { + const auto part = path_parts.back(); + const auto add_match = [&](const auto& host_part) { + current_path /= host_part; + guest_path /= part; + path_cache[guest_path] = current_path; + path_parts.pop_back(); + }; + // Can happen when the mismatch is in upper folder. + if (std::filesystem::exists(current_path / part)) { + add_match(part); continue; } - // We found a match, record the actual path in the cache. - add_match(candidate); - found_match = true; - break; - } - if (!found_match) { - return std::optional({}); + const auto part_low = Common::ToLower(part.string()); + bool found_match = false; + for (const auto& path : std::filesystem::directory_iterator(current_path)) { + const auto candidate = path.path().filename(); + const auto filename = Common::ToLower(candidate.string()); + // Check if a filename matches in case insensitive manner. + if (filename != part_low) { + continue; + } + // We found a match, record the actual path in the cache. + add_match(candidate); + found_match = true; + break; + } + if (!found_match) { + return std::optional({}); + } } } return std::optional(current_path); diff --git a/src/core/libraries/kernel/threads/exception.cpp b/src/core/libraries/kernel/threads/exception.cpp index e2fd032f5..3d855af3d 100644 --- a/src/core/libraries/kernel/threads/exception.cpp +++ b/src/core/libraries/kernel/threads/exception.cpp @@ -215,6 +215,28 @@ void SigactionHandler(int native_signum, siginfo_t* inf, ucontext_t* raw_context ctx.uc_mcontext.mc_gs = regs.__gs; ctx.uc_mcontext.mc_rip = regs.__rip; ctx.uc_mcontext.mc_addr = reinterpret_cast(inf->si_addr); +#elif defined(__FreeBSD__) + const auto& regs = raw_context->uc_mcontext; + ctx.uc_mcontext.mc_r8 = regs.mc_r8; + ctx.uc_mcontext.mc_r9 = regs.mc_r9; + ctx.uc_mcontext.mc_r10 = regs.mc_r10; + ctx.uc_mcontext.mc_r11 = regs.mc_r11; + ctx.uc_mcontext.mc_r12 = regs.mc_r12; + ctx.uc_mcontext.mc_r13 = regs.mc_r13; + ctx.uc_mcontext.mc_r14 = regs.mc_r14; + ctx.uc_mcontext.mc_r15 = regs.mc_r15; + ctx.uc_mcontext.mc_rdi = regs.mc_rdi; + ctx.uc_mcontext.mc_rsi = regs.mc_rsi; + ctx.uc_mcontext.mc_rbp = regs.mc_rbp; + ctx.uc_mcontext.mc_rbx = regs.mc_rbx; + ctx.uc_mcontext.mc_rdx = regs.mc_rdx; + ctx.uc_mcontext.mc_rax = regs.mc_rax; + ctx.uc_mcontext.mc_rcx = regs.mc_rcx; + ctx.uc_mcontext.mc_rsp = regs.mc_rsp; + ctx.uc_mcontext.mc_fs = regs.mc_fs; + ctx.uc_mcontext.mc_gs = regs.mc_gs; + ctx.uc_mcontext.mc_rip = regs.mc_rip; + ctx.uc_mcontext.mc_addr = uint64_t(regs.mc_addr); #else const auto& regs = raw_context->uc_mcontext.gregs; ctx.uc_mcontext.mc_r8 = regs[REG_R8]; @@ -303,7 +325,7 @@ s32 PS4_SYSV_ABI posix_sigaction(s32 sig, Sigaction* act, Sigaction* oact) { *__Error() = POSIX_EINVAL; return ORBIS_FAIL; } -#ifndef __APPLE__ +#if !defined(__APPLE__) && !defined(__FreeBSD__) if (native_sig >= __SIGRTMIN && native_sig < SIGRTMIN) { LOG_ERROR(Lib_Kernel, "Guest is attempting to use the HLE libc-reserved signal {}!", sig); *__Error() = POSIX_EINVAL; diff --git a/src/core/libraries/kernel/threads/exception.h b/src/core/libraries/kernel/threads/exception.h index c07242c1d..f8cd06549 100644 --- a/src/core/libraries/kernel/threads/exception.h +++ b/src/core/libraries/kernel/threads/exception.h @@ -47,7 +47,7 @@ constexpr s32 POSIX_SIGUSR2 = 31; constexpr s32 POSIX_SIGTHR = 32; constexpr s32 POSIX_SIGLIBRT = 33; -#ifdef __linux__ +#if defined(__linux__) || defined(__FreeBSD__) constexpr s32 _SIGEMT = 128; constexpr s32 _SIGINFO = 129; #elif !defined(_WIN32) diff --git a/src/core/libraries/kernel/time.cpp b/src/core/libraries/kernel/time.cpp index 3e1648b98..2967dd4bf 100644 --- a/src/core/libraries/kernel/time.cpp +++ b/src/core/libraries/kernel/time.cpp @@ -17,7 +17,7 @@ #include #include "common/ntapi.h" #else -#ifdef __APPLE__ +#if defined(__APPLE__) || defined(__FreeBSD__) #include #endif #include @@ -501,7 +501,7 @@ s32 PS4_SYSV_ABI sceKernelConvertUtcToLocaltime(time_t time, time_t* local_time, *dst_sec = res == TIME_ZONE_ID_DAYLIGHT ? -_dstbias : 0; } #else -#ifdef __APPLE__ +#if defined(__APPLE__) || defined(__FreeBSD__) // std::chrono::current_zone() not available yet. const auto* time_zone = date::current_zone(); #else diff --git a/src/core/libraries/network/net.cpp b/src/core/libraries/network/net.cpp index 6bf4764c4..baf45b64f 100644 --- a/src/core/libraries/network/net.cpp +++ b/src/core/libraries/network/net.cpp @@ -663,10 +663,12 @@ int PS4_SYSV_ABI sceNetEpollControl(OrbisNetId epollid, OrbisNetEpollFlag op, Or return ORBIS_NET_ERROR_EBADF; } +#ifndef __FreeBSD__ epoll_event native_event = {.events = ConvertEpollEventsIn(event->events), .data = {.fd = id}}; ASSERT(epoll_ctl(epoll->epoll_fd, EPOLL_CTL_ADD, *native_handle, &native_event) == 0); epoll->events.emplace_back(id, *event); +#endif break; } case Core::FileSys::FileType::Resolver: { @@ -711,10 +713,12 @@ int PS4_SYSV_ABI sceNetEpollControl(OrbisNetId epollid, OrbisNetEpollFlag op, Or return ORBIS_NET_ERROR_EBADF; } +#ifndef __FreeBSD__ epoll_event native_event = {.events = ConvertEpollEventsIn(event->events), .data = {.fd = id}}; ASSERT(epoll_ctl(epoll->epoll_fd, EPOLL_CTL_MOD, *native_handle, &native_event) == 0); *it = {id, *event}; +#endif break; } default: @@ -752,9 +756,10 @@ int PS4_SYSV_ABI sceNetEpollControl(OrbisNetId epollid, OrbisNetEpollFlag op, Or *sceNetErrnoLoc() = ORBIS_NET_EBADF; return ORBIS_NET_ERROR_EBADF; } - +#ifndef __FreeBSD__ ASSERT(epoll_ctl(epoll->epoll_fd, EPOLL_CTL_DEL, *native_handle, nullptr) == 0); epoll->events.erase(it); +#endif break; } case Core::FileSys::FileType::Resolver: { @@ -810,6 +815,9 @@ int PS4_SYSV_ABI sceNetEpollDestroy(OrbisNetId epollid) { int PS4_SYSV_ABI sceNetEpollWait(OrbisNetId epollid, OrbisNetEpollEvent* events, int maxevents, int timeout) { +#ifdef __FreeBSD__ + return 0; +#else auto file = FDTable::Instance()->GetEpoll(epollid); if (!file) { *sceNetErrnoLoc() = ORBIS_NET_EBADF; @@ -836,7 +844,6 @@ int PS4_SYSV_ABI sceNetEpollWait(OrbisNetId epollid, OrbisNetEpollEvent* events, } int i = 0; - if (result < 0) { LOG_ERROR(Lib_Net, "epoll_wait failed with {}", Common::GetLastErrorMsg()); switch (errno) { @@ -905,8 +912,8 @@ int PS4_SYSV_ABI sceNetEpollWait(OrbisNetId epollid, OrbisNetEpollEvent* events, ++i; } } - return i; +#endif } int* PS4_SYSV_ABI sceNetErrnoLoc() { diff --git a/src/core/libraries/network/net_epoll.cpp b/src/core/libraries/network/net_epoll.cpp index e64c8ac64..4f1b521ce 100644 --- a/src/core/libraries/network/net_epoll.cpp +++ b/src/core/libraries/network/net_epoll.cpp @@ -10,12 +10,14 @@ namespace Libraries::Net { u32 ConvertEpollEventsIn(u32 orbis_events) { u32 ret = 0; +#ifndef __FreeBSD__ if ((orbis_events & ORBIS_NET_EPOLLIN) != 0) { ret |= EPOLLIN; } if ((orbis_events & ORBIS_NET_EPOLLOUT) != 0) { ret |= EPOLLOUT; } +#endif return ret; } @@ -23,6 +25,7 @@ u32 ConvertEpollEventsIn(u32 orbis_events) { u32 ConvertEpollEventsOut(u32 epoll_events) { u32 ret = 0; +#ifndef __FreeBSD__ if ((epoll_events & EPOLLIN) != 0) { ret |= ORBIS_NET_EPOLLIN; } @@ -35,6 +38,7 @@ u32 ConvertEpollEventsOut(u32 epoll_events) { if ((epoll_events & EPOLLHUP) != 0) { ret |= ORBIS_NET_EPOLLHUP; } +#endif return ret; } diff --git a/src/core/libraries/network/net_epoll.h b/src/core/libraries/network/net_epoll.h index 37555484d..17716b36e 100644 --- a/src/core/libraries/network/net_epoll.h +++ b/src/core/libraries/network/net_epoll.h @@ -14,7 +14,8 @@ #include #endif -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +// ADD libepoll-shim if using freebsd! #include #include #endif @@ -82,4 +83,4 @@ private: std::mutex m_mutex; }; -} // namespace Libraries::Net \ No newline at end of file +} // namespace Libraries::Net diff --git a/src/core/libraries/network/net_util.cpp b/src/core/libraries/network/net_util.cpp index de47f7bc1..fd0ed877b 100644 --- a/src/core/libraries/network/net_util.cpp +++ b/src/core/libraries/network/net_util.cpp @@ -25,7 +25,7 @@ typedef int net_socket; #include #include #endif -#if __linux__ +#if defined(__linux__) || defined(__FreeBSD__) #include #include #include @@ -81,6 +81,8 @@ bool NetUtilInternal::RetrieveEthernetAddr() { } freeifaddrs(ifap); } +#elif defined(__FreeBSD__) + // todo #else ifreq ifr; ifconf ifc; @@ -226,7 +228,8 @@ bool NetUtilInternal::RetrieveDefaultGateway() { inet_ntop(AF_INET, gateAddr, str, sizeof(str)); this->default_gateway = str; return true; - +#elif defined(__FreeBSD__) + return true; #else std::ifstream route{"/proc/net/route"}; std::string line; @@ -398,4 +401,4 @@ int NetUtilInternal::ResolveHostname(const char* hostname, Libraries::Net::Orbis return ret; } -} // namespace NetUtil \ No newline at end of file +} // namespace NetUtil diff --git a/src/core/tls.cpp b/src/core/tls.cpp index 8b926cb39..f87248114 100644 --- a/src/core/tls.cpp +++ b/src/core/tls.cpp @@ -10,6 +10,8 @@ #ifdef _WIN32 #include +#elif defined(__FreeBSD__) +#include #elif defined(__APPLE__) && defined(ARCH_X86_64) #include #include @@ -157,12 +159,17 @@ Tcb* GetTcbBase() { #elif defined(ARCH_X86_64) -// Other POSIX x86_64 - +// Linux x86_64 +#if defined(__FreeBSD__) +void SetTcbBase(void* image_address) { + amd64_set_gsbase(image_address); +} +#else void SetTcbBase(void* image_address) { const int ret = syscall(SYS_arch_prctl, ARCH_SET_GS, (unsigned long)image_address); ASSERT_MSG(ret == 0, "Failed to set GS base: errno {}", errno); } +#endif Tcb* GetTcbBase() { return Libraries::Kernel::g_curthread->tcb; diff --git a/src/emulator.cpp b/src/emulator.cpp index a9496f5f0..616a21ed1 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -535,7 +535,7 @@ void Emulator::Restart(std::filesystem::path eboot_path, CloseHandle(pi.hProcess); CloseHandle(pi.hThread); -#elif defined(__APPLE__) || defined(__linux__) +#elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) std::vector argv; // Emulator executable diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index 766a336c2..060197533 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -96,7 +96,7 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameControllers* controller UNREACHABLE_MSG("Failed to initialize SDL video subsystem: {}", SDL_GetError()); } if (!SDL_Init(SDL_INIT_CAMERA)) { - UNREACHABLE_MSG("Failed to initialize SDL camera subsystem: {}", SDL_GetError()); + LOG_ERROR(Input, "Failed to initialize SDL camera subsystem: {}", SDL_GetError()); } SDL_InitSubSystem(SDL_INIT_AUDIO); @@ -141,7 +141,8 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameControllers* controller window_info.type = WindowSystemType::Windows; window_info.render_surface = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); -#elif defined(SDL_PLATFORM_LINUX) +#elif defined(SDL_PLATFORM_LINUX) || defined(__FreeBSD__) + // SDL doesn't have a platform define for FreeBSD AAAAAAAAAA if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) { window_info.type = WindowSystemType::X11; window_info.display_connection = SDL_GetPointerProperty( diff --git a/src/video_core/buffer_cache/region_manager.h b/src/video_core/buffer_cache/region_manager.h index a760dd596..742268753 100644 --- a/src/video_core/buffer_cache/region_manager.h +++ b/src/video_core/buffer_cache/region_manager.h @@ -7,7 +7,7 @@ #include "common/logging/log.h" #include "core/emulator_settings.h" -#ifdef __linux__ +#ifdef __unix__ #include "common/adaptive_mutex.h" #else #include "common/spin_lock.h" diff --git a/src/video_core/page_manager.cpp b/src/video_core/page_manager.cpp index 2bf16afe0..6a4bcbd7d 100644 --- a/src/video_core/page_manager.cpp +++ b/src/video_core/page_manager.cpp @@ -36,8 +36,8 @@ namespace VideoCore { -constexpr size_t PAGE_SIZE = 4_KB; -constexpr size_t PAGE_BITS = 12; +constexpr size_t PM_PAGE_SIZE = 4_KB; +constexpr size_t PM_PAGE_BITS = 12; struct PageManager::Impl { struct PageState { @@ -85,7 +85,7 @@ struct PageManager::Impl { }; static constexpr size_t ADDRESS_BITS = 40; - static constexpr size_t NUM_ADDRESS_PAGES = 1ULL << (40 - PAGE_BITS); + static constexpr size_t NUM_ADDRESS_PAGES = 1ULL << (40 - PM_PAGE_BITS); static constexpr size_t NUM_ADDRESS_LOCKS = NUM_ADDRESS_PAGES / PAGES_PER_LOCK; inline static Vulkan::Rasterizer* rasterizer; #ifdef ENABLE_USERFAULTFD @@ -222,8 +222,8 @@ struct PageManager::Impl { void UpdatePageWatchers(VAddr addr, u64 size) { RENDERER_TRACE; - size_t page = addr >> PAGE_BITS; - const u64 page_end = Common::DivCeil(addr + size, PAGE_SIZE); + size_t page = addr >> PM_PAGE_BITS; + const u64 page_end = Common::DivCeil(addr + size, PM_PAGE_SIZE); // Acquire locks for the range of pages const auto lock_start = locks.begin() + (page / PAGES_PER_LOCK); @@ -239,15 +239,15 @@ struct PageManager::Impl { if (range_bytes > 0) { RENDERER_TRACE; // Perform pending (un)protect action - Protect(range_begin << PAGE_BITS, range_bytes, perms); + Protect(range_begin << PM_PAGE_BITS, range_bytes, perms); range_bytes = 0; potential_range_bytes = 0; } }; // Iterate requested pages - const u64 aligned_addr = page << PAGE_BITS; - const u64 aligned_end = page_end << PAGE_BITS; + const u64 aligned_addr = page << PM_PAGE_BITS; + const u64 aligned_end = page_end << PM_PAGE_BITS; if (!rasterizer->IsMapped(aligned_addr, aligned_end - aligned_addr)) { LOG_WARNING(Render, "Tracking memory region {:#x} - {:#x} which is not fully GPU mapped.", @@ -266,7 +266,7 @@ struct PageManager::Impl { perms = new_perms; } else if (range_bytes != 0) { // If the protection did not change, extend the potential range - potential_range_bytes += PAGE_SIZE; + potential_range_bytes += PM_PAGE_SIZE; } // Only start a new range if the page must be (un)protected @@ -274,7 +274,7 @@ struct PageManager::Impl { if (range_bytes == 0) { // Start a new potential range range_begin = page; - potential_range_bytes = PAGE_SIZE; + potential_range_bytes = PM_PAGE_SIZE; } // Extend current range up to potential range range_bytes = potential_range_bytes; @@ -293,12 +293,12 @@ struct PageManager::Impl { if (start_range.second == end_range.second) { // if all pages are contiguous, use the regular UpdatePageWatchers - const VAddr start_addr = base_addr + (start_range.first << PAGE_BITS); - const u64 size = (start_range.second - start_range.first) << PAGE_BITS; + const VAddr start_addr = base_addr + (start_range.first << PM_PAGE_BITS); + const u64 size = (start_range.second - start_range.first) << PM_PAGE_BITS; return UpdatePageWatchers(start_addr, size); } - size_t base_page = (base_addr >> PAGE_BITS); + size_t base_page = (base_addr >> PM_PAGE_BITS); ASSERT(base_page % PAGES_PER_LOCK == 0); std::scoped_lock lk(locks[base_page / PAGES_PER_LOCK]); auto perms = cached_pages[base_page + start_range.first].Perms(); @@ -310,7 +310,7 @@ struct PageManager::Impl { if (range_bytes > 0) { RENDERER_TRACE; // Perform pending (un)protect action - Protect((range_begin << PAGE_BITS), range_bytes, perms); + Protect((range_begin << PM_PAGE_BITS), range_bytes, perms); range_bytes = 0; potential_range_bytes = 0; } @@ -331,7 +331,7 @@ struct PageManager::Impl { perms = new_perms; } else if (range_bytes != 0) { // If the protection did not change, extend the potential range - potential_range_bytes += PAGE_SIZE; + potential_range_bytes += PM_PAGE_SIZE; } // If the page is not being updated, skip it @@ -344,7 +344,7 @@ struct PageManager::Impl { if (range_bytes == 0) { // Start a new potential range range_begin = base_page + page; - potential_range_bytes = PAGE_SIZE; + potential_range_bytes = PM_PAGE_SIZE; } // Extend current rango up to potential range range_bytes = potential_range_bytes; @@ -356,7 +356,7 @@ struct PageManager::Impl { } std::array cached_pages{}; -#ifdef __linux__ +#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP using LockType = Common::AdaptiveMutex; #else using LockType = Common::SpinLock; diff --git a/src/video_core/page_manager.h b/src/video_core/page_manager.h index 4ca41cb43..fb53f7c98 100644 --- a/src/video_core/page_manager.h +++ b/src/video_core/page_manager.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include "common/alignment.h" #include "common/types.h" @@ -15,9 +16,10 @@ class Rasterizer; namespace VideoCore { class PageManager { + // PAGE_SIZE and PAGE_BITS conflicts with machine/param.h definitions on freebsd! // Use the same page size as the tracker. - static constexpr size_t PAGE_BITS = TRACKER_PAGE_BITS; - static constexpr size_t PAGE_SIZE = TRACKER_BYTES_PER_PAGE; + static constexpr size_t PM_PAGE_BITS = TRACKER_PAGE_BITS; + static constexpr size_t PM_PAGE_SIZE = TRACKER_BYTES_PER_PAGE; // Keep the lock granularity the same as region granularity. (since each regions has // itself a lock) @@ -43,12 +45,12 @@ public: /// Returns page aligned address. static constexpr VAddr GetPageAddr(VAddr addr) { - return Common::AlignDown(addr, PAGE_SIZE); + return Common::AlignDown(addr, PM_PAGE_SIZE); } /// Returns address of the next page. static constexpr VAddr GetNextPageAddr(VAddr addr) { - return Common::AlignUp(addr + 1, PAGE_SIZE); + return Common::AlignUp(addr + 1, PM_PAGE_SIZE); } private: From 969955b8a086b79dc107eabbb663ed9cf1308e60 Mon Sep 17 00:00:00 2001 From: Connor Garey Date: Mon, 30 Mar 2026 21:35:57 +0100 Subject: [PATCH 14/14] Addition of Nix flake development shell (#4184) * Initial devshell creation. * cmake has a check for clang and therefore override the stdenv. * Packages from old shell were renamed. * fixed xcb-util, added libglvnd * Added sdl3 dependencies provided by the website given on cmake configuration. * Lock file. * Nix format. * Added instructions for entering nix development shell. . * Added libuuid * Added copyright text to flake.nix * Added flake.lock to REUSE.toml as is a JSON file without comment support. * Updated instructions to refer to new build name. * Compiling however not yet correctly linking with debug derivation. * Hitting installPhase * Added nix result symlink. * correctly installs in place * Added a wrapper to load vulkan and ligl into environment. * Ensure that the name is applicable to the current project. . * Added mesa to LD_LIBRARY_PATH * game now launching with added X11 libraries. * Cleanup Formatting. Pulled cmakeFlags to top and added releaseWithDebugInfo Removed LD_LIBRARY_PATH from devshell. . * Added options for the different Nix build modes. * Debug / release mode flag cannot be bundled into one. --- .gitignore | 3 + REUSE.toml | 3 +- documents/building-linux.md | 17 ++++ flake.lock | 27 ++++++ flake.nix | 160 ++++++++++++++++++++++++++++++++++++ 5 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index 683f6f0a6..7ace5e4f6 100644 --- a/.gitignore +++ b/.gitignore @@ -418,3 +418,6 @@ FodyWeavers.xsd # JetBrains .idea cmake-build-* + +# Nix Result symlink +result diff --git a/REUSE.toml b/REUSE.toml index 22bed2a50..e8997f007 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -22,6 +22,7 @@ path = [ "documents/Screenshots/Linux/*", "documents/Screenshots/Windows/*", "externals/MoltenVK/MoltenVK_icd.json", + "flake.lock", "scripts/ps4_names.txt", "src/images/bronze.png", "src/images/gold.png", @@ -130,4 +131,4 @@ SPDX-License-Identifier = "MIT" [[annotations]] path = "src/video_core/host_shaders/fsr/*" SPDX-FileCopyrightText = "Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved." -SPDX-License-Identifier = "MIT" \ No newline at end of file +SPDX-License-Identifier = "MIT" diff --git a/documents/building-linux.md b/documents/building-linux.md index 49aacdfc9..9495a226b 100644 --- a/documents/building-linux.md +++ b/documents/building-linux.md @@ -53,6 +53,23 @@ sudo zypper install clang git cmake libasound2 libpulse-devel \ nix-shell shell.nix ``` +#### Nix Flake Development Shell +```bash +nix develop +cmake -S . -B build/ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON +ln -s ./build/compile_commands.json . +``` + +#### Nix Flake Build +```bash +nix build .?submodules=1#linux.debug +``` +```bash +nix build .?submodules=1#linux.release +``` +```bash +nix build .?submodules=1#linux.releaseWithDebugInfo +``` #### Other Linux distributions You can try one of two methods: diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..246cfd4e7 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1774386573, + "narHash": "sha256-4hAV26quOxdC6iyG7kYaZcM3VOskcPUrdCQd/nx8obc=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "46db2e09e1d3f113a13c0d7b81e2f221c63b8ce9", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..9042105c5 --- /dev/null +++ b/flake.nix @@ -0,0 +1,160 @@ +## SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +## SPDX-License-Identifier: GPL-2.0-or-later + +{ + description = "shadPS4 Nix Flake"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + }; + + outputs = + { self, nixpkgs }: + let + pkgsLinux = nixpkgs.legacyPackages.x86_64-linux; + in + { + devShells.x86_64-linux.default = pkgsLinux.mkShell.override { stdenv = pkgsLinux.clangStdenv; } { + packages = with pkgsLinux; [ + clang-tools + cmake + pkg-config + vulkan-tools + + renderdoc + gef + strace + + openal + zlib.dev + libedit.dev + vulkan-headers + vulkan-utility-libraries + ffmpeg.dev + fmt.dev + glslang.dev + wayland.dev + stb + libpng.dev + libuuid + + sdl3.dev + alsa-lib + hidapi + ibus.dev + jack2.dev + libdecor.dev + libthai.dev + fribidi.dev + libxcb.dev + libGL.dev + libpulseaudio.dev + libusb1.dev + libx11.dev + libxcursor.dev + libxext + libxfixes.dev + libxi.dev + libxinerama.dev + libxkbcommon + libxrandr.dev + libxrender.dev + libxtst + pipewire.dev + libxscrnsaver + sndio + ]; + shellHook = '' + echo "Entering shadPS4 development shell!" + ''; + }; + + linux = + let + execName = "shadps4"; + nativeInputs = with pkgsLinux; [ + cmake + ninja + pkg-config + magic-enum + fmt + eudev + ]; + buildInputs = with pkgsLinux; [ + boost + cli11 + openal + nlohmann_json + vulkan-loader + vulkan-headers + vulkan-memory-allocator + toml11 + zlib + zydis + pugixml + ffmpeg + libpulseaudio + pipewire + vulkan-loader + wayland + wayland-scanner + libX11 + libxrandr + libxext + libxcursor + libxi + libxscrnsaver + libxtst + libxcb + libdecor + libxkbcommon + libGL + libuuid + ]; + + defaultFlags = [ + "-DCMAKE_INSTALL_PREFIX=$out" + ]; + in + { + debug = pkgsLinux.stdenv.mkDerivation { + pname = "${execName}"; + version = "git"; + system = "x86_64-linux"; + src = ./.; + dontStrip = true; + + nativeBuildInputs = nativeInputs; + buildInputs = buildInputs; + cmakeFlags = [ + "-DCMAKE_BUILD_TYPE=Debug" + ] ++ [defaultFlags]; + }; + release = pkgsLinux.stdenv.mkDerivation { + pname = "${execName}"; + version = "git"; + system = "x86_64-linux"; + src = ./.; + + nativeBuildInputs = nativeInputs; + buildInputs = buildInputs; + cmakeFlags = [ + "-DCMAKE_BUILD_TYPE=Release" + ] ++ [defaultFlags]; + }; + releaseWithDebugInfo = pkgsLinux.stdenv.mkDerivation { + pname = "${execName}"; + version = "git"; + system = "x86_64-linux"; + src = ./.; + dontStrip = true; + + nativeBuildInputs = nativeInputs; + buildInputs = buildInputs; + cmakeFlags = [ + "-DCMAKE_BUILD_TYPE=Release" + ] ++ [defaultFlags]; + }; + }; + }; +}