T-025: MPRIS-Server (Medientasten, Plasma) — Play/Pause/Next/Previous/Seek/Position/Metadaten über mpris-server
This commit is contained in:
28
src-tauri/Cargo.lock
generated
28
src-tauri/Cargo.lock
generated
@ -1429,6 +1429,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||
name = "hoerwerk"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mpris-server",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
@ -2053,6 +2054,19 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mpris-server"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70cf358e9a9516cc35ed40eebd56cbe80f5190a7a2b3cd0c2d358c86b879132b"
|
||||
dependencies = [
|
||||
"async-channel",
|
||||
"futures-channel",
|
||||
"serde",
|
||||
"trait-variant",
|
||||
"zbus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "muda"
|
||||
version = "0.20.0"
|
||||
@ -3731,7 +3745,9 @@ dependencies = [
|
||||
"libc",
|
||||
"mio",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
@ -3920,6 +3936,17 @@ dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "trait-variant"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b19a4867a870f6edc4c283f2b455804b1879c0baf0e642f26b03ed8ee262d9d3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tray-icon"
|
||||
version = "0.25.1"
|
||||
@ -4672,6 +4699,7 @@ dependencies = [
|
||||
"rustix",
|
||||
"serde",
|
||||
"serde_repr",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"uds_windows",
|
||||
"uuid",
|
||||
|
||||
@ -22,6 +22,7 @@ tauri = { version = "2", features = ["protocol-asset"] }
|
||||
tauri-plugin-opener = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
mpris-server = { version = "0.10", features = ["tokio"] }
|
||||
|
||||
|
||||
# Read the optimization guideline for more details: https://tauri.app/concept/size/#cargo-configuration
|
||||
|
||||
@ -182,6 +182,7 @@ pub fn run() {
|
||||
.attach(app.handle().clone());
|
||||
let p = app.state::<SharedPlayer>().inner().clone();
|
||||
p.attach(app.handle().clone());
|
||||
player::spawn_mpris(p.clone());
|
||||
// Letztes Buch pausiert fortsetzen (T-024), sobald mpv bereit ist
|
||||
std::thread::Builder::new()
|
||||
.name("hoerwerk-resume".into())
|
||||
|
||||
@ -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}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user