mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 20:56:43 +01:00
refactor: device abstraction, layout components
This commit is contained in:
parent
d330d31ce4
commit
788dc1ccde
21 changed files with 1144 additions and 1166 deletions
341
src/engine.rs
341
src/engine.rs
|
|
@ -1,341 +0,0 @@
|
|||
//pub mod jack;
|
||||
//pub mod tui;
|
||||
|
||||
use crate::prelude::*;
|
||||
use std::sync::mpsc;
|
||||
use crossterm::event;
|
||||
|
||||
pub trait Exitable {
|
||||
fn exit (&mut self);
|
||||
fn exited (&self) -> bool;
|
||||
}
|
||||
|
||||
pub trait HandleInput {
|
||||
fn handle (&mut self, event: &Event) -> Result<(), Box<dyn Error>>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Event {
|
||||
/// An input event that must be handled.
|
||||
Input(::crossterm::event::Event),
|
||||
/// Update values but not the whole form.
|
||||
Update,
|
||||
/// Update the whole form.
|
||||
Redraw,
|
||||
}
|
||||
|
||||
pub struct Engine {
|
||||
exited: Arc<AtomicBool>,
|
||||
sender: Sender<Event>,
|
||||
receiver: Receiver<Event>,
|
||||
pub jack_client: Jack<Notifications>,
|
||||
}
|
||||
|
||||
pub fn activate_jack_client <N: NotificationHandler + Sync + 'static> (
|
||||
client: Client,
|
||||
notifications: N,
|
||||
handler: BoxedProcessHandler
|
||||
) -> Result<Jack<N>, Box<dyn Error>> {
|
||||
Ok(client.activate_async(notifications, ClosureProcessHandler::new(handler))?)
|
||||
}
|
||||
|
||||
pub trait Component: Exitable + WidgetRef + HandleInput + Send + 'static {}
|
||||
|
||||
impl<T: Exitable + WidgetRef + HandleInput + Send + 'static> Component for T {}
|
||||
|
||||
impl Engine {
|
||||
|
||||
pub fn new (name: Option<&str>) -> Result<Self, Box<dyn Error>> {
|
||||
let (sender, receiver) = mpsc::channel::<self::Event>();
|
||||
let exited = Arc::new(AtomicBool::new(false));
|
||||
let (client, _status) = Client::new(name.unwrap_or("engine"), ClientOptions::NO_START_SERVER)?;
|
||||
Ok(Self {
|
||||
receiver,
|
||||
sender: sender.clone(),
|
||||
exited: exited.clone(),
|
||||
jack_client: activate_jack_client(
|
||||
client,
|
||||
Notifications(sender.clone()),
|
||||
Box::new(move |_client: &Client, _ps: &ProcessScope| -> Control {
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
Control::Quit
|
||||
} else {
|
||||
sender.send(Event::Update).unwrap();
|
||||
Control::Continue
|
||||
}
|
||||
})
|
||||
)?
|
||||
})
|
||||
}
|
||||
|
||||
pub fn run (
|
||||
&mut self,
|
||||
mut state: impl Component,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let state = Arc::new(Mutex::new(state));
|
||||
|
||||
let input_thread = {
|
||||
let state = state.clone();
|
||||
let sender = self.sender.clone();
|
||||
let exited = self.exited.clone();
|
||||
let poll = std::time::Duration::from_millis(100);
|
||||
spawn(move || loop {
|
||||
// Exit if flag is set
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
}
|
||||
// Listen for events and send them to the main thread
|
||||
if event::poll(poll).is_ok() {
|
||||
let event = event::read().unwrap();
|
||||
let mut state = state.lock().unwrap();
|
||||
match event {
|
||||
crossterm::event::Event::Key(crossterm::event::KeyEvent {
|
||||
code: KeyCode::Char('c'),
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
..
|
||||
}) => {
|
||||
state.exit()
|
||||
},
|
||||
_ => if state.handle(&crate::engine::Event::Input(event)).is_err() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
stdout().execute(EnterAlternateScreen)?;
|
||||
enable_raw_mode()?;
|
||||
let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
let sleep = std::time::Duration::from_millis(16);
|
||||
let exited = self.exited.clone();
|
||||
std::panic::set_hook(Box::new(panic_hook));
|
||||
loop {
|
||||
terminal.draw(|frame|{
|
||||
let area = frame.size();
|
||||
frame.render_widget(
|
||||
&*state.lock().unwrap(),
|
||||
area
|
||||
);
|
||||
}).expect("Failed to render frame");
|
||||
if state.lock().unwrap().exited() {
|
||||
exited.store(true, Ordering::Relaxed);
|
||||
break
|
||||
}
|
||||
std::thread::sleep(sleep);
|
||||
};
|
||||
|
||||
//render_thread.join().expect("Failed to join render thread");
|
||||
|
||||
stdout()
|
||||
.queue(crossterm::terminal::LeaveAlternateScreen)?
|
||||
.flush()?;
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn panic_hook (info: &std::panic::PanicInfo) {
|
||||
stdout()
|
||||
.execute(crossterm::terminal::LeaveAlternateScreen)
|
||||
.unwrap();
|
||||
crossterm::terminal::disable_raw_mode()
|
||||
.unwrap();
|
||||
writeln!(std::io::stderr(), "{}", info);
|
||||
writeln!(std::io::stderr(), "{:?}", ::backtrace::Backtrace::new());
|
||||
}
|
||||
|
||||
pub struct Notifications(mpsc::Sender<self::Event>);
|
||||
|
||||
impl NotificationHandler for Notifications {
|
||||
fn thread_init (&self, _: &Client) {
|
||||
}
|
||||
|
||||
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
|
||||
}
|
||||
|
||||
fn freewheel (&mut self, _: &Client, is_enabled: bool) {
|
||||
}
|
||||
|
||||
fn sample_rate (&mut self, _: &Client, _: Frames) -> Control {
|
||||
Control::Quit
|
||||
}
|
||||
|
||||
fn client_registration (&mut self, _: &Client, name: &str, is_reg: bool) {
|
||||
}
|
||||
|
||||
fn port_registration (&mut self, _: &Client, port_id: PortId, is_reg: bool) {
|
||||
}
|
||||
|
||||
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn ports_connected (&mut self, _: &Client, id_a: PortId, id_b: PortId, are: bool) {
|
||||
}
|
||||
|
||||
fn graph_reorder (&mut self, _: &Client) -> Control {
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn xrun (&mut self, _: &Client) -> Control {
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
||||
//pub struct Jack {
|
||||
//client: Option<AsyncClient<
|
||||
//Notifications,
|
||||
//ClosureProcessHandler<Box<dyn FnMut(&Client, &ProcessScope)->Control + Send>>
|
||||
//>>,
|
||||
//pub transport: Option<Transport>,
|
||||
//audio_ins: BTreeMap<String, Port<AudioIn>>,
|
||||
//audio_outs: BTreeMap<String, Port<AudioOut>>,
|
||||
//midi_ins: BTreeMap<String, Port<MidiIn>>,
|
||||
//midi_outs: BTreeMap<String, Port<MidiOut>>,
|
||||
//}
|
||||
|
||||
//impl Jack {
|
||||
//pub fn init_from_cli (options: &Cli)
|
||||
//-> Result<Arc<Mutex<Self>>, Box<dyn Error>>
|
||||
//{
|
||||
//let jack = Self::init(&options.jack_client_name)?;
|
||||
//{
|
||||
//let jack = jack.clone();
|
||||
//let mut jack = jack.lock().unwrap();
|
||||
//for port in options.jack_audio_ins.iter() {
|
||||
//jack.add_audio_in(port)?;
|
||||
//}
|
||||
//for port in options.jack_audio_outs.iter() {
|
||||
//jack.add_audio_out(port)?;
|
||||
//}
|
||||
//for port in options.jack_midi_ins.iter() {
|
||||
//jack.add_midi_in(port)?;
|
||||
//}
|
||||
//for port in options.jack_midi_outs.iter() {
|
||||
//jack.add_midi_out(port)?;
|
||||
//}
|
||||
//}
|
||||
//Ok(jack.clone())
|
||||
//}
|
||||
//fn init (name: &str)
|
||||
//-> Result<Arc<Mutex<Self>>, Box<dyn Error>>
|
||||
//{
|
||||
//let jack = Arc::new(Mutex::new(Self {
|
||||
//client: None,
|
||||
//transport: None,
|
||||
//audio_ins: BTreeMap::new(),
|
||||
//audio_outs: BTreeMap::new(),
|
||||
//midi_ins: BTreeMap::new(),
|
||||
//midi_outs: BTreeMap::new(),
|
||||
//}));
|
||||
//let (client, status) = Client::new(name, ClientOptions::NO_START_SERVER)?;
|
||||
//println!("Client status: {status:?}");
|
||||
//let jack1 = jack.clone();
|
||||
//let mut jack1 = jack1.lock().unwrap();
|
||||
//let jack2 = jack.clone();
|
||||
//jack1.transport = Some(client.transport());
|
||||
//jack1.client = Some(client.activate_async(
|
||||
//Notifications, ClosureProcessHandler::new(Box::new(
|
||||
//move |_client: &Client, _ps: &ProcessScope| -> Control {
|
||||
//let jack = jack2.lock().expect("Failed to lock jack mutex");
|
||||
//jack.read_inputs();
|
||||
//jack.write_outputs();
|
||||
//Control::Continue
|
||||
//}
|
||||
//) as Box<dyn FnMut(&Client, &ProcessScope)->Control + Send>)
|
||||
//)?);
|
||||
//Ok(jack)
|
||||
//}
|
||||
//fn start (&self) {
|
||||
//}
|
||||
//fn process (&self, _: &Client, ps: &ProcessScope) -> Control {
|
||||
//Control::Continue
|
||||
//}
|
||||
//fn add_audio_in (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
|
||||
//let port = self.client
|
||||
//.as_ref()
|
||||
//.expect("Not initialized.")
|
||||
//.as_client()
|
||||
//.register_port(name, AudioIn::default())?;
|
||||
//self.audio_ins.insert(name.into(), port);
|
||||
//Ok(self)
|
||||
//}
|
||||
//fn add_audio_out (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
|
||||
//let port = self.client
|
||||
//.as_ref()
|
||||
//.expect("Not initialized.")
|
||||
//.as_client()
|
||||
//.register_port(name, AudioOut::default())?;
|
||||
//self.audio_outs.insert(name.into(), port);
|
||||
//Ok(self)
|
||||
//}
|
||||
//fn add_midi_in (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
|
||||
//let port = self.client
|
||||
//.as_ref()
|
||||
//.expect("Not initialized.")
|
||||
//.as_client()
|
||||
//.register_port(name, MidiIn::default())?;
|
||||
//self.midi_ins.insert(name.into(), port);
|
||||
//Ok(self)
|
||||
//}
|
||||
//fn add_midi_out (&mut self, name: &str) -> Result<&mut Self, Box<dyn Error>> {
|
||||
//let port = self.client
|
||||
//.as_ref()
|
||||
//.expect("Not initialized.")
|
||||
//.as_client()
|
||||
//.register_port(name, MidiOut::default())?;
|
||||
//self.midi_outs.insert(name.into(), port);
|
||||
//Ok(self)
|
||||
//}
|
||||
//fn read_inputs (&self) {
|
||||
//// read input buffers
|
||||
////println!("read");
|
||||
//}
|
||||
//fn write_outputs (&self) {
|
||||
//// clear output buffers
|
||||
//// write output buffers
|
||||
////println!("write");
|
||||
//}
|
||||
//}
|
||||
|
||||
//struct Notifications;
|
||||
|
||||
//impl NotificationHandler for Notifications {
|
||||
//fn thread_init (&self, _: &Client) {
|
||||
//}
|
||||
|
||||
//fn shutdown (&mut self, status: ClientStatus, reason: &str) {
|
||||
//}
|
||||
|
||||
//fn freewheel (&mut self, _: &Client, is_enabled: bool) {
|
||||
//}
|
||||
|
||||
//fn sample_rate (&mut self, _: &Client, _: Frames) -> Control {
|
||||
//Control::Quit
|
||||
//}
|
||||
|
||||
//fn client_registration (&mut self, _: &Client, name: &str, is_reg: bool) {
|
||||
//}
|
||||
|
||||
//fn port_registration (&mut self, _: &Client, port_id: PortId, is_reg: bool) {
|
||||
//}
|
||||
|
||||
//fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
|
||||
//Control::Continue
|
||||
//}
|
||||
|
||||
//fn ports_connected (&mut self, _: &Client, id_a: PortId, id_b: PortId, are: bool) {
|
||||
//}
|
||||
|
||||
//fn graph_reorder (&mut self, _: &Client) -> Control {
|
||||
//Control::Continue
|
||||
//}
|
||||
|
||||
//fn xrun (&mut self, _: &Client) -> Control {
|
||||
//Control::Continue
|
||||
//}
|
||||
//}
|
||||
Loading…
Add table
Add a link
Reference in a new issue