use crate::sys::*;
use crate::PluginInstance;
use crate::core::diagnostics::diag;
use crate::host::app::with_app_host_ffi;
unsafe extern "C" fn checkout_param_stub(
effect_ref: PF_ProgPtr,
index: PF_ParamIndex,
_what_time: A_long,
_time_step: A_long,
_time_scale: A_u_long,
param: *mut PF_ParamDef,
) -> PF_Err {
crate::core::ffi::guard("checkout_param_stub", || {
let index = index as usize;
if effect_ref.is_null() {
log::error!("checkout_param: effect_ref is null");
return PF_Err_BAD_CALLBACK_PARAM;
}
if param.is_null() {
log::warn!("checkout_param: param pointer is null");
return PF_Err_BAD_CALLBACK_PARAM;
}
let Some(instance) = PluginInstance::get_instance_ptr(effect_ref) else {
log::error!(
"checkout_param: no instance found for effect_ref {:#x}",
effect_ref as usize
);
return PF_Err_BAD_CALLBACK_PARAM;
};
let instance = unsafe { instance.as_ref() };
if index >= instance.param_count() {
unsafe { param.write(std::mem::zeroed()) };
log::debug!(
"checkout_param: index {} out of bounds (total={})",
index,
instance.param_count()
);
return PF_Err_INVALID_INDEX;
}
let src_param = instance.param_by_index(index).unwrap();
unsafe { *param = *src_param };
if src_param.param_type == PF_Param_LAYER as PF_ParamType {
crate::strict::checkout(crate::CheckoutKind::CheckoutParam, index as i32, param as usize);
}
diag!("InteractCallbacks/checkout_param",
"effect_ref" => format!("{:#x}", effect_ref as usize),
"index" => index,
"what_time" => _what_time,
"time_step" => _time_step,
"time_scale" => _time_scale,
"param (out)" => format!("{:#x}", param as usize);
result: format!("param is set to {:#x}", param as usize),
);
PF_Err_NONE
})
}
unsafe extern "C" fn checkin_param_stub(_effect_ref: PF_ProgPtr, _param: *mut PF_ParamDef) -> PF_Err {
crate::core::ffi::guard("checkin_param_stub", || {
if _param.is_null() {
log::warn!("checkin_param: param pointer is null");
return PF_Err_BAD_CALLBACK_PARAM;
}
if unsafe { (*_param).param_type } == PF_Param_LAYER as PF_ParamType {
crate::strict::checkout(crate::CheckoutKind::CheckinParam, 0, _param as usize);
}
log::debug!("checkin_param called for effect_ref={:#x}", _effect_ref as usize);
PF_Err_NONE
})
}
unsafe extern "C" fn add_param_sys(effect_ref: PF_ProgPtr, _index: PF_ParamIndex, def: PF_ParamDefPtr) -> PF_Err {
crate::core::ffi::guard("add_param_sys", || {
if def.is_null() {
log::error!("add_param: def is null");
return PF_Err_BAD_CALLBACK_PARAM;
}
let param = unsafe { *def };
if let Err(e) = crate::host::params::add_param_to_instance(effect_ref, param) {
log::error!("add_param: failed to add param: {}", e);
return PF_Err_BAD_CALLBACK_PARAM;
}
diag!("InteractCallbacks/add_param",
"effect_ref" => format!("{:#x}", effect_ref as usize),
"index" => _index,
"def" => format!("{:#x}", def as usize),
);
PF_Err_NONE
})
}
unsafe extern "C" fn abort_stub(effect_ref: PF_ProgPtr) -> PF_Err {
crate::core::ffi::guard("abort_stub", || {
with_app_host_ffi(|host| {
if host.abort_requested(effect_ref as usize) {
PF_Interrupt_CANCEL
} else {
PF_Err_NONE
}
})
})
}
unsafe extern "C" fn progress_stub(effect_ref: PF_ProgPtr, current: A_long, total: A_long) -> PF_Err {
crate::core::ffi::guard("progress_stub", || {
with_app_host_ffi(|host| {
if host.render_progress(effect_ref as usize, current, total) {
PF_Err_NONE
} else {
PF_Interrupt_CANCEL
}
})
})
}
unsafe extern "C" fn register_ui_stub(_effect_ref: PF_ProgPtr, custom_info: *mut PF_CustomUIInfo) -> PF_Err {
crate::core::ffi::guard("register_ui_stub", || {
if let Some(info) = unsafe { custom_info.as_ref() } {
log::info!(
"register_ui: events={:#x} comp={}x{} layer={}x{} preview={}x{}",
info.events,
info.comp_ui_width,
info.comp_ui_height,
info.layer_ui_width,
info.layer_ui_height,
info.preview_ui_width,
info.preview_ui_height
);
}
PF_Err_NONE
})
}
unsafe extern "C" fn checkout_layer_audio_stub(
_effect_ref: PF_ProgPtr,
_index: PF_ParamIndex,
_start_time: A_long,
_duration: A_long,
_time_scale: A_u_long,
_rate: PF_UFixed,
_bytes_per_sample: A_long,
_num_channels: A_long,
_fmt_signed: A_long,
_audio: *mut PF_LayerAudio,
) -> PF_Err {
crate::core::ffi::guard("checkout_layer_audio_stub", || {
log::warn!("STUB: checkout_layer_audio called");
PF_Err_NONE
})
}
unsafe extern "C" fn checkin_layer_audio_stub(_effect_ref: PF_ProgPtr, _audio: PF_LayerAudio) -> PF_Err {
crate::core::ffi::guard("checkin_layer_audio_stub", || {
log::warn!("STUB: checkin_layer_audio called");
PF_Err_NONE
})
}
unsafe extern "C" fn get_audio_data_stub(
_effect_ref: PF_ProgPtr,
_audio: PF_LayerAudio,
_data: *mut PF_SndSamplePtr,
_num_samples: *mut A_long,
_rate: *mut PF_UFixed,
_bytes_per_sample: *mut A_long,
_num_channels: *mut A_long,
_fmt_signed: *mut A_long,
) -> PF_Err {
crate::core::ffi::guard("get_audio_data_stub", || {
log::warn!("STUB: get_audio_data called");
PF_Err_NONE
})
}
pub fn create_interact_callbacks() -> PF_InteractCallbacks {
PF_InteractCallbacks {
checkout_param: Some(checkout_param_stub),
checkin_param: Some(checkin_param_stub),
add_param: Some(add_param_sys),
abort: Some(abort_stub),
progress: Some(progress_stub),
register_ui: Some(register_ui_stub),
checkout_layer_audio: Some(checkout_layer_audio_stub),
checkin_layer_audio: Some(checkin_layer_audio_stub),
get_audio_data: Some(get_audio_data_stub),
reserved_str: [std::ptr::null_mut(); 3],
reserved: [std::ptr::null_mut(); 10],
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::host::app::{AppHost, TEST_APP_HOST};
struct Cancelling;
impl AppHost for Cancelling {
fn abort_requested(&self, _: usize) -> bool {
true
}
fn render_progress(&self, _: usize, current: i32, total: i32) -> bool {
current * 2 < total
}
}
#[test]
fn abort_and_progress_follow_the_host() {
let cb = create_interact_callbacks();
let null = std::ptr::null_mut();
unsafe {
assert_eq!(cb.abort.unwrap()(null), PF_Err_NONE);
TEST_APP_HOST.set(Some(Box::new(Cancelling)));
assert_eq!(cb.abort.unwrap()(null), PF_Interrupt_CANCEL);
assert_eq!(cb.progress.unwrap()(null, 1, 10), PF_Err_NONE);
assert_eq!(cb.progress.unwrap()(null, 6, 10), PF_Interrupt_CANCEL);
}
TEST_APP_HOST.set(None);
}
}