From fb28134f5ad705d187abca83c50b0e87019bf704 Mon Sep 17 00:00:00 2001 From: glowcoil Date: Tue, 23 Apr 2024 12:42:01 -0500 Subject: [PATCH] introduce EditorContext type; move param edit methods from Host to EditorContext --- examples/gain/src/lib.rs | 2 +- src/editor.rs | 30 ++++++++++++++++++++++++++++++ src/format/clap/gui.rs | 16 ++++++++++++++-- src/format/clap/host.rs | 7 +------ src/format/clap/tests.rs | 4 ++-- src/format/vst3/host.rs | 7 +------ src/format/vst3/tests.rs | 4 ++-- src/format/vst3/view.rs | 16 ++++++++++++++-- src/host.rs | 34 +--------------------------------- src/plugin.rs | 4 ++-- 10 files changed, 68 insertions(+), 56 deletions(-) diff --git a/examples/gain/src/lib.rs b/examples/gain/src/lib.rs index 52680e6..a5ca1b9 100644 --- a/examples/gain/src/lib.rs +++ b/examples/gain/src/lib.rs @@ -86,7 +86,7 @@ impl Plugin for Gain { } } - fn editor(&mut self, parent: &Parent) -> Self::Editor { + fn editor(&mut self, _context: EditorContext, parent: &Parent) -> Self::Editor { GainEditor::open(parent).unwrap() } } diff --git a/src/editor.rs b/src/editor.rs index 4ed1010..7257121 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,7 +1,37 @@ use std::ffi::{c_ulong, c_void}; +use std::rc::Rc; use crate::params::{ParamId, ParamValue}; +pub trait EditorContextInner { + fn begin_gesture(&self, id: ParamId); + fn end_gesture(&self, id: ParamId); + fn set_param(&self, id: ParamId, value: ParamValue); +} + +#[derive(Clone)] +pub struct EditorContext { + inner: Rc, +} + +impl EditorContext { + pub fn from_inner(inner: Rc) -> EditorContext { + EditorContext { inner } + } + + pub fn begin_gesture(&self, id: ParamId) { + self.inner.begin_gesture(id); + } + + pub fn end_gesture(&self, id: ParamId) { + self.inner.end_gesture(id); + } + + pub fn set_param(&self, id: ParamId, value: ParamValue) { + self.inner.set_param(id, value); + } +} + #[derive(Copy, Clone)] pub enum RawParent { Win32(*mut c_void), diff --git a/src/format/clap/gui.rs b/src/format/clap/gui.rs index 1b7cea6..203711b 100644 --- a/src/format/clap/gui.rs +++ b/src/format/clap/gui.rs @@ -1,12 +1,22 @@ use std::ffi::{c_char, CStr}; +use std::rc::Rc; use clap_sys::ext::gui::*; use clap_sys::plugin::*; use super::instance::Instance; -use crate::editor::{Editor, Parent, RawParent}; +use crate::editor::{Editor, EditorContext, EditorContextInner, Parent, RawParent}; +use crate::params::{ParamId, ParamValue}; use crate::plugin::Plugin; +struct ClapEditorContext {} + +impl EditorContextInner for ClapEditorContext { + fn begin_gesture(&self, _id: ParamId) {} + fn end_gesture(&self, _id: ParamId) {} + fn set_param(&self, _id: ParamId, _value: ParamValue) {} +} + impl Instance

