Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.
C++
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
#include "wsm/guest_app_path.hpp"
namespace wsm::runtime {
// BSD ps etime: mm:ss, hh:mm:ss, or days-hh:mm:ss.
inline int64_t desktop_elapsed_seconds(std::string_view text) {
while (!text.empty() && (text.front() == ' ' || text.front() == '\t')) text.remove_prefix(1);
while (!text.empty() && (text.back() == ' ' || text.back() == '\r' || text.back() == '\n')) text.remove_suffix(1);
int64_t days = 0, total = 0, value = 0;
int colons = 0; bool digits = false, has_days = false;
for (char c : text) {
if (c >= '0' && c <= '9') {
value = value * 10 + c - '0'; digits = true;
if (value > 1000000) return -1;
} else if (c == '-' && digits && !has_days && colons == 0) {
days = value; value = 0; digits = false; has_days = true;
} else if (c == ':' && digits && colons < 2) {
total = (total + value) * 60; value = 0; digits = false; ++colons;
} else return -1;
}
if (!digits || colons == 0 || (has_days && colons != 2) || value >= 60) return -1;
return days * 86400 + total + value;
}
// This watchdog only runs after runtime-ready, outside provisioning. Do not
// repeat the first-install migration deadline on every completed-runtime boot.
inline bool account_recovery_due(int64_t seconds) { return seconds >= 30; }
inline bool window_watcher_timed_out(int64_t quiet_ms, bool received_output) {
return quiet_ms >= (received_output ? 15000 : 120000);
}
inline const char* desktop_probe_command() {
return "P=$(pgrep -x UserAccountUpdater | head -n 1); "
"if [ -n \"$P\" ]; then printf 'UAU %s ' \"$P\"; ps -p \"$P\" -o etime=; "
"elif [ \"$(stat -f %Su /dev/console)\" = wsm ]; then "
"pgrep -x Dock >/dev/null || launchctl kickstart gui/$(id -u)/com.apple.Dock.agent 2>/dev/null; "
"pgrep -x Finder >/dev/null || launchctl kickstart gui/$(id -u)/com.apple.Finder 2>/dev/null; "
"if pgrep -x Dock >/dev/null && pgrep -x Finder >/dev/null; then echo READY; else echo BOOTING; fi; "
"else echo BOOTING; fi";
}
inline std::string gui_open_command(const std::string& app) {
const char* option = app.find('/') != std::string::npos ? "" : app.find('.') != std::string::npos ? "-b " : "-a ";
return "sudo -n /bin/launchctl asuser $(id -u) /usr/bin/sudo -n -u \"$(id -un)\" /usr/bin/open " +
std::string(option) + guest_shell_quote(app);
}
inline std::string recover_runtime_account_command(uint32_t pid) {
const auto p = std::to_string(pid);
const std::string identity = "[ \"$(ps -p " + p + " -o comm=)\" = /System/Library/CoreServices/UserAccountUpdater ]";
// Same bounded recovery used by provisioning, also needed on later boots.
// Validate the live executable, and record one attempt per process per boot.
return identity + " || exit 1; M=/private/var/run/wsm-uau-" + p + ".recovered; "
"[ ! -e \"$M\" ] || exit 0; sudo -n touch \"$M\" || exit 1; "
"sudo -n pkill -TERM -x IndexSettings 2>/dev/null; "
"sudo -n pkill -TERM -x languageassetd 2>/dev/null; "
"sudo -n kill -TERM " + p + "; sleep 2; " + identity + " && sudo -n kill -KILL " + p + "; true";
}
}