Skip to main content

eov_plugin_api/
ffi.rs

1//! Stable ABI types for the plugin dynamic library interface.
2//!
3//! Uses [`abi_stable`] to define types that can safely cross the Rust dynamic
4//! library boundary. The plugin exports a `#[no_mangle]` function that returns
5//! a [`PluginVTable`], and the host loads it with
6//! [`abi_stable::library::RawLibrary`].
7
8use abi_stable::StableAbi;
9use abi_stable::std_types::{ROption, RResult, RString, RVec};
10
11#[repr(C)]
12#[derive(StableAbi, Clone, Debug)]
13pub struct ActiveSidebarFFI {
14    pub plugin_id: RString,
15    pub button_id: ROption<RString>,
16    pub width_px: u32,
17    pub ui_path: RString,
18    pub component: RString,
19}
20
21/// FFI-safe toolbar button registration data.
22#[repr(C)]
23#[derive(StableAbi, Clone, Debug)]
24pub struct ToolbarButtonFFI {
25    pub button_id: RString,
26    pub tooltip: RString,
27    pub icon_svg: RString,
28    pub action_id: RString,
29    pub tool_mode: ROption<HostToolModeFFI>,
30    pub hotkey: ROption<RString>,
31}
32
33/// FFI-safe HUD toolbar button registration data.
34#[repr(C)]
35#[derive(StableAbi, Clone, Debug)]
36pub struct HudToolbarButtonFFI {
37    pub button_id: RString,
38    pub tooltip: RString,
39    pub icon_svg: RString,
40    pub toggled_icon_svg: ROption<RString>,
41    pub action_id: RString,
42}
43
44#[repr(C)]
45#[derive(StableAbi, Clone, Debug)]
46pub struct UiPropertyFFI {
47    pub name: RString,
48    pub json_value: RString,
49}
50
51#[repr(C)]
52#[derive(StableAbi, Clone, Debug)]
53pub struct ViewportContextMenuItemFFI {
54    pub item_id: RString,
55    pub label: RString,
56    pub icon: RString,
57    pub enabled: bool,
58}
59
60#[repr(C)]
61#[derive(StableAbi, Clone, Debug)]
62pub struct ViewportOverlayPointFFI {
63    pub annotation_id: RString,
64    pub x_level0: f64,
65    pub y_level0: f64,
66    pub diameter_px: f32,
67    pub ring_red: u8,
68    pub ring_green: u8,
69    pub ring_blue: u8,
70}
71
72#[repr(C)]
73#[derive(StableAbi, Clone, Debug)]
74pub struct ViewportOverlayVertexFFI {
75    pub x_level0: f64,
76    pub y_level0: f64,
77}
78
79#[repr(C)]
80#[derive(StableAbi, Clone, Debug)]
81pub struct ViewportOverlayPolygonFFI {
82    pub annotation_id: RString,
83    pub vertices: RVec<ViewportOverlayVertexFFI>,
84    pub fill_red: u8,
85    pub fill_green: u8,
86    pub fill_blue: u8,
87}
88
89#[repr(C)]
90#[derive(StableAbi, Clone, Debug)]
91pub struct ConfirmationDialogRequestFFI {
92    pub title: RString,
93    pub message: RString,
94    pub confirm_label: RString,
95    pub cancel_label: RString,
96    pub confirm_callback: ROption<RString>,
97    pub confirm_args_json: ROption<RString>,
98    pub cancel_callback: ROption<RString>,
99    pub cancel_args_json: ROption<RString>,
100}
101
102#[repr(C)]
103#[derive(StableAbi, Clone, Debug)]
104pub struct ModalDialogRequestFFI {
105    pub ui_path: RString,
106    pub component: RString,
107    pub width_px: u32,
108    pub height_px: u32,
109}
110
111#[repr(C)]
112#[derive(StableAbi, Clone, Debug)]
113pub struct ViewportOverlayComponentRequestFFI {
114    pub ui_path: RString,
115    pub component: RString,
116    pub z_index: i32,
117}
118
119#[repr(C)]
120#[derive(StableAbi, Clone, Debug, Default)]
121pub struct PluginUndoRedoStateFFI {
122    pub enabled: bool,
123    pub undo_available: bool,
124    pub redo_available: bool,
125}
126
127/// FFI-safe response from a plugin action handler.
128#[repr(C)]
129#[derive(StableAbi, Clone, Debug)]
130pub struct ActionResponseFFI {
131    /// If `true`, the host should open this plugin's `.slint` UI window.
132    pub open_window: bool,
133}
134
135#[repr(u32)]
136#[derive(StableAbi, Clone, Copy, Debug, PartialEq, Eq)]
137pub enum HostLogLevelFFI {
138    Trace = 0,
139    Debug = 1,
140    Info = 2,
141    Warn = 3,
142    Error = 4,
143}
144
145#[repr(u32)]
146#[derive(StableAbi, Clone, Copy, Debug, PartialEq, Eq)]
147pub enum HostToolModeFFI {
148    Navigate = 0,
149    RegionOfInterest = 1,
150    MeasureDistance = 2,
151    PointAnnotation = 3,
152    PolygonAnnotation = 4,
153}
154
155#[repr(C)]
156#[derive(StableAbi, Clone, Debug)]
157pub struct OpenFileInfoFFI {
158    pub file_id: i32,
159    pub path: RString,
160    pub filename: RString,
161    pub width: u64,
162    pub height: u64,
163    pub level_count: u32,
164    pub vendor: ROption<RString>,
165    pub mpp_x: ROption<f64>,
166    pub mpp_y: ROption<f64>,
167    pub objective_power: ROption<f64>,
168    pub scan_date: ROption<RString>,
169}
170
171#[repr(C)]
172#[derive(StableAbi, Clone, Debug)]
173pub struct ViewportSnapshotFFI {
174    pub pane_index: u32,
175    pub file_id: i32,
176    pub file_path: RString,
177    pub filename: RString,
178    pub center_x: f64,
179    pub center_y: f64,
180    pub zoom: f64,
181    pub width: f64,
182    pub height: f64,
183    pub image_width: f64,
184    pub image_height: f64,
185    pub bounds_left: f64,
186    pub bounds_top: f64,
187    pub bounds_right: f64,
188    pub bounds_bottom: f64,
189}
190
191#[repr(C)]
192#[derive(StableAbi, Clone, Debug)]
193pub struct HostSnapshotFFI {
194    pub app_name: RString,
195    pub app_version: RString,
196    pub render_backend: RString,
197    pub filtering_mode: RString,
198    pub split_enabled: bool,
199    pub focused_pane: u32,
200    pub open_files: RVec<OpenFileInfoFFI>,
201    pub active_file: ROption<OpenFileInfoFFI>,
202    pub active_viewport: ROption<ViewportSnapshotFFI>,
203    pub recent_files: RVec<RString>,
204    pub active_sidebar: ROption<ActiveSidebarFFI>,
205}
206
207#[repr(C)]
208#[derive(StableAbi, Copy, Clone)]
209pub struct HostApiVTable {
210    pub context: u64,
211    pub get_snapshot: extern "C" fn(context: u64) -> HostSnapshotFFI,
212    pub read_region: extern "C" fn(
213        context: u64,
214        file_id: i32,
215        level: u32,
216        x: i64,
217        y: i64,
218        width: u32,
219        height: u32,
220    ) -> RResult<RVec<u8>, RString>,
221    pub open_file: extern "C" fn(context: u64, path: RString) -> RResult<(), RString>,
222    pub set_active_viewport: extern "C" fn(
223        context: u64,
224        center_x: f64,
225        center_y: f64,
226        zoom: f64,
227    ) -> RResult<(), RString>,
228    pub fit_active_viewport: extern "C" fn(context: u64) -> RResult<(), RString>,
229    pub frame_active_rect: extern "C" fn(
230        context: u64,
231        x: f64,
232        y: f64,
233        width: f64,
234        height: f64,
235    ) -> RResult<(), RString>,
236    pub cycle_focused_pane: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
237    pub cycle_active_tab: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
238    pub toggle_series_bar: extern "C" fn(context: u64) -> RResult<(), RString>,
239    pub cycle_series_selection: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
240    pub activate_selected_series_entry: extern "C" fn(context: u64) -> RResult<(), RString>,
241    pub set_active_tool: extern "C" fn(context: u64, tool: HostToolModeFFI) -> RResult<(), RString>,
242    pub request_render: extern "C" fn(context: u64) -> RResult<(), RString>,
243    pub set_toolbar_button_active:
244        extern "C" fn(context: u64, button_id: RString, active: bool) -> RResult<(), RString>,
245    pub set_hud_toolbar_button_active:
246        extern "C" fn(context: u64, button_id: RString, active: bool) -> RResult<(), RString>,
247    pub show_sidebar: extern "C" fn(
248        context: u64,
249        button_id: RString,
250        width_px: u32,
251        ui_path: RString,
252        component: RString,
253    ) -> RResult<(), RString>,
254    pub refresh_sidebar: extern "C" fn(context: u64) -> RResult<(), RString>,
255    pub hide_sidebar: extern "C" fn(context: u64) -> RResult<(), RString>,
256    pub show_confirmation_dialog:
257        extern "C" fn(context: u64, request: ConfirmationDialogRequestFFI) -> RResult<(), RString>,
258    pub show_modal_dialog:
259        extern "C" fn(context: u64, request: ModalDialogRequestFFI) -> RResult<(), RString>,
260    pub hide_modal_dialog: extern "C" fn(context: u64) -> RResult<(), RString>,
261    pub set_undo_redo_state:
262        extern "C" fn(context: u64, state: PluginUndoRedoStateFFI) -> RResult<(), RString>,
263    pub open_file_dialog: extern "C" fn(
264        context: u64,
265        filter_name: RString,
266        extension: RString,
267    ) -> RResult<RString, RString>,
268    pub save_file_dialog: extern "C" fn(
269        context: u64,
270        default_file_name: RString,
271        filter_name: RString,
272        extension: RString,
273    ) -> RResult<RString, RString>,
274    pub log_message: extern "C" fn(context: u64, level: HostLogLevelFFI, message: RString),
275}
276
277/// VTable of function pointers exported by each plugin shared library.
278///
279/// Each plugin crate exports:
280/// ```ignore
281/// #[unsafe(no_mangle)]
282/// pub extern "C" fn eov_get_plugin_vtable() -> PluginVTable { ... }
283/// ```
284#[repr(C)]
285#[derive(StableAbi, Copy, Clone)]
286pub struct PluginVTable {
287    /// Supplies the plugin with a host API vtable so it can query the app and
288    /// issue host commands later during callbacks.
289    pub set_host_api: extern "C" fn(host_api: HostApiVTable),
290
291    /// Returns the toolbar buttons this plugin wants to register.
292    pub get_toolbar_buttons: extern "C" fn() -> RVec<ToolbarButtonFFI>,
293
294    /// Returns HUD toolbar buttons shown inside each viewport.
295    pub get_hud_toolbar_buttons: extern "C" fn() -> RVec<HudToolbarButtonFFI>,
296
297    /// Called when a toolbar button registered by this plugin is clicked.
298    pub on_action: extern "C" fn(action_id: RString) -> ActionResponseFFI,
299
300    /// Called when a HUD toolbar button is clicked in a specific viewport.
301    pub on_hud_action:
302        extern "C" fn(action_id: RString, viewport: ViewportSnapshotFFI) -> ActionResponseFFI,
303
304    /// Called when a callback defined in the plugin's `.slint` UI is invoked.
305    /// `callback_name` is the kebab-case name of the callback as declared in
306    /// the `.slint` file.
307    pub on_ui_callback: extern "C" fn(callback_name: RString, args_json: RString),
308
309    /// Returns the current values for public properties on the active sidebar.
310    /// Values are encoded as JSON and converted to typed Slint values by the host.
311    pub get_sidebar_properties: extern "C" fn() -> RVec<UiPropertyFFI>,
312
313    /// Returns plugin-contributed viewport context menu items for the given viewport.
314    pub get_viewport_context_menu_items:
315        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportContextMenuItemFFI>,
316
317    /// Called when a plugin-contributed viewport context menu item is selected.
318    pub on_viewport_context_menu_action:
319        extern "C" fn(item_id: RString, viewport: ViewportSnapshotFFI) -> ActionResponseFFI,
320
321    /// Returns point overlay data that should be rendered over the given viewport.
322    pub get_viewport_overlay_points:
323        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportOverlayPointFFI>,
324
325    /// Returns polygon overlay data that should be rendered over the given viewport.
326    pub get_viewport_overlay_polygons:
327        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportOverlayPolygonFFI>,
328
329    /// Returns a viewport-scoped Slint component request for this plugin, if any.
330    pub get_viewport_overlay_component:
331        extern "C" fn() -> ROption<ViewportOverlayComponentRequestFFI>,
332
333    /// Returns the current public properties for the plugin viewport overlay component.
334    pub get_viewport_overlay_properties:
335        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<UiPropertyFFI>,
336
337    /// Called when the host commits a point annotation click for the active point tool.
338    pub on_point_annotation_placed:
339        extern "C" fn(viewport: ViewportSnapshotFFI, x_level0: f64, y_level0: f64),
340
341    /// Called when the host commits a polygon annotation for the active polygon tool.
342    pub on_polygon_annotation_placed:
343        extern "C" fn(viewport: ViewportSnapshotFFI, vertices: RVec<ViewportOverlayVertexFFI>),
344
345    /// Called when the host dispatches an undo request for this plugin.
346    pub on_undo: extern "C" fn() -> ActionResponseFFI,
347
348    /// Called when the host dispatches a redo request for this plugin.
349    pub on_redo: extern "C" fn() -> ActionResponseFFI,
350
351    /// Called when the host moves an existing point annotation for the active point tool.
352    pub on_point_annotation_moved: extern "C" fn(
353        viewport: ViewportSnapshotFFI,
354        annotation_id: RString,
355        x_level0: f64,
356        y_level0: f64,
357    ),
358
359    /// Called when the host moves an existing polygon annotation for the active polygon tool.
360    pub on_polygon_annotation_moved: extern "C" fn(
361        viewport: ViewportSnapshotFFI,
362        annotation_id: RString,
363        vertices: RVec<ViewportOverlayVertexFFI>,
364    ),
365
366    /// Called when the host selects or deselects a plugin-owned overlay annotation.
367    /// An empty annotation id means the current selection should be cleared.
368    pub on_viewport_annotation_selected:
369        extern "C" fn(viewport: ViewportSnapshotFFI, annotation_id: RString),
370
371    /// Returns viewport filter descriptors this plugin provides.
372    /// May return an empty vec if the plugin has no viewport filters.
373    pub get_viewport_filters: extern "C" fn() -> RVec<ViewportFilterFFI>,
374
375    /// Apply a CPU viewport filter in-place.
376    /// `filter_id` identifies which filter to apply.
377    /// `rgba_data` is a mutable pointer to width*height*4 bytes of RGBA8 data.
378    /// Returns `true` if the filter was applied successfully.
379    pub apply_filter_cpu: extern "C" fn(
380        filter_id: RString,
381        rgba_data: *mut u8,
382        len: u32,
383        width: u32,
384        height: u32,
385        viewport: *const ViewportSnapshotFFI,
386    ) -> bool,
387
388    /// Apply a GPU viewport filter using raw Vulkan handles.
389    /// `filter_id` identifies which filter to apply.
390    /// `ctx` is a pointer to a `GpuFilterContextFFI` struct with the Vulkan
391    /// device, image, and sync fence. The plugin must signal the fence when done.
392    /// Returns `true` if the filter was applied successfully.
393    pub apply_filter_gpu:
394        extern "C" fn(filter_id: RString, ctx: *const GpuFilterContextFFI) -> bool,
395
396    /// Enable or disable a viewport filter.
397    pub set_filter_enabled: extern "C" fn(filter_id: RString, enabled: bool),
398}
399
400/// FFI-safe GPU filter context passed from the host to the plugin.
401///
402/// Contains raw Vulkan handles for zero-copy GPU memory access. The plugin
403/// creates its own Vulkan pipeline on the shared device and submits commands
404/// that operate on `vk_image`. The plugin **must** signal `vk_done_fence`
405/// when its GPU work is complete.
406#[repr(C)]
407#[derive(StableAbi, Clone, Debug)]
408pub struct GpuFilterContextFFI {
409    /// `VkDevice` handle (as `u64`).
410    pub vk_device: u64,
411    /// `VkPhysicalDevice` handle (as `u64`).
412    pub vk_physical_device: u64,
413    /// `VkQueue` handle the plugin may submit to.
414    pub vk_queue: u64,
415    /// Queue family index for `vk_queue`.
416    pub queue_family_index: u32,
417    /// `VkImage` handle for the RGBA8 frame.
418    pub vk_image: u64,
419    /// Image width in pixels.
420    pub width: u32,
421    /// Image height in pixels.
422    pub height: u32,
423    /// `VkFormat` value (e.g. `VK_FORMAT_R8G8B8A8_UNORM` = 37).
424    pub vk_format: u32,
425    /// `VkFence` handle — the plugin **must** signal this when its GPU
426    /// commands have finished executing on `vk_image`.
427    pub vk_done_fence: u64,
428}
429
430/// FFI-safe viewport filter descriptor.
431#[repr(C)]
432#[derive(StableAbi, Clone, Debug)]
433pub struct ViewportFilterFFI {
434    pub filter_id: RString,
435    pub name: RString,
436    pub supports_cpu: bool,
437    pub supports_gpu: bool,
438    pub enabled: bool,
439}
440
441/// The type of the factory function each plugin exports.
442pub type GetPluginVTableFn = unsafe extern "C" fn() -> PluginVTable;
443
444/// Symbol name the host looks for in plugin shared libraries.
445pub const PLUGIN_VTABLE_SYMBOL: &[u8] = b"eov_get_plugin_vtable\0";
446
447/// Returns the expected shared library filename for a plugin on the current
448/// platform, derived from the plugin id.
449pub fn plugin_library_filename(id: &str) -> String {
450    if cfg!(target_os = "macos") {
451        format!("lib{id}.dylib")
452    } else if cfg!(target_os = "windows") {
453        format!("{id}.dll")
454    } else {
455        format!("lib{id}.so")
456    }
457}