playdoobs 0.1.0

Service and matching CLI application for controlling the currently active MPRIS2 player
Documentation
pub mod daemon;
pub mod provider;

use std::ops::Deref;

use color_eyre::eyre::{Result, bail};
use doobs_mpris::binding::{MPRIS_BUS_NAME_PREFIX, PlayerProxy, PlayerProxyBlocking};
use doobs_mpris::types::TRACK_ID_NO_TRACK;
pub use doobs_mpris::types::{Metadata, PlaybackStatus};
pub use zbus;

pub mod types {
    pub use doobs_mpris::types::*;
}

pub const VERSION: &str = env!("APP_VERSION");

/// The name under which the playdoobsd service is started
pub const PLAYDOOBSD_BUS_NAME: &str = "org.mpris.MediaPlayer2.playdoobsd";

/// Custom key under which we store the D-Bus service name of the active player in the player metadata.
pub const PLAYER_DBUS_NAME_METADATA_KEY: &str = "playdoobs:dbusName";

const PLAYERCTLD_BUS_NAME: &str = "org.mpris.MediaPlayer2.playerctld";

/// Playdoobsd is a D-Bus service that implements the [Media Player Remote Interfacing Specification][MPRIS]
/// and keeps track of currently active media players that also support MPRIS.
///
/// An instance of this struct represents a connection to the D-Bus session that allows you to call
/// the methods and access the properties of the currently active player.
///
/// [MPRIS]: http://specifications.freedesktop.org/mpris-spec/latest/
pub struct Playdoobsd<'p> {
    #[allow(dead_code)]
    conn: zbus::Connection,
    proxy: PlayerProxy<'p>,
}

impl<'p> Playdoobsd<'p> {
    pub async fn connect() -> zbus::Result<Self> {
        let conn = zbus::Connection::session().await?;
        let proxy = PlayerProxy::new(&conn, PLAYDOOBSD_BUS_NAME).await?;
        Ok(Self { conn, proxy })
    }
}

impl<'p> Deref for Playdoobsd<'p> {
    type Target = PlayerProxy<'p>;

    fn deref(&self) -> &Self::Target {
        &self.proxy
    }
}

/// Blocking version of [Playdoobsd].
pub struct PlaydoobsdBlocking<'p> {
    #[allow(dead_code)]
    conn: zbus::blocking::Connection,
    proxy: PlayerProxyBlocking<'p>,
}

impl<'p> PlaydoobsdBlocking<'p> {
    pub fn connect() -> zbus::Result<Self> {
        let conn = zbus::blocking::Connection::session()?;
        let proxy = PlayerProxyBlocking::new(&conn, PLAYDOOBSD_BUS_NAME)?;
        Ok(Self { conn, proxy })
    }
}

impl<'p> Deref for PlaydoobsdBlocking<'p> {
    type Target = PlayerProxyBlocking<'p>;

    fn deref(&self) -> &Self::Target {
        &self.proxy
    }
}

/// Parses the name of the player from the given bus service name.
pub fn parse_player_name(bus_name: &str) -> Result<String> {
    let Some(name_with_instance_num) = bus_name.strip_prefix(MPRIS_BUS_NAME_PREFIX) else {
        bail!("{bus_name}: Could not parse player name")
    };

    // TODO maybe do something if there's multiple instances
    let name = name_with_instance_num
        .split_once(".")
        .map(|(name, _instance_num)| name)
        .unwrap_or(name_with_instance_num);

    Ok(name.into())
}

pub fn no_track_metadata() -> Metadata {
    Metadata::new(TRACK_ID_NO_TRACK.into())
}