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++
5.5 KB
Raw Blame
#include "wsm/compositor.hpp"
#include "wsm/window_viewport.hpp"
#include <windows.h>
#include <windowsx.h>
#include <atomic>
#include <iostream>
#include <vector>

#define CHECK(x) do { if (!(x)) { std::cerr << "Fullscreen test failed at " << __LINE__ << ": " #x "\n"; return 1; } } while (0)
bool same_rect(RECT a, RECT b) { return EqualRect(&a, &b) != FALSE; }
int main(int argc, char** argv) {
    using namespace wsm::compositor;
    const auto view = window_viewport(1920, 1080, 800, 600, true);
    CHECK(view.x == 240 && view.y == 0 && view.width == 1440 && view.height == 1080);
    int x = 960, y = 540;
    CHECK(viewport_point(view, 800, 600, x, y) && x == 400 && y == 300);
    x = 10; y = 540; CHECK(!viewport_point(view, 800, 600, x, y));
    RootlessCompositor compositor;
    std::atomic<int> pointer_x{-1}, pointer_y{-1}, mouse_presses{0};
    std::atomic<int> last_button{-1}, last_clicks{0};
    InputCallbacks callbacks;
    callbacks.on_mouse_move = [&](const wsm::proto::InputMouseMoveEvent& event) { pointer_x = event.x; pointer_y = event.y; };
    callbacks.on_mouse_button = [&](const wsm::proto::InputMouseButtonEvent& event) {
        last_button = event.button; last_clicks = event.clicks;
        if (event.pressed) ++mouse_presses;
    };
    std::string error;
    CHECK(compositor.start(callbacks, error));
    const auto title = "WSM fullscreen test " + std::to_string(GetCurrentProcessId());
    wsm::proto::WindowDescriptor descriptor{};
    descriptor.window_id = 77; descriptor.pid = GetCurrentProcessId(); descriptor.visible = 1;
    descriptor.style_flags = 1u << 3; descriptor.bounds = {80, 70, 800, 600};
    CHECK(compositor.create_or_update_window(descriptor, title));
    HWND hwnd = nullptr;
    for (int i = 0; i < 100 && !hwnd; ++i) { Sleep(10); hwnd = FindWindowA("WsmRootlessWindowClass", title.c_str()); }
    CHECK(hwnd != nullptr);
    std::vector<uint32_t> pixels(800 * 600, 0xffb45a1e);
    CHECK(compositor.present_bgra_frame(77, 800, 600, 800 * 4, reinterpret_cast<uint8_t*>(pixels.data())));
    RECT original{}; GetWindowRect(hwnd, &original);
    MONITORINFO monitor{sizeof(monitor)}; CHECK(GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &monitor));
    SendMessageW(hwnd, WM_KEYDOWN, VK_F11, 0);
    RECT full{}; GetWindowRect(hwnd, &full); CHECK(same_rect(full, monitor.rcMonitor));
    SendMessageW(hwnd, WM_KEYUP, VK_F11, 0);
    CHECK(compositor.present_bgra_frame(77, 800, 600, 800 * 4, reinterpret_cast<uint8_t*>(pixels.data())));
    SendMessageW(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM((full.right-full.left)/2, (full.bottom-full.top)/2));
    CHECK(std::abs(pointer_x.load() - 400) <= 1 && std::abs(pointer_y.load() - 300) <= 1);
    if (argc > 1 && std::string(argv[1]) == "--inspect") { std::cout << title << std::endl; Sleep(60000); compositor.stop(); return 0; }
    const auto actual_view = window_viewport(full.right-full.left, full.bottom-full.top, 800, 600, true);
    if (actual_view.x > 20) {
        SendMessageW(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(5, (full.bottom-full.top)/2));
        SendMessageW(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(5, (full.bottom-full.top)/2));
        CHECK(mouse_presses == 0);
    }
    // Runtime geometry updates must not shrink a fullscreen host window.
    descriptor.bounds.x = 250; descriptor.bounds.y = 200;
    CHECK(compositor.create_or_update_window(descriptor, title)); Sleep(100);
    RECT updated{}; GetWindowRect(hwnd, &updated); CHECK(same_rect(updated, full));
    // Guest menus follow the fullscreen transform and still map clicks to guest points.
    auto menu = descriptor; menu.window_id = 78; menu.style_flags |= 1u << 6; menu.bounds = {350, 280, 180, 120};
    CHECK(compositor.create_or_update_window(menu, title + " menu")); Sleep(100);
    HWND menu_hwnd = FindWindowA("WsmRootlessWindowClass", (title + " menu").c_str()); CHECK(menu_hwnd != nullptr);
    RECT menu_rect{}; GetWindowRect(menu_hwnd, &menu_rect);
    const float scale = actual_view.width / 800;
    CHECK(std::abs(menu_rect.left - (full.left + actual_view.x + 100 * scale)) <= 1);
    CHECK(std::abs(menu_rect.top - (full.top + actual_view.y + 80 * scale)) <= 1);
    CHECK(compositor.destroy_window(78)); Sleep(50);
    SendMessageW(hwnd, WM_KEYDOWN, VK_ESCAPE, 0); SendMessageW(hwnd, WM_KEYUP, VK_ESCAPE, 0);
    RECT restored{}; GetWindowRect(hwnd, &restored); CHECK(same_rect(restored, original));
    // The guest green traffic-light button uses the same fullscreen path.
    SendMessageW(hwnd, WM_NCLBUTTONDOWN, HTMAXBUTTON, 0);
    GetWindowRect(hwnd, &updated); CHECK(same_rect(updated, full));
    SendMessageW(hwnd, WM_NCLBUTTONDOWN, HTMAXBUTTON, 0);
    GetWindowRect(hwnd, &updated); CHECK(same_rect(updated, original));
    const int presses_before = mouse_presses.load();
    SendMessageW(hwnd, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(200, 100));
    SendMessageW(hwnd, WM_RBUTTONUP, 0, MAKELPARAM(200, 100));
    CHECK(last_button == 1 && mouse_presses == presses_before + 1);
    SendMessageW(hwnd, WM_LBUTTONDBLCLK, MK_LBUTTON, MAKELPARAM(200, 100));
    CHECK(last_button == 0 && last_clicks == 2 && mouse_presses == presses_before + 2);
    SendMessageW(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(200, 100));
    SendMessageW(hwnd, WM_NCRBUTTONDOWN, HTCAPTION, MAKELPARAM(original.left + 200, original.top + 15));
    SendMessageW(hwnd, WM_NCRBUTTONUP, HTCAPTION, MAKELPARAM(original.left + 200, original.top + 15));
    CHECK(last_button == 1 && mouse_presses == presses_before + 3);
    compositor.stop();
    std::cout << "PASS: F11/green button fullscreen, Escape restore, input scaling, menus and geometry stability\n";
}