Skip to content

Commit

Permalink
Added list-players subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-kast committed Mar 7, 2021
1 parent 8f39ab5 commit d760075
Show file tree
Hide file tree
Showing 10 changed files with 809 additions and 674 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "empress"
version = "1.0.3"
version = "1.1.0"
authors = ["rookie1024 <[email protected]>"]
edition = "2018"
description = "A D-Bus MPRIS daemon for controlling media players."
Expand Down
34 changes: 28 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::{sync::Arc, time::Duration};

use anyhow::{Context, Error};
use dbus::nonblock::{Proxy, SyncConnection};
use dbus::{
arg::ReadAll,
nonblock::{Proxy, SyncConnection},
strings::Member,
};
use dbus_tokio::connection;
use tokio::{select, sync::oneshot, task};

use crate::{MethodId, Result, INTERFACE_NAME, SERVER_NAME, SERVER_PATH};
use crate::{ClientCommand, ExtraMethodId, Result, INTERFACE_NAME, SERVER_NAME, SERVER_PATH};

pub(super) async fn run(id: MethodId) -> Result {
pub(super) async fn run(cmd: ClientCommand) -> Result {
let (res, conn) = connection::new_session_sync().context("failed to connect to D-Bus")?;
let (close_tx, close_rx) = oneshot::channel();

Expand All @@ -20,7 +24,21 @@ pub(super) async fn run(id: MethodId) -> Result {
let run = async move {
let proxy = Proxy::new(&*SERVER_NAME, &*SERVER_PATH, Duration::from_secs(2), conn);

try_send(&proxy, id).await?;
match cmd {
ClientCommand::Extra(e) => match e {
ExtraMethodId::ListPlayers => {
let (players,): (Vec<(String, String)>,) =
try_send(&proxy, e.to_string()).await?;

for (player, status) in players {
println!("{}\t{}", player, status);
}
},
},
ClientCommand::Control(c) => {
try_send(&proxy, c.to_string()).await?;
},
}

Ok(())
};
Expand All @@ -33,14 +51,18 @@ pub(super) async fn run(id: MethodId) -> Result {
)
}

async fn try_send(proxy: &Proxy<'_, Arc<SyncConnection>>, id: MethodId) -> Result {
async fn try_send<T: ReadAll + 'static, M: for<'a> Into<Member<'a>>>(
proxy: &Proxy<'_, Arc<SyncConnection>>,
method: M,
) -> Result<T> {
const MAX_TRIES: usize = 5;

let method = method.into();
let mut i = 0;

loop {
match proxy
.method_call(&*INTERFACE_NAME, id.to_string(), ())
.method_call(&*INTERFACE_NAME, &method, ())
.await
.context("failed to contact empress server")
{
Expand Down
61 changes: 49 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
//! README](https://github.com/ray-kast/empress/blob/master/README.md) for more
//! details.
use std::{
fmt,
fmt::{Display, Formatter},
};

use anyhow::{Context, Error};
use lazy_static::lazy_static;
use structopt::StructOpt;
Expand All @@ -15,17 +20,33 @@ mod server;

type Result<T = (), E = Error> = std::result::Result<T, E>;

#[cfg(debug_assertions)]
lazy_static! {
static ref NAME_PREFIX: String = format!("net.ryan_s.debug.{}", *API_IDENT);
static ref PATH_PREFIX: String = format!("/net/ryan_s/debug/{}", *API_IDENT);
}

#[cfg(not(debug_assertions))]
lazy_static! {
static ref API_IDENT: String = format!("Empress{}", env!("CARGO_PKG_VERSION_MAJOR"));
static ref NAME_PREFIX: String = format!("net.ryan_s.{}", *API_IDENT);
static ref PATH_PREFIX: String = format!("/net/ryan_s/{}", *API_IDENT);
}

lazy_static! {
static ref API_IDENT: String = format!("Empress{}", env!("CARGO_PKG_VERSION_MAJOR"));
static ref INTERFACE_NAME: String = format!("{}.Daemon", *NAME_PREFIX);
static ref SERVER_NAME: String = NAME_PREFIX.clone();
static ref SERVER_PATH: String = format!("{}/Daemon", *PATH_PREFIX);
}

#[derive(Debug, Clone, Copy, StructOpt)]
enum MethodId {
enum ExtraMethodId {
/// List the players currently tracked by the daemon
ListPlayers,
}

#[derive(Debug, Clone, Copy, StructOpt)]
enum ControlMethodId {
/// Skip one track forwards
Next,
/// Skip one track backwards
Expand All @@ -40,17 +61,25 @@ enum MethodId {
Play,
}

const METHOD_IDS: &[MethodId] = &[
MethodId::Next,
MethodId::Previous,
MethodId::Pause,
MethodId::PlayPause,
MethodId::Stop,
MethodId::Play,
const CONTROL_METHOD_IDS: &[ControlMethodId] = &[
ControlMethodId::Next,
ControlMethodId::Previous,
ControlMethodId::Pause,
ControlMethodId::PlayPause,
ControlMethodId::Stop,
ControlMethodId::Play,
];

impl std::fmt::Display for MethodId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl Display for ExtraMethodId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(match self {
Self::ListPlayers => "ListPlayers",
})
}
}

impl Display for ControlMethodId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(match self {
Self::Next => "Next",
Self::Previous => "Previous",
Expand All @@ -67,7 +96,15 @@ enum Opts {
/// Launch a D-Bus service abstracting MPRIS players
Server,
#[structopt(flatten)]
Client(MethodId),
Client(ClientCommand),
}

#[derive(StructOpt)]
enum ClientCommand {
#[structopt(flatten)]
Extra(ExtraMethodId),
#[structopt(flatten)]
Control(ControlMethodId),
}

fn main() {
Expand Down
Loading

0 comments on commit d760075

Please sign in to comment.