Skip to content

Commit

Permalink
add Spirc.seek_offset command
Browse files Browse the repository at this point in the history
- preparation for MPRIS support
  • Loading branch information
wisp3rwind committed Oct 15, 2024
1 parent defd067 commit 04dcf63
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub enum SpircCommand {
Repeat(bool),
Disconnect,
SetPosition(u32),
SeekOffset(i32),
SetVolume(u16),
Activate,
Load(SpircLoadCommand),
Expand Down Expand Up @@ -436,6 +437,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)?)
}
Expand Down Expand Up @@ -638,6 +642,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)
Expand Down Expand Up @@ -1154,6 +1162,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
Expand Down

0 comments on commit 04dcf63

Please sign in to comment.