From b3c9744b296e6a1db5198bed870cb6cb47edd9e1 Mon Sep 17 00:00:00 2001 From: ashthespy Date: Tue, 5 Nov 2019 20:34:43 +0100 Subject: [PATCH] Add a `autoplay` option flag --- connect/src/spirc.rs | 29 +++++++++++++++++++---------- core/src/config.rs | 1 + src/main.rs | 22 +++++++++++++++------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index 96b7c8a4..4fd4288e 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -25,7 +25,7 @@ use protocol::spirc::{DeviceState, Frame, MessageType, PlayStatus, State, TrackR pub struct SpircTask { player: Player, mixer: Box, - linear_volume: bool, + config: SpircTaskConfig, sequence: SeqGenerator, @@ -56,6 +56,11 @@ pub enum SpircCommand { Shutdown, } +struct SpircTaskConfig { + linear_volume: bool, + autoplay: bool, +} + const CONTEXT_TRACKS_HISTORY: usize = 10; const CONTEXT_FETCH_THRESHOLD: u32 = 5; @@ -244,14 +249,16 @@ impl Spirc { let (cmd_tx, cmd_rx) = mpsc::unbounded(); let volume = config.volume; - let linear_volume = config.linear_volume; - + let task_config = SpircTaskConfig { + linear_volume: config.linear_volume, + autoplay: config.autoplay, + }; let device = initial_device_state(config); let mut task = SpircTask { player: player, mixer: mixer, - linear_volume: linear_volume, + config: task_config, sequence: SeqGenerator::new(1), @@ -383,7 +390,7 @@ impl Future for SpircTask { match self.autoplay_fut.poll() { Ok(Async::Ready(autoplay_station_uri)) => { - info!("Autoplay resolved to <{:?}>", autoplay_station_uri); + info!("Autoplay uri resolved to <{:?}>", autoplay_station_uri); self.context_fut = self.resolve_station(&autoplay_station_uri); progress = true; self.autoplay_fut = Box::new(future::empty()); @@ -688,11 +695,11 @@ impl SpircTask { self.context_fut = self.resolve_station(&context_uri); self.update_tracks_from_context(); } - if new_index == tracks_len - 1 { + if self.config.autoplay && new_index == tracks_len - 1 { // Extend the playlist // Note: This doesn't seem to reflect in the UI - // the additional tracks in the frame don't show up as with stations - info!("Extending playlist <{}>", context_uri); + // the additional tracks in the frame don't show up as with station view + debug!("Extending playlist <{}>", context_uri); self.update_tracks_from_context(); } if new_index >= tracks_len { @@ -840,7 +847,8 @@ impl SpircTask { debug!("Frame has {:?} tracks", tracks.len()); if context_uri.starts_with("spotify:station:") || context_uri.starts_with("spotify:dailymix:") { self.context_fut = self.resolve_station(&context_uri); - } else { + } else if self.config.autoplay { + info!("Fetching autoplay context uri"); // Get autoplay_station_uri for regular playlists self.autoplay_fut = self.resolve_autoplay_uri(&context_uri); } @@ -923,7 +931,8 @@ impl SpircTask { fn set_volume(&mut self, volume: u16) { self.device.set_volume(volume as u32); - self.mixer.set_volume(volume_to_mixer(volume, self.linear_volume)); + self.mixer + .set_volume(volume_to_mixer(volume, self.config.linear_volume)); if let Some(cache) = self.session.cache() { cache.save_volume(Volume { volume }) } diff --git a/core/src/config.rs b/core/src/config.rs index 293e2e90..7c053217 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -85,4 +85,5 @@ pub struct ConnectConfig { pub device_type: DeviceType, pub volume: u16, pub linear_volume: bool, + pub autoplay: bool, } diff --git a/src/main.rs b/src/main.rs index e3718fb8..e193257b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,18 +4,18 @@ extern crate getopts; extern crate librespot; #[macro_use] extern crate log; +extern crate hex; extern crate rpassword; +extern crate sha1; extern crate tokio_core; extern crate tokio_io; extern crate tokio_process; extern crate tokio_signal; extern crate url; -extern crate sha1; -extern crate hex; -use sha1::{Sha1, Digest}; use futures::sync::mpsc::UnboundedReceiver; use futures::{Async, Future, Poll, Stream}; +use sha1::{Digest, Sha1}; use std::env; use std::io::{self, stderr, Write}; use std::mem; @@ -188,6 +188,11 @@ fn setup(args: &[String]) -> Setup { "", "linear-volume", "increase volume linear instead of logarithmic.", + ) + .optflag( + "", + "autoplay", + "autoplay similar songs when your music ends.", ); let matches = match opts.parse(&args[1..]) { @@ -249,7 +254,8 @@ fn setup(args: &[String]) -> Setup { panic!("Initial volume must be in the range 0-100"); } (volume as i32 * 0xFFFF / 100) as u16 - }).or_else(|| cache.as_ref().and_then(Cache::volume)) + }) + .or_else(|| cache.as_ref().and_then(Cache::volume)) .unwrap_or(0x8000); let zeroconf_port = matches @@ -334,6 +340,7 @@ fn setup(args: &[String]) -> Setup { device_type: device_type, volume: initial_volume, linear_volume: matches.opt_present("linear-volume"), + autoplay: matches.opt_present("autoplay"), } }; @@ -503,13 +510,14 @@ impl Future for Main { if let Some(ref program) = self.player_event_program { let child = run_program_on_events(event, program) .expect("program failed to start") - .map(|status| if !status.success() { - error!("child exited with status {:?}", status.code()); + .map(|status| { + if !status.success() { + error!("child exited with status {:?}", status.code()); + } }) .map_err(|e| error!("failed to wait on child process: {}", e)); self.handle.spawn(child); - } } }