use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use serde_json::json;
use crate::app::App;
use crate::config;
use crate::ipc;
static SHUTDOWN: AtomicBool = AtomicBool::new(false);
#[cfg(unix)]
extern "C" fn on_signal(_: libc::c_int) {
SHUTDOWN.store(true, Ordering::SeqCst);
}
fn pid_alive(pid: u32) -> bool {
ipc::pid_alive(pid)
}
const HEARTBEAT_TTL: u64 = 15;
fn heartbeat_is_fresh(pid: u32) -> bool {
let Ok(raw) = fs::read_to_string(config::daemon_heartbeat_path()) else {
return false;
};
let mut parts = raw.split_whitespace();
let recorded: Option<u32> = parts.next().and_then(|value| value.parse().ok());
let stamp: Option<u64> = parts.next().and_then(|value| value.parse().ok());
if recorded != Some(pid) {
return false;
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_secs())
.unwrap_or(0);
stamp.is_some_and(|stamp| now.saturating_sub(stamp) <= HEARTBEAT_TTL)
}
fn write_heartbeat(pid: u32) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_secs())
.unwrap_or(0);
let _ = fs::write(config::daemon_heartbeat_path(), format!("{pid} {now}"));
}
pub fn running_pid() -> Option<u32> {
let pid = fs::read_to_string(config::daemon_pid_path())
.ok()
.and_then(|raw| raw.trim().parse::<u32>().ok())?;
if !pid_alive(pid) {
return None;
}
if heartbeat_is_fresh(pid) {
return Some(pid);
}
let recent = fs::metadata(config::daemon_pid_path())
.and_then(|meta| meta.modified())
.ok()
.and_then(|time| time.elapsed().ok())
.is_some_and(|age| age < Duration::from_secs(30));
recent.then_some(pid)
}
fn request_stop() {
let _ = fs::write(config::daemon_stop_path(), std::process::id().to_string());
}
pub fn is_running() -> bool {
running_pid().is_some()
}
pub fn spawn_background() -> Result<()> {
if is_running() {
return Ok(());
}
let exe = std::env::current_exe().context("locating the mfe-player binary")?;
let log = fs::OpenOptions::new()
.create(true)
.append(true)
.open(config::daemon_log_path())
.ok();
let mut command = Command::new(exe);
command
.arg("--daemon")
.stdin(Stdio::null())
.stdout(Stdio::null());
if let Some(log) = log {
command.stderr(Stdio::from(log));
} else {
command.stderr(Stdio::null());
}
ipc::detach(&mut command);
command.spawn().context("starting the background daemon")?;
Ok(())
}
pub fn stop_background() {
let Some(pid) = running_pid() else {
let _ = fs::remove_file(config::daemon_pid_path());
let _ = fs::remove_file(config::daemon_heartbeat_path());
let _ = fs::remove_file(config::daemon_stop_path());
return;
};
request_stop();
let graceful = Instant::now() + Duration::from_secs(4);
while Instant::now() < graceful && pid_alive(pid) {
thread::sleep(Duration::from_millis(80));
}
if !pid_alive(pid) {
let _ = fs::remove_file(config::daemon_pid_path());
let _ = fs::remove_file(config::daemon_heartbeat_path());
let _ = fs::remove_file(config::daemon_stop_path());
return;
}
let _ = fs::remove_file(config::daemon_stop_path());
ipc::terminate(pid);
let deadline = Instant::now() + Duration::from_secs(3);
while Instant::now() < deadline && pid_alive(pid) {
thread::sleep(Duration::from_millis(40));
}
if pid_alive(pid) {
ipc::kill(pid);
}
let _ = fs::remove_file(config::daemon_pid_path());
}
fn session_owner_alive() -> bool {
fs::read_to_string(config::session_lock_path())
.ok()
.and_then(|raw| raw.trim().parse::<u32>().ok())
.is_some_and(ipc::is_player_pid)
}
fn wait_for_session_release(timeout: Duration) -> bool {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if !session_owner_alive() {
return true;
}
thread::sleep(Duration::from_millis(50));
}
false
}
pub fn cleanup_stale_state() {
let lock = config::session_lock_path();
if let Some(pid) = fs::read_to_string(&lock)
.ok()
.and_then(|raw| raw.trim().parse::<u32>().ok())
{
if !ipc::is_player_pid(pid) {
let _ = fs::remove_file(&lock);
}
}
let handoff = config::handoff_path();
if let Some(pid) = fs::read_to_string(&handoff)
.ok()
.and_then(|raw| raw.trim().parse::<u32>().ok())
{
if !ipc::is_player_pid(pid) {
let _ = fs::remove_file(&handoff);
}
}
let Ok(entries) = fs::read_dir(config::runtime_dir()) else {
return;
};
for entry in entries.flatten() {
let name = entry.file_name();
let name = name.to_string_lossy();
let Some(pid) = name
.strip_prefix("mpv-")
.and_then(|rest| rest.strip_suffix(".sock"))
.and_then(|pid| pid.parse::<u32>().ok())
else {
continue;
};
if !ipc::is_player_pid(pid) {
let _ = fs::remove_file(entry.path());
}
}
}
pub fn takeover() {
if !is_running() {
return;
}
stop_background();
let _ = wait_for_session_release(Duration::from_secs(2));
}
pub fn run() -> Result<()> {
config::ensure_dirs()?;
let _ = fs::remove_file(config::daemon_stop_path());
let _ = fs::remove_file(config::daemon_control_path());
let _ = fs::write(config::daemon_pid_path(), std::process::id().to_string());
write_heartbeat(std::process::id());
#[cfg(unix)]
unsafe {
libc::signal(libc::SIGTERM, on_signal as *const () as libc::sighandler_t);
libc::signal(libc::SIGINT, on_signal as *const () as libc::sighandler_t);
}
if !wait_for_session_release(Duration::from_secs(10)) {
eprintln!("mfe-player daemon: previous session never released the player");
let _ = fs::remove_file(config::daemon_pid_path());
return Ok(());
}
thread::sleep(Duration::from_millis(350));
let _ = fs::remove_file(config::handoff_path());
let (app_config, warning) = config::load_config();
let mut app = App::new_headless(app_config)?;
if let Some(warning) = warning {
app.flash(warning);
}
thread::sleep(Duration::from_millis(700));
app.daemon_resume_if_idle();
app.persist();
let mut last_beat = Instant::now();
while !SHUTDOWN.load(Ordering::SeqCst) {
if config::daemon_stop_path().exists() {
break;
}
app.tick();
let commands = drain_control();
if !commands.is_empty() {
app.apply_remote(&commands);
}
if last_beat.elapsed() >= Duration::from_secs(1) {
write_heartbeat(std::process::id());
last_beat = Instant::now();
}
thread::sleep(Duration::from_millis(80));
}
app.release_audio();
app.persist();
let _ = fs::remove_file(config::daemon_pid_path());
let _ = fs::remove_file(config::daemon_heartbeat_path());
let _ = fs::remove_file(config::daemon_stop_path());
let _ = fs::remove_file(config::daemon_control_path());
Ok(())
}
pub fn mpv_command(command: serde_json::Value) -> Result<()> {
let mut stream = ipc::connect(&config::mpv_socket_path())
.context("no running player found (start mfe-player first)")?;
writeln!(stream, "{}", json!({ "command": command }))?;
stream.flush()?;
Ok(())
}
pub fn mpv_property(name: &str) -> Option<serde_json::Value> {
let mut stream = ipc::connect(&config::mpv_socket_path()).ok()?;
ipc::set_timeouts(&stream, Duration::from_millis(500));
writeln!(
stream,
"{}",
json!({ "command": ["get_property", name], "request_id": 4242 })
)
.ok()?;
stream.flush().ok()?;
let mut reader = BufReader::new(stream);
let mut line = String::new();
loop {
line.clear();
match reader.read_line(&mut line) {
Ok(0) => return None,
Ok(_) => {
let value: serde_json::Value = serde_json::from_str(&line).ok()?;
if value.get("request_id").and_then(|id| id.as_u64()) == Some(4242) {
return value.get("data").cloned();
}
}
Err(_) => return None,
}
}
}
pub fn mpv_properties(names: &[&str]) -> std::collections::HashMap<String, serde_json::Value> {
use std::collections::HashMap;
let mut out: HashMap<String, serde_json::Value> = HashMap::new();
let Ok(mut stream) = ipc::connect(&config::mpv_socket_path()) else {
return out;
};
ipc::set_timeouts(&stream, Duration::from_millis(500));
for (index, name) in names.iter().enumerate() {
let request_id = 5000u64 + index as u64;
if writeln!(
stream,
"{}",
json!({ "command": ["get_property", name], "request_id": request_id })
)
.is_err()
{
return out;
}
}
if stream.flush().is_err() {
return out;
}
let mut reader = BufReader::new(stream);
let mut line = String::new();
while out.len() < names.len() {
line.clear();
match reader.read_line(&mut line) {
Ok(0) => break,
Ok(_) => {
let Ok(value) = serde_json::from_str::<serde_json::Value>(&line) else {
continue;
};
let Some(id) = value.get("request_id").and_then(|id| id.as_u64()) else {
continue;
};
let Some(index) = id.checked_sub(5000).map(|id| id as usize) else {
continue;
};
if index >= names.len() {
continue;
}
let Some(data) = value.get("data") else {
continue;
};
if data.is_null() {
continue;
}
out.insert(names[index].to_string(), data.clone());
}
Err(_) => break,
}
}
out
}
pub fn status_json() -> serde_json::Value {
let state = crate::state::PersistedState::load().0;
let title = state
.now_playing
.as_ref()
.map(|track| track.title.clone())
.unwrap_or_else(|| "nothing playing".to_string());
let artist = state
.now_playing
.as_ref()
.map(|track| track.artist.clone())
.unwrap_or_default();
let album = state
.now_playing
.as_ref()
.map(|track| track.album.clone())
.unwrap_or_default();
let props = mpv_properties(&["idle-active", "pause", "time-pos", "duration"]);
let connected = props.contains_key("idle-active");
let paused = props
.get("pause")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let position = props
.get("time-pos")
.and_then(|v| v.as_f64())
.unwrap_or(state.position);
let duration = props
.get("duration")
.and_then(|v| v.as_f64())
.unwrap_or_else(|| {
state
.now_playing
.as_ref()
.and_then(|track| track.duration)
.map(|d| d as f64)
.unwrap_or(0.0)
});
let mode = if is_running() {
"daemon"
} else if connected {
"session"
} else {
"stopped"
};
let playing = connected && !paused && duration > 0.0
|| connected && !paused && title != "nothing playing";
let icon = if !connected {
"■"
} else if paused {
"⏸"
} else {
"▶"
};
let spectrum = crate::spectrum::read_published().or_else(crate::spectrum::latest);
let round = |value: f32| (value * 100.0).round() / 100.0;
let spectrum_bands: Vec<f32> = spectrum
.as_ref()
.map(|snapshot| snapshot.bands.iter().copied().map(round).collect())
.unwrap_or_default();
let text = if title == "nothing playing" {
format!("{icon} idle")
} else if artist.is_empty() {
format!("{icon} {title}")
} else {
format!("{icon} {title} — {artist}")
};
json!({
"text": text,
"alt": if !connected { "stopped" } else if paused { "paused" } else { "playing" },
"tooltip": format!("{title}\n{artist}\n{album}\nmode: {mode} · repeat: {}", state.repeat),
"class": if !connected { "stopped" } else if paused { "paused" } else { "playing" },
"title": title,
"artist": artist,
"album": album,
"position": position,
"duration": duration,
"paused": paused,
"playing": playing,
"repeat": state.repeat,
"shuffle": state.shuffle,
"volume": state.volume,
"mode": mode,
"connected": connected,
"spectrum": spectrum_bands,
"pulse": spectrum.as_ref().map(|snapshot| round(snapshot.pulse)).unwrap_or(0.0),
"energy": spectrum.as_ref().map(|snapshot| round(snapshot.energy)).unwrap_or(0.0)
})
}
pub fn stop_everything() -> Result<()> {
stop_background();
let _ = mpv_command(json!(["quit"]));
let _ = fs::remove_file(config::mpv_socket_path());
let _ = fs::remove_file(config::session_lock_path());
let _ = fs::remove_file(config::daemon_control_path());
Ok(())
}
pub fn toggle_pause() -> Result<()> {
control("toggle")
}
pub fn player_alive() -> bool {
is_running() || session_owner_alive()
}
pub fn send_control(command: &str) -> bool {
if !player_alive() {
return false;
}
let path = config::daemon_control_path();
fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.and_then(|mut file| writeln!(file, "{command}"))
.is_ok()
}
pub fn drain_control() -> Vec<String> {
let path = config::daemon_control_path();
let Ok(raw) = fs::read_to_string(&path) else {
return Vec::new();
};
if raw.trim().is_empty() {
return Vec::new();
}
let _ = fs::write(&path, "");
raw.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(str::to_string)
.collect()
}
fn direct_command(command: &str) -> Option<serde_json::Value> {
let mut parts = command.split_whitespace();
let name = parts.next()?;
let arg = parts.next();
let number = || arg.and_then(|raw| raw.parse::<f64>().ok());
Some(match name {
"play" => json!(["set_property", "pause", false]),
"pause" => json!(["set_property", "pause", true]),
"toggle" => json!(["cycle", "pause"]),
"mute" => json!(["cycle", "mute"]),
"seek" => json!(["seek", number()?, "relative"]),
"volume" => json!(["set_property", "volume", number()?]),
_ => return None,
})
}
pub fn control(command: &str) -> Result<()> {
if send_control(command) {
return Ok(());
}
match direct_command(command) {
Some(command) => mpv_command(command),
None => anyhow::bail!(
"no player running — open mfe, or press Ctrl+B in it to hand playback to the daemon"
),
}
}
pub fn volume_command(arg: Option<&str>) -> Result<()> {
let Some(volume) = mpv_property("volume").and_then(|value| value.as_f64()) else {
anyhow::bail!("no running player found (start mfe-player first)");
};
let current = volume;
let (config, _) = crate::config::load_config();
let max = config.max_volume;
let step = config.volume_step;
let Some(arg) = arg else {
println!("{current:.0}");
return Ok(());
};
let target = match arg.trim() {
"" => current,
"mute" => 0.0,
"up" => current + step,
"down" => current - step,
raw => match raw.parse::<f64>() {
Ok(value) if raw.starts_with('+') || raw.starts_with('-') => current + value,
Ok(value) => value,
Err(_) => anyhow::bail!("volume expects a number, +N, -N, up, down or mute"),
},
};
let target = target.clamp(0.0, max);
if !send_control(&format!("volume {target:.0}")) {
mpv_command(json!(["set_property", "volume", target]))?;
}
println!("{target:.0}");
Ok(())
}
pub fn status_line() -> String {
let state = crate::state::PersistedState::load().0;
let title = state
.now_playing
.as_ref()
.map(|track| {
if track.artist.trim().is_empty() {
track.title.clone()
} else {
format!("{} — {}", track.title, track.artist)
}
})
.unwrap_or_else(|| "nothing playing".to_string());
let mode = if is_running() {
"background daemon"
} else if ipc::connect(&config::mpv_socket_path()).is_ok() {
"attached session"
} else {
"stopped"
};
format!("{title} [{mode}, repeat {}]", state.repeat)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn direct_commands_cover_state_without_a_queue() {
assert!(direct_command("toggle").is_some());
assert!(direct_command("pause").is_some());
assert!(direct_command("play").is_some());
assert!(direct_command("mute").is_some());
assert!(direct_command("seek +10").is_some());
assert!(direct_command("seek -30.5").is_some());
assert!(direct_command("volume 65").is_some());
}
#[test]
fn queue_commands_require_the_player_process() {
for command in ["next", "prev", "shuffle on", "repeat one", "seek +x"] {
assert!(direct_command(command).is_none(), "{command}");
}
}
}