Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-window

Source of release 0.6.3

0.6.3 / tests / native_window / supported.lucb

from luce_window import window
import platform
import thread
import time

extern func pipe(descriptors: i32*) -> i32
extern func write(descriptor: i32, bytes: const void*, count: usize) -> isize
extern func read(descriptor: i32, bytes: void*, count: usize) -> isize
extern func close_descriptor as "close"(descriptor: i32) -> i32

## Make a watched pipe readable from another thread after a short delay.
func writer(context: void*?):
    let pointer = context else trap("missing test context")
    thread.sleep(50)
    var byte: u8 = 1
    assert(write(*(i32*)pointer, (const void*)&byte, 1) == 1)

## Linux: a non-ASCII title change (_NET_WM_NAME in UTF-8, WM_NAME in Latin-1), and
## a watched pipe ending a wait once it is readable.
func linux_window(surface: window.Window*) -> !:
    try surface.set_title("Supported target — retitled, café 日本")
    var ends: i32[2] = [0, 0]
    assert(pipe(&ends[0]) == 0)
    defer _ = close_descriptor(ends[0])
    defer _ = close_descriptor(ends[1])
    try window.watch(ends[0], window.Interest.readable)
    var writer_thread = try thread.spawn(writer, (void*)&ends[1])
    let started = time.now()
    while time.since(started) < 5000000000:
        _ = try surface.wait(3600000000000)
        var byte: u8 = 0
        if read(ends[0], (void*)&byte, 1) == 1:
            break
    try writer_thread.join()
    assert(time.since(started) < 5000000000)
    try window.unwatch(ends[0])

## A host with a window backend but no macOS test helpers (Linux, Windows):
## the API reports support, and opening a window either works (a display is
## there) or is refused as unsupported (none is, as on a headless runner).
pub func main(arguments: str[]) -> i32!:
    _ = arguments
    assert(window.supported())
    var surface = window.Window.open(window.Options(title = "Supported target contract")) catch failure:
        assert(failure.code == window.unsupported)
        print("ok supported window target")
        return 0
    defer surface.destroy()
    if platform.linux:
        try linux_window(&surface)
    # A distinct line, so a gate that needs a real window can tell it from a refusal.
    print("ok supported window target (opened)")
    return 0