librespot/src/main.rs
Jörg Krause c1e570f48d Say goodbye when terminating
When librespot is terminated while a session is active it will now send
a goodbye message, so that the Spotify client unregisters the device
from its list.

Closes: #114
2016-09-20 21:00:01 +02:00

63 lines
1.7 KiB
Rust

extern crate getopts;
extern crate librespot;
extern crate env_logger;
#[macro_use]
extern crate log;
extern crate simple_signal;
use std::process::exit;
use std::thread;
use std::env;
use librespot::spirc::SpircManager;
use librespot::main_helper;
use simple_signal::{Signal, Signals};
fn usage(program: &str, opts: &getopts::Options) -> String {
let brief = format!("Usage: {} [options]", program);
format!("{}", opts.usage(&brief))
}
fn main() {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info,librespot=trace")
}
env_logger::init().unwrap();
let mut opts = getopts::Options::new();
main_helper::add_session_arguments(&mut opts);
main_helper::add_authentication_arguments(&mut opts);
main_helper::add_player_arguments(&mut opts);
let args: Vec<String> = std::env::args().collect();
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
error!("Error: {}\n{}", f.to_string(), usage(&args[0], &opts));
exit(1)
}
};
let session = main_helper::create_session(&matches);
let credentials = main_helper::get_credentials(&session, &matches);
session.login(credentials).unwrap();
let player = main_helper::create_player(&session, &matches);
let spirc = SpircManager::new(session.clone(), player);
let spirc_signal = spirc.clone();
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 {
session.poll();
}
}