remove trait JackActivate

This commit is contained in:
🪞👃🪞 2024-12-29 18:39:05 +01:00
parent 02878dd954
commit b96fa34702
2 changed files with 31 additions and 41 deletions

View file

@ -9,10 +9,6 @@ pub use ::jack::{
Transport, TransportState, MidiIter, MidiWriter, RawMidi,
};
pub mod activate;
pub(crate) use self::activate::*;
pub use self::activate::JackActivate;
pub mod jack_event;
pub(crate) use self::jack_event::*;
@ -98,7 +94,7 @@ impl JackConnection {
///
/// Needs work. Strange ownership situation between the callback
/// and the host object.
pub fn activate (
fn activate (
self,
mut cb: impl FnMut(&Arc<RwLock<Self>>, &Client, &ProcessScope) -> Control + Send + 'static,
) -> Usually<Arc<RwLock<Self>>>
@ -114,6 +110,36 @@ impl JackConnection {
*state.write().unwrap() = Self::Active(client.activate_async(events, frames)?);
Ok(state)
}
/// Consume a `JackConnection::Inactive`, activate a client,
/// initialize an app around it with the `init` callback,
/// then return the result of it all.
pub fn activate_with <T: Audio + 'static> (
self,
init: impl FnOnce(&Arc<RwLock<JackConnection>>)->Usually<T>
)
-> Usually<Arc<RwLock<T>>>
{
let client = Arc::new(RwLock::new(self));
let target = Arc::new(RwLock::new(init(&client)?));
let event = Box::new(move|_|{/*TODO*/}) as Box<dyn Fn(JackEvent) + Send + Sync>;
let events = Notifications(event);
let frame = Box::new({
let target = target.clone();
move|c: &_, s: &_|if let Ok(mut target) = target.write() {
target.process(c, s)
} else {
Control::Quit
}
});
let frames = ClosureProcessHandler::new(frame as BoxedAudioHandler);
let mut buffer = Self::Activating;
std::mem::swap(&mut*client.write().unwrap(), &mut buffer);
*client.write().unwrap() = Self::Active(Client::from(buffer).activate_async(
events,
frames,
)?);
Ok(target)
}
pub fn port_by_name (&self, name: &str) -> Option<Port<Unowned>> {
self.client().port_by_name(name)
}

View file

@ -1,36 +0,0 @@
use crate::*;
pub trait JackActivate: Sized {
fn activate_with <T: Audio + 'static> (
self,
init: impl FnOnce(&Arc<RwLock<JackConnection>>)->Usually<T>
)
-> Usually<Arc<RwLock<T>>>;
}
impl JackActivate for JackConnection {
fn activate_with <T: Audio + 'static> (
self,
init: impl FnOnce(&Arc<RwLock<JackConnection>>)->Usually<T>
)
-> Usually<Arc<RwLock<T>>>
{
let client = Arc::new(RwLock::new(self));
let target = Arc::new(RwLock::new(init(&client)?));
let event = Box::new(move|_|{/*TODO*/}) as Box<dyn Fn(JackEvent) + Send + Sync>;
let events = Notifications(event);
let frame = Box::new({
let target = target.clone();
move|c: &_, s: &_|if let Ok(mut target) = target.write() {
target.process(c, s)
} else {
Control::Quit
}
});
let frames = ClosureProcessHandler::new(frame as BoxedAudioHandler);
let mut buffer = Self::Activating;
std::mem::swap(&mut*client.write().unwrap(), &mut buffer);
*client.write().unwrap() = Self::Active(Client::from(buffer).activate_async(events, frames)?);
Ok(target)
}
}