1#![allow(non_camel_case_types)]
12
13use std::ffi::{c_char, c_int, c_void};
14
15pub const MOUSER_ENGINE_ABI: u32 = 1;
17pub const MOUSER_ENGINE_ABI_V2: u32 = 2;
19pub const MOUSER_ENGINE_ABI_MIN: u32 = 1;
21pub const ENTRY_SYMBOL: &[u8] = b"mouser_engine_v1\0";
23pub const ENTRY_SYMBOL_V2: &[u8] = b"mouser_engine_v2\0";
25
26pub const CAP_VSYNC_CONTROL: u32 = 1;
28pub const CAP_DIRECT_SURFACE: u32 = 2;
29pub const CAP_PIXELS: u32 = 4;
30pub const FORMAT_XRGB8888: u32 = 1;
32
33pub const SURFACE_WAYLAND: u32 = 1;
34pub const SURFACE_XLIB: u32 = 2;
35pub const SURFACE_XCB: u32 = 3;
36pub const SURFACE_APPKIT: u32 = 4;
37pub const SURFACE_WIN32: u32 = 5;
38pub const SURFACE_PIXELS: u32 = 6;
41
42pub const OP_RECT: u32 = 0;
43pub const OP_GLYPH: u32 = 1;
44pub const OP_IMAGE: u32 = 2;
45
46pub const DRAW_OK: c_int = 0;
48pub const DRAW_AGAIN: c_int = 1;
49
50#[repr(C)]
54#[derive(Debug, Clone, Copy)]
55pub struct MouserSurface {
56 pub kind: u32,
57 pub display: *mut c_void,
58 pub window: *mut c_void,
59}
60
61#[repr(C)]
65#[derive(Debug, Clone, Copy)]
66pub struct MouserPixels {
67 pub data: *mut u32,
68 pub width: u32,
69 pub height: u32,
70 pub stride: u32,
71}
72
73#[repr(C)]
75#[derive(Debug, Clone, Copy, Default, PartialEq)]
76pub struct MouserRect {
77 pub x: i32,
78 pub y: i32,
79 pub w: i32,
80 pub h: i32,
81 pub color: u32,
82}
83
84#[repr(C)]
88#[derive(Debug, Clone, Copy)]
89pub struct MouserGlyph {
90 pub x: i32,
91 pub y: i32,
92 pub w: u32,
93 pub h: u32,
94 pub stride: u32,
95 pub color: u32,
96 pub coverage: *const u8,
97 pub key: u64,
98}
99
100#[repr(C)]
104#[derive(Debug, Clone, Copy)]
105pub struct MouserImage {
106 pub x: i32,
107 pub y: i32,
108 pub w: u32,
109 pub h: u32,
110 pub img_w: u32,
111 pub img_h: u32,
112 pub src_x: u32,
113 pub src_y: u32,
114 pub src_w: u32,
115 pub src_h: u32,
116 pub rgba: *const u8,
117 pub key: u64,
118}
119
120#[repr(C)]
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub struct MouserOp {
124 pub kind: u32,
125 pub index: u32,
126}
127
128#[repr(C)]
130#[derive(Debug, Clone, Copy)]
131pub struct MouserFrame {
132 pub width: u32,
133 pub height: u32,
134 pub clear: u32,
135 pub dirty_x: i32,
137 pub dirty_y: i32,
138 pub dirty_w: i32,
139 pub dirty_h: i32,
140 pub ops: *const MouserOp,
141 pub op_count: usize,
142 pub rects: *const MouserRect,
143 pub rect_count: usize,
144 pub glyphs: *const MouserGlyph,
145 pub glyph_count: usize,
146 pub images: *const MouserImage,
147 pub image_count: usize,
148}
149
150#[repr(C)]
152pub struct MouserEngineV1 {
153 pub abi: u32,
154 pub size: u32,
156 pub name: *const c_char,
158 pub create: unsafe extern "C" fn(surface: *const MouserSurface, w: u32, h: u32, err: *mut c_char, err_len: usize) -> *mut c_void,
160 pub resize: unsafe extern "C" fn(engine: *mut c_void, w: u32, h: u32),
161 pub draw: unsafe extern "C" fn(engine: *mut c_void, frame: *const MouserFrame) -> c_int,
163 pub destroy: unsafe extern "C" fn(engine: *mut c_void),
164}
165
166unsafe impl Sync for MouserEngineV1 {}
168
169#[repr(C)]
171#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
172pub struct MouserCaps {
173 pub size: u32,
175 pub flags: u32,
177 pub formats: u32,
179 pub max_texture: u32,
181}
182
183#[repr(C)]
188pub struct MouserEngineV2 {
189 pub base: MouserEngineV1,
190 pub abi_min: u32,
192 pub abi_max: u32,
193 pub caps: Option<unsafe extern "C" fn(engine: *mut c_void, caps: *mut MouserCaps)>,
195 pub set_scale: Option<unsafe extern "C" fn(engine: *mut c_void, scale: f32)>,
197 pub evict: Option<unsafe extern "C" fn(engine: *mut c_void, key: u64)>,
199 pub set_vsync: Option<unsafe extern "C" fn(engine: *mut c_void, on: c_int)>,
201}
202
203unsafe impl Sync for MouserEngineV2 {}
205
206pub fn negotiate(engine_min: u32, engine_max: u32) -> Result<u32, String> {
208 let hi = engine_max.min(MOUSER_ENGINE_ABI_V2);
209 if engine_min <= engine_max && engine_min.max(MOUSER_ENGINE_ABI_MIN) <= hi {
210 Ok(hi)
211 } else {
212 Err(format!("engine ABI {engine_min}-{engine_max} is not supported (this Mouser speaks ABI {MOUSER_ENGINE_ABI_MIN}-{MOUSER_ENGINE_ABI_V2})"))
213 }
214}
215
216impl MouserFrame {
217 pub unsafe fn parts(&self) -> (&[MouserOp], &[MouserRect], &[MouserGlyph], &[MouserImage]) {
222 unsafe fn s<'a, T>(p: *const T, n: usize) -> &'a [T] {
223 if n == 0 || p.is_null() { &[] } else { unsafe { std::slice::from_raw_parts(p, n) } }
224 }
225 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)) }
226 }
227}
228
229pub unsafe fn write_err(err: *mut c_char, len: usize, msg: &str) {
234 if err.is_null() || len == 0 {
235 return;
236 }
237 let n = msg.len().min(len - 1);
238 unsafe {
239 std::ptr::copy_nonoverlapping(msg.as_ptr(), err as *mut u8, n);
240 *err.add(n) = 0;
241 }
242}
243
244#[cfg(test)]
245mod tests {
246 use super::*;
247
248 #[test]
249 fn layout_is_stable() {
250 #[cfg(target_pointer_width = "64")]
252 {
253 assert_eq!(std::mem::size_of::<MouserRect>(), 20);
254 assert_eq!(std::mem::size_of::<MouserGlyph>(), 40);
255 assert_eq!(std::mem::size_of::<MouserImage>(), 56);
256 assert_eq!(std::mem::size_of::<MouserOp>(), 8);
257 assert_eq!(std::mem::size_of::<MouserSurface>(), 24);
258 assert_eq!(std::mem::size_of::<MouserFrame>(), 96);
259 assert_eq!(std::mem::size_of::<MouserEngineV1>(), 48);
260 assert_eq!(std::mem::size_of::<MouserPixels>(), 24);
261 assert_eq!(std::mem::size_of::<MouserEngineV2>(), 88);
262 assert_eq!(std::mem::size_of::<MouserCaps>(), 16);
263 }
264 }
265
266 #[test]
267 fn negotiates_the_newest_shared_abi() {
268 assert_eq!(negotiate(1, 2), Ok(2));
269 assert_eq!(negotiate(2, 9), Ok(2));
270 assert_eq!(negotiate(1, 1), Ok(1));
271 assert!(negotiate(3, 4).unwrap_err().contains("ABI 3-4"));
272 assert!(negotiate(0, 0).is_err());
273 assert!(negotiate(2, 1).is_err());
274 }
275
276 #[test]
277 fn err_is_truncated_and_terminated() {
278 let mut buf = [1 as c_char; 4];
279 unsafe { write_err(buf.as_mut_ptr(), buf.len(), "hello") };
280 assert_eq!(buf.map(|c| c as u8), *b"hel\0");
281 }
282}