Add Linux home path for GetUserPath(). (#36)

This commit is contained in:
Darío 2024-12-15 08:55:53 -03:00 committed by GitHub
parent 4a38878aa0
commit 9dcadb9e06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View file

@ -4,13 +4,19 @@
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
#include <ShlObj_core.h>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
#elif defined(__linux__)
#include <unistd.h>
#include <pwd.h>
#endif #endif
#ifdef SWA_D3D12 #ifdef SWA_D3D12
#include <dxcapi.h> #include <dxcapi.h>
#endif #endif
#include <ShlObj_core.h>
#include <algorithm> #include <algorithm>
#include <mutex> #include <mutex>
#include <filesystem> #include <filesystem>
@ -35,14 +41,11 @@
#include <imgui_impl_sdl2.h> #include <imgui_impl_sdl2.h>
#include <o1heap.h> #include <o1heap.h>
#include <cstddef> #include <cstddef>
#include <wrl/client.h>
#include <smolv.h> #include <smolv.h>
#include <set> #include <set>
#include <miniaudio.h> #include <miniaudio.h>
#include <extras/miniaudio_libvorbis.h> #include <extras/miniaudio_libvorbis.h>
#include <fmt/core.h> #include <fmt/core.h>
using Microsoft::WRL::ComPtr;
#include "framework.h" #include "framework.h"
#include "mutex.h" #include "mutex.h"

View file

@ -12,14 +12,35 @@ inline std::filesystem::path GetUserPath()
if (std::filesystem::exists("portable.txt")) if (std::filesystem::exists("portable.txt"))
return std::filesystem::current_path(); return std::filesystem::current_path();
std::filesystem::path userPath{}; std::filesystem::path userPath;
// TODO: handle platform-specific paths. #if defined(_WIN32)
PWSTR knownPath = NULL; PWSTR knownPath = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &knownPath) == S_OK) if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &knownPath) == S_OK)
userPath = std::filesystem::path{ knownPath } / USER_DIRECTORY; userPath = std::filesystem::path{ knownPath } / USER_DIRECTORY;
CoTaskMemFree(knownPath); CoTaskMemFree(knownPath);
#elif defined(__linux__)
const char *homeDir = getenv("HOME");
if (homeDir == nullptr)
{
homeDir = getpwuid(getuid())->pw_dir;
}
if (homeDir != nullptr)
{
// Prefer to store in the .config directory if it exists. Use the home directory otherwise.
const std::string dirName = "." USER_DIRECTORY;
std::filesystem::path homePath = homeDir;
std::filesystem::path configPath = homePath / ".config";
if (std::filesystem::exists(configPath))
userPath = configPath / dirName;
else
userPath = homePath / dirName;
}
#else
static_assert(false, "GetUserPath() not implemented for this platform.");
#endif
return userPath; return userPath;
} }