Skip to main content

maolan_plugin_protocol/
protocol.rs

1use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
2
3/// Magic number: "MAOL" in big-endian ASCII.
4pub const MAGIC: u32 = 0x4D41_4F4C;
5
6/// Current protocol version.
7/// Version 2: parent_window changed from AtomicU32 to AtomicU64 to support 64-bit HWNDs on Windows.
8/// Version 3: Added MIDI output ring for plugin-generated MIDI events.
9/// Version 4: Per-port MIDI input/output rings (MAX_MIDI_PORTS each direction).
10pub const VERSION: u32 = 4;
11
12/// Maximum number of audio channels (main + sidechain combined).
13pub const MAX_CHANNELS: usize = 32;
14
15/// Number of audio buses (main + sidechain).
16pub const NUM_BUSES: usize = 2;
17
18/// Maximum audio block size in samples.
19pub const MAX_BLOCK_SIZE: usize = 4096;
20
21/// Capacity of each ring buffer in slots (power of two).
22pub const RING_CAPACITY: usize = 4096;
23
24/// Maximum number of MIDI ports per direction.
25/// Runtime counts may be lower; this is the SHM capacity.
26pub const MAX_MIDI_PORTS: usize = 16;
27
28// --- Section sizes ---
29pub const HEADER_SIZE: usize = 256;
30pub const CONTROL_SIZE: usize = 256;
31pub const AUDIO_BUFFER_SIZE: usize = MAX_CHANNELS * NUM_BUSES * MAX_BLOCK_SIZE * 4; // f32
32pub const PARAM_RING_SIZE: usize = RING_CAPACITY * std::mem::size_of::<ParameterEvent>();
33/// Size of the data area for one MIDI port ring (event slots only).
34pub const MIDI_RING_SIZE: usize = RING_CAPACITY * std::mem::size_of::<MidiEvent>();
35/// Size of one MIDI port ring area including embedded write/read atomics.
36pub const MIDI_PORT_RING_SIZE: usize = {
37    let raw = 8 + MIDI_RING_SIZE; // head + tail atomics + event slots
38    (raw + 15) & !15 // align up to 16 bytes for MidiEvent
39};
40pub const TRANSPORT_SIZE: usize = 256;
41pub const SCRATCH_SIZE: usize = 65536;
42
43// --- Offsets into the shared-memory segment ---
44/// Control area starts right after the header.
45pub const CONTROL_OFFSET: usize = HEADER_SIZE;
46/// Audio buffers start after the control area.
47pub const AUDIO_OFFSET: usize = HEADER_SIZE + CONTROL_SIZE;
48/// Parameter ring buffer.
49pub const PARAM_RING_OFFSET: usize = AUDIO_OFFSET + AUDIO_BUFFER_SIZE;
50/// Echo/parameter-change ring buffer.
51pub const ECHO_RING_OFFSET: usize = PARAM_RING_OFFSET + PARAM_RING_SIZE;
52pub const ECHO_RING_SIZE: usize = RING_CAPACITY * std::mem::size_of::<ParameterEvent>();
53/// Per-port MIDI input rings start after the echo ring.
54pub const MIDI_IN_RINGS_OFFSET: usize = {
55    let end = ECHO_RING_OFFSET + ECHO_RING_SIZE;
56    (end + 255) & !255
57};
58pub const MIDI_IN_RINGS_SIZE: usize = MAX_MIDI_PORTS * MIDI_PORT_RING_SIZE;
59/// Per-port MIDI output rings follow the input rings.
60pub const MIDI_OUT_RINGS_OFFSET: usize = MIDI_IN_RINGS_OFFSET + MIDI_IN_RINGS_SIZE;
61pub const MIDI_OUT_RINGS_SIZE: usize = MAX_MIDI_PORTS * MIDI_PORT_RING_SIZE;
62/// Transport state block (256-byte aligned from here).
63pub const TRANSPORT_OFFSET: usize = {
64    let end = MIDI_OUT_RINGS_OFFSET + MIDI_OUT_RINGS_SIZE;
65    // Align up to 256 bytes
66    (end + 255) & !255
67};
68/// State blob scratch area.
69pub const SCRATCH_OFFSET: usize = TRANSPORT_OFFSET + TRANSPORT_SIZE;
70
71/// Total bytes actively used by the protocol layout.
72pub const LAYOUT_SIZE: usize = SCRATCH_OFFSET + SCRATCH_SIZE;
73
74/// Total shared-memory allocation size (4 MiB, page-aligned).
75pub const SHM_SIZE: usize = 4 * 1024 * 1024;
76
77// --- Control-area indices (all 4-byte atomics inside CONTROL_OFFSET..CONTROL_OFFSET+256) ---
78pub const PARAM_WRITE_IDX_OFFSET: usize = CONTROL_OFFSET;
79pub const PARAM_READ_IDX_OFFSET: usize = CONTROL_OFFSET + 4;
80pub const ECHO_WRITE_IDX_OFFSET: usize = CONTROL_OFFSET + 8;
81pub const ECHO_READ_IDX_OFFSET: usize = CONTROL_OFFSET + 12;
82pub const GUI_MODE_OFFSET: usize = CONTROL_OFFSET + 16;
83
84/// GUI mode requested by the DAW.
85#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
86pub enum GuiMode {
87    /// DAW provides a parent window; plugin UI should be embedded.
88    #[default]
89    Embedded = 0,
90    /// DAW cannot provide a parent window; plugin-host must create a top-level window.
91    Floating = 1,
92}
93
94impl GuiMode {
95    pub fn from_u32(value: u32) -> Self {
96        match value {
97            1 => GuiMode::Floating,
98            _ => GuiMode::Embedded,
99        }
100    }
101
102    pub fn as_u32(self) -> u32 {
103        self as u32
104    }
105}
106
107// --- Structs ---
108
109pub const PARAM_EVENT_VALUE: u32 = 0;
110pub const PARAM_EVENT_MOD: u32 = 1;
111pub const PARAM_EVENT_GESTURE_BEGIN: u32 = 2;
112pub const PARAM_EVENT_GESTURE_END: u32 = 3;
113
114/// Fixed-size parameter change event (16 bytes, 16-byte aligned).
115#[repr(C, align(16))]
116#[derive(Clone, Copy, Debug, Default)]
117pub struct ParameterEvent {
118    pub param_index: u32,
119    pub value: f32,
120    pub sample_offset: u32,
121    pub event_kind: u32,
122}
123
124/// Fixed-size MIDI event (16 bytes, 16-byte aligned).
125#[repr(C, align(16))]
126#[derive(Clone, Copy, Debug, Default)]
127pub struct MidiEvent {
128    pub sample_offset: u32,
129    pub data: [u8; 3],
130    pub channel: u8,
131    pub flags: u16,
132    pub _pad: u16,
133}
134
135/// Transport state block (256 bytes).
136#[repr(C, align(256))]
137#[derive(Clone, Copy, Debug)]
138pub struct TransportState {
139    pub playhead_sample: u64,
140    pub tempo: f64,
141    pub numerator: u32,
142    pub denominator: u32,
143    pub flags: u32,
144    pub sample_rate_hz: f64,
145    _pad: [u8; 256 - 40],
146}
147
148impl Default for TransportState {
149    fn default() -> Self {
150        Self {
151            playhead_sample: 0,
152            tempo: 120.0,
153            numerator: 4,
154            denominator: 4,
155            flags: 0,
156            sample_rate_hz: 0.0,
157            _pad: [0; 256 - 40],
158        }
159    }
160}
161
162/// Shared-memory header (256 bytes).
163#[repr(C, align(256))]
164pub struct ShmHeader {
165    pub magic: u32,
166    pub version: u32,
167    pub flags: u32,
168    pub ready: AtomicU32,
169    pub heartbeat: AtomicU32,
170    pub error_code: u32,
171    pub shutdown_request: AtomicU32,
172    pub tasks_issued: AtomicU32,
173    pub tasks_completed: AtomicU32,
174    pub block_size: AtomicU32,
175    pub num_input_channels: AtomicU32,
176    pub num_output_channels: AtomicU32,
177    /// Number of MIDI input ports actually used by the plugin (<= MAX_MIDI_PORTS).
178    pub midi_in_port_count: AtomicU32,
179    /// Number of MIDI output ports actually used by the plugin (<= MAX_MIDI_PORTS).
180    pub midi_out_port_count: AtomicU32,
181    /// Request type: 0 = none, 1 = save_state, 2 = restore_state, 3 = gui_show, 4 = gui_hide,
182    /// 5 = set_resource_directory, 6 = enumerate_file_references, 7 = update_file_reference
183    pub request_type: AtomicU32,
184    /// Request status: 0 = pending, 1 = success, 2 = error
185    pub request_status: AtomicU32,
186    /// Valid bytes in scratch area for state operations
187    pub scratch_size: AtomicU32,
188    /// Parent window ID for GUI embedding (X11 window ID on Unix, HWND on Windows)
189    pub parent_window: AtomicU64,
190    /// Set to 1 by the plugin-host when the plugin calls clap_host_state.mark_dirty()
191    pub state_dirty: AtomicU32,
192    _pad: [u8; 256 - 84],
193}
194
195impl ShmHeader {
196    /// Load parent_window as a `usize` (handles 32- and 64-bit platforms).
197    pub fn parent_window_usize(&self) -> usize {
198        self.parent_window.load(Ordering::Acquire) as usize
199    }
200
201    /// Store a `usize` parent_window (truncates on 32-bit, but HWNDs/XIDs are
202    /// always within 64 bits).
203    pub fn set_parent_window(&self, window: usize) {
204        self.parent_window.store(window as u64, Ordering::Release);
205    }
206
207    fn gui_mode_atomic(&self) -> &AtomicU32 {
208        // SAFETY: GUI_MODE_OFFSET is inside the control area, which is within the
209        // header's 256-byte allocation. The offset is aligned to 4 bytes.
210        unsafe {
211            let base = self as *const Self as *const u8;
212            &*(base.add(GUI_MODE_OFFSET) as *const AtomicU32)
213        }
214    }
215
216    /// Load the requested GUI mode.
217    pub fn gui_mode(&self) -> GuiMode {
218        GuiMode::from_u32(self.gui_mode_atomic().load(Ordering::Acquire))
219    }
220
221    /// Store the requested GUI mode.
222    pub fn set_gui_mode(&self, mode: GuiMode) {
223        self.gui_mode_atomic()
224            .store(mode.as_u32(), Ordering::Release);
225    }
226}
227
228impl Default for ShmHeader {
229    fn default() -> Self {
230        Self {
231            magic: MAGIC,
232            version: VERSION,
233            flags: 0,
234            ready: AtomicU32::new(0),
235            heartbeat: AtomicU32::new(0),
236            error_code: 0,
237            shutdown_request: AtomicU32::new(0),
238            tasks_issued: AtomicU32::new(0),
239            tasks_completed: AtomicU32::new(0),
240            block_size: AtomicU32::new(0),
241            num_input_channels: AtomicU32::new(0),
242            num_output_channels: AtomicU32::new(0),
243            midi_in_port_count: AtomicU32::new(0),
244            midi_out_port_count: AtomicU32::new(0),
245            request_type: AtomicU32::new(0),
246            request_status: AtomicU32::new(0),
247            scratch_size: AtomicU32::new(0),
248            parent_window: AtomicU64::new(0),
249            state_dirty: AtomicU32::new(0),
250            _pad: [0; 256 - 84],
251        }
252    }
253}
254
255// --- Layout helpers ---
256
257/// Zero-initialize the entire shared-memory region and write the header.
258///
259/// # Safety
260/// `ptr` must be a valid pointer to a memory region of `size` bytes.
261pub unsafe fn init_shm_layout(ptr: *mut u8, size: usize) {
262    unsafe {
263        std::ptr::write_bytes(ptr, 0, size);
264        let header = ptr as *mut ShmHeader;
265        std::ptr::write(header, ShmHeader::default());
266    }
267}
268
269/// Returns a reference to the header at the start of the mapping.
270///
271/// # Safety
272/// `ptr` must point to a valid allocation containing at least `ShmHeader`'s size.
273pub unsafe fn header_ref(ptr: *mut u8) -> &'static ShmHeader {
274    unsafe { &*(ptr as *mut ShmHeader) }
275}
276
277/// Returns a mutable reference to the header.
278///
279/// # Safety
280/// `ptr` must point to a valid allocation containing at least `ShmHeader`'s size.
281pub unsafe fn header_mut(ptr: *mut u8) -> &'static mut ShmHeader {
282    unsafe { &mut *(ptr as *mut ShmHeader) }
283}
284
285/// Returns a pointer to the audio buffer region.
286///
287/// # Safety
288/// `ptr` must point to an allocation large enough to contain the audio buffer.
289pub unsafe fn audio_ptr(ptr: *mut u8) -> *mut f32 {
290    unsafe { ptr.add(AUDIO_OFFSET) as *mut f32 }
291}
292
293/// Returns a pointer to a specific channel/bus plane.
294///
295/// `channel` is 0-based up to `MAX_CHANNELS - 1`.
296/// `bus` is 0 (main) or 1 (sidechain).
297///
298/// # Safety
299/// `ptr` must point to a valid allocation large enough to contain the audio data.
300pub unsafe fn audio_channel_ptr(ptr: *mut u8, channel: usize, bus: usize) -> *mut f32 {
301    let plane_size = MAX_BLOCK_SIZE * std::mem::size_of::<f32>();
302    let offset = AUDIO_OFFSET + (channel * NUM_BUSES + bus) * plane_size;
303    unsafe { ptr.add(offset) as *mut f32 }
304}
305
306/// Returns a pointer to the parameter ring buffer slot array.
307///
308/// # Safety
309/// `ptr` must point to a valid allocation large enough to contain the parameter ring.
310pub unsafe fn param_ring_ptr(ptr: *mut u8) -> *mut ParameterEvent {
311    unsafe { ptr.add(PARAM_RING_OFFSET) as *mut ParameterEvent }
312}
313
314/// Returns pointers to the parameter ring write/read atomics.
315///
316/// # Safety
317/// `ptr` must point to a valid allocation containing the parameter ring atomics.
318pub unsafe fn param_indices(ptr: *mut u8) -> (*mut AtomicU32, *mut AtomicU32) {
319    unsafe {
320        (
321            ptr.add(PARAM_WRITE_IDX_OFFSET) as *mut AtomicU32,
322            ptr.add(PARAM_READ_IDX_OFFSET) as *mut AtomicU32,
323        )
324    }
325}
326
327/// Returns a pointer to the echo ring buffer slot array.
328///
329/// # Safety
330/// `ptr` must point to a valid allocation large enough to contain the echo ring.
331pub unsafe fn echo_ring_ptr(ptr: *mut u8) -> *mut ParameterEvent {
332    unsafe { ptr.add(ECHO_RING_OFFSET) as *mut ParameterEvent }
333}
334
335/// Returns pointers to the echo ring write/read atomics.
336///
337/// # Safety
338/// `ptr` must point to a valid allocation containing the echo ring atomics.
339pub unsafe fn echo_indices(ptr: *mut u8) -> (*mut AtomicU32, *mut AtomicU32) {
340    unsafe {
341        (
342            ptr.add(ECHO_WRITE_IDX_OFFSET) as *mut AtomicU32,
343            ptr.add(ECHO_READ_IDX_OFFSET) as *mut AtomicU32,
344        )
345    }
346}
347
348const fn midi_port_ring_offset(base_offset: usize, port: usize) -> usize {
349    base_offset + port * MIDI_PORT_RING_SIZE
350}
351
352/// Returns pointers to the embedded write/read atomics for a MIDI input port ring.
353///
354/// # Safety
355/// `ptr` must point to a valid allocation and `port` must be < MAX_MIDI_PORTS.
356pub unsafe fn midi_in_indices(ptr: *mut u8, port: usize) -> (*mut AtomicU32, *mut AtomicU32) {
357    unsafe {
358        let base = ptr.add(midi_port_ring_offset(MIDI_IN_RINGS_OFFSET, port));
359        (base as *mut AtomicU32, base.add(4) as *mut AtomicU32)
360    }
361}
362
363/// Returns a pointer to the MIDI input port ring buffer slot array.
364///
365/// # Safety
366/// `ptr` must point to a valid allocation and `port` must be < MAX_MIDI_PORTS.
367pub unsafe fn midi_in_ring_ptr(ptr: *mut u8, port: usize) -> *mut MidiEvent {
368    unsafe { ptr.add(midi_port_ring_offset(MIDI_IN_RINGS_OFFSET, port) + 8) as *mut MidiEvent }
369}
370
371/// Returns pointers to the embedded write/read atomics for a MIDI output port ring.
372///
373/// # Safety
374/// `ptr` must point to a valid allocation and `port` must be < MAX_MIDI_PORTS.
375pub unsafe fn midi_out_indices(ptr: *mut u8, port: usize) -> (*mut AtomicU32, *mut AtomicU32) {
376    unsafe {
377        let base = ptr.add(midi_port_ring_offset(MIDI_OUT_RINGS_OFFSET, port));
378        (base as *mut AtomicU32, base.add(4) as *mut AtomicU32)
379    }
380}
381
382/// Returns a pointer to the MIDI output port ring buffer slot array.
383///
384/// # Safety
385/// `ptr` must point to a valid allocation and `port` must be < MAX_MIDI_PORTS.
386pub unsafe fn midi_out_ring_ptr(ptr: *mut u8, port: usize) -> *mut MidiEvent {
387    unsafe { ptr.add(midi_port_ring_offset(MIDI_OUT_RINGS_OFFSET, port) + 8) as *mut MidiEvent }
388}
389
390/// Returns a reference to the transport state.
391///
392/// # Safety
393/// `ptr` must point to a valid allocation containing at least `TransportState`'s size.
394pub unsafe fn transport_ref(ptr: *mut u8) -> &'static TransportState {
395    unsafe { &*(ptr.add(TRANSPORT_OFFSET) as *mut TransportState) }
396}
397
398/// Returns a mutable reference to the transport state.
399///
400/// # Safety
401/// `ptr` must point to a valid allocation containing at least `TransportState`'s size.
402pub unsafe fn transport_mut(ptr: *mut u8) -> &'static mut TransportState {
403    unsafe { &mut *(ptr.add(TRANSPORT_OFFSET) as *mut TransportState) }
404}
405
406/// Returns a pointer to the scratch buffer region.
407///
408/// # Safety
409/// `ptr` must point to an allocation large enough to contain the scratch buffer.
410pub unsafe fn scratch_ptr(ptr: *mut u8) -> *mut u8 {
411    unsafe { ptr.add(SCRATCH_OFFSET) }
412}
413
414/// Write a plugin name to the start of the scratch buffer.
415/// The name is encoded as a little-endian u32 length followed by UTF-8 bytes.
416///
417/// # Safety
418/// `ptr` must point to a valid SHM allocation.
419pub unsafe fn write_plugin_name_to_scratch(ptr: *mut u8, name: &str) {
420    unsafe {
421        let scratch = scratch_ptr(ptr);
422        let bytes = name.as_bytes();
423        let len = bytes.len().min(SCRATCH_SIZE - 4);
424        std::ptr::write_unaligned(scratch as *mut u32, len as u32);
425        std::ptr::copy_nonoverlapping(bytes.as_ptr(), scratch.add(4), len);
426    }
427}
428
429/// Read a plugin name from the start of the scratch buffer.
430///
431/// # Safety
432/// `ptr` must point to a valid SHM allocation.
433pub unsafe fn read_plugin_name_from_scratch(ptr: *mut u8) -> Option<String> {
434    unsafe {
435        let scratch = scratch_ptr(ptr);
436        let len = std::ptr::read_unaligned(scratch as *mut u32) as usize;
437        if len == 0 || len > SCRATCH_SIZE - 4 {
438            return None;
439        }
440        let bytes = std::slice::from_raw_parts(scratch.add(4), len);
441        String::from_utf8(bytes.to_vec()).ok()
442    }
443}
444
445/// Magic value written before port counts in scratch.
446pub const PORT_COUNTS_MAGIC: u32 = 0x504F_5254; // "PORT"
447
448/// Offset within scratch where port counts are stored (after plugin name).
449const PORT_COUNTS_OFFSET: usize = 1024;
450
451/// Write audio/MIDI port counts to scratch.
452///
453/// # Safety
454/// `ptr` must point to a valid SHM allocation.
455pub unsafe fn write_port_counts_to_scratch(
456    ptr: *mut u8,
457    audio_in: u32,
458    audio_out: u32,
459    midi_in: u32,
460    midi_out: u32,
461) {
462    unsafe {
463        let dest = scratch_ptr(ptr).add(PORT_COUNTS_OFFSET);
464        std::ptr::write_unaligned(dest as *mut u32, PORT_COUNTS_MAGIC);
465        std::ptr::write_unaligned(dest.add(4) as *mut u32, audio_in);
466        std::ptr::write_unaligned(dest.add(8) as *mut u32, audio_out);
467        std::ptr::write_unaligned(dest.add(12) as *mut u32, midi_in);
468        std::ptr::write_unaligned(dest.add(16) as *mut u32, midi_out);
469    }
470}
471
472/// Read audio/MIDI port counts from scratch.
473///
474/// # Safety
475/// `ptr` must point to a valid SHM allocation.
476pub unsafe fn read_port_counts_from_scratch(ptr: *mut u8) -> Option<(u32, u32, u32, u32)> {
477    unsafe {
478        let src = scratch_ptr(ptr).add(PORT_COUNTS_OFFSET);
479        let magic = std::ptr::read_unaligned(src as *mut u32);
480        if magic != PORT_COUNTS_MAGIC {
481            return None;
482        }
483        let audio_in = std::ptr::read_unaligned(src.add(4) as *mut u32);
484        let audio_out = std::ptr::read_unaligned(src.add(8) as *mut u32);
485        let midi_in = std::ptr::read_unaligned(src.add(12) as *mut u32);
486        let midi_out = std::ptr::read_unaligned(src.add(16) as *mut u32);
487        Some((audio_in, audio_out, midi_in, midi_out))
488    }
489}
490
491/// Magic value written before file-reference string list in scratch.
492pub const FILE_REFS_MAGIC: u32 = 0x4649_4C45; // "FILE"
493
494/// Offset within scratch where file-reference string list is stored.
495const FILE_REFS_OFFSET: usize = 2048;
496
497/// Maximum total bytes available for the file-reference list.
498const FILE_REFS_MAX_SIZE: usize = SCRATCH_SIZE - FILE_REFS_OFFSET;
499
500/// A file reference returned by a plugin, paired with its plugin-side index.
501pub type FileReference = (u32, String);
502
503/// Write a list of file-reference (index, path) pairs to scratch.
504/// Format: magic (u32), count (u32), then for each entry:
505///   index (u32), length (u32) followed by UTF-8 bytes.
506///
507/// # Safety
508/// `ptr` must point to a valid SHM allocation.
509pub unsafe fn write_file_references_to_scratch(
510    ptr: *mut u8,
511    refs: &[FileReference],
512) -> Result<(), String> {
513    unsafe {
514        let mut dest = scratch_ptr(ptr).add(FILE_REFS_OFFSET);
515        let mut remaining = FILE_REFS_MAX_SIZE;
516        if remaining < 8 {
517            return Err("scratch too small for file references".to_string());
518        }
519        std::ptr::write_unaligned(dest as *mut u32, FILE_REFS_MAGIC);
520        dest = dest.add(4);
521        remaining -= 4;
522        let count = refs.len().min(u32::MAX as usize) as u32;
523        std::ptr::write_unaligned(dest as *mut u32, count);
524        dest = dest.add(4);
525        remaining -= 4;
526        for (index, path) in refs.iter().take(count as usize) {
527            if remaining < 8 {
528                return Err("scratch overflow writing file references".to_string());
529            }
530            std::ptr::write_unaligned(dest as *mut u32, *index);
531            dest = dest.add(4);
532            remaining -= 4;
533            let bytes = path.as_bytes();
534            let len = bytes
535                .len()
536                .min(u32::MAX as usize)
537                .min(remaining.saturating_sub(4));
538            if len < bytes.len() {
539                return Err("scratch overflow writing file references".to_string());
540            }
541            std::ptr::write_unaligned(dest as *mut u32, len as u32);
542            dest = dest.add(4);
543            remaining -= 4;
544            std::ptr::copy_nonoverlapping(bytes.as_ptr(), dest, len);
545            dest = dest.add(len);
546            remaining -= len;
547        }
548        Ok(())
549    }
550}
551
552/// Read a list of file-reference (index, path) pairs from scratch.
553///
554/// # Safety
555/// `ptr` must point to a valid SHM allocation.
556pub unsafe fn read_file_references_from_scratch(ptr: *mut u8) -> Option<Vec<FileReference>> {
557    unsafe {
558        let mut src = scratch_ptr(ptr).add(FILE_REFS_OFFSET);
559        let mut remaining = FILE_REFS_MAX_SIZE;
560        if remaining < 8 {
561            return None;
562        }
563        let magic = std::ptr::read_unaligned(src as *mut u32);
564        if magic != FILE_REFS_MAGIC {
565            return None;
566        }
567        src = src.add(4);
568        remaining -= 4;
569        let count = std::ptr::read_unaligned(src as *mut u32) as usize;
570        src = src.add(4);
571        remaining -= 4;
572        let mut refs = Vec::with_capacity(count);
573        for _ in 0..count {
574            if remaining < 8 {
575                return None;
576            }
577            let index = std::ptr::read_unaligned(src as *mut u32);
578            src = src.add(4);
579            remaining -= 4;
580            let len = std::ptr::read_unaligned(src as *mut u32) as usize;
581            src = src.add(4);
582            remaining -= 4;
583            if len > remaining {
584                return None;
585            }
586            let bytes = std::slice::from_raw_parts(src, len);
587            let path = String::from_utf8(bytes.to_vec()).ok()?;
588            refs.push((index, path));
589            src = src.add(len);
590            remaining -= len;
591        }
592        Some(refs)
593    }
594}
595
596/// Write a resource-directory / base-directory path to scratch.
597/// Format: magic (u32), length (u32), UTF-8 bytes.
598///
599/// # Safety
600/// `ptr` must point to a valid SHM allocation.
601pub unsafe fn write_resource_directory_to_scratch(ptr: *mut u8, path: &str) -> Result<(), String> {
602    unsafe {
603        let scratch = scratch_ptr(ptr);
604        let bytes = path.as_bytes();
605        let len = bytes.len().min(SCRATCH_SIZE - 8);
606        if len < bytes.len() {
607            return Err("resource directory path too long".to_string());
608        }
609        std::ptr::write_unaligned(scratch as *mut u32, FILE_REFS_MAGIC);
610        std::ptr::write_unaligned(scratch.add(4) as *mut u32, len as u32);
611        std::ptr::copy_nonoverlapping(bytes.as_ptr(), scratch.add(8), len);
612        Ok(())
613    }
614}
615
616/// Read a resource-directory / base-directory path from scratch.
617///
618/// # Safety
619/// `ptr` must point to a valid SHM allocation.
620pub unsafe fn read_resource_directory_from_scratch(ptr: *mut u8) -> Option<String> {
621    unsafe {
622        let scratch = scratch_ptr(ptr);
623        let magic = std::ptr::read_unaligned(scratch as *mut u32);
624        if magic != FILE_REFS_MAGIC {
625            return None;
626        }
627        let len = std::ptr::read_unaligned(scratch.add(4) as *mut u32) as usize;
628        if len == 0 || len > SCRATCH_SIZE - 8 {
629            return None;
630        }
631        let bytes = std::slice::from_raw_parts(scratch.add(8), len);
632        String::from_utf8(bytes.to_vec()).ok()
633    }
634}
635
636/// Magic value for a single file-reference update in scratch.
637pub const FILE_REF_UPDATE_MAGIC: u32 = 0x5550_4441; // "UPDA"
638
639/// Write a file-reference update (index + new path) to scratch.
640/// Format: magic (u32), index (u32), length (u32), UTF-8 bytes.
641///
642/// # Safety
643/// `ptr` must point to a valid SHM allocation.
644pub unsafe fn write_file_reference_update_to_scratch(
645    ptr: *mut u8,
646    index: u32,
647    path: &str,
648) -> Result<(), String> {
649    unsafe {
650        let scratch = scratch_ptr(ptr);
651        let bytes = path.as_bytes();
652        let len = bytes.len().min(SCRATCH_SIZE - 12);
653        if len < bytes.len() {
654            return Err("file-reference update path too long".to_string());
655        }
656        std::ptr::write_unaligned(scratch as *mut u32, FILE_REF_UPDATE_MAGIC);
657        std::ptr::write_unaligned(scratch.add(4) as *mut u32, index);
658        std::ptr::write_unaligned(scratch.add(8) as *mut u32, len as u32);
659        std::ptr::copy_nonoverlapping(bytes.as_ptr(), scratch.add(12), len);
660        Ok(())
661    }
662}
663
664/// Read a file-reference update (index + new path) from scratch.
665///
666/// # Safety
667/// `ptr` must point to a valid SHM allocation.
668pub unsafe fn read_file_reference_update_from_scratch(ptr: *mut u8) -> Option<(u32, String)> {
669    unsafe {
670        let scratch = scratch_ptr(ptr);
671        let magic = std::ptr::read_unaligned(scratch as *mut u32);
672        if magic != FILE_REF_UPDATE_MAGIC {
673            return None;
674        }
675        let index = std::ptr::read_unaligned(scratch.add(4) as *mut u32);
676        let len = std::ptr::read_unaligned(scratch.add(8) as *mut u32) as usize;
677        if len == 0 || len > SCRATCH_SIZE - 12 {
678            return None;
679        }
680        let bytes = std::slice::from_raw_parts(scratch.add(12), len);
681        let path = String::from_utf8(bytes.to_vec()).ok()?;
682        Some((index, path))
683    }
684}
685
686// --- Static assertions for sizes ---
687
688const _: () = assert!(std::mem::size_of::<ShmHeader>() == 256);
689const _: () = assert!(std::mem::align_of::<ShmHeader>() == 256);
690const _: () = assert!(std::mem::size_of::<ParameterEvent>() == 16);
691const _: () = assert!(std::mem::align_of::<ParameterEvent>() == 16);
692const _: () = assert!(std::mem::size_of::<MidiEvent>() == 16);
693const _: () = assert!(std::mem::align_of::<MidiEvent>() == 16);
694const _: () = assert!(std::mem::size_of::<TransportState>() == 256);
695const _: () = assert!(std::mem::align_of::<TransportState>() == 256);
696const _: () = assert!(LAYOUT_SIZE <= SHM_SIZE);
697
698/// Wait (spin + yield) until `ready` becomes non-zero or timeout elapses.
699pub fn wait_for_ready(header: &ShmHeader, timeout: std::time::Duration) -> bool {
700    let start = std::time::Instant::now();
701    while header.ready.load(Ordering::Acquire) == 0 {
702        if start.elapsed() >= timeout {
703            return false;
704        }
705        std::thread::yield_now();
706    }
707    true
708}