Skip to content

Commit

Permalink
Add MPRIS controls for volume
Browse files Browse the repository at this point in the history
  • Loading branch information
z80maniac committed May 23, 2024
1 parent 2668722 commit c17aded
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,28 @@ impl App {
self.change_volume(VOL_STEP);
}

fn set_vol(&mut self, new_volume: f32) {
fn set_vol(&mut self, new_volume: f32, show_popup: bool) {
let new_volume = new_volume.clamp(0.0, 1.0);
let steps_count = (new_volume / VOL_STEP as f32).round();
let new_volume = steps_count * VOL_STEP as f32;
self.state.volume = new_volume;
self.player.set_volume(new_volume);

self.update_tray(true);
self.update_tray(show_popup);
self.state.save().ignore_err();
}

fn user_action_vol_down(&mut self) {
let new_volume = self.state.volume - VOL_STEP as f32;
self.set_vol(new_volume);
self.set_vol(new_volume, true);
}

fn user_action_vol_up(&mut self) {
let new_volume = self.state.volume + VOL_STEP as f32;
self.set_vol(new_volume);
self.set_vol(new_volume, true);
}

fn user_action_set_vol(&mut self, new_volume: f32) {
self.set_vol(new_volume, false);
}

fn user_action_seek_by(&self, forward: bool, length: Duration) {
Expand Down Expand Up @@ -334,6 +337,9 @@ impl App {

self.media_controls
.mut_map(|c| c.set_metadata(&self.meta).ignore_err());
self.media_controls
.mut_map(|c| c.set_volume(self.state.volume));
self.player.request_position(); // because set_volume resets the position

if show_popup {
self.popup.show(&tooltip);
Expand Down Expand Up @@ -439,6 +445,9 @@ impl App {
PlayerResponse::PlaybackStateChanged { state, position } => {
self.set_playback_state(state, Some(position));
}
PlayerResponse::PositionRequested { position } => {
self.set_playback_state(self.playback_state.clone(), Some(position));
}
PlayerResponse::Seeked { position } => {
let state = self.playback_state.clone();
self.last_seek_position = Some(position);
Expand Down Expand Up @@ -481,7 +490,7 @@ impl App {
MediaControlEvent::Quit => self.user_action_quit(),
MediaControlEvent::SetPosition(pos) => self.user_action_seek_to(pos.0),
MediaControlEvent::OpenUri(uri) => self.user_action_open_uri(uri),
MediaControlEvent::SetVolume(_) => {}
MediaControlEvent::SetVolume(vol) => self.user_action_set_vol(vol as f32),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/media_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ impl MediaControls {
.context("cannot set metadata")?;
return Ok(());
}

pub fn set_volume(&mut self, volume: f32) -> Result<()> {
self.controls
.set_volume(volume as f64)
.context("cannot set volume")?;
return Ok(());
}
}
18 changes: 18 additions & 0 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum PlayerCmd {
Pause,
UnPause,
Stop,
RequestPosition,

Next,
Prev,
Expand Down Expand Up @@ -74,6 +75,9 @@ pub enum PlayerResponse {
state: PlaybackState,
position: Duration,
},
PositionRequested {
position: Duration,
},
PositionCallback {
callback: PositionCallback,
position: Duration,
Expand Down Expand Up @@ -468,6 +472,13 @@ impl PlayerThread {
return Ok(());
}

fn send_position(&self) {
let position = self.decoder.playback_position();
self.tx
.send(PlayerResponse::PositionRequested { position })
.unwrap();
}

fn process_client_cmd(&mut self) -> Result<bool> {
let recv_timeout = if self.need_fast_read {
Duration::ZERO
Expand Down Expand Up @@ -511,6 +522,9 @@ impl PlayerThread {
PlayerCmd::Stop => {
self.stop();
}
PlayerCmd::RequestPosition => {
self.send_position();
}
PlayerCmd::Next => {
self.stop();
self.next(true, true).context("cannot play next track")?;
Expand Down Expand Up @@ -713,6 +727,10 @@ impl PlayerTx {
self.send(PlayerCmd::Stop);
}

pub fn request_position(&self) {
self.send(PlayerCmd::RequestPosition);
}

pub fn next(&self) {
self.send(PlayerCmd::Next);
}
Expand Down

0 comments on commit c17aded

Please sign in to comment.