T-025: MPRIS-Server (Medientasten, Plasma) — Play/Pause/Next/Previous/Seek/Position/Metadaten über mpris-server

This commit is contained in:
2026-11-06 20:35:27 +01:00
parent ed0ff900ca
commit a20543c8fe
4 changed files with 179 additions and 0 deletions

View File

@ -604,3 +604,152 @@ mod real_tests {
p.stop().unwrap();
}
}
// — MPRIS (T-025): Medientasten, Plasma-Widget —
use mpris_server::{zbus, zbus::fdo, Metadata, PlaybackStatus, PlayerInterface, RootInterface, Server, Time};
pub struct PlayerBridge(pub SharedPlayer);
impl PlayerBridge {
fn snap(&self) -> PlayerState {
let p: &dyn Player = &self.0;
p.snapshot()
}
}
impl RootInterface for PlayerBridge {
async fn identity(&self) -> fdo::Result<String> {
Ok("Hörwerk".into())
}
async fn desktop_entry(&self) -> fdo::Result<String> {
Ok("hoerwerk".into())
}
async fn raise(&self) -> fdo::Result<()> { Ok(()) }
async fn quit(&self) -> fdo::Result<()> { Ok(()) }
async fn can_quit(&self) -> fdo::Result<bool> { Ok(false) }
async fn can_raise(&self) -> fdo::Result<bool> { Ok(false) }
async fn fullscreen(&self) -> fdo::Result<bool> { Ok(false) }
async fn set_fullscreen(&self, _: bool) -> zbus::Result<()> { Ok(()) }
async fn can_set_fullscreen(&self) -> fdo::Result<bool> { Ok(false) }
async fn has_track_list(&self) -> fdo::Result<bool> { Ok(false) }
async fn supported_uri_schemes(&self) -> fdo::Result<Vec<String>> { Ok(vec![]) }
async fn supported_mime_types(&self) -> fdo::Result<Vec<String>> { Ok(vec![]) }
}
impl PlayerInterface for PlayerBridge {
async fn playback_status(&self) -> fdo::Result<PlaybackStatus> {
let s = self.snap();
Ok(if !s.loaded {
PlaybackStatus::Stopped
} else if s.playing {
PlaybackStatus::Playing
} else {
PlaybackStatus::Paused
})
}
async fn metadata(&self) -> fdo::Result<Metadata> {
let s = self.snap();
let mut b = Metadata::builder();
if let Some(t) = s.title.as_deref() {
b = b.title(t);
}
b = b.album("Hörwerk");
b = b.length(Time::from_micros((s.duration.max(0.0) * 1_000_000.0) as i64));
Ok(b.build())
}
async fn position(&self) -> fdo::Result<Time> {
Ok(Time::from_micros((self.snap().position * 1_000_000.0) as i64))
}
async fn play(&self) -> fdo::Result<()> {
let p = &self.0;
if !p.snapshot().playing {
let _ = p.toggle();
}
Ok(())
}
async fn pause(&self) -> fdo::Result<()> {
let p = &self.0;
if p.snapshot().playing {
let _ = p.toggle();
}
Ok(())
}
async fn play_pause(&self) -> fdo::Result<()> {
let _ = self.0.toggle();
Ok(())
}
async fn stop(&self) -> fdo::Result<()> {
let _ = self.0.stop();
Ok(())
}
async fn next(&self) -> fdo::Result<()> {
let p = &self.0;
let ch = p.snapshot().chapter;
let _ = p.set_chapter(ch + 1);
Ok(())
}
async fn previous(&self) -> fdo::Result<()> {
let p = &self.0;
let ch = p.snapshot().chapter;
let _ = p.set_chapter((ch - 1).max(0));
Ok(())
}
async fn seek(&self, offset: Time) -> fdo::Result<()> {
let secs = offset.as_micros() as f64 / 1_000_000.0;
let _ = self.0.seek_by(secs);
Ok(())
}
async fn set_position(
&self,
_track_id: mpris_server::TrackId,
position: Time,
) -> fdo::Result<()> {
let secs = position.as_micros() as f64 / 1_000_000.0;
let _ = self.0.set_position(secs);
Ok(())
}
async fn open_uri(&self, _: String) -> fdo::Result<()> { Ok(()) }
async fn loop_status(&self) -> fdo::Result<mpris_server::LoopStatus> { Ok(mpris_server::LoopStatus::None) }
async fn set_loop_status(&self, _: mpris_server::LoopStatus) -> zbus::Result<()> { Ok(()) }
async fn rate(&self) -> fdo::Result<f64> { Ok(self.snap().speed) }
async fn set_rate(&self, _: f64) -> zbus::Result<()> { Ok(()) }
async fn shuffle(&self) -> fdo::Result<bool> { Ok(false) }
async fn set_shuffle(&self, _: bool) -> zbus::Result<()> { Ok(()) }
async fn volume(&self) -> fdo::Result<f64> { Ok(1.0) }
async fn set_volume(&self, _: f64) -> zbus::Result<()> { Ok(()) }
async fn minimum_rate(&self) -> fdo::Result<f64> { Ok(0.5) }
async fn maximum_rate(&self) -> fdo::Result<f64> { Ok(3.0) }
async fn can_go_next(&self) -> fdo::Result<bool> { Ok(true) }
async fn can_go_previous(&self) -> fdo::Result<bool> { Ok(true) }
async fn can_play(&self) -> fdo::Result<bool> { Ok(true) }
async fn can_pause(&self) -> fdo::Result<bool> { Ok(true) }
async fn can_seek(&self) -> fdo::Result<bool> { Ok(self.snap().duration > 0.0) }
async fn can_control(&self) -> fdo::Result<bool> { Ok(true) }
}
pub fn spawn_mpris(player: SharedPlayer) {
tauri::async_runtime::spawn(async move {
match Server::new("org.mpris.MediaPlayer2.hoerwerk", PlayerBridge(player)).await {
Ok(server) => {
// Server versorgt sich intern; Future aufrechterhalten
std::future::pending::<()>().await;
let _ = server;
}
Err(e) => {
eprintln!("MPRIS nicht verfügbar: {e}");
}
}
});
}