Add support for default player control

Leaving out the ids=["...", "..."] field in config.toml applies the action on the currently active player
This commit is contained in:
GHOSCHT 2024-03-14 10:50:56 +01:00
parent fadd962e8a
commit 2f325db170
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
3 changed files with 66 additions and 43 deletions

View file

@ -20,7 +20,7 @@ fn serialize_default() -> Result<String, Error> {
NanoKeys::Param4Mute, NanoKeys::Param4Mute,
vec![ vec![
KeyMapVariant::Mpris { KeyMapVariant::Mpris {
ids: vec!["Feishin".to_string()], ids: Option::Some(vec!["Feishin".to_string()]),
action: MprisAction::PlayPause, action: MprisAction::PlayPause,
}, },
KeyMapVariant::PipeWire { KeyMapVariant::PipeWire {
@ -34,6 +34,7 @@ fn serialize_default() -> Result<String, Error> {
NanoKeys::Play, NanoKeys::Play,
vec![KeyMapVariant::Exec { vec![KeyMapVariant::Exec {
command: "echo hello".to_string(), command: "echo hello".to_string(),
args: Option::None,
}], }],
); );
mpris.insert("music".to_string(), "Feishin".to_string()); mpris.insert("music".to_string(), "Feishin".to_string());
@ -93,7 +94,7 @@ pub enum MprisAction {
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum KeyMapVariant { pub enum KeyMapVariant {
Mpris { Mpris {
ids: Vec<String>, ids: Option<Vec<String>>,
action: MprisAction, action: MprisAction,
}, },
PipeWire { PipeWire {
@ -104,6 +105,7 @@ pub enum KeyMapVariant {
}, },
Exec { Exec {
command: String, command: String,
args: Option<Vec<String>>,
}, },
} }

View file

@ -27,6 +27,19 @@ fn exec(in_channel: Receiver<KeyEvent>, config: Arc<Config>) {
} }
} }
fn exec_binary_functionality<F>(player_ids: &Option<Vec<String>>, 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<Config>) { fn eval(key: NanoKeys, state: u8, config: &Arc<Config>) {
match config.keymap.get(&key) { match config.keymap.get(&key) {
Some(actions) => { Some(actions) => {
@ -38,50 +51,48 @@ fn eval(key: NanoKeys, state: u8, config: &Arc<Config>) {
match action { match action {
KeyMapVariant::Mpris { ids, action } => match action { KeyMapVariant::Mpris { ids, action } => match action {
MprisAction::Play => { MprisAction::Play => {
if is_logical_true(state) { exec_binary_functionality(ids, state, |player_id: Option<&str>| {
continue; mpris_client::play(player_id)
} })
ids.iter().for_each(|id| mpris_client::play(id));
} }
MprisAction::Pause => { MprisAction::Pause => {
if is_logical_true(state) { exec_binary_functionality(ids, state, |player_id: Option<&str>| {
continue; mpris_client::pause(player_id)
} })
ids.iter().for_each(|id| mpris_client::pause(id));
} }
MprisAction::PlayPause => { MprisAction::PlayPause => {
if is_logical_true(state) { exec_binary_functionality(ids, state, |player_id: Option<&str>| {
continue; mpris_client::play_pause(player_id)
} })
ids.iter().for_each(|id| mpris_client::play_pause(id));
} }
MprisAction::Stop => { MprisAction::Stop => {
if is_logical_true(state) { exec_binary_functionality(ids, state, |player_id: Option<&str>| {
continue; mpris_client::stop(player_id)
} })
ids.iter().for_each(|id| mpris_client::stop(id));
} }
MprisAction::Next => { MprisAction::Next => {
if is_logical_true(state) { exec_binary_functionality(ids, state, |player_id: Option<&str>| {
continue; mpris_client::next(player_id)
} })
ids.iter().for_each(|id| mpris_client::next(id));
} }
MprisAction::Previous => { MprisAction::Previous => {
if is_logical_true(state) { exec_binary_functionality(ids, state, |player_id: Option<&str>| {
continue; mpris_client::previous(player_id)
} })
ids.iter().for_each(|id| mpris_client::previous(id));
} }
MprisAction::Volume => { MprisAction::Volume => {
let volume = state as f64 / 100.0; let volume = state as f64 / 100.0;
ids.iter() match ids {
.for_each(|id| mpris_client::set_volume(id, volume)); 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::PipeWire { ids } => {}
KeyMapVariant::Key { keycode } => {} KeyMapVariant::Key { keycode } => {}
KeyMapVariant::Exec { command } => {} KeyMapVariant::Exec { command, args } => {}
} }
} }
} }

View file

@ -42,44 +42,54 @@ where
function(&player); function(&player);
} }
pub fn play(player_id: &str) -> () { fn exec_functionality<F>(player_id: Option<&str>, exec_closure: F)
for_all_players(player_id, |player| { 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(); let _ = player.play();
}); });
} }
pub fn pause(player_id: &str) -> () { pub fn pause(player_id: Option<&str>) -> () {
for_all_players(player_id, |player| { exec_functionality(player_id, |player: &Player| {
let _ = player.pause(); let _ = player.pause();
}); });
} }
pub fn play_pause(player_id: &str) -> () { pub fn play_pause(player_id: Option<&str>) -> () {
for_all_players(player_id, |player| { exec_functionality(player_id, |player: &Player| {
let _ = player.play_pause(); let _ = player.play_pause();
}); });
} }
pub fn stop(player_id: &str) -> () { pub fn stop(player_id: Option<&str>) -> () {
for_all_players(player_id, |player| { exec_functionality(player_id, |player: &Player| {
let _ = player.stop(); let _ = player.stop();
}); });
} }
pub fn next(player_id: &str) -> () { pub fn next(player_id: Option<&str>) -> () {
for_all_players(player_id, |player| { exec_functionality(player_id, |player: &Player| {
let _ = player.next(); let _ = player.next();
}); });
} }
pub fn previous(player_id: &str) -> () { pub fn previous(player_id: Option<&str>) -> () {
for_all_players(player_id, |player| { exec_functionality(player_id, |player: &Player| {
let _ = player.previous(); let _ = player.previous();
}); });
} }
pub fn set_volume(player_id: &str, volume: f64) -> () { pub fn set_volume(player_id: Option<&str>, volume: f64) -> () {
for_all_players(player_id, |player| { exec_functionality(player_id, |player: &Player| {
let _ = player.set_volume(volume); let _ = player.set_volume(volume);
}); });
} }