diff --git a/src/wrapper/clap.rs b/src/wrapper/clap.rs index 747974f2..cc86a2dc 100644 --- a/src/wrapper/clap.rs +++ b/src/wrapper/clap.rs @@ -1,8 +1,11 @@ -use crate::ClapPlugin; +mod factory; /// Re-export for the wrapper. -pub use ::clap_sys::entry::clap_plugin_entry; -pub use ::clap_sys::version::CLAP_VERSION; +pub use self::factory::Factory; +pub use clap_sys::entry::clap_plugin_entry; +pub use clap_sys::plugin_factory::CLAP_PLUGIN_FACTORY_ID; +pub use clap_sys::version::CLAP_VERSION; +pub use lazy_static::lazy_static; /// Export a CLAP plugin from this library using the provided plugin type. #[macro_export] @@ -12,6 +15,15 @@ macro_rules! nih_export_clap { // `$plugin_ty`, so we need to generate the function inside of this macro #[doc(hidden)] mod clap { + // Because `$plugin_ty` is likely defined in the enclosing scope + use super::*; + + // We don't use generics inside of statics, so this lazy_static is used as kind of an + // escape hatch + ::nih_plug::wrapper::clap::lazy_static! { + static ref factory: ::nih_plug::wrapper::clap::Factory<$plugin_ty> = ::nih_plug::wrapper::clap::Factory::default(); + } + // We don't need any special initialization or deinitialization handling pub fn init(_plugin_path: *const ::std::os::raw::c_char) -> bool { nih_log!("clap::init()"); @@ -26,11 +38,16 @@ macro_rules! nih_export_clap { pub fn get_factory( factory_id: *const ::std::os::raw::c_char, ) -> *const ::std::ffi::c_void { - nih_log!("clap::get_factory({:?})", unsafe { - ::std::ffi::CStr::from_ptr(factory_id) - }); - - std::ptr::null() + if !factory_id.is_null() + && unsafe { ::std::ffi::CStr::from_ptr(factory_id) } + == unsafe { ::std::ffi::CStr::from_ptr( + ::nih_plug::wrapper::clap::CLAP_PLUGIN_FACTORY_ID, + ) } + { + &(*factory).clap_plugin_factory() as *const _ as *const ::std::ffi::c_void + } else { + std::ptr::null() + } } } diff --git a/src/wrapper/clap/factory.rs b/src/wrapper/clap/factory.rs new file mode 100644 index 00000000..be9e75f2 --- /dev/null +++ b/src/wrapper/clap/factory.rs @@ -0,0 +1,31 @@ +use clap_sys::plugin_factory::clap_plugin_factory; +use std::marker::PhantomData; +use std::mem; + +use crate::ClapPlugin; + +/// The plugin's factory. Initialized using a lazy_static from the entry poiunt's `get_factory()` +/// function. From this point onwards we don't need to generate code with macros anymore. +#[doc(hidden)] +pub struct Factory { + /// The type will be used for constructing plugin instances later. + _phantom: PhantomData

, +} + +impl Default for Factory

{ + fn default() -> Self { + Self { + _phantom: PhantomData, + } + } +} + +impl Factory

{ + pub fn clap_plugin_factory(&self) -> clap_plugin_factory { + clap_plugin_factory { + get_plugin_count: todo!(), + get_plugin_descriptor: todo!(), + create_plugin: todo!(), + } + } +}