Use single program on events, and pass events in envars.

This commit is contained in:
Simon Persson 2018-02-20 22:45:14 +01:00
parent 081a282e12
commit 2eb4aa61d3
2 changed files with 28 additions and 40 deletions

View file

@ -95,9 +95,7 @@ fn setup(args: &[String]) -> Setup {
.reqopt("n", "name", "Device name", "NAME") .reqopt("n", "name", "Device name", "NAME")
.optopt("", "device-type", "Displayed device type", "DEVICE_TYPE") .optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
.optopt("b", "bitrate", "Bitrate (96, 160 or 320). Defaults to 160", "BITRATE") .optopt("b", "bitrate", "Bitrate (96, 160 or 320). Defaults to 160", "BITRATE")
.optopt("", "onstart", "Run PROGRAM when playback is about to begin.", "PROGRAM") .optopt("", "onevent", "Run PROGRAM when playback is about to begin.", "PROGRAM")
.optopt("", "onstop", "Run PROGRAM when playback has ended.", "PROGRAM")
.optopt("", "onchange", "Run PROGRAM between two tracks.", "PROGRAM")
.optflag("v", "verbose", "Enable verbose output") .optflag("v", "verbose", "Enable verbose output")
.optopt("u", "username", "Username to sign in with", "USERNAME") .optopt("u", "username", "Username to sign in with", "USERNAME")
.optopt("p", "password", "Password", "PASSWORD") .optopt("p", "password", "Password", "PASSWORD")
@ -189,9 +187,7 @@ fn setup(args: &[String]) -> Setup {
PlayerConfig { PlayerConfig {
bitrate: bitrate, bitrate: bitrate,
event_sender: run_program_on_events(matches.opt_str("onstart"), event_sender: matches.opt_str("onevent").map(run_program_on_events)
matches.opt_str("onstop"),
matches.opt_str("onchange"))
} }
}; };

View file

@ -1,50 +1,42 @@
use std::process::Command; use std::process::Command;
use std::sync::mpsc::{channel, Sender}; use std::sync::mpsc::{channel, Sender};
use std::thread; use std::thread;
use std::collections::HashMap;
use librespot::playback::player::PlayerEvent; use librespot::playback::player::PlayerEvent;
fn run_program(program: &str, args: Vec<String>) { fn run_program(program: &str, env_vars: HashMap<&str, String>) {
info!("Running {}", program);
let mut v: Vec<&str> = program.split_whitespace().collect(); let mut v: Vec<&str> = program.split_whitespace().collect();
info!("Running {:?}", v);
let status = Command::new(&v.remove(0)) let status = Command::new(&v.remove(0))
.args(&v) .args(&v)
.args(args) .envs(env_vars.iter())
.status() .status()
.expect("program failed to start"); .expect("program failed to start");
info!("Exit status: {}", status); info!("Exit status: {}", status);
} }
pub fn run_program_on_events(onstart: Option<String>, pub fn run_program_on_events(onevent: String) -> Sender<PlayerEvent> {
onstop: Option<String>,
onchange: Option<String>) -> Option<Sender<PlayerEvent>> {
if onstart.is_none() && onstop.is_none() && onchange.is_none() {
None
} else {
let (sender, receiver) = channel(); let (sender, receiver) = channel();
thread::spawn(move || { thread::spawn(move || {
while let Ok(msg) = receiver.recv() { while let Ok(msg) = receiver.recv() {
let mut env_vars = HashMap::new();
match msg { match msg {
PlayerEvent::Changed { old_track_id, new_track_id } => { PlayerEvent::Changed { old_track_id, new_track_id } => {
let args = vec![old_track_id.to_base16(), new_track_id.to_base16()]; env_vars.insert("PLAYER_EVENT", "change".to_string());
if let Some(ref onchange) = onchange.as_ref() { env_vars.insert("OLD_TRACK_ID", old_track_id.to_base16());
run_program(onchange, args); env_vars.insert("TRACK_ID", new_track_id.to_base16());
}
}, },
PlayerEvent::Started { track_id } => { PlayerEvent::Started { track_id } => {
let args = vec![track_id.to_base16()]; env_vars.insert("PLAYER_EVENT", "start".to_string());
if let Some(ref onstart) = onstart.as_ref() { env_vars.insert("TRACK_ID", track_id.to_base16());
run_program(onstart, args);
}
} }
PlayerEvent::Stopped { track_id } => { PlayerEvent::Stopped { track_id } => {
let args = vec![track_id.to_base16()]; env_vars.insert("PLAYER_EVENT", "stop".to_string());
if let Some(ref onstop) = onstop.as_ref() { env_vars.insert("TRACK_ID", track_id.to_base16());
run_program(onstop, args);
}
} }
} }
run_program(&onevent, env_vars);
} }
}); });
Some(sender) sender
}
} }