mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
trait Run
This commit is contained in:
parent
cc2b59d772
commit
3ae2467acc
11 changed files with 212 additions and 141 deletions
|
|
@ -25,65 +25,3 @@ pub enum AppEvent {
|
|||
/// JACK notification
|
||||
Jack(JackEvent)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum JackEvent {
|
||||
ThreadInit,
|
||||
Shutdown(ClientStatus, String),
|
||||
Freewheel(bool),
|
||||
SampleRate(Frames),
|
||||
ClientRegistration(String, bool),
|
||||
PortRegistration(PortId, bool),
|
||||
PortRename(PortId, String, String),
|
||||
PortsConnected(PortId, PortId, bool),
|
||||
GraphReorder,
|
||||
XRun,
|
||||
}
|
||||
|
||||
pub struct Notifications<T: Fn(AppEvent) + Send>(pub T);
|
||||
|
||||
impl<T: Fn(AppEvent) + Send> NotificationHandler for Notifications<T> {
|
||||
fn thread_init (&self, _: &Client) {
|
||||
self.0(AppEvent::Jack(JackEvent::ThreadInit));
|
||||
}
|
||||
|
||||
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
|
||||
self.0(AppEvent::Jack(JackEvent::Shutdown(status, reason.into())));
|
||||
}
|
||||
|
||||
fn freewheel (&mut self, _: &Client, enabled: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::Freewheel(enabled)));
|
||||
}
|
||||
|
||||
fn sample_rate (&mut self, _: &Client, frames: Frames) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::SampleRate(frames)));
|
||||
Control::Quit
|
||||
}
|
||||
|
||||
fn client_registration (&mut self, _: &Client, name: &str, reg: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::ClientRegistration(name.into(), reg)));
|
||||
}
|
||||
|
||||
fn port_registration (&mut self, _: &Client, id: PortId, reg: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::PortRegistration(id, reg)));
|
||||
}
|
||||
|
||||
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::PortRename(id, old.into(), new.into())));
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn ports_connected (&mut self, _: &Client, a: PortId, b: PortId, are: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::PortsConnected(a, b, are)));
|
||||
}
|
||||
|
||||
fn graph_reorder (&mut self, _: &Client) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::GraphReorder));
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn xrun (&mut self, _: &Client) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::XRun));
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,3 +33,65 @@ pub use ::jack::{
|
|||
TransportState,
|
||||
TransportStatePosition
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum JackEvent {
|
||||
ThreadInit,
|
||||
Shutdown(ClientStatus, String),
|
||||
Freewheel(bool),
|
||||
SampleRate(Frames),
|
||||
ClientRegistration(String, bool),
|
||||
PortRegistration(PortId, bool),
|
||||
PortRename(PortId, String, String),
|
||||
PortsConnected(PortId, PortId, bool),
|
||||
GraphReorder,
|
||||
XRun,
|
||||
}
|
||||
|
||||
pub struct Notifications<T: Fn(AppEvent) + Send>(pub T);
|
||||
|
||||
impl<T: Fn(AppEvent) + Send> NotificationHandler for Notifications<T> {
|
||||
fn thread_init (&self, _: &Client) {
|
||||
self.0(AppEvent::Jack(JackEvent::ThreadInit));
|
||||
}
|
||||
|
||||
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
|
||||
self.0(AppEvent::Jack(JackEvent::Shutdown(status, reason.into())));
|
||||
}
|
||||
|
||||
fn freewheel (&mut self, _: &Client, enabled: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::Freewheel(enabled)));
|
||||
}
|
||||
|
||||
fn sample_rate (&mut self, _: &Client, frames: Frames) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::SampleRate(frames)));
|
||||
Control::Quit
|
||||
}
|
||||
|
||||
fn client_registration (&mut self, _: &Client, name: &str, reg: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::ClientRegistration(name.into(), reg)));
|
||||
}
|
||||
|
||||
fn port_registration (&mut self, _: &Client, id: PortId, reg: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::PortRegistration(id, reg)));
|
||||
}
|
||||
|
||||
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::PortRename(id, old.into(), new.into())));
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn ports_connected (&mut self, _: &Client, a: PortId, b: PortId, are: bool) {
|
||||
self.0(AppEvent::Jack(JackEvent::PortsConnected(a, b, are)));
|
||||
}
|
||||
|
||||
fn graph_reorder (&mut self, _: &Client) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::GraphReorder));
|
||||
Control::Continue
|
||||
}
|
||||
|
||||
fn xrun (&mut self, _: &Client) -> Control {
|
||||
self.0(AppEvent::Jack(JackEvent::XRun));
|
||||
Control::Continue
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ macro_rules! submod {
|
|||
($($name:ident)*) => { $(mod $name; pub use self::$name::*;)* };
|
||||
}
|
||||
|
||||
submod!( device handle jack keymap note port process render time );
|
||||
submod!( device handle jack keymap port render run time );
|
||||
|
||||
pub use std::error::Error;
|
||||
pub use std::io::{stdout, Stdout, Write};
|
||||
|
|
@ -31,73 +31,3 @@ pub use ::crossterm::{
|
|||
pub use ::ratatui::prelude::*;
|
||||
pub use ::midly::{MidiMessage, live::LiveEvent, num::u7};
|
||||
pub use crate::{key, keymap};
|
||||
|
||||
/// Run a device as the root of the app.
|
||||
pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn Error>> {
|
||||
let device = Arc::new(Mutex::new(device));
|
||||
let exited = Arc::new(AtomicBool::new(false));
|
||||
|
||||
// Spawn input (handle) thread
|
||||
let _input_thread = {
|
||||
let poll = std::time::Duration::from_millis(100);
|
||||
let exited = exited.clone();
|
||||
let device = device.clone();
|
||||
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 ::crossterm::event::poll(poll).is_ok() {
|
||||
let event = ::crossterm::event::read().unwrap();
|
||||
match event {
|
||||
Event::Key(KeyEvent {
|
||||
code: KeyCode::Char('c'),
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
..
|
||||
}) => {
|
||||
exited.store(true, Ordering::Relaxed);
|
||||
},
|
||||
_ => if device.lock().unwrap().handle(&AppEvent::Input(event)).is_err() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
// Set up terminal
|
||||
stdout().execute(EnterAlternateScreen)?;
|
||||
enable_raw_mode()?;
|
||||
let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
|
||||
// Set up panic hook
|
||||
let better_panic_handler = better_panic::Settings::auto()
|
||||
.verbosity(better_panic::Verbosity::Full)
|
||||
.create_panic_handler();
|
||||
std::panic::set_hook(Box::new(move |info: &std::panic::PanicInfo|{
|
||||
stdout().execute(LeaveAlternateScreen).unwrap();
|
||||
crossterm::terminal::disable_raw_mode().unwrap();
|
||||
better_panic_handler(info);
|
||||
}));
|
||||
|
||||
// Main (render) loop
|
||||
let sleep = std::time::Duration::from_millis(16);
|
||||
loop {
|
||||
terminal.draw(|frame|{
|
||||
let area = frame.size();
|
||||
let buffer = frame.buffer_mut();
|
||||
device.lock().unwrap().render(buffer, area).expect("Failed to render content.");
|
||||
}).expect("Failed to render frame");
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
}
|
||||
std::thread::sleep(sleep);
|
||||
};
|
||||
|
||||
// Cleanup
|
||||
stdout().execute(LeaveAlternateScreen)?;
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::core::*;
|
||||
use ratatui::widgets::WidgetRef;
|
||||
|
||||
/// Trait for things that render to the display.
|
||||
pub trait Render {
|
||||
|
|
@ -28,13 +29,13 @@ impl Render for Box<dyn Device> {
|
|||
}
|
||||
}
|
||||
|
||||
impl ratatui::widgets::WidgetRef for &dyn Render {
|
||||
impl WidgetRef for &dyn Render {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Render::render(*self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
impl ratatui::widgets::WidgetRef for dyn Render {
|
||||
impl WidgetRef for dyn Render {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Render::render(self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
|
|
|
|||
97
src/core/run.rs
Normal file
97
src/core/run.rs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
use crate::core::*;
|
||||
|
||||
pub trait Run: Render + Handle + Send + Sized + 'static {
|
||||
fn run (self) -> Usually<()> {
|
||||
let device = Arc::new(Mutex::new(self));
|
||||
let exited = Arc::new(AtomicBool::new(false));
|
||||
let _input_thread = input_thread(&exited, &device);
|
||||
terminal_setup()?;
|
||||
panic_hook_setup();
|
||||
main_thread(&exited, &device)?;
|
||||
terminal_teardown()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Render + Handle + Send + Sized + 'static> Run for T {}
|
||||
|
||||
/// Spawn thread that listens for user input
|
||||
pub fn input_thread (
|
||||
exited: &Arc<AtomicBool>,
|
||||
device: &Arc<Mutex<impl Handle + Send + 'static>>
|
||||
) -> JoinHandle<()> {
|
||||
let poll = std::time::Duration::from_millis(100);
|
||||
let exited = exited.clone();
|
||||
let device = device.clone();
|
||||
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 ::crossterm::event::poll(poll).is_ok() {
|
||||
let event = ::crossterm::event::read().unwrap();
|
||||
match event {
|
||||
Event::Key(KeyEvent {
|
||||
code: KeyCode::Char('c'),
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
..
|
||||
}) => {
|
||||
exited.store(true, Ordering::Relaxed);
|
||||
},
|
||||
_ => if device.lock().unwrap().handle(&AppEvent::Input(event)).is_err() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Set up terminal
|
||||
pub fn terminal_setup () -> Usually<()> {
|
||||
stdout().execute(EnterAlternateScreen)?;
|
||||
enable_raw_mode()?;
|
||||
Ok(())
|
||||
}
|
||||
/// Set up panic hook
|
||||
pub fn panic_hook_setup () {
|
||||
let better_panic_handler = better_panic::Settings::auto()
|
||||
.verbosity(better_panic::Verbosity::Full)
|
||||
.create_panic_handler();
|
||||
std::panic::set_hook(Box::new(move |info: &std::panic::PanicInfo|{
|
||||
stdout().execute(LeaveAlternateScreen).unwrap();
|
||||
crossterm::terminal::disable_raw_mode().unwrap();
|
||||
better_panic_handler(info);
|
||||
}));
|
||||
}
|
||||
/// Main thread render loop
|
||||
pub fn main_thread (
|
||||
exited: &Arc<AtomicBool>,
|
||||
device: &Arc<Mutex<impl Render>>
|
||||
) -> Usually<()> {
|
||||
let exited = exited.clone();
|
||||
let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
let sleep = std::time::Duration::from_millis(16);
|
||||
loop {
|
||||
|
||||
terminal.draw(|frame|{
|
||||
let area = frame.size();
|
||||
let buffer = frame.buffer_mut();
|
||||
device.lock().unwrap().render(buffer, area).expect("Failed to render content.");
|
||||
}).expect("Failed to render frame");
|
||||
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
}
|
||||
|
||||
std::thread::sleep(sleep);
|
||||
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Cleanup
|
||||
pub fn terminal_teardown () -> Usually<()> {
|
||||
stdout().execute(LeaveAlternateScreen)?;
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
use crate::core::*;
|
||||
use super::*;
|
||||
pub fn handle (state: &mut Launcher, event: &AppEvent) -> Usually<bool> {
|
||||
//if let Some(ref modal) = state.modal {
|
||||
//if modal.handle_with_state(state, event)? {
|
||||
//return Ok(true)
|
||||
//}
|
||||
//}
|
||||
Ok(handle_keymap(state, event, KEYMAP)? || match state.view {
|
||||
LauncherView::Modal(ref mut device) => device.handle(event)?,
|
||||
LauncherView::Tracks => handle_keymap(state, event, KEYMAP_TRACKS)?,
|
||||
LauncherView::Sequencer => {
|
||||
let i = state.col().saturating_sub(1);
|
||||
|
|
|
|||
|
|
@ -18,8 +18,13 @@ pub struct Launcher {
|
|||
scenes: Vec<Scene>,
|
||||
show_help: bool,
|
||||
view: LauncherView,
|
||||
modal: Option<Box<dyn Modal<Self>>>,
|
||||
}
|
||||
pub enum LauncherView {
|
||||
Tracks,
|
||||
Sequencer,
|
||||
Chains
|
||||
}
|
||||
pub enum LauncherView { Tracks, Sequencer, Chains, Modal(Box<dyn Device>) }
|
||||
impl LauncherView {
|
||||
fn is_tracks (&self) -> bool {
|
||||
match self { Self::Tracks => true, _ => false }
|
||||
|
|
@ -70,6 +75,7 @@ impl Launcher {
|
|||
]))?,
|
||||
] },
|
||||
show_help: true,
|
||||
modal: None
|
||||
}).activate(client)
|
||||
}
|
||||
fn cols (&self) -> usize {
|
||||
|
|
@ -209,6 +215,13 @@ pub fn render (state: &Launcher, buf: &mut Buffer, mut area: Rect) -> Usually<Re
|
|||
let hide = "[Tab] Mode [Arrows] Move [.,] Value [F1] Toggle help ";
|
||||
hide.blit(buf, x + (width - hide.len() as u16) / 2, height - 1, style);
|
||||
}
|
||||
|
||||
if let Some(ref modal) = state.modal {
|
||||
for cell in buf.content.iter_mut() {
|
||||
cell.fg = ::ratatui::style::Color::Gray;
|
||||
cell.modifier = ::ratatui::style::Modifier::DIM;
|
||||
}
|
||||
}
|
||||
Ok(area)
|
||||
}
|
||||
fn draw_section_sequencer (state: &Launcher, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@ mod lozenge; pub use self::lozenge::*;
|
|||
|
||||
use crate::core::*;
|
||||
|
||||
pub trait Modal<T>: Device {
|
||||
fn handle_with_state (&self, state: &mut T, event: &AppEvent) -> Usually<bool>;
|
||||
}
|
||||
|
||||
pub trait MaxHeight: Device {
|
||||
fn max_height (&self) -> u16 {
|
||||
u16::MAX
|
||||
|
|
|
|||
30
src/main.rs
30
src/main.rs
|
|
@ -16,6 +16,27 @@ pub mod layout;
|
|||
use crate::core::*;
|
||||
use crate::device::*;
|
||||
|
||||
mod new {
|
||||
use crate::core::*;
|
||||
type Phrase = (String, usize, BTreeMap<usize, Vec<Vec<u8>>>);
|
||||
type Scene = (String, Option<usize>, Vec<Option<AtomicUsize>>);
|
||||
type Track = (String, usize);
|
||||
#[derive(Default)]
|
||||
struct App {
|
||||
phrases: BTreeMap<usize, Arc<Mutex<Phrase>>>,
|
||||
scenes: BTreeMap<usize, Arc<Mutex<Scene>>>,
|
||||
tracks: BTreeMap<usize, Arc<Mutex<Track>>>,
|
||||
}
|
||||
fn main () {
|
||||
App::default().run()
|
||||
}
|
||||
impl App {
|
||||
fn run (self) {
|
||||
panic_hook_setup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! sample {
|
||||
($note:expr, $name:expr, $src:expr) => {
|
||||
{
|
||||
|
|
@ -35,7 +56,7 @@ macro_rules! sample {
|
|||
};
|
||||
}
|
||||
|
||||
fn main () -> Result<(), Box<dyn Error>> {
|
||||
fn main () -> Usually<()> {
|
||||
let _cli = cli::Cli::parse();
|
||||
let xdg = microxdg::XdgApp::new("tek")?;
|
||||
crate::config::create_dirs(&xdg)?;
|
||||
|
|
@ -58,7 +79,7 @@ fn main () -> Result<(), Box<dyn Error>> {
|
|||
phrase
|
||||
}}
|
||||
}
|
||||
let app = Launcher::new("Launcher#0", &timebase,
|
||||
Launcher::new("Launcher#0", &timebase,
|
||||
Some(vec![
|
||||
|
||||
Track::new("Drums", &timebase, Some(vec![
|
||||
|
|
@ -191,6 +212,7 @@ fn main () -> Result<(), Box<dyn Error>> {
|
|||
//Scene::new(&"Scene#05", &[None, None, None, None]),
|
||||
])
|
||||
|
||||
)?.connect(input, &output)?;
|
||||
run(app)
|
||||
)?
|
||||
.connect(input, &output)?
|
||||
.run()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue