add Spirc.seek_offset command

- preparation for MPRIS support
This commit is contained in:
wisp3rwind 2024-09-17 15:27:47 +02:00
parent 055e0d8f38
commit 80939819dd

View file

@ -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