summaryrefslogtreecommitdiffstats
path: root/src/bin/play.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-07-19 17:14:42 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-07-19 17:14:42 -0500
commite12cd9dfb9f20f432edd69414b4ff83325226995 (patch)
tree986e640f3c6355215d36958476be605819da1c22 /src/bin/play.rs
parent28bcf144224838e9e93d5926dcdbb20a4d45a8bf (diff)
feat: working
Diffstat (limited to 'src/bin/play.rs')
-rw-r--r--src/bin/play.rs129
1 files changed, 56 insertions, 73 deletions
diff --git a/src/bin/play.rs b/src/bin/play.rs
index 0863b7a..bf4f489 100644
--- a/src/bin/play.rs
+++ b/src/bin/play.rs
@@ -1,96 +1,79 @@
use std::sync::Arc;
-use futures_util::StreamExt;
use i3blocks::{
- dbus::{
- player::{PlaybackStatus, PlayerProxy},
- playerctld::PlayerctldProxy,
- },
- i3bar::Block,
- Error,
-};
-use tokio::{
- sync::{mpsc::Sender, Mutex},
- task::JoinSet,
+ dbus::player::{PlaybackStatus, PlayerProxy},
+ i3bar::{Block, Click},
+ Button, Component, Error, Update,
};
+use tokio::sync::{mpsc::Sender, Mutex};
use zbus::Connection;
#[tokio::main]
async fn main() -> Result<(), main_error::MainError> {
- let mut join_set = JoinSet::new();
+ i3blocks::run::<Play>(Default::default())
+ .await
+ .map_err(Into::into)
+}
- let (tx_player, mut rx_player) = tokio::sync::mpsc::channel(128);
- let (tx_value, mut rx_value) = tokio::sync::mpsc::channel(128);
+pub struct Play;
- let conn = Connection::session().await?;
- let proxy = PlayerctldProxy::builder(&conn).build().await?;
+impl Component for Play {
+ const NAME: &'static str = "play";
+ type Updater = Self;
+ type Colorer = ();
+ type Handler = Self;
+}
- tokio::spawn(player_listener(tx_player, proxy));
+impl Update for Play {
+ type Value = PlaybackStatus;
- let status: Arc<Mutex<Block>> = Default::default();
+ async fn listen(tx: Sender<Self::Value>, proxy: PlayerProxy<'_>) -> Result<(), Error> {
+ use futures_util::StreamExt;
- loop {
- tokio::select! {
- Some(name) = rx_player.recv() => {
- println!("name: {name:?}");
- join_set.shutdown().await;
- if name.is_empty() {
- let mut status = status.lock().await;
- status.full_text = Default::default();
- } else {
- let proxy = PlayerProxy::builder(&conn)
- .destination(name)?
- .build()
- .await?;
- join_set.spawn(value_listener(tx_value.clone(), proxy));
- }
- }
- Some(value) = rx_value.recv() => {
- println!("value: {value:?}");
- let mut status = status.lock().await;
- let (color, background) = value.into();
- status.color = color;
- status.background = background;
- status.full_text = match value {
- PlaybackStatus::Playing => " 󰏤 ",
- _ => " 󰐊 ",
- }.into();
+ tx.send(proxy.playback_status().await?).await?;
+ let mut stream = proxy.receive_playback_status_changed().await;
+ while let Some(signal) = stream.next().await {
+ if let Ok(value) = signal.get().await {
+ tx.send(value).await?;
}
}
-
- let s = status.lock().await;
- s.write_stdout()?;
+ Ok(())
}
-}
-pub async fn value_listener(
- tx: Sender<PlaybackStatus>,
- proxy: PlayerProxy<'_>,
-) -> Result<(), Error> {
- tx.send(proxy.playback_status().await?).await?;
- let mut stream = proxy.receive_playback_status_changed().await;
- while let Some(signal) = stream.next().await {
- if let Ok(value) = signal.get().await {
- tx.send(value).await?;
- }
+ async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<bool, Error> {
+ let black = std::env::var("BASE16_COLOR_00_HEX").ok();
+ let cyan = std::env::var("BASE16_COLOR_0C_HEX").ok();
+ let yellow = std::env::var("BASE16_COLOR_0A_HEX").ok();
+
+ let (full_text, color, background) = match value {
+ PlaybackStatus::Playing => (" 󰏤 ", black.clone(), cyan.clone()),
+ PlaybackStatus::Paused => (" 󰐊 ", black.clone(), yellow.clone()),
+ PlaybackStatus::Stopped => (" 󰐊 ", None, None),
+ };
+
+ let mut block = block.lock().await;
+ block.full_text = full_text.into();
+ block.color = color;
+ block.background = background;
+ Ok(true)
}
- Ok(())
}
-async fn player_listener(tx: Sender<String>, proxy: PlayerctldProxy<'_>) -> Result<(), Error> {
- tx.send(
- proxy
- .player_names()
- .await?
- .into_iter()
- .next()
- .unwrap_or_default(),
- )
- .await?;
+impl Button for Play {
+ async fn handle(conn: Connection, click: Click) -> Result<(), Error> {
+ let Some(name) = click.instance else {
+ return Ok(());
+ };
+
+ if click.button == 1 {
+ PlayerProxy::builder(&conn)
+ .destination(name)?
+ .build()
+ .await?
+ .play_pause()
+ .await?
+ }
- let mut stream = proxy.receive_active_player_change_begin().await?;
- while let Some(signal) = stream.next().await {
- tx.send(signal.args()?.name.to_owned()).await?;
+ Ok(())
}
- Ok(())
}