Files
chandra/compositor/sys/sys.mbt
T
2026-11-25 01:18:46 +00:00

173 lines
3.8 KiB
MoonBit

///| Minimal POSIX OS primitives for the pure-MoonBit Wayland compositor.
///|
#borrow(path)
extern "C" fn c_unix_listen(path : Bytes) -> Int = "mp_unix_listen"
///|
extern "C" fn c_unix_accept(listen_fd : Int) -> Int = "mp_unix_accept"
///|
#borrow(path)
extern "C" fn c_unix_connect(path : Bytes) -> Int = "mp_unix_connect"
///|
extern "C" fn c_close_fd(fd : Int) -> Int = "mp_close_fd"
///|
#borrow(path)
extern "C" fn c_unlink(path : Bytes) -> Int = "mp_unlink"
///|
extern "C" fn c_poll_read(fd : Int, timeout_ms : Int) -> Int = "mp_poll_read"
///|
#borrow(buf, out_fd)
extern "C" fn c_recvmsg_fd(
fd : Int,
buf : FixedArray[Byte],
max_len : Int,
out_fd : FixedArray[Int],
) -> Int = "mp_recvmsg_fd"
///|
#borrow(buf)
extern "C" fn c_sendmsg_fd(
fd : Int,
buf : Bytes,
len : Int,
send_fd : Int,
) -> Int = "mp_sendmsg_fd"
///|
#borrow(name)
extern "C" fn c_memfd_create(name : Bytes, size : Int) -> Int = "mp_memfd_create"
///|
#borrow(buf)
extern "C" fn c_pwrite_bytes(
fd : Int,
buf : Bytes,
len : Int,
offset : Int,
) -> Int = "mp_pwrite_bytes"
///|
extern "C" fn c_mmap_read(fd : Int, size : Int) -> Bytes = "mp_mmap_read"
///|
#borrow(path)
extern "C" fn c_read_file(path : Bytes) -> Bytes = "mp_read_file"
///|
#borrow(path, buf)
extern "C" fn c_write_file(path : Bytes, buf : Bytes, len : Int) -> Int = "mp_write_file"
///|
extern "C" fn c_sleep_ms(ms : Int) -> Unit = "mp_sleep_ms"
///|
extern "C" fn c_getpid() -> Int = "mp_getpid"
///| Encode ASCII/UTF-8 String into Bytes for C paths/names.
pub fn str_to_utf8_bytes(s : String) -> Bytes {
let buf : Array[Byte] = []
for ch in s {
let code = ch.to_int()
if code < 0x80 {
buf.push(code.to_byte())
} else if code < 0x800 {
buf.push((0xC0 | (code >> 6)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
} else if code < 0x10000 {
buf.push((0xE0 | (code >> 12)).to_byte())
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
} else {
buf.push((0xF0 | (code >> 18)).to_byte())
buf.push((0x80 | ((code >> 12) & 0x3F)).to_byte())
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
}
}
Bytes::from_array(buf)
}
///|
pub fn unix_listen(path : String) -> Int {
c_unix_listen(str_to_utf8_bytes(path))
}
///|
pub fn unix_accept(listen_fd : Int) -> Int {
c_unix_accept(listen_fd)
}
///|
pub fn unix_connect(path : String) -> Int {
c_unix_connect(str_to_utf8_bytes(path))
}
///|
pub fn close_fd(fd : Int) -> Unit {
ignore(c_close_fd(fd))
}
///|
pub fn unlink(path : String) -> Unit {
ignore(c_unlink(str_to_utf8_bytes(path)))
}
///|
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
c_poll_read(fd, timeout_ms)
}
///| Receive bytes and optional SCM_RIGHTS fd. Returns (bytes_read, received_fd).
/// bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
let out_fd : FixedArray[Int] = FixedArray::make(1, -1)
let n = c_recvmsg_fd(fd, buf, max_len, out_fd)
(n, out_fd[0])
}
///|
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
c_sendmsg_fd(fd, buf, buf.length(), send_fd)
}
///|
pub fn memfd_create(name : String, size : Int) -> Int {
c_memfd_create(str_to_utf8_bytes(name), size)
}
///|
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
c_pwrite_bytes(fd, buf, buf.length(), offset)
}
///|
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
c_mmap_read(fd, size)
}
///|
pub fn read_file_bytes(path : String) -> Bytes {
c_read_file(str_to_utf8_bytes(path))
}
///|
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
c_write_file(str_to_utf8_bytes(path), data, data.length())
}
///|
pub fn sleep_ms(ms : Int) -> Unit {
c_sleep_ms(ms)
}
///|
pub fn getpid() -> Int {
c_getpid()
}