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.0 KB
Raw Blame
#pragma once
#include <cstdint>
#include <cstddef>
#include <string>
#include <vector>

namespace wsm::proto {

constexpr uint32_t WSM_MAGIC = 0x57534D31; // "WSM1"
constexpr uint16_t WSM_PROTO_VERSION = 1;

// Channel identifiers over virtio-serial / vsock / TCP bridge
enum class Channel : uint16_t {
    Control    = 1, // Lifecycle, heartbeat, guest info, process launch/kill
    Window     = 2, // Rootless window metadata, geometry, framebuffers, input events
    Clipboard  = 3, // Bidirectional text/HTML/RTF/image/file clipboard sync
    Audio      = 4, // CoreAudio <-> WASAPI low-latency PCM frames
    GpuControl = 5, // Paravirtualized GPU surface / IOSurface handle sharing
    FileSync   = 6, // Shared folder notifications & file association open requests
};

enum class MsgType : uint16_t {
    // Control channel
    Hello            = 0x0001,
    HelloAck         = 0x0002,
    Ping             = 0x0003,
    Pong             = 0x0004,
    LaunchApp        = 0x0010,
    LaunchAppReply   = 0x0011,
    TerminateApp     = 0x0012,
    ListApps         = 0x0013,
    ListAppsReply    = 0x0014,
    ProcessExited    = 0x0015,
    GuestShutdown    = 0x0016,
    GuestStats       = 0x0017,

    // Window channel (guest -> host)
    WindowCreated    = 0x0100,
    WindowDestroyed  = 0x0101,
    WindowUpdated    = 0x0102, // title, bounds, flags, z-order, icon
    WindowFrame      = 0x0103, // BGRA32 or shared handle frame update
    WindowIcon       = 0x0104,
    MenuUpdated      = 0x0105,

    // Window channel (host -> guest)
    InputKey         = 0x0120,
    InputMouseMove   = 0x0121,
    InputMouseButton = 0x0122,
    InputScroll      = 0x0123,
    InputText        = 0x0124,
    WindowAction     = 0x0125, // move, resize, focus, minimize, maximize, close

    // Clipboard channel
    ClipboardOffer   = 0x0200,
    ClipboardData    = 0x0201,
    ClipboardRequest = 0x0202,

    // Audio channel
    AudioConfig      = 0x0300,
    AudioPcmOut      = 0x0301,
    AudioPcmIn       = 0x0302,
    AudioVolume      = 0x0303,

    // Error / Diagnostics
    Error            = 0xFFFF,
};

#pragma pack(push, 1)
struct FrameHeader {
    uint32_t magic = WSM_MAGIC;
    uint16_t version = WSM_PROTO_VERSION;
    uint16_t channel = 0;
    uint16_t type = 0;
    uint16_t flags = 0;
    uint32_t seq = 0;
    uint32_t payload_size = 0;
};

struct WindowRect {
    int32_t x = 0;
    int32_t y = 0;
    int32_t width = 0;
    int32_t height = 0;
};

struct WindowDescriptor {
    uint64_t window_id = 0;
    uint64_t parent_window_id = 0;
    uint32_t pid = 0;
    uint32_t style_flags = 0; // bit0=resizable, bit1=closable, bit2=minimizable, bit3=popup, bit4=modal, bit5=transparent
    WindowRect bounds{};
    float scale_factor = 1.0f;
    uint8_t visible = 1;
    uint8_t focused = 0;
    uint16_t title_len = 0;
    // Followed by UTF-8 title bytes of length title_len
};

struct WindowFrameHeader {
    uint64_t window_id = 0;
    uint32_t width = 0;
    uint32_t height = 0;
    uint32_t stride = 0;
    uint32_t pixel_format = 0; // 0 = BGRA8888, 1 = RGBA8888, 2 = SharedNtHandle
    uint32_t dirty_x = 0;
    uint32_t dirty_y = 0;
    uint32_t dirty_w = 0;
    uint32_t dirty_h = 0;
    uint64_t iosurface_id = 0;
    uint64_t timestamp_us = 0;
    // Followed by pixel bytes or 64-bit shared handle info
};

struct InputKeyEvent {
    uint64_t window_id = 0;
    uint32_t vk_code = 0;
    uint32_t mac_keycode = 0;
    uint32_t modifiers = 0; // bit0=Shift, bit1=Ctrl, bit2=Alt/Opt, bit3=Cmd/Win
    uint8_t pressed = 0;
    uint8_t repeat = 0;
    uint16_t reserved = 0;
};

struct InputMouseMoveEvent {
    uint64_t window_id = 0;
    int32_t x = 0;
    int32_t y = 0;
    uint32_t modifiers = 0;
};

struct InputMouseButtonEvent {
    uint64_t window_id = 0;
    int32_t x = 0;
    int32_t y = 0;
    uint8_t button = 0; // 0=left, 1=right, 2=middle, 3=x1, 4=x2
    uint8_t pressed = 0;
    uint16_t clicks = 1;
    uint32_t modifiers = 0;
};

struct InputScrollEvent {
    uint64_t window_id = 0;
    int32_t x = 0;
    int32_t y = 0;
    float delta_x = 0.0f;
    float delta_y = 0.0f;
    uint32_t modifiers = 0;
};

enum class WindowActionKind : uint32_t {
    Move     = 1,
    Resize   = 2,
    Focus    = 3,
    Minimize = 4,
    Maximize = 5,
    Restore  = 6,
    Close    = 7,
};

struct WindowActionEvent {
    uint64_t window_id = 0;
    uint32_t action = 0;
    int32_t arg1 = 0;
    int32_t arg2 = 0;
    int32_t arg3 = 0;
    int32_t arg4 = 0;
};

struct AudioPcmHeader {
    uint32_t sample_rate = 48000;
    uint16_t channels = 2;
    uint16_t bits_per_sample = 32; // float32
    uint32_t frame_count = 0;
    uint64_t timestamp_us = 0;
};
#pragma pack(pop)

// Max single frame payload: 64 MiB (bounds checking against untrusted guest input)
constexpr uint32_t WSM_MAX_PAYLOAD_SIZE = 64u * 1024u * 1024u;

inline bool validate_header(const FrameHeader& hdr) noexcept {
    return hdr.magic == WSM_MAGIC &&
           hdr.version == WSM_PROTO_VERSION &&
           hdr.payload_size <= WSM_MAX_PAYLOAD_SIZE;
}

} // namespace wsm::proto