#laplaceliu/tycho_plat

    Part of the Tycho graphics stack — used as the laplaceliu/tycho/plat subpackage.

    Target-neutral platform description and window API for Tycho.

    #Why this exists

    laplaceliu/tycho_window names three operating systems (Linux / Windows / MacOS) and cannot express "Linux but headless", "Android", "iOS" or "web canvas". It also owns its native codec directly, so nothing above it can be compiled for the JS target.

    This module splits the problem in two:

    • laplaceliu/tycho/plat (this package, concrete): Target, Window, Surface and their methods. Pure MoonBit, compiles on every backend — including js.
    • laplaceliu/tycho/plat/backend (virtual): primitive-only free functions (UInt64 handles, Int32 codes, String). One implementation per target:
      • backend/native — forwards to tycho_window (X11 / Wayland / Win32 / Cocoa C stubs)
      • backend/web — canvas + DOM via extern "js" (planned)

    A virtual package cannot carry methods or concrete types, so the typed API lives here and the boundary is deliberately primitive. See the M0 V1 spike, roadmap/spikes/m0-v1-virtual-package-boundary.md in the workspace.

    #Using it

    Because a virtual package's override may only be declared by the package being built, the executable picks the backend:

    // examples/.../moon.pkg
    import {
    "laplaceliu/tycho/plat" @plat,
    }

    pkgtype(kind: "executable")
    supported_targets = "native"
    options(overrides: [ "laplaceliu/tycho/plat/backend/native" ])

    let target = @plat.Target::current()
    println("os=\{target.os.name()} window_system=\{target.window_system.name()}")
    let win = @plat.Window::new(800, 600, "hello") // raises WindowError
    let surface = win.surface() // kind + native handles

    #Status

    PieceState
    Target / Os / WindowSystem / SurfaceKinddone
    Window (create / size / title / fullscreen / DPI / surface / destroy)done
    Event / KeyCode / Modifiers / MouseButton / CursorMode / CursorShapedone
    Window::poll_events / mouse_pos / is_mouse_down / set_cursor_* / clipboard / wait_eventsdone
    surface_extensions_for / surface_supported (capability intersection)done (pure part; RHI wiring in M2)
    backend/native (desktop)done
    backend/native (Android / iOS)pending (M4 / M5)
    backend/webpending (M6)

    #Verification

    moon run --target native tycho/plat/smoke # prints the resolved target and opens one window moon test --target native tycho/plat/smoke # target capability rules moon check --target js tycho/plat # typed layer must have no native dependency

    WindowError

    pub suberror WindowError {
    InitFailed
    CreateFailed
    }

    impl Show for WindowError

    CursorMode

    pub(all) enum CursorMode {
    Normal
    Hidden
    Locked
    } derive(Eq,
    Debug
    )

    CursorShape

    pub(all) enum CursorShape {
    Default
    Pointer
    IBeam
    Crosshair
    HResize
    VResize
    } derive(Eq,
    Debug
    )

    Event

    pub(all) enum Event {
    CloseRequested
    Resized(Int, Int)
    KeyDown(KeyCode, Modifiers)
    KeyUp(KeyCode, Modifiers)
    MouseMove(Int, Int)
    MouseDown(MouseButton, Int, Int)
    MouseUp(MouseButton, Int, Int)
    MouseScroll(Int, Int)
    FocusChanged(Bool)
    TextInput(String)
    ImePreedit(String)
    ImePreeditClear
    ImeCommit(String)
    ImeCancel
    TouchDown(Int, Int, Int)
    TouchMove(Int, Int, Int)
    TouchUp(Int, Int, Int)
    SurfaceCreated
    SurfaceDestroyed
    AppSuspended
    AppResumed
    } derive(Eq,
    Debug
    )

    KeyCode

    pub(all) enum KeyCode {
    Escape
    Return
    Tab
    Backspace
    Space
    Left
    Up
    Right
    Down
    Home
    End
    PageUp
    PageDown
    Insert
    Delete
    Num0
    Num1
    Num2
    Num3
    Num4
    Num5
    Num6
    Num7
    Num8
    Num9
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    O
    P
    Q
    R
    S
    T
    U
    V
    W
    X
    Y
    Z
    F1
    F2
    F3
    F4
    F5
    F6
    F7
    F8
    F9
    F10
    F11
    F12
    LShift
    RShift
    LCtrl
    RCtrl
    LAlt
    RAlt
    Unknown(Int)
    } derive(Eq,
    Debug
    )

    Logical key identity. The platform layer reports X11 keysyms for historical reasons; from_keysym / to_keysym translate losslessly, and keys the enum does not name are preserved as Unknown(keysym).

    KeyCode::from_keysym

    fn KeyCode::from_keysym(sym : Int) -> KeyCode

    KeyCode::to_keysym

    fn KeyCode::to_keysym(self : KeyCode) -> Int

    Modifiers

    pub(all) struct Modifiers {
    shift : Bool
    ctrl : Bool
    alt : Bool
    super_key : Bool
    } derive(Eq,
    Debug
    )

    Keyboard modifiers, decoded from the X11-compatible mask the platform layer reports (Shift=1, Lock=2, Ctrl=4, Mod1/Alt=8, Mod4/Super=64).

    Modifiers::bits

    fn Modifiers::bits(self : Modifiers) -> Int

    Modifiers::from_bits

    fn Modifiers::from_bits(bits : Int) -> Modifiers

    Modifiers::is_empty

    fn Modifiers::is_empty(self : Modifiers) -> Bool

    Modifiers::new

    fn Modifiers::new(shift? : Bool, ctrl? : Bool, alt? : Bool, super_key? : Bool) -> Modifiers

    Modifiers::none

    fn Modifiers::none() -> Modifiers

    MouseButton

    pub(all) enum MouseButton {
    Left
    Right
    Middle
    Other(Int)
    } derive(Eq,
    Debug
    )

    MouseButton::from_index

    fn MouseButton::from_index(index : Int) -> MouseButton

    Platform button index (X11 numbering: 1 = left, 2 = middle, 3 = right, 8/9 = side buttons).

    MouseButton::index

    fn MouseButton::index(self : MouseButton) -> Int

    pub(all) enum Os {
    Linux
    Windows
    MacOS
    Android
    IOS
    Web
    } derive(Eq,
    Debug
    )

    Os::name

    fn Os::name(self : Os) -> String

    Surface

    pub(all) struct Surface {
    kind : SurfaceKind
    a : UInt64
    b : UInt64
    } derive(Eq,
    Debug
    )

    What a backend needs to hand to a graphics API in order to create a surface. a / b are the native display and window handles (or the CAMetalLayer pointer, or a canvas id on the web); their meaning is fixed per kind.

    SurfaceKind

    pub(all) enum SurfaceKind {
    Xlib
    Xcb
    Wayland
    Win32
    Metal
    Android
    CanvasGpu
    Headless
    } derive(Eq,
    Debug
    )

    SurfaceKind::name

    fn SurfaceKind::name(self : SurfaceKind) -> String

    Target

    pub(all) struct Target {
    os : Os
    window_system : WindowSystem
    } derive(Eq,
    Debug
    )

    Target::current

    fn Target::current() -> Target

    The target this binary is running on, as reported by the backend.

    Target::is_mobile

    fn Target::is_mobile(self : Target) -> Bool

    Whether this target is a mobile OS (iOS / Android) with host-driven lifecycle and touch input.

    Target::supports_dx12

    fn Target::supports_dx12(self : Target) -> Bool

    Whether Direct3D 12 can exist on this OS.

    Target::supports_metal

    fn Target::supports_metal(self : Target) -> Bool

    Whether Apple Metal can exist on this OS (macOS / iOS).

    Target::supports_vulkan

    fn Target::supports_vulkan(self : Target) -> Bool

    Whether a Vulkan surface can exist here at all. Web has no Vulkan loader.

    Target::supports_webgpu

    fn Target::supports_webgpu(self : Target) -> Bool

    Whether WebGPU (browser or wgpu-native / Dawn) is part of the target matrix.

    Target::uses_external_surface

    fn Target::uses_external_surface(self : Target) -> Bool

    Whether the OS owns the render surface (CAMetalLayer on iOS, ANativeWindow on Android) rather than the engine creating a desktop window.

    Window

    pub struct Window {
    // private fields
    }

    Window::clipboard_text

    fn Window::clipboard_text(self : Window) -> String

    Window::content_scale

    fn Window::content_scale(self : Window) -> Double

    Window::destroy

    fn Window::destroy(self : Window) -> Unit

    Window::framebuffer_size

    fn Window::framebuffer_size(self : Window) -> (Int, Int)

    Window::handle

    fn Window::handle(self : Window) -> UInt64

    Window::height

    fn Window::height(self : Window) -> Int

    Window::is_mouse_down

    fn Window::is_mouse_down(self : Window, button : MouseButton) -> Bool

    Window::mouse_pos

    fn Window::mouse_pos(self : Window) -> (Double, Double)

    Window::new

    fn Window::new(w : Int, h : Int, title : String) -> Window raise WindowError

    Window::poll_events

    fn Window::poll_events(self : Window) -> Array[Event]

    Window::set_clipboard_text

    fn Window::set_clipboard_text(self : Window, text : String) -> Unit

    Window::set_cursor_mode

    fn Window::set_cursor_mode(self : Window, mode : CursorMode) -> Unit

    Window::set_cursor_shape

    fn Window::set_cursor_shape(self : Window, shape : CursorShape) -> Unit

    Window::set_fullscreen

    fn Window::set_fullscreen(self : Window, on : Bool) -> Unit

    Window::set_should_close

    fn Window::set_should_close(self : Window, close : Bool) -> Unit

    Window::set_title

    fn Window::set_title(self : Window, title : String) -> Unit

    Window::should_close

    fn Window::should_close(self : Window) -> Bool

    Window::surface

    fn Window::surface(self : Window) -> Surface

    Window::width

    fn Window::width(self : Window) -> Int

    WindowSystem

    pub(all) enum WindowSystem {
    X11
    Wayland
    Xcb
    Win32
    Cocoa
    UIKit
    Android
    Canvas
    Headless
    } derive(Eq,
    Debug
    )

    WindowSystem::name

    fn WindowSystem::name(self : WindowSystem) -> String

    event_from_code

    fn event_from_code(kind : Int, a : Int, b : Int, c : Int) -> Event

    refresh_rate

    fn refresh_rate() -> Double

    Display refresh rate in Hz.

    surface_candidates

    fn surface_candidates(kind : SurfaceKind) -> Array[String]

    Candidate instance extensions required to present through kind, in the order they should be enabled. Empty when the kind has no Vulkan surface (the web canvas).

    surface_extensions_for

    fn surface_extensions_for(kind : SurfaceKind, available : Array[String]) -> Array[String]

    The subset of surface_candidates(kind) that available actually contains. Enable exactly this list, never the raw candidates.

    surface_supported

    fn surface_supported(kind : SurfaceKind, available : Array[String]) -> Bool

    Whether this surface kind can present through Vulkan at all on a loader that reports available. False both for kinds without a Vulkan surface (web) and for loaders missing the platform extension.

    wait_events

    fn wait_events(timeout_ms : Int) -> Unit

    Wait for the next batch of platform events (compositor-driven).

    wait_frame

    fn wait_frame() -> Unit

    Sleep until the next display vsync (Android AChoreographer clock; no-op elsewhere). Used to pace the render loop to the refresh boundary.