tycho_window

    Thin pure-MoonBit native windowing (Xlib on Linux, zero GLFW), input events, and Vulkan WSI surface helper for Tycho

    vulkan
    x11
    xlib
    window
    wsi
    input
    tycho
    Download zip
    Version
    0.10.0
    License
    Apache-2.0
    Last updated
    2 hours ago
    Downloads
    87

    #laplaceliu/tycho_window

    Part of the Tycho graphics stack — mounted there as the window/ submodule.

    Thin pure-MoonBit native windowing, input event queue, and Vulkan WSI surface wrapper across Linux (Wayland + X11 via dlopen), Windows (Win32 via LoadLibraryA), and macOS (Cocoa + CAMetalLayer via dlopen) — zero GLFW, libwayland-dev, libx11-dev, or link-flag dependencies.

    #Features

    • Cross-platform window backends (zero native cc-link-flags required):
      • Linux: automatic WAYLAND_DISPLAY detection (xdg-shell, zxdg_decoration_manager_v1, zwp_pointer_constraints_v1, zwp_relative_pointer_manager_v1, libxkbcommon, libwayland-cursor via dlopen) with dlopen("libX11.so.6") Xlib fallback; override with TYCHO_WINDOW_BACKEND=wayland|x11
      • Windows: pure-C Win32 (user32.dll via LoadLibraryA), HiDPI GetDpiForWindow scaling, borderless fullscreen, cursor lock/hide, VK_KHR_win32_surface
      • macOS: pure-C Cocoa (NSWindow) + CAMetalLayer via libobjc.A.dylib runtime dispatch, Retina backingScaleFactor, VK_EXT_metal_surface
    • Typed API first: Window::new(WindowDesc(size, title)) / Window::init() raise WindowError (InitFailed / CreateFailed), and every window, input, cursor, fullscreen, and clipboard operation is a Window method — the raw UInt64 handle never appears in the public surface unless asked for with Window::handle().
    • Window lifecycle: Window::new, Window::destroy, Window::should_close, Window::set_should_close, Window::set_title, Window::init, terminate, is_wayland
    • Resize & geometry: Window::width, Window::height, Window::framebuffer_size, Window::take_resized, Window::content_scale, Window::set_fullscreen, Window::is_fullscreen
    • Event & input queries: Window::poll_events returning typed events — KeyDown(KeyCode, Modifiers) / KeyUp(KeyCode, Modifiers) / MouseDown(MouseButton, x, y) / MouseUp(MouseButton, x, y) / MouseMove / MouseScroll / Resized / FocusChanged / TextInput / IME — plus wait_events, Window::mouse_pos, Window::is_mouse_down(MouseButton), Window::set_cursor_mode, Window::set_cursor_shape, time.
    • KeyCode (letters, digits, arrows, Home/End/Insert/Delete, F1–F12, modifiers, Unknown(keysym)) converts losslessly with KeyCode::from_keysym / to_keysym; Modifiers exposes shift / ctrl / alt / super_key with bits() for the legacy X11 mask.
    • IME:
      • X11 uses XIM (XFilterEvent + preedit draw callback + Xutf8LookupString) and emits Event::ImePreedit / ImePreeditClear / ImeCommit / ImeCancel.
      • Wayland binds zwp_text_input_manager_v3 / zwp_text_input_v3 (hand-written protocol tables) for native preedit/commit; Unicode keymap commits still work as a fallback.
      • Hosts feed these into ui.TextController::set_preedit / commit_preedit / clear_preedit.
    • Clipboard & appearance: set_clipboard_text / clipboard_text, set_bg_color
    • Vulkan WSI surface creation: create_surface (VK_KHR_wayland_surface, VK_KHR_xlib_surface, VK_KHR_win32_surface, VK_EXT_metal_surface), surface_extensions, platform

    None required! Downstream moon.pkg files only need to import laplaceliu/tycho_window.

    #Previewing Wayland on X11

    From the meta-repo root, python tools/run-wayland.py <example> --window automatically launches a nested weston --backend=x11-backend.so window (if no Wayland compositor is running) and sets WAYLAND_DISPLAY, XDG_RUNTIME_DIR, TYCHO_WINDOW_BACKEND=wayland, and VK_DRIVER_FILES (defaulting to lavapipe inside nested Weston). See window/wayland for manual weston steps.

    WindowError

    pub suberror WindowError {
    InitFailed(String)
    CreateFailed(String)
    }

    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
    } 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

    Platform

    pub(all) enum Platform {
    Linux
    Windows
    MacOS
    Android
    } derive(Eq)

    Which desktop platform the window backend is running on.

    Kept for source compatibility with callers that branch on the OS. New code should prefer laplaceliu/tycho/plat's Target, which also covers Android, iOS and the browser.

    Window

    pub struct Window {
    // private fields
    }

    Opaque native window handle. Every window, input, and clipboard operation is a method here; the raw UInt64 never leaves the module unless explicitly requested with handle().

    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::finish_sync

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

    Window::framebuffer_size

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

    Window::from_handle

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

    Wraps a handle produced elsewhere (e.g. a swapchain that already owns one).

    Window::handle

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

    Window::has_pending_sync

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

    Window::height

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

    Window::init

    fn Window::init() -> Unit raise WindowError

    Idempotent platform initialization. Window::new calls this for you.

    Window::is_fullscreen

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

    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::native_handle

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

    Window::new

    fn Window::new(desc : WindowDesc) -> Window raise WindowError

    Window::poll_events

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

    Window::set_bg_color

    fn Window::set_bg_color(self : Window, r : Int, g : Int, b : Int) -> Unit

    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, fullscreen : 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::take_resized

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

    Window::width

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

    WindowDesc

    pub(all) struct WindowDesc {
    width : Int
    height : Int
    title : String
    }

    WindowDesc::new

    fn WindowDesc::new(width : Int, height : Int, title : String) -> WindowDesc

    KEY_0

    let KEY_0 : Int

    KEY_1

    let KEY_1 : Int

    KEY_2

    let KEY_2 : Int

    KEY_3

    let KEY_3 : Int

    KEY_4

    let KEY_4 : Int

    KEY_5

    let KEY_5 : Int

    KEY_6

    let KEY_6 : Int

    KEY_7

    let KEY_7 : Int

    KEY_8

    let KEY_8 : Int

    KEY_9

    let KEY_9 : Int

    KEY_A

    let KEY_A : Int

    KEY_B

    let KEY_B : Int

    KEY_BACKSPACE

    let KEY_BACKSPACE : Int

    KEY_C

    let KEY_C : Int

    KEY_D

    let KEY_D : Int

    KEY_DOWN

    let KEY_DOWN : Int

    KEY_E

    let KEY_E : Int

    KEY_ESCAPE

    let KEY_ESCAPE : Int

    KEY_F

    let KEY_F : Int

    KEY_F1

    let KEY_F1 : Int

    KEY_F10

    let KEY_F10 : Int

    KEY_F11

    let KEY_F11 : Int

    KEY_F12

    let KEY_F12 : Int

    KEY_F2

    let KEY_F2 : Int

    KEY_F3

    let KEY_F3 : Int

    KEY_F4

    let KEY_F4 : Int

    KEY_F5

    let KEY_F5 : Int

    KEY_F6

    let KEY_F6 : Int

    KEY_F7

    let KEY_F7 : Int

    KEY_F8

    let KEY_F8 : Int

    KEY_F9

    let KEY_F9 : Int

    KEY_G

    let KEY_G : Int

    KEY_H

    let KEY_H : Int

    KEY_I

    let KEY_I : Int

    KEY_J

    let KEY_J : Int

    KEY_K

    let KEY_K : Int

    KEY_L

    let KEY_L : Int

    KEY_LALT

    let KEY_LALT : Int

    KEY_LCTRL

    let KEY_LCTRL : Int

    KEY_LEFT

    let KEY_LEFT : Int

    KEY_LSHIFT

    let KEY_LSHIFT : Int

    KEY_M

    let KEY_M : Int

    KEY_N

    let KEY_N : Int

    KEY_O

    let KEY_O : Int

    KEY_P

    let KEY_P : Int

    KEY_Q

    let KEY_Q : Int

    KEY_R

    let KEY_R : Int

    KEY_RALT

    let KEY_RALT : Int

    KEY_RCTRL

    let KEY_RCTRL : Int

    KEY_RETURN

    let KEY_RETURN : Int

    KEY_RIGHT

    let KEY_RIGHT : Int

    KEY_RSHIFT

    let KEY_RSHIFT : Int

    KEY_S

    let KEY_S : Int

    KEY_SPACE

    let KEY_SPACE : Int

    KEY_T

    let KEY_T : Int

    KEY_TAB

    let KEY_TAB : Int

    KEY_U

    let KEY_U : Int

    KEY_UP

    let KEY_UP : Int

    KEY_V

    let KEY_V : Int

    KEY_W

    let KEY_W : Int

    KEY_X

    let KEY_X : Int

    KEY_Y

    let KEY_Y : Int

    KEY_Z

    let KEY_Z : Int

    clipboard_text

    fn clipboard_text(win : UInt64) -> String

    content_scale

    fn content_scale(win : UInt64) -> Double

    create

    fn create(w : Int, h : Int, title : String) -> UInt64

    destroy

    fn destroy(win : UInt64) -> Unit

    finish_sync

    fn finish_sync(win : UInt64) -> Unit

    framebuffer_size

    fn framebuffer_size(win : UInt64) -> (Int, Int)

    has_pending_sync

    fn has_pending_sync(win : UInt64) -> Bool

    height

    fn height(win : UInt64) -> Int

    initialize

    fn initialize() -> Int

    is_fullscreen

    fn is_fullscreen(win : UInt64) -> Bool

    is_mouse_down

    fn is_mouse_down(win : UInt64, button : MouseButton) -> Bool

    is_wayland

    fn is_wayland() -> Bool

    monitor_handle

    fn monitor_handle(win : UInt64) -> UInt64

    The raw native monitor handle: HMONITOR on Windows (MonitorFromWindow), 0 on other platforms. Needed by VK_EXT_full_screen_exclusive.

    mouse_pos

    fn mouse_pos(win : UInt64) -> (Double, Double)

    native_display

    fn native_display(win : UInt64) -> UInt64

    The raw native display/instance handle: HINSTANCE on Windows, Display* (X11) or wl_display* (Wayland) on Linux.

    native_handle

    fn native_handle(win : UInt64) -> UInt64

    The raw native window handle: HWND on Windows, Window (X11) on Linux and the CAMetalLayer pointer on macOS. Needed by backends that create their own swapchain, e.g. DXGI's CreateSwapChainForHwnd.

    next_event_raw

    fn next_event_raw(win : UInt64, out4 : FixedArray[Int]) -> Bool

    Raw event fetch for the platform backend layer: fills out4 with [kind, a, b, c] and reports whether one event was dequeued. This is the same encoding the C backends produce, so tycho_plat/backend can stay primitive-only instead of depending on the typed Event.

    platform

    fn platform() -> Platform

    poll

    fn poll() -> Unit

    poll_events

    fn poll_events(win : UInt64) -> Array[Event]

    refresh_rate

    fn refresh_rate() -> Double

    Display refresh rate in Hz (Android AChoreographer-measured; 60 elsewhere).

    set_bg_color

    fn set_bg_color(win : UInt64, r : Int, g : Int, b : Int) -> Unit

    set_clipboard_text

    fn set_clipboard_text(win : UInt64, text : String) -> Unit

    set_cursor_mode

    fn set_cursor_mode(win : UInt64, mode : CursorMode) -> Unit

    set_cursor_shape

    fn set_cursor_shape(win : UInt64, shape : CursorShape) -> Unit

    set_fullscreen

    fn set_fullscreen(win : UInt64, fullscreen : Bool) -> Unit

    set_should_close

    fn set_should_close(win : UInt64, close : Bool) -> Unit

    set_title

    fn set_title(win : UInt64, title : String) -> Unit

    should_close

    fn should_close(win : UInt64) -> Int

    surface_extensions

    fn surface_extensions() -> FixedArray[String]

    Instance extensions needed to present to a native window on this platform.

    This is a pure table of names -- it never touches a Vulkan type, which is why it can stay here. Actually creating the surface is Vulkan work and lives in laplaceliu/tycho_window/wsi.

    take_resized

    fn take_resized(win : UInt64) -> Bool

    terminate

    fn terminate() -> Unit

    time

    fn time() -> Double

    wait_events

    fn wait_events(timeout_ms : Int) -> Unit

    wait_frame

    fn wait_frame() -> Unit

    Sleep until the next display vsync. On Android with the AChoreographer clock enabled (TYCHO_ANDROID_CHOREOGRAPHER=1) this aligns the render loop to the refresh boundary; elsewhere it is a no-op (FIFO present already vsync-locks).

    width

    fn width(win : UInt64) -> Int

    Source Files