use atomic_refcell::AtomicRefMut;
use clap_sys::ext::draft::remote_controls::{
clap_remote_controls_page, CLAP_REMOTE_CONTROLS_COUNT,
};
use clap_sys::id::{clap_id, CLAP_INVALID_ID};
use clap_sys::string_sizes::CLAP_NAME_SIZE;
use std::cell::Cell;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use super::wrapper::{OutputParamEvent, Task, Wrapper};
use crate::event_loop::EventLoop;
use crate::prelude::{
ClapPlugin, GuiContext, InitContext, ParamPtr, PluginApi, PluginNoteEvent, ProcessContext,
RemoteControlsContext, RemoteControlsPage, RemoteControlsSection, Transport,
};
use crate::wrapper::util::strlcpy;
pub(crate) struct WrapperInitContext<'a, P: ClapPlugin> {
pub(super) wrapper: &'a Wrapper<P>,
pub(super) pending_requests: PendingInitContextRequests,
}
#[derive(Debug, Default)]
pub(crate) struct PendingInitContextRequests {
latency_changed: Cell<Option<u32>>,
}
pub(crate) struct WrapperProcessContext<'a, P: ClapPlugin> {
pub(super) wrapper: &'a Wrapper<P>,
pub(super) input_events_guard: AtomicRefMut<'a, VecDeque<PluginNoteEvent<P>>>,
pub(super) output_events_guard: AtomicRefMut<'a, VecDeque<PluginNoteEvent<P>>>,
pub(super) transport: Transport,
}
pub(crate) struct WrapperGuiContext<P: ClapPlugin> {
pub(super) wrapper: Arc<Wrapper<P>>,
#[cfg(debug_assertions)]
pub(super) param_gesture_checker:
atomic_refcell::AtomicRefCell<crate::wrapper::util::context_checks::ParamGestureChecker>,
}
pub(crate) struct RemoteControlPages<'a> {
param_ptr_to_hash: &'a HashMap<ParamPtr, u32>,
pages: &'a mut Vec<clap_remote_controls_page>,
}
impl<P: ClapPlugin> Drop for WrapperInitContext<'_, P> {
fn drop(&mut self) {
if let Some(samples) = self.pending_requests.latency_changed.take() {
self.wrapper.set_latency_samples(samples)
}
}
}
impl<P: ClapPlugin> InitContext<P> for WrapperInitContext<'_, P> {
fn plugin_api(&self) -> PluginApi {
PluginApi::Clap
}
fn execute(&self, task: P::BackgroundTask) {
(self.wrapper.task_executor.lock())(task);
}
fn set_latency_samples(&self, samples: u32) {
self.pending_requests.latency_changed.set(Some(samples));
}
fn set_current_voice_capacity(&self, capacity: u32) {
self.wrapper.set_current_voice_capacity(capacity)
}
}
impl<P: ClapPlugin> ProcessContext<P> for WrapperProcessContext<'_, P> {
fn plugin_api(&self) -> PluginApi {
PluginApi::Clap
}
fn execute_background(&self, task: P::BackgroundTask) {
let task_posted = self.wrapper.schedule_background(Task::PluginTask(task));
nih_debug_assert!(task_posted, "The task queue is full, dropping task...");
}
fn execute_gui(&self, task: P::BackgroundTask) {
let task_posted = self.wrapper.schedule_gui(Task::PluginTask(task));
nih_debug_assert!(task_posted, "The task queue is full, dropping task...");
}
#[inline]
fn transport(&self) -> &Transport {
&self.transport
}
fn next_event(&mut self) -> Option<PluginNoteEvent<P>> {
self.input_events_guard.pop_front()
}
fn send_event(&mut self, event: PluginNoteEvent<P>) {
self.output_events_guard.push_back(event);
}
fn set_latency_samples(&self, samples: u32) {
self.wrapper.set_latency_samples(samples)
}
fn set_current_voice_capacity(&self, capacity: u32) {
self.wrapper.set_current_voice_capacity(capacity)
}
}
impl<P: ClapPlugin> GuiContext for WrapperGuiContext<P> {
fn plugin_api(&self) -> PluginApi {
PluginApi::Clap
}
fn request_resize(&self) -> bool {
self.wrapper.request_resize()
}
unsafe fn raw_begin_set_parameter(&self, param: ParamPtr) {
match self.wrapper.param_ptr_to_hash.get(¶m) {
Some(hash) => {
let success = self
.wrapper
.queue_parameter_event(OutputParamEvent::BeginGesture { param_hash: *hash });
nih_debug_assert!(
success,
"Parameter output event queue was full, parameter change will not be sent to \
the host"
);
}
None => nih_debug_assert_failure!("Unknown parameter: {:?}", param),
}
#[cfg(debug_assertions)]
match self.wrapper.param_id_from_ptr(param) {
Some(param_id) => self
.param_gesture_checker
.borrow_mut()
.begin_set_parameter(param_id),
None => nih_debug_assert_failure!(
"raw_begin_set_parameter() called with an unknown ParamPtr"
),
}
}
unsafe fn raw_set_parameter_normalized(&self, param: ParamPtr, normalized: f32) {
match self.wrapper.param_ptr_to_hash.get(¶m) {
Some(hash) => {
let clap_plain_value = normalized as f64 * param.step_count().unwrap_or(1) as f64;
let success = self
.wrapper
.queue_parameter_event(OutputParamEvent::SetValue {
param_hash: *hash,
clap_plain_value,
});
nih_debug_assert!(
success,
"Parameter output event queue was full, parameter change will not be sent to \
the host"
);
}
None => nih_debug_assert_failure!("Unknown parameter: {:?}", param),
}
#[cfg(debug_assertions)]
match self.wrapper.param_id_from_ptr(param) {
Some(param_id) => self
.param_gesture_checker
.borrow_mut()
.set_parameter(param_id),
None => {
nih_debug_assert_failure!("raw_set_parameter() called with an unknown ParamPtr")
}
}
}
unsafe fn raw_end_set_parameter(&self, param: ParamPtr) {
match self.wrapper.param_ptr_to_hash.get(¶m) {
Some(hash) => {
let success = self
.wrapper
.queue_parameter_event(OutputParamEvent::EndGesture { param_hash: *hash });
nih_debug_assert!(
success,
"Parameter output event queue was full, parameter change will not be sent to \
the host"
);
}
None => nih_debug_assert_failure!("Unknown parameter: {:?}", param),
}
#[cfg(debug_assertions)]
match self.wrapper.param_id_from_ptr(param) {
Some(param_id) => self
.param_gesture_checker
.borrow_mut()
.end_set_parameter(param_id),
None => {
nih_debug_assert_failure!("raw_end_set_parameter() called with an unknown ParamPtr")
}
}
}
fn get_state(&self) -> crate::wrapper::state::PluginState {
self.wrapper.get_state_object()
}
fn set_state(&self, state: crate::wrapper::state::PluginState) {
self.wrapper.set_state_object_from_gui(state)
}
}
pub(crate) struct Section {
pages: Vec<Page>,
}
pub(crate) struct Page {
name: String,
params: Vec<Option<ParamPtr>>,
}
impl<'a> RemoteControlPages<'a> {
pub fn define_remote_control_pages<P: ClapPlugin>(
plugin: &P,
pages: &'a mut Vec<clap_remote_controls_page>,
param_ptr_to_hash: &'a HashMap<ParamPtr, u32>,
) {
plugin.remote_controls(&mut Self {
pages,
param_ptr_to_hash,
});
}
fn add_clap_page(
&mut self,
section: &str,
page_name: &str,
params: impl IntoIterator<Item = Option<ParamPtr>>,
) {
let mut page = clap_remote_controls_page {
section_name: [0; CLAP_NAME_SIZE],
page_id: self.pages.len() as clap_id,
page_name: [0; CLAP_NAME_SIZE],
param_ids: [CLAP_INVALID_ID; CLAP_REMOTE_CONTROLS_COUNT],
is_for_preset: false,
};
strlcpy(&mut page.section_name, section);
strlcpy(&mut page.page_name, page_name);
let mut params = params.into_iter();
for (param_id, param_ptr) in page.param_ids.iter_mut().zip(&mut params) {
if let Some(param_ptr) = param_ptr {
*param_id = self.param_ptr_to_id(param_ptr);
}
}
nih_debug_assert!(
params.next().is_none(),
"More than eight parameters were passed to 'RemoteControlPages::add_page()', this is \
a NIH-plug bug."
);
self.pages.push(page);
}
fn param_ptr_to_id(&self, ptr: ParamPtr) -> clap_id {
match self.param_ptr_to_hash.get(&ptr) {
Some(id) => *id,
None => {
nih_debug_assert_failure!(
"An unknown parameter was added to a remote control page, ignoring..."
);
CLAP_INVALID_ID
}
}
}
}
impl RemoteControlsContext for RemoteControlPages<'_> {
type Section = Section;
fn add_section(&mut self, name: impl Into<String>, f: impl FnOnce(&mut Self::Section)) {
let section_name = name.into();
let mut section = Section {
pages: Vec::with_capacity(1),
};
f(&mut section);
for page in section.pages {
if page.params.len() > CLAP_REMOTE_CONTROLS_COUNT {
for (subpage_idx, subpage_params) in
page.params.chunks(CLAP_REMOTE_CONTROLS_COUNT).enumerate()
{
let subpage_name = format!("{} {}", page.name, subpage_idx + 1);
self.add_clap_page(
§ion_name,
&subpage_name,
subpage_params.iter().copied(),
);
}
} else {
self.add_clap_page(§ion_name, &page.name, page.params);
}
}
}
}
impl RemoteControlsSection for Section {
type Page = Page;
fn add_page(&mut self, name: impl Into<String>, f: impl FnOnce(&mut Self::Page)) {
let mut page = Page {
name: name.into(),
params: Vec::with_capacity(CLAP_REMOTE_CONTROLS_COUNT),
};
f(&mut page);
self.pages.push(page);
}
}
impl RemoteControlsPage for Page {
fn add_param(&mut self, param: &impl crate::prelude::Param) {
self.params.push(Some(param.as_ptr()));
}
fn add_spacer(&mut self) {
self.params.push(None);
}
}