{ pub(super) const GUI: clap_plugin_gui = clap_plugin_gui { is_api_supported: Some(Self::gui_is_api_supported), @@ -151,7 +161,9 @@ impl Instance

{ let instance = &*(plugin as *const Self); let main_thread_state = &mut *instance.main_thread_state.get(); - let editor = main_thread_state.plugin.editor(&Parent::from_raw(raw_parent)); + let context = EditorContext::from_inner(Rc::new(ClapEditorContext {})); + let parent = Parent::from_raw(raw_parent); + let editor = main_thread_state.plugin.editor(context, &parent); main_thread_state.editor = Some(editor); true diff --git a/src/format/clap/host.rs b/src/format/clap/host.rs index 58c4f87..0c93086 100644 --- a/src/format/clap/host.rs +++ b/src/format/clap/host.rs @@ -1,10 +1,5 @@ use crate::host::HostInner; -use crate::params::{ParamId, ParamValue}; pub struct ClapHost {} -impl HostInner for ClapHost { - fn begin_gesture(&self, _id: ParamId) {} - fn end_gesture(&self, _id: ParamId) {} - fn set_param(&self, _id: ParamId, _value: ParamValue) {} -} +impl HostInner for ClapHost {} diff --git a/src/format/clap/tests.rs b/src/format/clap/tests.rs index 4f727c7..cc11817 100644 --- a/src/format/clap/tests.rs +++ b/src/format/clap/tests.rs @@ -2,7 +2,7 @@ use std::ffi::{c_char, CStr}; use std::io::{self, Read, Write}; use crate::buffers::Buffers; -use crate::editor::{Editor, Parent, Size}; +use crate::editor::{Editor, EditorContext, Parent, Size}; use crate::events::Events; use clap_sys::plugin_factory::{clap_plugin_factory, CLAP_PLUGIN_FACTORY_ID}; @@ -57,7 +57,7 @@ impl Plugin for TestPlugin { fn processor(&mut self, _config: Config) -> Self::Processor { TestProcessor } - fn editor(&mut self, _parent: &Parent) -> Self::Editor { + fn editor(&mut self, _context: EditorContext, _parent: &Parent) -> Self::Editor { TestEditor } diff --git a/src/format/vst3/host.rs b/src/format/vst3/host.rs index d5eeef8..a4ae2f3 100644 --- a/src/format/vst3/host.rs +++ b/src/format/vst3/host.rs @@ -1,10 +1,5 @@ use crate::host::HostInner; -use crate::params::{ParamId, ParamValue}; pub struct Vst3Host {} -impl HostInner for Vst3Host { - fn begin_gesture(&self, _id: ParamId) {} - fn end_gesture(&self, _id: ParamId) {} - fn set_param(&self, _id: ParamId, _value: ParamValue) {} -} +impl HostInner for Vst3Host {} diff --git a/src/format/vst3/tests.rs b/src/format/vst3/tests.rs index 4a367a8..06ed351 100644 --- a/src/format/vst3/tests.rs +++ b/src/format/vst3/tests.rs @@ -4,7 +4,7 @@ use std::io::{self, Read, Write}; use std::{ptr, slice}; use crate::buffers::Buffers; -use crate::editor::{Editor, Parent, Size}; +use crate::editor::{Editor, EditorContext, Parent, Size}; use crate::events::Events; use crate::host::Host; use crate::params::{ParamId, ParamValue}; @@ -64,7 +64,7 @@ impl Plugin for TestPlugin { fn processor(&mut self, _config: Config) -> Self::Processor { TestProcessor } - fn editor(&mut self, _parent: &Parent) -> Self::Editor { + fn editor(&mut self, _context: EditorContext, _parent: &Parent) -> Self::Editor { TestEditor } diff --git a/src/format/vst3/view.rs b/src/format/vst3/view.rs index d81f25a..2f98c69 100644 --- a/src/format/vst3/view.rs +++ b/src/format/vst3/view.rs @@ -1,13 +1,23 @@ use std::cell::UnsafeCell; use std::ffi::{c_void, CStr}; +use std::rc::Rc; use std::sync::Arc; use vst3::{Class, Steinberg::*}; use super::component::MainThreadState; -use crate::editor::{Editor, Parent, RawParent}; +use crate::editor::{Editor, EditorContext, EditorContextInner, Parent, RawParent}; +use crate::params::{ParamId, ParamValue}; use crate::plugin::Plugin; +struct Vst3EditorContext {} + +impl EditorContextInner for Vst3EditorContext { + fn begin_gesture(&self, _id: ParamId) {} + fn end_gesture(&self, _id: ParamId) {} + fn set_param(&self, _id: ParamId, _value: ParamValue) {} +} + pub struct View { main_thread_state: Arc>>, } @@ -60,7 +70,9 @@ impl IPlugViewTrait for View

{ let main_thread_state = &mut *self.main_thread_state.get(); - let editor = main_thread_state.plugin.editor(&Parent::from_raw(raw_parent)); + let context = EditorContext::from_inner(Rc::new(Vst3EditorContext {})); + let parent = Parent::from_raw(raw_parent); + let editor = main_thread_state.plugin.editor(context, &parent); main_thread_state.editor = Some(editor); kResultOk diff --git a/src/host.rs b/src/host.rs index 8ee5278..ce91c3a 100644 --- a/src/host.rs +++ b/src/host.rs @@ -1,12 +1,6 @@ use std::sync::Arc; -use crate::params::{ParamId, ParamValue}; - -pub trait HostInner { - fn begin_gesture(&self, id: ParamId); - fn end_gesture(&self, id: ParamId); - fn set_param(&self, id: ParamId, value: ParamValue); -} +pub trait HostInner {} #[derive(Clone)] pub struct Host { @@ -17,30 +11,4 @@ impl Host { pub fn from_inner(inner: Arc) -> Host { Host { inner } } - - pub fn edit_param(&self, id: ParamId) -> Gesture { - self.inner.begin_gesture(id); - - Gesture { - inner: Arc::clone(&self.inner), - id, - } - } -} - -pub struct Gesture { - inner: Arc, - id: ParamId, -} - -impl Gesture { - pub fn set_value(&self, value: ParamValue) { - self.inner.set_param(self.id, value); - } -} - -impl Drop for Gesture { - fn drop(&mut self) { - self.inner.end_gesture(self.id); - } } diff --git a/src/plugin.rs b/src/plugin.rs index 9a7c639..6a2d182 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,7 +1,7 @@ use std::io::{self, Read, Write}; use crate::bus::{BusInfo, Layout}; -use crate::editor::{Editor, Parent}; +use crate::editor::{Editor, EditorContext, Parent}; use crate::host::Host; use crate::params::{ParamId, ParamInfo, ParamValue}; use crate::process::{Config, Processor}; @@ -46,7 +46,7 @@ pub trait Plugin: Send + Sized + 'static { fn save(&self, output: &mut impl Write) -> io::Result<()>; fn load(&mut self, input: &mut impl Read) -> io::Result<()>; fn processor(&mut self, config: Config) -> Self::Processor; - fn editor(&mut self, parent: &Parent) -> Self::Editor; + fn editor(&mut self, context: EditorContext, parent: &Parent) -> Self::Editor; #[allow(unused_variables)] fn latency(&self, config: &Config) -> u64 {