#![allow(non_camel_case_types)]
use std::ffi::{c_char, c_int, c_void};
pub const MOUSER_ENGINE_ABI: u32 = 1;
pub const MOUSER_ENGINE_ABI_V2: u32 = 2;
pub const MOUSER_ENGINE_ABI_MIN: u32 = 1;
pub const ENTRY_SYMBOL: &[u8] = b"mouser_engine_v1\0";
pub const ENTRY_SYMBOL_V2: &[u8] = b"mouser_engine_v2\0";
pub const CAP_VSYNC_CONTROL: u32 = 1;
pub const CAP_DIRECT_SURFACE: u32 = 2;
pub const CAP_PIXELS: u32 = 4;
pub const FORMAT_XRGB8888: u32 = 1;
pub const SURFACE_WAYLAND: u32 = 1;
pub const SURFACE_XLIB: u32 = 2;
pub const SURFACE_XCB: u32 = 3;
pub const SURFACE_APPKIT: u32 = 4;
pub const SURFACE_WIN32: u32 = 5;
pub const SURFACE_PIXELS: u32 = 6;
pub const OP_RECT: u32 = 0;
pub const OP_GLYPH: u32 = 1;
pub const OP_IMAGE: u32 = 2;
pub const DRAW_OK: c_int = 0;
pub const DRAW_AGAIN: c_int = 1;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MouserSurface {
pub kind: u32,
pub display: *mut c_void,
pub window: *mut c_void,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MouserPixels {
pub data: *mut u32,
pub width: u32,
pub height: u32,
pub stride: u32,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct MouserRect {
pub x: i32,
pub y: i32,
pub w: i32,
pub h: i32,
pub color: u32,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MouserGlyph {
pub x: i32,
pub y: i32,
pub w: u32,
pub h: u32,
pub stride: u32,
pub color: u32,
pub coverage: *const u8,
pub key: u64,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MouserImage {
pub x: i32,
pub y: i32,
pub w: u32,
pub h: u32,
pub img_w: u32,
pub img_h: u32,
pub src_x: u32,
pub src_y: u32,
pub src_w: u32,
pub src_h: u32,
pub rgba: *const u8,
pub key: u64,
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MouserOp {
pub kind: u32,
pub index: u32,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MouserFrame {
pub width: u32,
pub height: u32,
pub clear: u32,
pub dirty_x: i32,
pub dirty_y: i32,
pub dirty_w: i32,
pub dirty_h: i32,
pub ops: *const MouserOp,
pub op_count: usize,
pub rects: *const MouserRect,
pub rect_count: usize,
pub glyphs: *const MouserGlyph,
pub glyph_count: usize,
pub images: *const MouserImage,
pub image_count: usize,
}
#[repr(C)]
pub struct MouserEngineV1 {
pub abi: u32,
pub size: u32,
pub name: *const c_char,
pub create: unsafe extern "C" fn(surface: *const MouserSurface, w: u32, h: u32, err: *mut c_char, err_len: usize) -> *mut c_void,
pub resize: unsafe extern "C" fn(engine: *mut c_void, w: u32, h: u32),
pub draw: unsafe extern "C" fn(engine: *mut c_void, frame: *const MouserFrame) -> c_int,
pub destroy: unsafe extern "C" fn(engine: *mut c_void),
}
unsafe impl Sync for MouserEngineV1 {}
#[repr(C)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct MouserCaps {
pub size: u32,
pub flags: u32,
pub formats: u32,
pub max_texture: u32,
}
#[repr(C)]
pub struct MouserEngineV2 {
pub base: MouserEngineV1,
pub abi_min: u32,
pub abi_max: u32,
pub caps: Option<unsafe extern "C" fn(engine: *mut c_void, caps: *mut MouserCaps)>,
pub set_scale: Option<unsafe extern "C" fn(engine: *mut c_void, scale: f32)>,
pub evict: Option<unsafe extern "C" fn(engine: *mut c_void, key: u64)>,
pub set_vsync: Option<unsafe extern "C" fn(engine: *mut c_void, on: c_int)>,
}
unsafe impl Sync for MouserEngineV2 {}
pub fn negotiate(engine_min: u32, engine_max: u32) -> Result<u32, String> {
let hi = engine_max.min(MOUSER_ENGINE_ABI_V2);
if engine_min <= engine_max && engine_min.max(MOUSER_ENGINE_ABI_MIN) <= hi {
Ok(hi)
} else {
Err(format!("engine ABI {engine_min}-{engine_max} is not supported (this Mouser speaks ABI {MOUSER_ENGINE_ABI_MIN}-{MOUSER_ENGINE_ABI_V2})"))
}
}
impl MouserFrame {
pub unsafe fn parts(&self) -> (&[MouserOp], &[MouserRect], &[MouserGlyph], &[MouserImage]) {
unsafe fn s<'a, T>(p: *const T, n: usize) -> &'a [T] {
if n == 0 || p.is_null() { &[] } else { unsafe { std::slice::from_raw_parts(p, n) } }
}
unsafe { (s(self.ops, self.op_count), s(self.rects, self.rect_count), s(self.glyphs, self.glyph_count), s(self.images, self.image_count)) }
}
}
pub unsafe fn write_err(err: *mut c_char, len: usize, msg: &str) {
if err.is_null() || len == 0 {
return;
}
let n = msg.len().min(len - 1);
unsafe {
std::ptr::copy_nonoverlapping(msg.as_ptr(), err as *mut u8, n);
*err.add(n) = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn layout_is_stable() {
#[cfg(target_pointer_width = "64")]
{
assert_eq!(std::mem::size_of::<MouserRect>(), 20);
assert_eq!(std::mem::size_of::<MouserGlyph>(), 40);
assert_eq!(std::mem::size_of::<MouserImage>(), 56);
assert_eq!(std::mem::size_of::<MouserOp>(), 8);
assert_eq!(std::mem::size_of::<MouserSurface>(), 24);
assert_eq!(std::mem::size_of::<MouserFrame>(), 96);
assert_eq!(std::mem::size_of::<MouserEngineV1>(), 48);
assert_eq!(std::mem::size_of::<MouserPixels>(), 24);
assert_eq!(std::mem::size_of::<MouserEngineV2>(), 88);
assert_eq!(std::mem::size_of::<MouserCaps>(), 16);
}
}
#[test]
fn negotiates_the_newest_shared_abi() {
assert_eq!(negotiate(1, 2), Ok(2));
assert_eq!(negotiate(2, 9), Ok(2));
assert_eq!(negotiate(1, 1), Ok(1));
assert!(negotiate(3, 4).unwrap_err().contains("ABI 3-4"));
assert!(negotiate(0, 0).is_err());
assert!(negotiate(2, 1).is_err());
}
#[test]
fn err_is_truncated_and_terminated() {
let mut buf = [1 as c_char; 4];
unsafe { write_err(buf.as_mut_ptr(), buf.len(), "hello") };
assert_eq!(buf.map(|c| c as u8), *b"hel\0");
}
}