//! The login-session display backend: calls into compositor/kms/kms_stub.c (built by build.rs), //! the same shim the native compositor uses. One display, one seat, global state. use crate::moonplasma::host::display::{KeyState, Modifiers, Output, PointerPos, SeatEvent}; extern "C" { fn mp_kms_open() -> i32; fn mp_kms_width() -> i32; fn mp_kms_height() -> i32; fn mp_kms_quit_requested() -> i32; fn mp_kms_poll() -> i32; fn mp_kms_frame_ready() -> i32; fn mp_kms_set_cursor_shape(shape: i32); fn mp_kms_next_event() -> i32; fn mp_kms_arg(i: i32) -> i32; fn mp_kms_keymap_len() -> i32; fn mp_kms_close(); fn hw_kms_present(pixels: *mut u8, w: i32, h: i32) -> i32; fn hw_kms_keymap_copy(out: *mut u8, cap: i32) -> i32; } /// Open the seat, the first connected output and libinput. None (the shim has said why on stderr) /// when this is not an active login VT. pub fn open() -> Option { unsafe { if mp_kms_open() != 0 { mp_kms_close(); return None; } let n = mp_kms_keymap_len(); let mut keymap = vec![0u8; n.max(0) as usize]; let got = if n > 0 { hw_kms_keymap_copy(keymap.as_mut_ptr(), n) } else { 0 }; keymap.truncate(got.max(0) as usize); Some(Output { width: mp_kms_width() as u32, height: mp_kms_height() as u32, keymap }) } } /// Service the seat, DRM and input fds. False once Ctrl+Alt+Backspace was pressed. pub fn poll() -> bool { unsafe { mp_kms_poll(); mp_kms_quit_requested() == 0 } } pub fn frame_ready() -> bool { unsafe { mp_kms_frame_ready() != 0 } } /// Present an RGBA8888 frame; ignored if the buffer is smaller than the stated size. pub fn present(pixels: &[u8], width: u32, height: u32) { if pixels.len() < (width as usize) * (height as usize) * 4 { return; } // The shim only reads the pixels; it takes a non-const pointer for the MoonBit byte type. unsafe { hw_kms_present(pixels.as_ptr() as *mut u8, width as i32, height as i32) }; } pub fn set_cursor_shape(shape: u32) { unsafe { mp_kms_set_cursor_shape(shape as i32) }; } /// Input queued since the last call. pub fn take_input() -> Vec { let mut out = Vec::new(); unsafe { loop { let t = mp_kms_next_event(); if t == 0 { break; } let (a, b) = (mp_kms_arg(1), mp_kms_arg(2)); match t { 1 => out.push(SeatEvent::PointerMove(PointerPos { x: a as f64, y: b as f64 })), 2 => out.push(SeatEvent::PointerButton(KeyState { code: a as u32, pressed: b != 0 })), 3 => out.push(SeatEvent::Key(KeyState { code: a as u32, pressed: b != 0 })), 4 => out.push(SeatEvent::Modifiers(Modifiers { depressed: a as u32, latched: b as u32, locked: mp_kms_arg(3) as u32, group: mp_kms_arg(4) as u32, })), _ => {} } } } out } pub fn close() { unsafe { mp_kms_close() }; }