Skip to content

Commit c57d777

Browse files
committed
introduce EditorHost type; move param gesture methods from Host to EditorHost
1 parent 3135ab9 commit c57d777

File tree

11 files changed

+122
-87
lines changed

11 files changed

+122
-87
lines changed

examples/gain/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Plugin for Gain {
8686
}
8787
}
8888

89-
fn editor(&mut self, parent: &ParentWindow) -> Self::Editor {
89+
fn editor(&mut self, _host: EditorHost, parent: &ParentWindow) -> Self::Editor {
9090
GainEditor::open(parent).unwrap()
9191
}
9292
}

src/editor.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
use std::ffi::{c_ulong, c_void};
2+
use std::marker::PhantomData;
3+
use std::rc::Rc;
24

35
use crate::params::{ParamId, ParamValue};
46

7+
pub trait EditorHostInner {
8+
fn begin_gesture(&self, id: ParamId);
9+
fn end_gesture(&self, id: ParamId);
10+
fn set_param(&self, id: ParamId, value: ParamValue);
11+
}
12+
13+
#[derive(Clone)]
14+
pub struct EditorHost {
15+
inner: Rc<dyn EditorHostInner>,
16+
// Ensure !Send and !Sync
17+
_marker: PhantomData<*mut ()>,
18+
}
19+
20+
impl EditorHost {
21+
pub fn from_inner(inner: Rc<dyn EditorHostInner>) -> EditorHost {
22+
EditorHost {
23+
inner,
24+
_marker: PhantomData,
25+
}
26+
}
27+
28+
pub fn begin_gesture(&self, id: ParamId) {
29+
self.inner.begin_gesture(id);
30+
}
31+
32+
pub fn end_gesture(&self, id: ParamId) {
33+
self.inner.end_gesture(id);
34+
}
35+
36+
pub fn set_param(&self, id: ParamId, value: ParamValue) {
37+
self.inner.set_param(id, value);
38+
}
39+
}
40+
541
#[derive(Copy, Clone)]
642
pub enum RawParent {
743
Win32(*mut c_void),

src/format/clap/gui.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
use std::ffi::{c_char, CStr};
2+
use std::rc::Rc;
23

34
use clap_sys::ext::gui::*;
45
use clap_sys::plugin::*;
56

67
use super::instance::Instance;
7-
use crate::editor::{Editor, ParentWindow, RawParent};
8+
use crate::editor::{Editor, EditorHost, EditorHostInner, ParentWindow, RawParent};
9+
use crate::params::{ParamId, ParamValue};
810
use crate::plugin::Plugin;
911

12+
struct ClapEditorHost {}
13+
14+
impl EditorHostInner for ClapEditorHost {
15+
fn begin_gesture(&self, _id: ParamId) {}
16+
fn end_gesture(&self, _id: ParamId) {}
17+
fn set_param(&self, _id: ParamId, _value: ParamValue) {}
18+
}
19+
1020
impl<P: Plugin> Instance<P> {
1121
pub(super) const GUI: clap_plugin_gui = clap_plugin_gui {
1222
is_api_supported: Some(Self::gui_is_api_supported),
@@ -151,7 +161,9 @@ impl<P: Plugin> Instance<P> {
151161
let instance = &*(plugin as *const Self);
152162
let main_thread_state = &mut *instance.main_thread_state.get();
153163

154-
let editor = main_thread_state.plugin.editor(&ParentWindow::from_raw(raw_parent));
164+
let host = EditorHost::from_inner(Rc::new(ClapEditorHost {}));
165+
let parent = ParentWindow::from_raw(raw_parent);
166+
let editor = main_thread_state.plugin.editor(host, &parent);
155167
main_thread_state.editor = Some(editor);
156168

157169
true

src/format/clap/host.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
use crate::host::HostInner;
2-
use crate::params::{ParamId, ParamValue};
32

43
pub struct ClapHost {}
54

6-
impl HostInner for ClapHost {
7-
fn begin_gesture(&self, _id: ParamId) {}
8-
fn end_gesture(&self, _id: ParamId) {}
9-
fn set_param(&self, _id: ParamId, _value: ParamValue) {}
10-
}
5+
impl HostInner for ClapHost {}

src/format/clap/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ffi::{c_char, CStr};
22
use std::io::{self, Read, Write};
33

44
use crate::buffers::Buffers;
5-
use crate::editor::{Editor, ParentWindow, Size};
5+
use crate::editor::{Editor, EditorHost, ParentWindow, Size};
66
use crate::events::Events;
77

88
use clap_sys::plugin_factory::{clap_plugin_factory, CLAP_PLUGIN_FACTORY_ID};
@@ -57,7 +57,7 @@ impl Plugin for TestPlugin {
5757
fn processor(&mut self, _config: Config) -> Self::Processor {
5858
TestProcessor
5959
}
60-
fn editor(&mut self, _parent: &ParentWindow) -> Self::Editor {
60+
fn editor(&mut self, _host: EditorHost, _parent: &ParentWindow) -> Self::Editor {
6161
TestEditor
6262
}
6363

src/format/vst3/component.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use std::cell::UnsafeCell;
22
use std::collections::{HashMap, HashSet};
33
use std::ffi::{c_void, CStr};
44
use std::ptr;
5+
use std::rc::Rc;
56
use std::sync::Arc;
67

78
use vst3::{Class, ComRef, ComWrapper, Steinberg::Vst::*, Steinberg::*};
89

910
use super::buffers::ScratchBuffers;
1011
use super::host::Vst3Host;
1112
use super::util::{copy_wstring, utf16_from_ptr};
12-
use super::view::View;
13+
use super::view::{View, Vst3EditorHost};
1314
use crate::bus::{BusDir, Format, Layout};
1415
use crate::editor::Editor;
1516
use crate::events::{Data, Event, Events};
@@ -39,6 +40,7 @@ pub struct MainThreadState<P: Plugin> {
3940
pub config: Config,
4041
pub plugin: P,
4142
pub editor_params: Vec<f64>,
43+
pub editor_host: Rc<Vst3EditorHost>,
4244
pub editor: Option<P::Editor>,
4345
}
4446

@@ -57,7 +59,7 @@ pub struct Component<P: Plugin> {
5759
param_map: HashMap<ParamId, usize>,
5860
plugin_params: ParamValues,
5961
processor_params: ParamValues,
60-
host: Arc<Vst3Host>,
62+
_host: Arc<Vst3Host>,
6163
main_thread_state: Arc<UnsafeCell<MainThreadState<P>>>,
6264
// When the audio processor is *not* active, references to ProcessState may only be formed from
6365
// the main thread. When the audio processor *is* active, references to ProcessState may only
@@ -107,11 +109,12 @@ impl<P: Plugin> Component<P> {
107109
param_map,
108110
plugin_params: ParamValues::new(&info.params),
109111
processor_params: ParamValues::new(&info.params),
110-
host: host.clone(),
112+
_host: host.clone(),
111113
main_thread_state: Arc::new(UnsafeCell::new(MainThreadState {
112114
config: config.clone(),
113115
plugin: P::new(Host::from_inner(host)),
114116
editor_params,
117+
editor_host: Rc::new(Vst3EditorHost::new()),
115118
editor: None,
116119
})),
117120
process_state: UnsafeCell::new(ProcessState {
@@ -693,7 +696,9 @@ impl<P: Plugin> IEditControllerTrait for Component<P> {
693696
}
694697

695698
unsafe fn setComponentHandler(&self, handler: *mut IComponentHandler) -> tresult {
696-
let mut current_handler = self.host.handler.write().unwrap();
699+
let main_thread_state = &mut *self.main_thread_state.get();
700+
701+
let mut current_handler = main_thread_state.editor_host.handler.borrow_mut();
697702
if let Some(handler) = ComRef::from_raw(handler) {
698703
*current_handler = Some(handler.to_com_ptr());
699704
} else {

src/format/vst3/host.rs

+3-43
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,11 @@
1-
use std::sync::RwLock;
2-
3-
use vst3::ComPtr;
4-
use vst3::Steinberg::Vst::{IComponentHandler, IComponentHandlerTrait};
5-
61
use crate::host::HostInner;
7-
use crate::params::{ParamId, ParamValue};
82

9-
pub struct Vst3Host {
10-
pub handler: RwLock<Option<ComPtr<IComponentHandler>>>,
11-
}
3+
pub struct Vst3Host {}
124

135
impl Vst3Host {
146
pub fn new() -> Vst3Host {
15-
Vst3Host {
16-
handler: RwLock::new(None),
17-
}
7+
Vst3Host {}
188
}
199
}
2010

21-
impl HostInner for Vst3Host {
22-
fn begin_gesture(&self, id: ParamId) {
23-
let handler = self.handler.read().unwrap();
24-
if let Some(handler) = &*handler {
25-
// TODO: only call this on main thread
26-
unsafe {
27-
handler.beginEdit(id);
28-
}
29-
}
30-
}
31-
32-
fn end_gesture(&self, id: ParamId) {
33-
let handler = self.handler.read().unwrap();
34-
if let Some(handler) = &*handler {
35-
// TODO: only call this on main thread
36-
unsafe {
37-
handler.endEdit(id);
38-
}
39-
}
40-
}
41-
42-
fn set_param(&self, id: ParamId, value: ParamValue) {
43-
let handler = self.handler.read().unwrap();
44-
if let Some(handler) = &*handler {
45-
// TODO: only call this on main thread
46-
unsafe {
47-
handler.performEdit(id, value);
48-
}
49-
}
50-
}
51-
}
11+
impl HostInner for Vst3Host {}

src/format/vst3/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::{self, Read, Write};
44
use std::{ptr, slice};
55

66
use crate::buffers::Buffers;
7-
use crate::editor::{Editor, ParentWindow, Size};
7+
use crate::editor::{Editor, EditorHost, ParentWindow, Size};
88
use crate::events::Events;
99
use crate::host::Host;
1010
use crate::params::{ParamId, ParamValue};
@@ -64,7 +64,7 @@ impl Plugin for TestPlugin {
6464
fn processor(&mut self, _config: Config) -> Self::Processor {
6565
TestProcessor
6666
}
67-
fn editor(&mut self, _parent: &ParentWindow) -> Self::Editor {
67+
fn editor(&mut self, _host: EditorHost, _parent: &ParentWindow) -> Self::Editor {
6868
TestEditor
6969
}
7070

src/format/vst3/view.rs

+49-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
1-
use std::cell::UnsafeCell;
1+
use std::cell::{RefCell, UnsafeCell};
22
use std::ffi::{c_void, CStr};
33
use std::sync::Arc;
44

5-
use vst3::{Class, Steinberg::*};
5+
use vst3::Steinberg::Vst::{IComponentHandler, IComponentHandlerTrait};
6+
use vst3::{Class, ComPtr, Steinberg::*};
67

78
use super::component::MainThreadState;
8-
use crate::editor::{Editor, ParentWindow, RawParent};
9+
use crate::editor::{Editor, EditorHost, EditorHostInner, ParentWindow, RawParent};
10+
use crate::params::{ParamId, ParamValue};
911
use crate::plugin::Plugin;
1012

13+
pub struct Vst3EditorHost {
14+
pub handler: RefCell<Option<ComPtr<IComponentHandler>>>,
15+
}
16+
17+
impl Vst3EditorHost {
18+
pub fn new() -> Vst3EditorHost {
19+
Vst3EditorHost {
20+
handler: RefCell::new(None),
21+
}
22+
}
23+
}
24+
25+
impl EditorHostInner for Vst3EditorHost {
26+
fn begin_gesture(&self, id: ParamId) {
27+
let handler = self.handler.borrow();
28+
if let Some(handler) = &*handler {
29+
unsafe {
30+
handler.beginEdit(id);
31+
}
32+
}
33+
}
34+
35+
fn end_gesture(&self, id: ParamId) {
36+
let handler = self.handler.borrow();
37+
if let Some(handler) = &*handler {
38+
unsafe {
39+
handler.endEdit(id);
40+
}
41+
}
42+
}
43+
44+
fn set_param(&self, id: ParamId, value: ParamValue) {
45+
let handler = self.handler.borrow();
46+
if let Some(handler) = &*handler {
47+
unsafe {
48+
handler.performEdit(id, value);
49+
}
50+
}
51+
}
52+
}
53+
1154
pub struct View<P: Plugin> {
1255
main_thread_state: Arc<UnsafeCell<MainThreadState<P>>>,
1356
}
@@ -60,7 +103,9 @@ impl<P: Plugin> IPlugViewTrait for View<P> {
60103

61104
let main_thread_state = &mut *self.main_thread_state.get();
62105

63-
let editor = main_thread_state.plugin.editor(&ParentWindow::from_raw(raw_parent));
106+
let host = EditorHost::from_inner(main_thread_state.editor_host.clone());
107+
let parent = ParentWindow::from_raw(raw_parent);
108+
let editor = main_thread_state.plugin.editor(host, &parent);
64109
main_thread_state.editor = Some(editor);
65110

66111
kResultOk

src/host.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
use std::sync::Arc;
22

3-
use crate::params::{ParamId, ParamValue};
4-
5-
pub trait HostInner {
6-
fn begin_gesture(&self, id: ParamId);
7-
fn end_gesture(&self, id: ParamId);
8-
fn set_param(&self, id: ParamId, value: ParamValue);
9-
}
3+
pub trait HostInner {}
104

115
#[derive(Clone)]
126
pub struct Host {
13-
inner: Arc<dyn HostInner + Send + Sync>,
7+
_inner: Arc<dyn HostInner + Send + Sync>,
148
}
159

1610
impl Host {
1711
pub fn from_inner(inner: Arc<dyn HostInner + Send + Sync>) -> Host {
18-
Host { inner }
19-
}
20-
21-
pub fn begin_gesture(&self, id: ParamId) {
22-
self.inner.begin_gesture(id);
23-
}
24-
25-
pub fn end_gesture(&self, id: ParamId) {
26-
self.inner.end_gesture(id);
27-
}
28-
29-
pub fn set_param(&self, id: ParamId, value: ParamValue) {
30-
self.inner.set_param(id, value);
12+
Host { _inner: inner }
3113
}
3214
}

src/plugin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::{self, Read, Write};
22

33
use crate::bus::{BusInfo, Layout};
4-
use crate::editor::{Editor, ParentWindow};
4+
use crate::editor::{Editor, EditorHost, ParentWindow};
55
use crate::host::Host;
66
use crate::params::{ParamId, ParamInfo, ParamValue};
77
use crate::process::{Config, Processor};
@@ -46,7 +46,7 @@ pub trait Plugin: Send + Sized + 'static {
4646
fn save(&self, output: &mut impl Write) -> io::Result<()>;
4747
fn load(&mut self, input: &mut impl Read) -> io::Result<()>;
4848
fn processor(&mut self, config: Config) -> Self::Processor;
49-
fn editor(&mut self, parent: &ParentWindow) -> Self::Editor;
49+
fn editor(&mut self, host: EditorHost, parent: &ParentWindow) -> Self::Editor;
5050

5151
#[allow(unused_variables)]
5252
fn latency(&self, config: &Config) -> u64 {

0 commit comments

Comments
 (0)