Skip to content

armgit / WSM

Public Imported from GitHub
0

Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.

C++
1.9 KB
Raw Blame
#pragma once
#include <windows.h>
#include <mutex>

namespace wsm::runtime {
// Explicitly enabled by wsmd. Children survive a daemon hot-swap; only the user's
// Quit WSM action terminates the job. Other WSM installations never join it.
inline std::mutex children_mutex;
inline HANDLE children_job = nullptr;
inline bool children_closing = false;
inline bool initialize_child_processes() {
    std::lock_guard<std::mutex> lock(children_mutex);
    if (!children_job) children_job = CreateJobObjectW(nullptr, nullptr);
    return children_job != nullptr;
}
inline BOOL create_owned_process_a(LPCSTR app, LPSTR line, LPSECURITY_ATTRIBUTES process_security,
    LPSECURITY_ATTRIBUTES thread_security, BOOL inherit, DWORD flags, LPVOID environment,
    LPCSTR directory, LPSTARTUPINFOA startup, LPPROCESS_INFORMATION process) {
    std::lock_guard<std::mutex> lock(children_mutex);
    if (children_closing) { SetLastError(ERROR_SHUTDOWN_IN_PROGRESS); return FALSE; }
    if (!children_job) return CreateProcessA(app, line, process_security, thread_security, inherit,
                                             flags, environment, directory, startup, process);
    if (!CreateProcessA(app, line, process_security, thread_security, inherit, flags | CREATE_SUSPENDED,
                        environment, directory, startup, process)) return FALSE;
    if (!AssignProcessToJobObject(children_job, process->hProcess)) {
        const auto error = GetLastError();
        TerminateProcess(process->hProcess, 1);
        CloseHandle(process->hThread); CloseHandle(process->hProcess);
        *process = {}; SetLastError(error); return FALSE;
    }
    if (!(flags & CREATE_SUSPENDED)) ResumeThread(process->hThread);
    return TRUE;
}
inline void stop_child_processes() {
    std::lock_guard<std::mutex> lock(children_mutex);
    children_closing = true;
    if (children_job) TerminateJobObject(children_job, 0);
}
}