2022-03-01 02:53:31 +11:00
|
|
|
use clap_sys::host::clap_host;
|
|
|
|
use clap_sys::plugin::{clap_plugin, clap_plugin_descriptor};
|
2022-03-01 02:31:49 +11:00
|
|
|
use clap_sys::plugin_factory::clap_plugin_factory;
|
2022-03-01 04:44:03 +11:00
|
|
|
use std::ffi::CStr;
|
2022-03-01 02:53:31 +11:00
|
|
|
use std::os::raw::c_char;
|
2022-03-01 03:46:20 +11:00
|
|
|
use std::ptr;
|
2022-03-04 01:06:13 +11:00
|
|
|
use std::sync::Arc;
|
2022-03-01 02:31:49 +11:00
|
|
|
|
2022-03-01 04:44:03 +11:00
|
|
|
use super::descriptor::PluginDescriptor;
|
2022-03-04 01:08:14 +11:00
|
|
|
use super::wrapper::Wrapper;
|
2022-03-04 09:23:51 +11:00
|
|
|
use crate::plugin::ClapPlugin;
|
2022-03-01 02:31:49 +11:00
|
|
|
|
|
|
|
/// 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)]
|
2022-03-01 02:50:16 +11:00
|
|
|
#[repr(C)]
|
2022-03-01 02:31:49 +11:00
|
|
|
pub struct Factory<P: ClapPlugin> {
|
2022-03-01 02:50:16 +11:00
|
|
|
// Keep the vtable as the first field so we can do a simple pointer cast
|
|
|
|
pub clap_plugin_factory: clap_plugin_factory,
|
|
|
|
|
2022-03-01 04:44:03 +11:00
|
|
|
plugin_descriptor: PluginDescriptor<P>,
|
2022-03-01 02:31:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: ClapPlugin> Default for Factory<P> {
|
|
|
|
fn default() -> Self {
|
2022-03-01 04:44:03 +11:00
|
|
|
Self {
|
2022-03-01 02:50:16 +11:00
|
|
|
clap_plugin_factory: clap_plugin_factory {
|
|
|
|
get_plugin_count: Self::get_plugin_count,
|
2022-03-01 02:53:31 +11:00
|
|
|
get_plugin_descriptor: Self::get_plugin_descriptor,
|
|
|
|
create_plugin: Self::create_plugin,
|
2022-03-01 02:50:16 +11:00
|
|
|
},
|
2022-03-01 04:44:03 +11:00
|
|
|
plugin_descriptor: PluginDescriptor::default(),
|
|
|
|
}
|
2022-03-01 02:31:49 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: ClapPlugin> Factory<P> {
|
2022-03-01 02:50:16 +11:00
|
|
|
unsafe extern "C" fn get_plugin_count(_factory: *const clap_plugin_factory) -> u32 {
|
|
|
|
1
|
2022-03-01 02:31:49 +11:00
|
|
|
}
|
2022-03-01 02:53:31 +11:00
|
|
|
|
|
|
|
unsafe extern "C" fn get_plugin_descriptor(
|
|
|
|
factory: *const clap_plugin_factory,
|
|
|
|
index: u32,
|
|
|
|
) -> *const clap_plugin_descriptor {
|
2022-03-01 03:46:20 +11:00
|
|
|
let factory = &*(factory as *const Self);
|
|
|
|
|
|
|
|
if index == 0 {
|
2022-03-01 04:44:03 +11:00
|
|
|
factory.plugin_descriptor.clap_plugin_descriptor()
|
2022-03-01 03:46:20 +11:00
|
|
|
} else {
|
|
|
|
ptr::null()
|
|
|
|
}
|
2022-03-01 02:53:31 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn create_plugin(
|
|
|
|
factory: *const clap_plugin_factory,
|
|
|
|
host: *const clap_host,
|
|
|
|
plugin_id: *const c_char,
|
|
|
|
) -> *const clap_plugin {
|
2022-03-01 04:27:57 +11:00
|
|
|
let factory = &*(factory as *const Self);
|
|
|
|
|
2022-03-01 04:44:03 +11:00
|
|
|
if !plugin_id.is_null() && CStr::from_ptr(plugin_id) == factory.plugin_descriptor.clap_id()
|
|
|
|
{
|
2022-03-04 01:06:13 +11:00
|
|
|
// Arc does not have a convenient leak function like Box, so this gets a bit awkward
|
|
|
|
// This pointer gets turned into an Arc and its reference count decremented in
|
|
|
|
// [Wrapper::destroy()]
|
|
|
|
&(*Arc::into_raw(Wrapper::<P>::new(host))).clap_plugin
|
2022-03-01 04:27:57 +11:00
|
|
|
} else {
|
|
|
|
ptr::null()
|
|
|
|
}
|
2022-03-01 02:53:31 +11:00
|
|
|
}
|
2022-03-01 02:31:49 +11:00
|
|
|
}
|