use std::thread;
use std::time::Duration;
use mpris::{PlaybackStatus, Player, PlayerFinder};
use crate::config::Config;
const INIT_WAIT_TIME: Duration = Duration::from_secs(1);
const BUS_NAME_PREFIX: &str = "org.mpris.MediaPlayer2.";
pub fn is_active(player: &Player) -> bool {
if !player.is_running() {
return false;
}
match player.get_playback_status() {
Ok(PlaybackStatus::Playing) => true,
_ => false,
}
}
fn bus_name<'p>(player: &'p Player) -> &'p str {
player
.bus_name()
.as_cstr()
.to_str()
.unwrap_or("")
.trim_start_matches(BUS_NAME_PREFIX)
.split('.') .next()
.unwrap() }
fn is_whitelisted(config: &Config, player: &Player) -> bool {
if let Some(ref whitelist) = config.player_whitelist {
if !whitelist.is_empty() {
return whitelist.contains(player.identity()) || whitelist.contains(bus_name(player));
}
}
true
}
pub fn wait_for_player<'f>(config: &Config, finder: &'f PlayerFinder) -> Player<'f> {
loop {
let players = match finder.find_all() {
Ok(players) => players,
_ => {
thread::sleep(INIT_WAIT_TIME);
continue;
}
};
for player in players {
if is_active(&player) && is_whitelisted(config, &player) {
return player;
}
}
thread::sleep(INIT_WAIT_TIME);
}
}