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,
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<String, Error> {
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<String>,
ids: Option<Vec<String>>,
action: MprisAction,
},
PipeWire {
@ -104,6 +105,7 @@ pub enum KeyMapVariant {
},
Exec {
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>) {
match config.keymap.get(&key) {
Some(actions) => {
@ -38,50 +51,48 @@ fn eval(key: NanoKeys, state: u8, config: &Arc<Config>) {
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 } => {}
}
}
}

View file

@ -42,44 +42,54 @@ where
function(&player);
}
pub fn play(player_id: &str) -> () {
for_all_players(player_id, |player| {
fn exec_functionality<F>(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);
});
}