Files
chandra/wasmhost/bin/display.mbt
T
2026-11-25 19:48:38 +00:00

205 lines
4.8 KiB
MoonBit

///| `chandra:host/display`: the login session (DRM/KMS output, libinput, xkbcommon, libseat),
/// through compositor/kms's shim (kms_stub.c), the one the native compositor uses.
///|
extern "C" fn mp_kms_open() -> Int = "mp_kms_open"
///|
extern "C" fn mp_kms_width() -> Int = "mp_kms_width"
///|
extern "C" fn mp_kms_height() -> Int = "mp_kms_height"
///|
extern "C" fn mp_kms_quit_requested() -> Int = "mp_kms_quit_requested"
///|
extern "C" fn mp_kms_poll() -> Int = "mp_kms_poll"
///|
extern "C" fn mp_kms_frame_ready() -> Int = "mp_kms_frame_ready"
///|
#borrow(pixels)
extern "C" fn mp_kms_present(pixels : Bytes, w : Int, h : Int) -> Int = "mp_kms_present"
///|
extern "C" fn mp_kms_set_cursor_shape(shape : Int) = "mp_kms_set_cursor_shape"
///|
extern "C" fn mp_kms_set_power(on : Int) = "mp_kms_set_power"
///|
extern "C" fn mp_kms_next_event() -> Int = "mp_kms_next_event"
///|
extern "C" fn mp_kms_arg(i : Int) -> Int = "mp_kms_arg"
///|
extern "C" fn mp_kms_keymap_len() -> Int = "mp_kms_keymap_len"
///|
#borrow(out)
extern "C" fn mp_kms_keymap_copy(out : Bytes) -> Int = "mp_kms_keymap_copy"
///|
extern "C" fn mp_kms_close() = "mp_kms_close"
///|
extern "C" fn mp_kms_fd(i : Int) -> Int = "mp_kms_fd"
///| Show the rasteriser's framebuffer (raster_stub.c).
extern "C" fn rs_present_kms() = "rs_present_kms"
///| The seat, DRM and input fds while the display is open (none otherwise), for an idle wait.
fn display_wait_fds() -> Array[Int] {
let out : Array[Int] = []
for i in 0..<3 {
let fd = mp_kms_fd(i)
if fd >= 0 {
out.push(fd)
}
}
out
}
///| Input queued since the last call, as seat events.
fn take_input() -> Array[@wt.Val] {
let out : Array[@wt.Val] = []
while true {
let t = mp_kms_next_event()
if t == 0 {
break
}
let a = mp_kms_arg(1)
let b = mp_kms_arg(2)
let key_state = fn() -> @wt.Val {
Record([("code", U32(a.reinterpret_as_uint())), ("pressed", Bool(b != 0))])
}
match t {
1 =>
out.push(
Variant(
"pointer-move",
Some(Record([("x", F64(a.to_double())), ("y", F64(b.to_double()))])),
),
)
2 => out.push(Variant("pointer-button", Some(key_state())))
3 => out.push(Variant("key", Some(key_state())))
4 =>
out.push(
Variant(
"modifiers",
Some(
Record([
("depressed", U32(a.reinterpret_as_uint())),
("latched", U32(b.reinterpret_as_uint())),
("locked", U32(mp_kms_arg(3).reinterpret_as_uint())),
("group", U32(mp_kms_arg(4).reinterpret_as_uint())),
]),
),
),
)
5 =>
out.push(
Variant(
"scroll",
Some(
Record([
("axis", U32(a.reinterpret_as_uint())),
("value", F64(b.to_double() / 256.0)),
("discrete", S32(mp_kms_arg(3))),
]),
),
),
)
_ => ()
}
}
out
}
///|
fn display_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
[
(
"open",
_ => {
if mp_kms_open() != 0 {
// Not an active login VT; the shim has said why on stderr.
mp_kms_close()
return [Option(None)]
}
let n = mp_kms_keymap_len()
let keymap = if n > 0 {
let buf = Bytes::make(n, b'\x00')
let got = mp_kms_keymap_copy(buf)
buf[0:@cmp.maximum(got, 0)].to_owned()
} else {
b""
}
[
Option(
Some(
Record([
("width", U32(mp_kms_width().reinterpret_as_uint())),
("height", U32(mp_kms_height().reinterpret_as_uint())),
("keymap", @wt.Val::bytes(keymap)),
]),
),
),
]
},
),
(
"poll",
_ => {
mp_kms_poll() |> ignore
boolean(mp_kms_quit_requested() == 0)
},
),
("frame-ready", _ => boolean(mp_kms_frame_ready() != 0)),
(
"present",
a => {
let px = a[0].to_bytes()
let w = a[1].to_int()
let h = a[2].to_int()
if px.length() >= w * h * 4 {
mp_kms_present(px, w, h) |> ignore
}
none
},
),
(
"present-frame",
_ => {
rs_present_kms()
none
},
),
(
"set-cursor-shape",
a => {
mp_kms_set_cursor_shape(a[0].to_int())
none
},
),
(
"set-power",
a => {
mp_kms_set_power(if a[0].to_bool() { 1 } else { 0 })
none
},
),
("take-input", _ => [List(take_input())]),
(
"close",
_ => {
mp_kms_close()
none
},
),
]
}