1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 17:59:24 +02:00

feat(mpris): Add support for desktop entry

This commit is contained in:
Paul Fariello 2025-09-30 08:50:21 +02:00
parent a3680792fd
commit 274d001500
2 changed files with 21 additions and 8 deletions

View file

@ -2021,7 +2021,7 @@ async fn main() {
} }
#[cfg(feature = "with-mpris")] #[cfg(feature = "with-mpris")]
let mpris = MprisEventHandler::spawn(player.clone(), &setup.connect_config.name) let mpris = MprisEventHandler::spawn(player.clone(), &setup.connect_config.name, None)
.await .await
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
error!("could not initialize MPRIS: {e}"); error!("could not initialize MPRIS: {e}");

View file

@ -153,6 +153,7 @@ type TimeInUs = i64;
struct MprisService { struct MprisService {
identity: String, identity: String,
desktop_entry: Option<String>,
} }
#[zbus::interface(name = "org.mpris.MediaPlayer2")] #[zbus::interface(name = "org.mpris.MediaPlayer2")]
@ -277,7 +278,7 @@ impl MprisService {
debug!("org.mpris.MediaPlayer2::DesktopEntry"); debug!("org.mpris.MediaPlayer2::DesktopEntry");
// FIXME: The spec doesn't say anything about the case when there is no .desktop. // FIXME: The spec doesn't say anything about the case when there is no .desktop.
// Is there any convention? Any value that common clients handle in a sane way? // Is there any convention? Any value that common clients handle in a sane way?
"".to_owned() self.desktop_entry.clone().unwrap_or_default()
} }
// The URI schemes supported by the media player. // The URI schemes supported by the media player.
@ -1123,9 +1124,14 @@ pub struct MprisEventHandler {
} }
impl MprisEventHandler { impl MprisEventHandler {
fn connection_builder<'a>(identity: &str, name: &str) -> zbus::Result<connection::Builder<'a>> { fn connection_builder<'a>(
identity: &str,
name: &str,
desktop_entry: Option<&str>,
) -> zbus::Result<connection::Builder<'a>> {
let mpris_service = MprisService { let mpris_service = MprisService {
identity: identity.to_string(), identity: identity.to_string(),
desktop_entry: desktop_entry.map(|desktop_entry| desktop_entry.to_string()),
}; };
let mpris_player_service = MprisPlayerService { let mpris_player_service = MprisPlayerService {
spirc: None, spirc: None,
@ -1145,10 +1151,15 @@ impl MprisEventHandler {
.serve_at("/org/mpris/MediaPlayer2", mpris_player_service) .serve_at("/org/mpris/MediaPlayer2", mpris_player_service)
} }
pub async fn spawn(player: Arc<Player>, name: &str) -> Result<MprisEventHandler, MprisError> { pub async fn spawn(
player: Arc<Player>,
name: &str,
desktop_entry: Option<&str>,
) -> Result<MprisEventHandler, MprisError> {
let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
let connection = Self::connection_builder(name, "org.mpris.MediaPlayer2.librespot")? let connection =
Self::connection_builder(name, "org.mpris.MediaPlayer2.librespot", desktop_entry)?
.build() .build()
.await; .await;
let connection = match connection { let connection = match connection {
@ -1157,7 +1168,9 @@ impl MprisEventHandler {
format!("org.mpris.MediaPlayer2.librespot.instance{}", process::id()); format!("org.mpris.MediaPlayer2.librespot.instance{}", process::id());
warn!("zbus name taken, trying with pid specific name: {pid_name}"); warn!("zbus name taken, trying with pid specific name: {pid_name}");
Self::connection_builder(name, &pid_name)?.build().await Self::connection_builder(name, &pid_name, desktop_entry)?
.build()
.await
} }
_ => connection, _ => connection,
}?; }?;