diff --git a/src/config.rs b/src/config.rs index d1ecdb6..4ead019 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,7 +20,7 @@ fn serialize_default() -> Result { NanoKeys::Param4Mute, vec![ KeyMapVariant::Mpris { - ids: vec!["Feishin".to_string()], + ids: Option::Some(vec!["Feishin".to_string()]), action: MprisAction::PlayPause, }, KeyMapVariant::PipeWire { @@ -34,6 +34,7 @@ fn serialize_default() -> Result { NanoKeys::Play, vec![KeyMapVariant::Exec { command: "echo hello".to_string(), + args: Option::None, }], ); mpris.insert("music".to_string(), "Feishin".to_string()); @@ -93,7 +94,7 @@ pub enum MprisAction { #[serde(tag = "type")] pub enum KeyMapVariant { Mpris { - ids: Vec, + ids: Option>, action: MprisAction, }, PipeWire { @@ -104,6 +105,7 @@ pub enum KeyMapVariant { }, Exec { command: String, + args: Option>, }, } diff --git a/src/controller.rs b/src/controller.rs index 30a7e2f..6399733 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -27,6 +27,19 @@ fn exec(in_channel: Receiver, config: Arc) { } } +fn exec_binary_functionality(player_ids: &Option>, state: u8, exec_closure: F) +where + F: Fn(Option<&str>) -> (), +{ + if is_logical_true(state) { + return; + } + match player_ids { + Some(ids) => ids.iter().for_each(|id| exec_closure(Option::Some(id))), + None => exec_closure(Option::None), + } +} + fn eval(key: NanoKeys, state: u8, config: &Arc) { match config.keymap.get(&key) { Some(actions) => { @@ -38,50 +51,48 @@ fn eval(key: NanoKeys, state: u8, config: &Arc) { match action { KeyMapVariant::Mpris { ids, action } => match action { MprisAction::Play => { - if is_logical_true(state) { - continue; - } - ids.iter().for_each(|id| mpris_client::play(id)); + exec_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::play(player_id) + }) } MprisAction::Pause => { - if is_logical_true(state) { - continue; - } - ids.iter().for_each(|id| mpris_client::pause(id)); + exec_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::pause(player_id) + }) } MprisAction::PlayPause => { - if is_logical_true(state) { - continue; - } - ids.iter().for_each(|id| mpris_client::play_pause(id)); + exec_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::play_pause(player_id) + }) } MprisAction::Stop => { - if is_logical_true(state) { - continue; - } - ids.iter().for_each(|id| mpris_client::stop(id)); + exec_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::stop(player_id) + }) } MprisAction::Next => { - if is_logical_true(state) { - continue; - } - ids.iter().for_each(|id| mpris_client::next(id)); + exec_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::next(player_id) + }) } MprisAction::Previous => { - if is_logical_true(state) { - continue; - } - ids.iter().for_each(|id| mpris_client::previous(id)); + exec_binary_functionality(ids, state, |player_id: Option<&str>| { + mpris_client::previous(player_id) + }) } MprisAction::Volume => { let volume = state as f64 / 100.0; - ids.iter() - .for_each(|id| mpris_client::set_volume(id, volume)); + match ids { + Some(some_ids) => some_ids.iter().for_each(|id| { + mpris_client::set_volume(Option::Some(id), volume) + }), + None => mpris_client::set_volume(Option::None, volume), + } } }, KeyMapVariant::PipeWire { ids } => {} KeyMapVariant::Key { keycode } => {} - KeyMapVariant::Exec { command } => {} + KeyMapVariant::Exec { command, args } => {} } } } diff --git a/src/mpris_client.rs b/src/mpris_client.rs index 47e1b02..0f122af 100644 --- a/src/mpris_client.rs +++ b/src/mpris_client.rs @@ -42,44 +42,54 @@ where function(&player); } -pub fn play(player_id: &str) -> () { - for_all_players(player_id, |player| { +fn exec_functionality(player_id: Option<&str>, exec_closure: F) +where + F: Fn(&Player) -> (), +{ + match player_id { + Some(id) => for_all_players(id, exec_closure), + None => for_active_player(exec_closure), + } +} + +pub fn play(player_id: Option<&str>) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.play(); }); } -pub fn pause(player_id: &str) -> () { - for_all_players(player_id, |player| { +pub fn pause(player_id: Option<&str>) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.pause(); }); } -pub fn play_pause(player_id: &str) -> () { - for_all_players(player_id, |player| { +pub fn play_pause(player_id: Option<&str>) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.play_pause(); }); } -pub fn stop(player_id: &str) -> () { - for_all_players(player_id, |player| { +pub fn stop(player_id: Option<&str>) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.stop(); }); } -pub fn next(player_id: &str) -> () { - for_all_players(player_id, |player| { +pub fn next(player_id: Option<&str>) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.next(); }); } -pub fn previous(player_id: &str) -> () { - for_all_players(player_id, |player| { +pub fn previous(player_id: Option<&str>) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.previous(); }); } -pub fn set_volume(player_id: &str, volume: f64) -> () { - for_all_players(player_id, |player| { +pub fn set_volume(player_id: Option<&str>, volume: f64) -> () { + exec_functionality(player_id, |player: &Player| { let _ = player.set_volume(volume); }); }