Merge pull request #119 from joerg-krause/say-goodbye

Say goodbye when terminating
This commit is contained in:
Paul Lietar 2016-09-21 22:06:34 -07:00 committed by GitHub
commit 6fa4e4d458
4 changed files with 448 additions and 149 deletions

579
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -44,6 +44,7 @@ url = "~0.5.0"
log = "0.3.5" log = "0.3.5"
env_logger = "0.3.2" env_logger = "0.3.2"
shannon = { git = "https://github.com/plietar/rust-shannon" } shannon = { git = "https://github.com/plietar/rust-shannon" }
simple-signal = "1.0.6"
vorbis = "~0.0.14" vorbis = "~0.0.14"
tremor = { git = "https://github.com/plietar/rust-tremor", optional = true } tremor = { git = "https://github.com/plietar/rust-tremor", optional = true }

View file

@ -3,6 +3,7 @@ extern crate librespot;
extern crate env_logger; extern crate env_logger;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate simple_signal;
use std::process::exit; use std::process::exit;
use std::thread; use std::thread;
@ -11,6 +12,8 @@ use std::env;
use librespot::spirc::SpircManager; use librespot::spirc::SpircManager;
use librespot::main_helper; use librespot::main_helper;
use simple_signal::{Signal, Signals};
fn usage(program: &str, opts: &getopts::Options) -> String { fn usage(program: &str, opts: &getopts::Options) -> String {
let brief = format!("Usage: {} [options]", program); let brief = format!("Usage: {} [options]", program);
format!("{}", opts.usage(&brief)) format!("{}", opts.usage(&brief))
@ -44,7 +47,15 @@ fn main() {
let player = main_helper::create_player(&session, &matches); let player = main_helper::create_player(&session, &matches);
let spirc = SpircManager::new(session.clone(), player); let spirc = SpircManager::new(session.clone(), player);
let spirc_signal = spirc.clone();
thread::spawn(move || spirc.run()); thread::spawn(move || spirc.run());
Signals::set_handler(&[Signal::Int, Signal::Term],
move |signals| {
println!("Signal received: {:?}. Say goodbye and exit.", signals);
spirc_signal.send_goodbye();
exit(0);
}
);
loop { loop {
session.poll(); session.poll();

View file

@ -165,6 +165,12 @@ impl SpircManager {
.send(); .send();
} }
pub fn send_goodbye(&self) {
let mut internal = self.0.lock().unwrap();
CommandSender::new(&mut *internal, MessageType::kMessageTypeGoodbye)
.send();
}
pub fn get_queue(&self) -> Vec<SpotifyId> { pub fn get_queue(&self) -> Vec<SpotifyId> {
self.0.lock().unwrap().tracks.clone() self.0.lock().unwrap().tracks.clone()
} }