Skip to content

Commit

Permalink
introduce EditorContext type; move param edit methods from Host to Ed…
Browse files Browse the repository at this point in the history
…itorContext
  • Loading branch information
micahrj committed Apr 23, 2024
1 parent fc227f5 commit fb28134
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion examples/gain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -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<dyn EditorContextInner>,
}

impl EditorContext {
pub fn from_inner(inner: Rc<dyn EditorContextInner>) -> 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),
Expand Down
16 changes: 14 additions & 2 deletions src/format/clap/gui.rs
Original file line number Diff line number Diff line change
@@ -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<P: Plugin> Instance<P> {
pub(super) const GUI: clap_plugin_gui = clap_plugin_gui {
is_api_supported: Some(Self::gui_is_api_supported),
Expand Down Expand Up @@ -151,7 +161,9 @@ impl<P: Plugin> Instance<P> {
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
Expand Down
7 changes: 1 addition & 6 deletions src/format/clap/host.rs
Original file line number Diff line number Diff line change
@@ -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 {}
4 changes: 2 additions & 2 deletions src/format/clap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 1 addition & 6 deletions src/format/vst3/host.rs
Original file line number Diff line number Diff line change
@@ -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 {}
4 changes: 2 additions & 2 deletions src/format/vst3/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 14 additions & 2 deletions src/format/vst3/view.rs
Original file line number Diff line number Diff line change
@@ -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<P: Plugin> {
main_thread_state: Arc<UnsafeCell<MainThreadState<P>>>,
}
Expand Down Expand Up @@ -60,7 +70,9 @@ impl<P: Plugin> IPlugViewTrait for View<P> {

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
Expand Down
34 changes: 1 addition & 33 deletions src/host.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,30 +11,4 @@ impl Host {
pub fn from_inner(inner: Arc<dyn HostInner + Send + Sync>) -> 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<dyn HostInner + Send + Sync>,
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);
}
}
4 changes: 2 additions & 2 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fb28134

Please sign in to comment.