Skip to main content

strata_platform/
raw_abi.rs

1//! Zero-dependency raw Native OS ABI protocols and descriptors for Windows, Wayland, and macOS.
2//!
3//! Provides pure-Rust structures, message constants, and wire protocol parsers
4//! allowing Strata to communicate directly with OS display servers without heavy C library bindings.
5
6// ============================================================================
7// 1. Windows Win32 & DXGI 1.6 Flip Model Protocol
8// ============================================================================
9
10pub mod win32 {
11    /// Opaque window handle.
12    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
13    pub struct Hwnd(pub usize);
14
15    /// Device context handle.
16    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
17    pub struct Hdc(pub usize);
18
19    /// Application instance handle.
20    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
21    pub struct Hinstance(pub usize);
22
23    /// Pointer-sized message result.
24    pub type LResult = isize;
25
26    // Standard Window Messages
27    pub const WM_NULL: u32 = 0x0000;
28    pub const WM_CREATE: u32 = 0x0001;
29    pub const WM_DESTROY: u32 = 0x0002;
30    pub const WM_MOVE: u32 = 0x0003;
31    pub const WM_SIZE: u32 = 0x0005;
32    pub const WM_ACTIVATE: u32 = 0x0006;
33    pub const WM_SETFOCUS: u32 = 0x0007;
34    pub const WM_KILLFOCUS: u32 = 0x0008;
35    pub const WM_PAINT: u32 = 0x000F;
36    pub const WM_CLOSE: u32 = 0x0010;
37    pub const WM_QUIT: u32 = 0x0012;
38    pub const WM_ERASEBKGND: u32 = 0x0014;
39    pub const WM_KEYDOWN: u32 = 0x0100;
40    pub const WM_KEYUP: u32 = 0x0101;
41    pub const WM_CHAR: u32 = 0x0102;
42    pub const WM_MOUSEMOVE: u32 = 0x0200;
43    pub const WM_LBUTTONDOWN: u32 = 0x0201;
44    pub const WM_LBUTTONUP: u32 = 0x0202;
45    pub const WM_RBUTTONDOWN: u32 = 0x0204;
46    pub const WM_RBUTTONUP: u32 = 0x0205;
47    pub const WM_MBUTTONDOWN: u32 = 0x0207;
48    pub const WM_MBUTTONUP: u32 = 0x0208;
49    pub const WM_MOUSEWHEEL: u32 = 0x020A;
50    pub const WM_DPICHANGED: u32 = 0x02E0;
51
52    // DXGI 1.6 Swapchain Flags & Swap Effects
53    pub const DXGI_SWAP_EFFECT_DISCARD: u32 = 0;
54    pub const DXGI_SWAP_EFFECT_SEQUENTIAL: u32 = 1;
55    pub const DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL: u32 = 3;
56    pub const DXGI_SWAP_EFFECT_FLIP_DISCARD: u32 = 4;
57    pub const DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING: u32 = 2048;
58
59    /// 2D Point structure matching Win32 `POINT`.
60    #[repr(C)]
61    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
62    pub struct Point {
63        pub x: i32,
64        pub y: i32,
65    }
66
67    /// Rectangle structure matching Win32 `RECT`.
68    #[repr(C)]
69    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
70    pub struct Rect {
71        pub left: i32,
72        pub top: i32,
73        pub right: i32,
74        pub bottom: i32,
75    }
76
77    impl From<Point> for strata_primitives::geom::Point<f32> {
78        fn from(p: Point) -> Self {
79            strata_primitives::geom::Point::new(p.x as f32, p.y as f32)
80        }
81    }
82
83    impl From<strata_primitives::geom::Point<f32>> for Point {
84        fn from(p: strata_primitives::geom::Point<f32>) -> Self {
85            Point {
86                x: p.x as i32,
87                y: p.y as i32,
88            }
89        }
90    }
91
92    impl From<Rect> for strata_primitives::geom::Rect<f32> {
93        fn from(r: Rect) -> Self {
94            strata_primitives::geom::Rect::from_xywh(
95                r.left as f32,
96                r.top as f32,
97                (r.right - r.left).max(0) as f32,
98                (r.bottom - r.top).max(0) as f32,
99            )
100        }
101    }
102
103    impl From<strata_primitives::geom::Rect<f32>> for Rect {
104        fn from(r: strata_primitives::geom::Rect<f32>) -> Self {
105            Rect {
106                left: r.min_x() as i32,
107                top: r.min_y() as i32,
108                right: r.max_x() as i32,
109                bottom: r.max_y() as i32,
110            }
111        }
112    }
113
114    /// Native message structure matching Win32 `MSG`.
115    #[repr(C)]
116    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
117    pub struct Msg {
118        pub hwnd: Hwnd,
119        pub message: u32,
120        pub wparam: usize,
121        pub lparam: isize,
122        pub time: u32,
123        pub pt: Point,
124    }
125
126    /// Window class structure matching Win32 `WNDCLASSEXW`.
127    #[repr(C)]
128    pub struct WndClassExW {
129        pub cb_size: u32,
130        pub style: u32,
131        pub lpfn_wnd_proc: usize,
132        pub cb_cls_extra: i32,
133        pub cb_wnd_extra: i32,
134        pub h_instance: Hinstance,
135        pub h_icon: usize,
136        pub h_cursor: usize,
137        pub hbr_background: usize,
138        pub lpsz_menu_name: *const u16,
139        pub lpsz_class_name: *const u16,
140        pub h_icon_sm: usize,
141    }
142}
143
144// ============================================================================
145// 2. Linux Wayland Client Wire Protocol
146// ============================================================================
147
148pub mod wayland {
149    /// Wayland socket path resolver.
150    pub fn default_socket_path() -> String {
151        if let Ok(runtime_dir) = std::env::var("XDG_RUNTIME_DIR") {
152            let display =
153                std::env::var("WAYLAND_DISPLAY").unwrap_or_else(|_| "wayland-0".to_string());
154            format!("{}/{}", runtime_dir, display)
155        } else {
156            "/run/user/1000/wayland-0".to_string()
157        }
158    }
159
160    /// 8-byte Wayland wire protocol message header: [object_id (u32), opcode (u16), size (u16)].
161    #[repr(C)]
162    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
163    pub struct WaylandHeader {
164        pub object_id: u32,
165        pub opcode: u16,
166        pub size: u16,
167    }
168
169    impl WaylandHeader {
170        /// Encodes header into big-endian / native byte buffer.
171        pub fn to_bytes(&self) -> [u8; 8] {
172            let mut buf = [0u8; 8];
173            buf[0..4].copy_from_slice(&self.object_id.to_ne_bytes());
174            buf[4..6].copy_from_slice(&self.opcode.to_ne_bytes());
175            buf[6..8].copy_from_slice(&self.size.to_ne_bytes());
176            buf
177        }
178
179        /// Parses header from 8-byte wire slice.
180        pub fn from_bytes(slice: &[u8]) -> Option<Self> {
181            if slice.len() < 8 {
182                return None;
183            }
184            let object_id = u32::from_ne_bytes(slice[0..4].try_into().ok()?);
185            let opcode = u16::from_ne_bytes(slice[4..6].try_into().ok()?);
186            let size = u16::from_ne_bytes(slice[6..8].try_into().ok()?);
187            Some(Self {
188                object_id,
189                opcode,
190                size,
191            })
192        }
193    }
194
195    // Protocol object IDs & standard opcodes
196    pub const WL_DISPLAY_ID: u32 = 1;
197
198    // wl_display opcodes
199    pub const WL_DISPLAY_SYNC: u16 = 0;
200    pub const WL_DISPLAY_GET_REGISTRY: u16 = 1;
201
202    // wl_registry opcodes
203    pub const WL_REGISTRY_BIND: u16 = 0;
204
205    // wl_compositor opcodes
206    pub const WL_COMPOSITOR_CREATE_SURFACE: u16 = 0;
207    pub const WL_COMPOSITOR_CREATE_REGION: u16 = 1;
208
209    // xdg_wm_base opcodes
210    pub const XDG_WM_BASE_DESTROY: u16 = 0;
211    pub const XDG_WM_BASE_GET_XDG_SURFACE: u16 = 2;
212    pub const XDG_WM_BASE_PONG: u16 = 3;
213}
214
215// ============================================================================
216// 3. Apple macOS Cocoa & Metal Runtime ABI
217// ============================================================================
218
219pub mod cocoa {
220    /// Objective-C opaque object pointer (`id`).
221    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
222    pub struct Id(pub usize);
223
224    /// Objective-C selector pointer (`SEL`).
225    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
226    pub struct Sel(pub usize);
227
228    /// Objective-C class pointer (`Class`).
229    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
230    pub struct Class(pub usize);
231
232    // NSWindowStyleMask bitflags
233    pub const NS_WINDOW_STYLE_BORDERLESS: usize = 0;
234    pub const NS_WINDOW_STYLE_TITLED: usize = 1 << 0;
235    pub const NS_WINDOW_STYLE_CLOSABLE: usize = 1 << 1;
236    pub const NS_WINDOW_STYLE_MINIATURIZABLE: usize = 1 << 2;
237    pub const NS_WINDOW_STYLE_RESIZABLE: usize = 1 << 3;
238    pub const NS_WINDOW_STYLE_FULL_SIZE_CONTENT: usize = 1 << 15;
239
240    // NSBackingStoreType
241    pub const NS_BACKING_STORE_BUFFERED: usize = 2;
242
243    /// 2D origin and size matching Cocoa `NSRect`.
244    #[repr(C)]
245    #[derive(Debug, Clone, Copy, PartialEq, Default)]
246    pub struct NsRect {
247        pub x: f64,
248        pub y: f64,
249        pub width: f64,
250        pub height: f64,
251    }
252
253    impl NsRect {
254        pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
255            Self {
256                x,
257                y,
258                width,
259                height,
260            }
261        }
262    }
263
264    /// CoreVideo DisplayLink return codes.
265    pub type CvReturn = i32;
266    pub const K_CV_RETURN_SUCCESS: CvReturn = 0;
267
268    /// CoreVideo time stamp representation for hardware display refresh synchronization.
269    #[repr(C)]
270    #[derive(Debug, Clone, Copy, PartialEq, Default)]
271    pub struct CvTimeStamp {
272        pub version: u32,
273        pub video_time_scale: i32,
274        pub video_time: i64,
275        pub host_time: u64,
276        pub rate_scalar: f64,
277        pub video_refresh_period: i64,
278        pub smpte_time: [u8; 16],
279        pub flags: u64,
280        pub reserved: u64,
281    }
282}