librespot/src/main.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

extern crate getopts;
2015-07-09 19:08:56 +00:00
extern crate librespot;
extern crate ctrlc;
2015-04-25 20:32:07 +00:00
use std::io::{stderr, Write};
use std::process::exit;
use std::thread;
use librespot::spirc::SpircManager;
use librespot::main_helper;
use librespot::authentication::get_credentials;
2016-03-16 00:05:05 +00:00
fn usage(program: &str, opts: &getopts::Options) -> String {
let brief = format!("Usage: {} [options]", program);
format!("{}", opts.usage(&brief))
}
2015-04-25 20:32:07 +00:00
fn main() {
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);
2016-01-02 02:30:24 +00:00
let args: Vec<String> = std::env::args().collect();
let matches = match opts.parse(&args[1..]) {
2016-01-02 15:19:39 +00:00
Ok(m) => m,
Err(f) => {
writeln!(stderr(), "error: {}\n{}", f.to_string(), usage(&args[0], &opts)).unwrap();
exit(1);
2016-04-24 11:15:53 +00:00
}
};
main_helper::setup_logging(&matches);
let session = main_helper::create_session(&matches);
let credentials = get_credentials(&session, matches.opt_str("username"),
matches.opt_str("password"));
session.login(credentials).unwrap();
let player = main_helper::create_player(&session, &matches);
let spirc = SpircManager::new(session.clone(), player);
let spirc_signal = spirc.clone();
2016-01-02 15:19:39 +00:00
thread::spawn(move || spirc.run());
ctrlc::set_handler(move || {
spirc_signal.send_goodbye();
exit(0);
});
2016-01-01 23:16:12 +00:00
loop {
session.poll();
}
2015-07-01 17:49:03 +00:00
}