Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.
C++
#pragma once
#include <windows.h>
#include <shlobj.h>
#include <filesystem>
#include <fstream>
#include <string>
namespace wsm::core {
// Portable, machine-agnostic WSM path resolver.
// Priority:
// 1. %WSM_ROOT% environment variable (if set)
// 2. %LOCALAPPDATA%\WSM\root.txt (user-selected storage folder in WSM Manager)
// 3. Directory containing the executable if it contains a `runtime` or `ui` sibling
// 4. %LOCALAPPDATA%\WSM (universal default on any Windows 10/11 PC)
inline std::string resolve_wsm_root() {
namespace fs = std::filesystem;
// 1. Environment variable override
char env_buf[MAX_PATH] = {};
DWORD n = GetEnvironmentVariableA("WSM_ROOT", env_buf, MAX_PATH);
if (n > 0 && n < MAX_PATH) {
return std::string(env_buf, n);
}
// 2. Persistent user-chosen storage path (%LOCALAPPDATA%\WSM\root.txt)
char local_app[MAX_PATH] = {};
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, local_app))) {
fs::path cfg = fs::path(local_app) / "WSM" / "root.txt";
if (fs::exists(cfg)) {
std::ifstream f(cfg);
std::string line;
if (std::getline(f, line) && !line.empty()) {
while (!line.empty() && (line.back() == '\r' || line.back() == '\n' || line.back() == ' ')) line.pop_back();
if (!line.empty()) return line;
}
}
}
// 3. Portable layout relative to exe (e.g. <root>\bin\wsmd.exe or <root>\build\native\bin\wsmd.exe)
char exe_buf[MAX_PATH] = {};
if (GetModuleFileNameA(nullptr, exe_buf, MAX_PATH) > 0) {
fs::path p = fs::path(exe_buf).parent_path();
for (int up = 0; up < 4; ++up) {
if (fs::exists(p / "src" / "wsm-manager") || fs::exists(p / "ui")) {
return p.string();
}
if (!p.has_parent_path() || p == p.parent_path()) break;
p = p.parent_path();
}
}
// 4. Universal fallback: %LOCALAPPDATA%\WSM
if (local_app[0] != '\0') {
return (fs::path(local_app) / "WSM").string();
}
return "C:\\WSM";
}
inline bool save_wsm_root(const std::string& new_root) {
namespace fs = std::filesystem;
char local_app[MAX_PATH] = {};
if (FAILED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, local_app))) return false;
fs::path dir = fs::path(local_app) / "WSM";
std::error_code ec;
fs::create_directories(dir, ec);
std::ofstream f(dir / "root.txt", std::ios::trunc);
if (!f) return false;
f << new_root << "\n";
return true;
}
// Auto-tunes guest vCPUs & RAM dynamically for ANY host PC (8 GB / 16 GB / 32 GB / 64 GB+)
inline void compute_recommended_guest_resources(uint32_t& out_vcpus, uint32_t& out_ram_mb) {
SYSTEM_INFO si{};
GetSystemInfo(&si);
uint32_t threads = si.dwNumberOfProcessors;
out_vcpus = (threads >= 12) ? 6 : (threads >= 8 ? 4 : (threads >= 4 ? 2 : 2));
MEMORYSTATUSEX ms{};
ms.dwLength = sizeof(ms);
uint64_t total_mb = 8192;
if (GlobalMemoryStatusEx(&ms)) {
total_mb = ms.ullTotalPhys / (1024ULL * 1024ULL);
}
// Use ~33% of host RAM, clamped to [4096 MB, 12288 MB] so Windows never swap-thrashes
uint64_t target = (total_mb / 3ULL / 1024ULL) * 1024ULL;
if (target < 4096) target = 4096;
if (target > 12288) target = 12288;
out_ram_mb = static_cast<uint32_t>(target);
}
} // namespace wsm::core