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/// One level of a slide's pyramid.
172///
173/// `downsample` is the level-0 size divided by this level's size, as the format
174/// reports it. It is not always a power of two: Aperio pyramids commonly go
175/// 1, 4, 16, so a plugin that assumes halving reads the wrong region.
176#[repr(C)]
177#[derive(StableAbi, Clone, Copy, Debug)]
178pub struct LevelInfoFFI {
179    pub level: u32,
180    pub width: u64,
181    pub height: u64,
182    pub downsample: f64,
183}
184
185#[repr(C)]
186#[derive(StableAbi, Clone, Debug)]
187pub struct ViewportSnapshotFFI {
188    pub pane_index: u32,
189    pub file_id: i32,
190    pub file_path: RString,
191    pub filename: RString,
192    pub center_x: f64,
193    pub center_y: f64,
194    pub zoom: f64,
195    pub width: f64,
196    pub height: f64,
197    pub image_width: f64,
198    pub image_height: f64,
199    pub bounds_left: f64,
200    pub bounds_top: f64,
201    pub bounds_right: f64,
202    pub bounds_bottom: f64,
203}
204
205#[repr(C)]
206#[derive(StableAbi, Clone, Debug)]
207pub struct HostSnapshotFFI {
208    pub app_name: RString,
209    pub app_version: RString,
210    pub render_backend: RString,
211    pub filtering_mode: RString,
212    pub split_enabled: bool,
213    pub focused_pane: u32,
214    pub open_files: RVec<OpenFileInfoFFI>,
215    pub active_file: ROption<OpenFileInfoFFI>,
216    pub active_viewport: ROption<ViewportSnapshotFFI>,
217    pub recent_files: RVec<RString>,
218    pub active_sidebar: ROption<ActiveSidebarFFI>,
219}
220
221#[repr(C)]
222#[derive(StableAbi, Copy, Clone)]
223pub struct HostApiVTable {
224    pub context: u64,
225    pub get_snapshot: extern "C" fn(context: u64) -> HostSnapshotFFI,
226    pub read_region: extern "C" fn(
227        context: u64,
228        file_id: i32,
229        level: u32,
230        x: i64,
231        y: i64,
232        width: u32,
233        height: u32,
234    ) -> RResult<RVec<u8>, RString>,
235    pub open_file: extern "C" fn(context: u64, path: RString) -> RResult<(), RString>,
236    pub set_active_viewport: extern "C" fn(
237        context: u64,
238        center_x: f64,
239        center_y: f64,
240        zoom: f64,
241    ) -> RResult<(), RString>,
242    pub fit_active_viewport: extern "C" fn(context: u64) -> RResult<(), RString>,
243    pub frame_active_rect: extern "C" fn(
244        context: u64,
245        x: f64,
246        y: f64,
247        width: f64,
248        height: f64,
249    ) -> RResult<(), RString>,
250    pub cycle_focused_pane: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
251    pub cycle_active_tab: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
252    pub toggle_series_bar: extern "C" fn(context: u64) -> RResult<(), RString>,
253    pub cycle_series_selection: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
254    pub activate_selected_series_entry: extern "C" fn(context: u64) -> RResult<(), RString>,
255    pub set_active_tool: extern "C" fn(context: u64, tool: HostToolModeFFI) -> RResult<(), RString>,
256    pub request_render: extern "C" fn(context: u64) -> RResult<(), RString>,
257    pub set_toolbar_button_active:
258        extern "C" fn(context: u64, button_id: RString, active: bool) -> RResult<(), RString>,
259    pub set_hud_toolbar_button_active:
260        extern "C" fn(context: u64, button_id: RString, active: bool) -> RResult<(), RString>,
261    pub show_sidebar: extern "C" fn(
262        context: u64,
263        button_id: RString,
264        width_px: u32,
265        ui_path: RString,
266        component: RString,
267    ) -> RResult<(), RString>,
268    pub refresh_sidebar: extern "C" fn(context: u64) -> RResult<(), RString>,
269    pub hide_sidebar: extern "C" fn(context: u64) -> RResult<(), RString>,
270    pub show_confirmation_dialog:
271        extern "C" fn(context: u64, request: ConfirmationDialogRequestFFI) -> RResult<(), RString>,
272    pub show_modal_dialog:
273        extern "C" fn(context: u64, request: ModalDialogRequestFFI) -> RResult<(), RString>,
274    pub hide_modal_dialog: extern "C" fn(context: u64) -> RResult<(), RString>,
275    pub set_undo_redo_state:
276        extern "C" fn(context: u64, state: PluginUndoRedoStateFFI) -> RResult<(), RString>,
277    pub open_file_dialog: extern "C" fn(
278        context: u64,
279        filter_name: RString,
280        extension: RString,
281    ) -> RResult<RString, RString>,
282    pub save_file_dialog: extern "C" fn(
283        context: u64,
284        default_file_name: RString,
285        filter_name: RString,
286        extension: RString,
287    ) -> RResult<RString, RString>,
288    pub log_message: extern "C" fn(context: u64, level: HostLogLevelFFI, message: RString),
289    /// Every pyramid level of an open file, in level order, so a plugin can
290    /// pick a level to read from instead of guessing the downsample factors.
291    /// `OpenFileInfoFFI::level_count` is the length of what this returns.
292    pub get_level_info:
293        extern "C" fn(context: u64, file_id: i32) -> RResult<RVec<LevelInfoFFI>, RString>,
294}
295
296/// VTable of function pointers exported by each plugin shared library.
297///
298/// Each plugin crate exports:
299/// ```ignore
300/// #[unsafe(no_mangle)]
301/// pub extern "C" fn eov_get_plugin_vtable() -> PluginVTable { ... }
302/// ```
303#[repr(C)]
304#[derive(StableAbi, Copy, Clone)]
305pub struct PluginVTable {
306    /// Supplies the plugin with a host API vtable so it can query the app and
307    /// issue host commands later during callbacks.
308    pub set_host_api: extern "C" fn(host_api: HostApiVTable),
309
310    /// Returns the toolbar buttons this plugin wants to register.
311    pub get_toolbar_buttons: extern "C" fn() -> RVec<ToolbarButtonFFI>,
312
313    /// Returns HUD toolbar buttons shown inside each viewport.
314    pub get_hud_toolbar_buttons: extern "C" fn() -> RVec<HudToolbarButtonFFI>,
315
316    /// Called when a toolbar button registered by this plugin is clicked.
317    pub on_action: extern "C" fn(action_id: RString) -> ActionResponseFFI,
318
319    /// Called when a HUD toolbar button is clicked in a specific viewport.
320    pub on_hud_action:
321        extern "C" fn(action_id: RString, viewport: ViewportSnapshotFFI) -> ActionResponseFFI,
322
323    /// Called when a callback defined in the plugin's `.slint` UI is invoked.
324    /// `callback_name` is the kebab-case name of the callback as declared in
325    /// the `.slint` file.
326    pub on_ui_callback: extern "C" fn(callback_name: RString, args_json: RString),
327
328    /// Returns the current values for public properties on the active sidebar.
329    /// Values are encoded as JSON and converted to typed Slint values by the host.
330    pub get_sidebar_properties: extern "C" fn() -> RVec<UiPropertyFFI>,
331
332    /// Returns plugin-contributed viewport context menu items for the given viewport.
333    pub get_viewport_context_menu_items:
334        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportContextMenuItemFFI>,
335
336    /// Called when a plugin-contributed viewport context menu item is selected.
337    pub on_viewport_context_menu_action:
338        extern "C" fn(item_id: RString, viewport: ViewportSnapshotFFI) -> ActionResponseFFI,
339
340    /// Returns point overlay data that should be rendered over the given viewport.
341    pub get_viewport_overlay_points:
342        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportOverlayPointFFI>,
343
344    /// Returns polygon overlay data that should be rendered over the given viewport.
345    pub get_viewport_overlay_polygons:
346        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportOverlayPolygonFFI>,
347
348    /// Returns a viewport-scoped Slint component request for this plugin, if any.
349    pub get_viewport_overlay_component:
350        extern "C" fn() -> ROption<ViewportOverlayComponentRequestFFI>,
351
352    /// Returns the current public properties for the plugin viewport overlay component.
353    pub get_viewport_overlay_properties:
354        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<UiPropertyFFI>,
355
356    /// Called when the host commits a point annotation click for the active point tool.
357    pub on_point_annotation_placed:
358        extern "C" fn(viewport: ViewportSnapshotFFI, x_level0: f64, y_level0: f64),
359
360    /// Called when the host commits a polygon annotation for the active polygon tool.
361    pub on_polygon_annotation_placed:
362        extern "C" fn(viewport: ViewportSnapshotFFI, vertices: RVec<ViewportOverlayVertexFFI>),
363
364    /// Called when the host dispatches an undo request for this plugin.
365    pub on_undo: extern "C" fn() -> ActionResponseFFI,
366
367    /// Called when the host dispatches a redo request for this plugin.
368    pub on_redo: extern "C" fn() -> ActionResponseFFI,
369
370    /// Called when the host moves an existing point annotation for the active point tool.
371    pub on_point_annotation_moved: extern "C" fn(
372        viewport: ViewportSnapshotFFI,
373        annotation_id: RString,
374        x_level0: f64,
375        y_level0: f64,
376    ),
377
378    /// Called when the host moves an existing polygon annotation for the active polygon tool.
379    pub on_polygon_annotation_moved: extern "C" fn(
380        viewport: ViewportSnapshotFFI,
381        annotation_id: RString,
382        vertices: RVec<ViewportOverlayVertexFFI>,
383    ),
384
385    /// Called when the host selects or deselects a plugin-owned overlay annotation.
386    /// An empty annotation id means the current selection should be cleared.
387    pub on_viewport_annotation_selected:
388        extern "C" fn(viewport: ViewportSnapshotFFI, annotation_id: RString),
389
390    /// Returns viewport filter descriptors this plugin provides.
391    /// May return an empty vec if the plugin has no viewport filters.
392    pub get_viewport_filters: extern "C" fn() -> RVec<ViewportFilterFFI>,
393
394    /// Apply a CPU viewport filter in-place.
395    /// `filter_id` identifies which filter to apply.
396    /// `rgba_data` is a mutable pointer to width*height*4 bytes of RGBA8 data.
397    /// Returns `true` if the filter was applied successfully.
398    pub apply_filter_cpu: extern "C" fn(
399        filter_id: RString,
400        rgba_data: *mut u8,
401        len: u32,
402        width: u32,
403        height: u32,
404        viewport: *const ViewportSnapshotFFI,
405    ) -> bool,
406
407    /// Apply a GPU viewport filter using raw Vulkan handles.
408    /// `filter_id` identifies which filter to apply.
409    /// `ctx` is a pointer to a `GpuFilterContextFFI` struct with the Vulkan
410    /// device, image, and sync fence. The plugin must signal the fence when done.
411    /// Returns `true` if the filter was applied successfully.
412    pub apply_filter_gpu:
413        extern "C" fn(filter_id: RString, ctx: *const GpuFilterContextFFI) -> bool,
414
415    /// Enable or disable a viewport filter.
416    pub set_filter_enabled: extern "C" fn(filter_id: RString, enabled: bool),
417}
418
419/// FFI-safe GPU filter context passed from the host to the plugin.
420///
421/// Contains raw Vulkan handles for zero-copy GPU memory access. The plugin
422/// creates its own Vulkan pipeline on the shared device and submits commands
423/// that operate on `vk_image`. The plugin **must** signal `vk_done_fence`
424/// when its GPU work is complete.
425#[repr(C)]
426#[derive(StableAbi, Clone, Debug)]
427pub struct GpuFilterContextFFI {
428    /// `VkDevice` handle (as `u64`).
429    pub vk_device: u64,
430    /// `VkPhysicalDevice` handle (as `u64`).
431    pub vk_physical_device: u64,
432    /// `VkQueue` handle the plugin may submit to.
433    pub vk_queue: u64,
434    /// Queue family index for `vk_queue`.
435    pub queue_family_index: u32,
436    /// `VkImage` handle for the RGBA8 frame.
437    pub vk_image: u64,
438    /// Image width in pixels.
439    pub width: u32,
440    /// Image height in pixels.
441    pub height: u32,
442    /// `VkFormat` value (e.g. `VK_FORMAT_R8G8B8A8_UNORM` = 37).
443    pub vk_format: u32,
444    /// `VkFence` handle — the plugin **must** signal this when its GPU
445    /// commands have finished executing on `vk_image`.
446    pub vk_done_fence: u64,
447}
448
449/// FFI-safe viewport filter descriptor.
450#[repr(C)]
451#[derive(StableAbi, Clone, Debug)]
452pub struct ViewportFilterFFI {
453    pub filter_id: RString,
454    pub name: RString,
455    pub supports_cpu: bool,
456    pub supports_gpu: bool,
457    pub enabled: bool,
458}
459
460/// The type of the factory function each plugin exports.
461pub type GetPluginVTableFn = unsafe extern "C" fn() -> PluginVTable;
462
463/// Symbol name the host looks for in plugin shared libraries.
464pub const PLUGIN_VTABLE_SYMBOL: &[u8] = b"eov_get_plugin_vtable\0";
465
466/// Returns the expected shared library filename for a plugin on the current
467/// platform, derived from the plugin id.
468pub fn plugin_library_filename(id: &str) -> String {
469    if cfg!(target_os = "macos") {
470        format!("lib{id}.dylib")
471    } else if cfg!(target_os = "windows") {
472        format!("{id}.dll")
473    } else {
474        format!("lib{id}.so")
475    }
476}