diff --git a/src/player.rs b/src/player.rs index 40268e8..ecac997 100644 --- a/src/player.rs +++ b/src/player.rs @@ -290,6 +290,8 @@ impl Player { /// /// `rx` & `tx` are used to communicate with it, for example when to /// skip tracks or pause. + /// + /// This will also initialize a [Downloader] as well as an MPRIS server if enabled. pub async fn play( player: Arc, tx: Sender, @@ -302,7 +304,11 @@ impl Player { // specifically when it occurs, unlike the UI which passively reads the // information each frame. Blame MPRIS, not me. #[cfg(feature = "mpris")] - let mpris = mpris::Server::new(Arc::clone(&player), tx.clone()).await?; + let mpris = mpris::Server::new(Arc::clone(&player), tx.clone()) + .await + .inspect_err(|x| { + dbg!(x); + })?; // `itx` is used to notify the `Downloader` when it needs to download new tracks. let downloader = Downloader::new(Arc::clone(&player)); diff --git a/src/player/mpris.rs b/src/player/mpris.rs index 83ae17f..87b022b 100644 --- a/src/player/mpris.rs +++ b/src/player/mpris.rs @@ -1,6 +1,6 @@ //! Contains the code for the MPRIS server & other helper functions. -use std::sync::Arc; +use std::{process, sync::Arc}; use mpris_server::{ zbus::{self, fdo, Result}, @@ -262,9 +262,9 @@ impl Server { /// Creates a new MPRIS server. pub async fn new(player: Arc, sender: Sender) -> eyre::Result { - let server = mpris_server::Server::new("lowfi", Player { player, sender }) - .await - .unwrap(); + let suffix = format!("lowfi.{}.instance{}", player.list.name, process::id()); + + let server = mpris_server::Server::new(&suffix, Player { player, sender }).await?; Ok(Self { inner: server }) } diff --git a/src/tracks/list.rs b/src/tracks/list.rs index 2e2e26e..70de8f9 100644 --- a/src/tracks/list.rs +++ b/src/tracks/list.rs @@ -2,6 +2,7 @@ //! as well as obtaining track names & downloading the raw mp3 data. use bytes::Bytes; +use eyre::OptionExt; use rand::Rng; use reqwest::Client; use tokio::fs; @@ -13,6 +14,9 @@ use super::Track; /// See the [README](https://github.com/talwat/lowfi?tab=readme-ov-file#the-format) for more details about the format. #[derive(Clone)] pub struct List { + /// The "name" of the list, usually derived from a filename. + pub name: String, + /// Just the raw file, but seperated by `/n` (newlines). /// `lines[0]` is the base, with the rest being tracks. lines: Vec, @@ -59,13 +63,16 @@ impl List { } /// Parses text into a [List]. - pub fn new(text: &str) -> Self { + pub fn new(name: &str, text: &str) -> Self { let lines: Vec = text .split_ascii_whitespace() .map(ToOwned::to_owned) .collect(); - Self { lines } + Self { + lines, + name: name.to_owned(), + } } /// Reads a [List] from the filesystem using the CLI argument provided. @@ -77,15 +84,21 @@ impl List { .join("lowfi") .join(format!("{}.txt", arg)); - let raw = if name.exists() { - fs::read_to_string(name).await? - } else { - fs::read_to_string(arg).await? - }; + let name = if name.exists() { name } else { arg.into() }; - Ok(Self::new(&raw)) + let raw = fs::read_to_string(name.clone()).await?; + + let name = name + .file_stem() + .and_then(|x| x.to_str()) + .ok_or_eyre("invalid track path")?; + + Ok(Self::new(name, &raw)) } else { - Ok(Self::new(include_str!("../../data/lofigirl.txt"))) + Ok(Self::new( + "lofigirl", + include_str!("../../data/lofigirl.txt"), + )) } } }