2018-02-15 23:16:38 +00:00
|
|
|
use std::process::Command;
|
|
|
|
use std::sync::mpsc::{channel, Sender};
|
|
|
|
use std::thread;
|
2018-02-20 21:45:14 +00:00
|
|
|
use std::collections::HashMap;
|
2018-02-20 20:57:42 +00:00
|
|
|
use librespot::playback::player::PlayerEvent;
|
2018-02-15 23:16:38 +00:00
|
|
|
|
2018-02-20 21:45:14 +00:00
|
|
|
fn run_program(program: &str, env_vars: HashMap<&str, String>) {
|
2018-02-15 23:16:38 +00:00
|
|
|
let mut v: Vec<&str> = program.split_whitespace().collect();
|
2018-02-20 21:45:14 +00:00
|
|
|
info!("Running {:?}", v);
|
2018-02-15 23:16:38 +00:00
|
|
|
let status = Command::new(&v.remove(0))
|
|
|
|
.args(&v)
|
2018-02-20 21:45:14 +00:00
|
|
|
.envs(env_vars.iter())
|
2018-02-15 23:16:38 +00:00
|
|
|
.status()
|
|
|
|
.expect("program failed to start");
|
|
|
|
info!("Exit status: {}", status);
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:45:14 +00:00
|
|
|
pub fn run_program_on_events(onevent: String) -> Sender<PlayerEvent> {
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
thread::spawn(move || {
|
|
|
|
while let Ok(msg) = receiver.recv() {
|
|
|
|
let mut env_vars = HashMap::new();
|
|
|
|
match msg {
|
|
|
|
PlayerEvent::Changed { old_track_id, new_track_id } => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "change".to_string());
|
|
|
|
env_vars.insert("OLD_TRACK_ID", old_track_id.to_base16());
|
|
|
|
env_vars.insert("TRACK_ID", new_track_id.to_base16());
|
|
|
|
},
|
|
|
|
PlayerEvent::Started { track_id } => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "start".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", track_id.to_base16());
|
|
|
|
}
|
|
|
|
PlayerEvent::Stopped { track_id } => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "stop".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", track_id.to_base16());
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 21:45:14 +00:00
|
|
|
run_program(&onevent, env_vars);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sender
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|