From 80939819ddbca9923feb8e9bb832735575d789ab Mon Sep 17 00:00:00 2001 From: wisp3rwind <17089248+wisp3rwind@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:27:47 +0200 Subject: [PATCH] add Spirc.seek_offset command - preparation for MPRIS support --- connect/src/spirc.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index 9b512f92..ec441516 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -121,6 +121,7 @@ pub enum SpircCommand { Repeat(bool), Disconnect, SetPosition(u32), + SeekOffset(i32), SetVolume(u16), Activate, Load(SpircLoadCommand), @@ -439,6 +440,9 @@ impl Spirc { pub fn set_position_ms(&self, position_ms: u32) -> Result<(), Error> { Ok(self.commands.send(SpircCommand::SetPosition(position_ms))?) } + pub fn seek_offset(&self, offset_ms: i32) -> Result<(), Error> { + Ok(self.commands.send(SpircCommand::SeekOffset(offset_ms))?) + } pub fn disconnect(&self) -> Result<(), Error> { Ok(self.commands.send(SpircCommand::Disconnect)?) } @@ -656,6 +660,10 @@ impl SpircTask { self.handle_seek(position); self.notify(None) } + SpircCommand::SeekOffset(offset) => { + self.handle_seek_offset(offset); + self.notify(None) + } SpircCommand::SetVolume(volume) => { self.set_volume(volume); self.notify(None) @@ -1172,6 +1180,25 @@ impl SpircTask { }; } + fn handle_seek_offset(&mut self, offset_ms: i32) { + let position_ms = match self.play_status { + SpircPlayStatus::Stopped => return, + SpircPlayStatus::LoadingPause { position_ms } + | SpircPlayStatus::LoadingPlay { position_ms } + | SpircPlayStatus::Paused { position_ms, .. } => position_ms, + SpircPlayStatus::Playing { + nominal_start_time, .. + } => { + let now = self.now_ms(); + (now - nominal_start_time) as u32 + } + }; + + let position_ms = ((position_ms as i32) + offset_ms).max(0) as u32; + + self.handle_seek(position_ms); + } + fn consume_queued_track(&mut self) -> usize { // Removes current track if it is queued // Returns the index of the next track