|
@@ -2,12 +2,48 @@ mod block;
|
|
|
|
|
|
use tokio::signal::unix::{signal, SignalKind};
|
|
use tokio::signal::unix::{signal, SignalKind};
|
|
use tokio::time::{sleep, Duration};
|
|
use tokio::time::{sleep, Duration};
|
|
|
|
+use tokio::sync::mpsc;
|
|
|
|
+use tokio::task;
|
|
|
|
|
|
use itertools::Itertools;
|
|
use itertools::Itertools;
|
|
use xcb::x;
|
|
use xcb::x;
|
|
|
|
+use mpris::PlayerFinder;
|
|
|
|
|
|
use block::*;
|
|
use block::*;
|
|
|
|
|
|
|
|
+fn setup_mpris_event_listener() -> mpsc::Receiver<()> {
|
|
|
|
+ let (tx, rx) = mpsc::channel::<()>(10);
|
|
|
|
+
|
|
|
|
+ task::spawn_blocking(move || {
|
|
|
|
+ let finder = PlayerFinder::new().expect("Could not create finder");
|
|
|
|
+
|
|
|
|
+ loop {
|
|
|
|
+ let player = match finder.find_active() {
|
|
|
|
+ Ok(player) => player,
|
|
|
|
+ Err(_) => {
|
|
|
|
+ std::thread::sleep(std::time::Duration::from_secs(5));
|
|
|
|
+ continue;
|
|
|
|
+ },
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ println!("Player found: {}", player.identity());
|
|
|
|
+
|
|
|
|
+ let mut events = player.events().expect("Could not listen for events");
|
|
|
|
+
|
|
|
|
+ loop {
|
|
|
|
+ if let Some(_event) = events.next() {
|
|
|
|
+ tx.blocking_send(()).expect("Can't send");
|
|
|
|
+ } else {
|
|
|
|
+ println!("Player lost");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ rx
|
|
|
|
+}
|
|
|
|
+
|
|
#[tokio::main]
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let (conn, screen_num) = xcb::Connection::connect(None)?;
|
|
let (conn, screen_num) = xcb::Connection::connect(None)?;
|
|
@@ -27,6 +63,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
];
|
|
];
|
|
|
|
|
|
let mut sigusr1 = signal(SignalKind::user_defined1())?;
|
|
let mut sigusr1 = signal(SignalKind::user_defined1())?;
|
|
|
|
+ let mut mpris_rx = setup_mpris_event_listener();
|
|
|
|
|
|
loop {
|
|
loop {
|
|
let status_bar: String = blocks
|
|
let status_bar: String = blocks
|
|
@@ -50,6 +87,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
tokio::select! {
|
|
tokio::select! {
|
|
_ = sleep(Duration::from_secs(1)) => {},
|
|
_ = sleep(Duration::from_secs(1)) => {},
|
|
_ = sigusr1.recv() => {},
|
|
_ = sigusr1.recv() => {},
|
|
|
|
+ _ = mpris_rx.recv() => {},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|