Add Linux home path for GetUserPath().

This commit is contained in:
Dario 2024-12-14 16:04:41 -03:00
parent 9b6e4406b7
commit 2664f3bfd7
2 changed files with 30 additions and 6 deletions

View file

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

View file

@ -12,14 +12,35 @@ inline std::filesystem::path GetUserPath()
if (std::filesystem::exists("portable.txt"))
return std::filesystem::current_path();
std::filesystem::path userPath{};
std::filesystem::path userPath;
// TODO: handle platform-specific paths.
#if defined(_WIN32)
PWSTR knownPath = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &knownPath) == S_OK)
userPath = std::filesystem::path{ knownPath } / USER_DIRECTORY;
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;
}