feat: make MPRIS bus suffixes unique

This commit is contained in:
Tal 2024-10-27 14:17:30 +01:00
parent b12a7077a8
commit 56b03290d0
3 changed files with 33 additions and 14 deletions

View File

@ -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<Self>,
tx: Sender<Messages>,
@ -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));

View File

@ -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<super::Player>, sender: Sender<Messages>) -> eyre::Result<Self> {
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 })
}

View File

@ -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<String>,
@ -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<String> = 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"),
))
}
}
}