diff --git a/src/clients/midi.rs b/src/clients/midi.rs index 01a6d0e..918307f 100644 --- a/src/clients/midi.rs +++ b/src/clients/midi.rs @@ -1,11 +1,8 @@ use crate::{config::NanoKeys, interaction_server::InteractionServer}; use crossbeam_channel::Sender; use midir::MidiInput; -use shaku::{module, HasComponent, Interface}; -use std::marker::PhantomData; -use std::sync::Arc; -use super::midi_client::MidiPort; +use super::midi_client::{MidiClient, MidiPort, MockMidiClient, MockMidiPort}; use super::midi_service::MidiService; use super::midi_service_impl::{MidiServiceImpl, MidiServiceImplParameters}; use super::midir_client_impl::{MidirClientImpl, MidirPortImpl}; @@ -13,51 +10,32 @@ use super::midir_client_impl::{MidirClientImpl, MidirPortImpl}; const CLIENT_NAME: &str = "PicoKontroller"; const PORT_NAME: &str = "PicoController Input"; -pub struct MidiClient { - pub device_name: String, - pub out_channel: Sender, -} +//pub struct MidiClient { +// pub device_name: String, +// pub out_channel: Sender, +//} pub type KeyEvent = (NanoKeys, KeyState); pub type KeyState = u8; -module! { - MyModule { - components = [MidiServiceImpl, MidirClientImpl], - providers = [], - } -} -impl MidiClient { - pub fn new(device_name: String, out_channel: Sender) -> MidiClient { - MidiClient { - device_name, - out_channel, - } - } - - pub fn start(self) -> () { - let module: MyModule = MyModule::builder() - .with_component_parameters::>( - MidiServiceImplParameters:: { - device_name: "nanoKONTROL2".to_string(), - out_channel: self.out_channel, - phantom: PhantomData, - }, - ) - .build(); - - let service: Arc> = module.resolve(); - let handle = service.start(); - let _ = handle.join(); - } -} - -pub fn list_ports() -> () { - let midi_in = MidiInput::new(CLIENT_NAME).expect("Creating MIDI device failed"); - let in_ports = midi_in.ports(); - println!("All available MIDI devices:"); - for (_, p) in in_ports.iter().enumerate() { - println!("{}", midi_in.port_name(p).unwrap()); - } -} +//impl MidiClient { +// pub fn new(device_name: String, out_channel: Sender) -> MidiClient { +// MidiClient { +// device_name, +// out_channel, +// } +// } +// +// pub fn start(self) -> () { +// } +//} +// +//pub fn list_ports() -> () { +// let midi_in = MidiInput::new(CLIENT_NAME).expect("Creating MIDI device failed"); +// let in_ports = midi_in.ports(); +// println!("All available MIDI devices:"); +// for (_, p) in in_ports.iter().enumerate() { +// println!("{}", midi_in.port_name(p).unwrap()); +// } +//} diff --git a/src/clients/midi_client.rs b/src/clients/midi_client.rs index 6205651..2617f80 100644 --- a/src/clients/midi_client.rs +++ b/src/clients/midi_client.rs @@ -1,14 +1,16 @@ +use std::any::Any; + use shaku::{self, Interface}; pub type MidiConnectionCallback = Box; -#[cfg_attr(test, mockall::automock)] +#[mockall::automock] pub trait MidiClient: Interface { fn ports(&self) -> Vec; fn connect(&self, port: &MP, port_name: &str, callback: MidiConnectionCallback) -> (); } -#[cfg_attr(test, mockall::automock)] -pub trait MidiPort: Interface { +#[mockall::automock] +pub trait MidiPort: Interface + Send{ fn name(&self) -> String; } diff --git a/src/clients/midir_client_impl.rs b/src/clients/midir_client_impl.rs index 53f2a63..497036e 100644 --- a/src/clients/midir_client_impl.rs +++ b/src/clients/midir_client_impl.rs @@ -2,7 +2,7 @@ use midir::{Ignore, MidiInput, MidiInputPort}; use shaku::{self, Component}; use super::midi_client::{MidiClient, MidiConnectionCallback, MidiPort}; -use std::time; +use std::{any::Any, time}; fn create_input(client_name: &str) -> MidiInput { let mut input = MidiInput::new(client_name).expect("Creating MIDI device failed"); @@ -56,6 +56,9 @@ impl MidiPort for MidirPortImpl { let midi_in = create_input(&self.client_name); midi_in.port_name(&self.port).unwrap() } + fn as_any(&self) -> &dyn Any { + self + } } impl MidirPortImpl { diff --git a/src/main.rs b/src/main.rs index 158eb3e..3fa8183 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,11 +10,18 @@ mod controllers { mod clients; use clap::Parser; -use clients::midi::MidiClient; -use crossbeam_channel::unbounded; +use clients::midi::KeyEvent; +use clients::midi_client::{MidiClient, MidiPort, MockMidiClient, MockMidiPort}; +use clients::midi_service::MidiService; +use clients::midi_service_impl::{MidiServiceImpl, MidiServiceImplParameters}; +use clients::midir_client_impl::{MidirClientImpl, MidirPortImpl}; +use crossbeam_channel::{unbounded, Sender}; use directories::ProjectDirs; use std::{path::PathBuf, process::exit, sync::Arc}; +use shaku::{module, HasComponent, Interface, ModuleBuilder}; +use std::marker::PhantomData; + #[derive(Parser)] #[command(version, about, long_about = None)] struct Cli { @@ -24,12 +31,32 @@ struct Cli { list_mpris: bool, } +module! { + MyModule { + components = [MidiServiceImpl, MidirClientImpl], + providers = [], + } +} + +fn setup_module( + device_name: String, + out_channel: Sender, +) -> ModuleBuilder> { + MyModule::builder().with_component_parameters::>( + MidiServiceImplParameters:: { + device_name, + out_channel, + phantom: PhantomData, + }, + ) +} + fn main() { env_logger::init(); let cli = Cli::parse(); if cli.list_midi { - clients::midi::list_ports(); + //clients::midi::list_ports(); exit(0); } else if cli.list_mpris { controllers::mpris::list_players(); @@ -42,10 +69,25 @@ fn main() { let arc_cfg = Arc::new(cfg); let (sender, receiver) = unbounded::(); - let midi = MidiClient::new(arc_cfg.general.midi_device.clone(), sender); + + let mut client_mock = MockMidiClient::::new(); + let mut port_mock = MockMidiPort::new(); + port_mock + .expect_name() + .times(1) + .returning(|| "nanoKONTROL2:nanoKONTROL2 nanoKONTROL2 _ CTR 28:0".to_string()); + client_mock + .expect_ports() + .times(1) + .returning(|| vec![port_mock]); + + let module: MyModule = MyModule::builder() + .with_component_override::>(Box::new(client_mock)) + .build(); + + let service: Arc> = module.resolve(); + let handle = service.start(); + let _ = handle.join(); router::run(receiver, Arc::clone(&arc_cfg)); - - midi.start(); - println!("fooo"); }