2022-02-07 04:46:16 +11:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use raw_window_handle::RawWindowHandle;
|
|
|
|
use std::any::Any;
|
|
|
|
use std::ffi::{c_void, CStr};
|
|
|
|
use std::mem;
|
|
|
|
use std::sync::Arc;
|
2022-02-17 06:41:00 +11:00
|
|
|
use vst3_com::utils::SharedVstPtr;
|
2022-02-07 04:46:16 +11:00
|
|
|
use vst3_sys::base::{kInvalidArgument, kResultFalse, kResultOk, tresult, TBool};
|
2022-02-17 06:41:00 +11:00
|
|
|
use vst3_sys::gui::{IPlugFrame, IPlugView};
|
2022-02-07 04:46:16 +11:00
|
|
|
use vst3_sys::VST3;
|
|
|
|
|
|
|
|
use super::inner::WrapperInner;
|
2022-02-17 06:43:53 +11:00
|
|
|
use super::util::{ObjectPtr, VstPtr};
|
2022-03-01 05:21:14 +11:00
|
|
|
use crate::plugin::{Editor, Vst3Plugin};
|
2022-02-07 04:46:16 +11:00
|
|
|
use crate::ParentWindowHandle;
|
|
|
|
|
|
|
|
// Alias needed for the VST3 attribute macro
|
|
|
|
use vst3_sys as vst3_com;
|
|
|
|
|
|
|
|
// Window handle type constants missing from vst3-sys
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_HWND: &str = "HWND";
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_HIVIEW: &str = "HIView";
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_NSVIEW: &str = "NSView";
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_UIVIEW: &str = "UIView";
|
|
|
|
#[allow(unused)]
|
|
|
|
const VST3_PLATFORM_X11_WINDOW: &str = "X11EmbedWindowID";
|
|
|
|
|
|
|
|
/// The plugin's [IPlugView] instance created in [IEditController::create_view] if `P` has an
|
|
|
|
/// editor. This is managed separately so the lifetime bounds match up.
|
|
|
|
#[VST3(implements(IPlugView))]
|
2022-03-01 05:21:14 +11:00
|
|
|
pub(crate) struct WrapperView<P: Vst3Plugin> {
|
2022-02-07 04:46:16 +11:00
|
|
|
inner: Arc<WrapperInner<P>>,
|
|
|
|
editor: Arc<dyn Editor>,
|
|
|
|
editor_handle: RwLock<Option<Box<dyn Any>>>,
|
2022-02-17 06:41:00 +11:00
|
|
|
|
|
|
|
/// The `IPlugFrame` instance passed by the host during `IPlugView::set_frame`.
|
|
|
|
pub plug_frame: RwLock<Option<VstPtr<dyn IPlugFrame>>>,
|
2022-02-07 04:46:16 +11:00
|
|
|
}
|
|
|
|
|
2022-03-01 05:21:14 +11:00
|
|
|
impl<P: Vst3Plugin> WrapperView<P> {
|
2022-02-07 04:46:16 +11:00
|
|
|
pub fn new(inner: Arc<WrapperInner<P>>, editor: Arc<dyn Editor>) -> Box<Self> {
|
2022-02-17 06:41:00 +11:00
|
|
|
Self::allocate(inner, editor, RwLock::new(None), RwLock::new(None))
|
2022-02-07 04:46:16 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 05:21:14 +11:00
|
|
|
impl<P: Vst3Plugin> IPlugView for WrapperView<P> {
|
2022-02-07 04:46:16 +11:00
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
|
|
|
unsafe fn is_platform_type_supported(&self, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
match type_.to_str() {
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_X11_WINDOW => kResultOk,
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Invalid window handle type: {:?}", type_);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(target_os = "macos"))]
|
|
|
|
unsafe fn is_platform_type_supported(&self, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
match type_.to_str() {
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_NSVIEW => kResultOk,
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Invalid window handle type: {:?}", type_);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(target_os = "windows"))]
|
|
|
|
unsafe fn is_platform_type_supported(&self, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
match type_.to_str() {
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_HWND => kResultOk,
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Invalid window handle type: {:?}", type_);
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn attached(&self, parent: *mut c_void, type_: vst3_sys::base::FIDString) -> tresult {
|
|
|
|
let mut editor_handle = self.editor_handle.write();
|
|
|
|
if editor_handle.is_none() {
|
|
|
|
let type_ = CStr::from_ptr(type_);
|
|
|
|
let handle = match type_.to_str() {
|
|
|
|
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_X11_WINDOW => {
|
2022-02-08 07:58:59 +11:00
|
|
|
let mut handle = raw_window_handle::XcbHandle::empty();
|
2022-02-07 04:46:16 +11:00
|
|
|
handle.window = parent as usize as u32;
|
|
|
|
RawWindowHandle::Xcb(handle)
|
|
|
|
}
|
|
|
|
#[cfg(all(target_os = "macos"))]
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_NSVIEW => {
|
2022-02-08 07:58:59 +11:00
|
|
|
let mut handle = raw_window_handle::AppKitHandle::empty();
|
2022-02-07 04:46:16 +11:00
|
|
|
handle.ns_view = parent;
|
2022-02-08 07:58:59 +11:00
|
|
|
RawWindowHandle::AppKit(handle)
|
2022-02-07 04:46:16 +11:00
|
|
|
}
|
|
|
|
#[cfg(all(target_os = "windows"))]
|
|
|
|
Ok(type_) if type_ == VST3_PLATFORM_HWND => {
|
2022-02-08 07:58:59 +11:00
|
|
|
let mut handle = raw_window_handle::Win32Handle::empty();
|
2022-02-07 04:46:16 +11:00
|
|
|
handle.hwnd = parent;
|
2022-02-08 07:58:59 +11:00
|
|
|
RawWindowHandle::Win32(handle)
|
2022-02-07 04:46:16 +11:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
nih_debug_assert_failure!("Unknown window handle type: {:?}", type_);
|
|
|
|
return kInvalidArgument;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-04 02:33:41 +11:00
|
|
|
*editor_handle = Some(self.editor.spawn(
|
|
|
|
ParentWindowHandle { handle },
|
|
|
|
self.inner.clone().make_gui_context(),
|
|
|
|
));
|
2022-02-17 06:43:53 +11:00
|
|
|
*self.inner.plug_view.write() = Some(ObjectPtr::from(self));
|
|
|
|
|
2022-02-07 04:46:16 +11:00
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn removed(&self) -> tresult {
|
|
|
|
let mut editor_handle = self.editor_handle.write();
|
|
|
|
if editor_handle.is_some() {
|
2022-02-17 06:43:53 +11:00
|
|
|
*self.inner.plug_view.write() = None;
|
2022-02-07 04:46:16 +11:00
|
|
|
*editor_handle = None;
|
2022-02-17 06:43:53 +11:00
|
|
|
|
2022-02-07 04:46:16 +11:00
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_wheel(&self, _distance: f32) -> tresult {
|
|
|
|
// We'll let the plugin use the OS' input mechamisms because not all DAWs (or very few
|
|
|
|
// actually) implement these functions
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_key_down(
|
|
|
|
&self,
|
|
|
|
_key: vst3_sys::base::char16,
|
|
|
|
_key_code: i16,
|
|
|
|
_modifiers: i16,
|
|
|
|
) -> tresult {
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_key_up(
|
|
|
|
&self,
|
|
|
|
_key: vst3_sys::base::char16,
|
|
|
|
_key_code: i16,
|
|
|
|
_modifiers: i16,
|
|
|
|
) -> tresult {
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_size(&self, size: *mut vst3_sys::gui::ViewRect) -> tresult {
|
|
|
|
check_null_ptr!(size);
|
|
|
|
|
|
|
|
*size = mem::zeroed();
|
|
|
|
|
|
|
|
let (width, height) = self.editor.size();
|
|
|
|
let size = &mut *size;
|
|
|
|
size.left = 0;
|
|
|
|
size.right = width as i32;
|
|
|
|
size.top = 0;
|
|
|
|
size.bottom = height as i32;
|
|
|
|
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_size(&self, _new_size: *mut vst3_sys::gui::ViewRect) -> tresult {
|
|
|
|
// TODO: Implement resizing
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_focus(&self, _state: TBool) -> tresult {
|
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
2022-02-17 06:41:00 +11:00
|
|
|
unsafe fn set_frame(&self, frame: *mut c_void) -> tresult {
|
|
|
|
// The correct argument type is missing from the bindings
|
|
|
|
let frame: SharedVstPtr<dyn IPlugFrame> = mem::transmute(frame);
|
|
|
|
match frame.upgrade() {
|
|
|
|
Some(frame) => *self.plug_frame.write() = Some(frame.into()),
|
|
|
|
None => *self.plug_frame.write() = None,
|
|
|
|
}
|
|
|
|
|
2022-02-07 04:46:16 +11:00
|
|
|
kResultOk
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn can_resize(&self) -> tresult {
|
|
|
|
// TODO: Implement resizing
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn check_size_constraint(&self, rect: *mut vst3_sys::gui::ViewRect) -> tresult {
|
|
|
|
check_null_ptr!(rect);
|
|
|
|
|
|
|
|
// TODO: Add this with the resizing
|
|
|
|
if (*rect).right - (*rect).left > 0 && (*rect).bottom - (*rect).top > 0 {
|
|
|
|
kResultOk
|
|
|
|
} else {
|
|
|
|
kResultFalse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|