///| Native display backend for running chandra as a login session: DRM/KMS /// output (dumb buffers, page flips, hardware cursor), libinput keyboard and /// pointer, xkbcommon keymap, libseat for device access and VT switching. /// All state lives in the C shim (kms_stub.c); this wrapper exposes it with the /// same shape as `@nested.NestedWindow` so the main loop can drive either. ///| extern "C" fn c_open() -> Int = "mp_kms_open" ///| extern "C" fn c_width() -> Int = "mp_kms_width" ///| extern "C" fn c_height() -> Int = "mp_kms_height" ///| extern "C" fn c_quit_requested() -> Int = "mp_kms_quit_requested" ///| extern "C" fn c_poll() -> Int = "mp_kms_poll" ///| extern "C" fn c_frame_ready() -> Int = "mp_kms_frame_ready" ///| #borrow(pixels) extern "C" fn c_present(pixels : FixedArray[Byte], w : Int, h : Int) -> Int = "mp_kms_present" ///| extern "C" fn c_set_cursor_shape(shape : Int) = "mp_kms_set_cursor_shape" ///| extern "C" fn c_next_event() -> Int = "mp_kms_next_event" ///| extern "C" fn c_arg(i : Int) -> Int = "mp_kms_arg" ///| extern "C" fn c_keymap_len() -> Int = "mp_kms_keymap_len" ///| #borrow(out) extern "C" fn c_keymap_copy(out : FixedArray[Byte]) -> Int = "mp_kms_keymap_copy" ///| extern "C" fn c_close() = "mp_kms_close" ///| extern "C" fn c_fd(i : Int) -> Int = "mp_kms_fd" ///| extern "C" fn c_set_power(on : Int) = "mp_kms_set_power" ///| extern "C" fn c_set_rotation(rot : Int) -> Int = "mp_kms_set_rotation" ///| Turn the screen on or off (DPMS). While it is off no frame is shown (`frame_ready` is /// false); on shows the last frame again. pub fn Kms::set_power(_self : Kms, on : Bool) -> Unit { c_set_power(if on { 1 } else { 0 }) } ///| Rotate the output (`0=0`, `1=90 CW`, `2=180`, `3=270 CW`); returns new logical `(w, h)`. pub fn Kms::set_rotation(_self : Kms, rot : Int) -> (Int, Int) { let p = c_set_rotation(rot) ((p >> 16) & 0xFFFF, p & 0xFFFF) } ///| pub(all) struct Kms { width : Int height : Int keymap : Bytes } ///| Open the seat, the first connected DRM output and libinput. Returns None /// (after printing why to stderr) when not running on an active login VT. pub fn Kms::open() -> Kms? { if c_open() != 0 { c_close() return None } let n = c_keymap_len() let buf : FixedArray[Byte] = FixedArray::make(n, b'\x00') let got = if n > 0 { c_keymap_copy(buf) } else { 0 } let keymap = Bytes::from_fixedarray(buf, len=got) Some({ width: c_width(), height: c_height(), keymap }) } ///| Service the seat, DRM and input fds. False once Ctrl+Alt+Backspace was pressed. pub fn Kms::poll(_self : Kms) -> Bool { ignore(c_poll()) c_quit_requested() == 0 } ///| True when a new frame can be presented (seat active, no page flip in flight). pub fn Kms::frame_ready(_self : Kms) -> Bool { c_frame_ready() != 0 } ///| Present a composed frame (its size must match the output mode). pub fn Kms::present(_self : Kms, pixmap : @renderer.Pixmap) -> Unit { ignore(c_present(pixmap.data, pixmap.width, pixmap.height)) } ///| Show the cursor for a wp_cursor_shape_v1 value. pub fn Kms::set_cursor_shape(_self : Kms, shape : UInt) -> Unit { c_set_cursor_shape(shape.reinterpret_as_int()) } ///| Input queued since the last call, in the compositor's event type. pub fn Kms::take_input(_self : Kms) -> Array[@nested.InputEvent] { let out : Array[@nested.InputEvent] = [] while true { let t = c_next_event() if t == 0 { break } let a = c_arg(1) let b = c_arg(2) match t { 1 => out.push(PointerMove(a.to_double(), b.to_double())) 2 => out.push(PointerButton(a.reinterpret_as_uint(), b != 0)) 3 => out.push(Key(a.reinterpret_as_uint(), b != 0)) 4 => out.push( Modifiers( a.reinterpret_as_uint(), b.reinterpret_as_uint(), c_arg(3).reinterpret_as_uint(), c_arg(4).reinterpret_as_uint(), ), ) 5 => out.push( Scroll( a.reinterpret_as_uint(), b.to_double() / 256.0, c_arg(3), ), ) _ => () } } out } ///| Restore the previous video mode and release the seat. pub fn Kms::close(_self : Kms) -> Unit { c_close() } ///| The native build draws with the software renderer and uses `present`; there is no host /// framebuffer to show. pub fn Kms::present_frame(_self : Kms) -> Unit { abort("present_frame is only available to the wasm compositor") } ///| The seat, DRM and input fds `poll` services, for an idle loop to block on. pub fn Kms::wait_fds(_self : Kms) -> Array[Int] { let out : Array[Int] = [] for i = 0; i < 4; i = i + 1 { let fd = c_fd(i) if fd >= 0 { out.push(fd) } } out }