From 508abcb0cc86855ca6fbae475bce777afacaea0b Mon Sep 17 00:00:00 2001 From: mars Date: Wed, 7 Dec 2022 18:11:10 -0700 Subject: [PATCH] Replace smol with async-std in music player + notification timeouts --- apps/notifications/Cargo.toml | 4 +- apps/notifications/src/main.rs | 75 ++++++++++++++++++++++------------ 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/apps/notifications/Cargo.toml b/apps/notifications/Cargo.toml index 615470b..05b4425 100644 --- a/apps/notifications/Cargo.toml +++ b/apps/notifications/Cargo.toml @@ -9,11 +9,11 @@ path = "src/main.rs" required-features = ["bin"] [dependencies] +async-std = { version = "1.12", optional = true, features = ["attributes"] } canary-magpie = { path = "../magpie", optional = true, features = ["async"] } serde = { version = "1", features = ["derive"] } serde_json = "1" -smol = { version = "1.2", optional = true } zbus = { version = "3.5", optional = true } [features] -bin = ["dep:canary-magpie", "dep:smol", "dep:zbus"] +bin = ["dep:async-std", "dep:canary-magpie", "dep:zbus"] diff --git a/apps/notifications/src/main.rs b/apps/notifications/src/main.rs index 6a7122a..8bfc0f9 100644 --- a/apps/notifications/src/main.rs +++ b/apps/notifications/src/main.rs @@ -2,16 +2,17 @@ use std::collections::HashMap; use std::future::pending; use std::path::PathBuf; +use async_std::channel::{unbounded, Sender}; +use async_std::os::unix::net::UnixStream; use canary_magpie::protocol::*; use canary_notifications::Contents; -use smol::net::unix::UnixStream; use zbus::{dbus_interface, zvariant::Value, ConnectionBuilder, SignalContext}; pub type MagpieClient = ClientMessenger; pub struct Notifications { module_path: PathBuf, - magpie: MagpieClient, + magpie_sender: Sender, next_id: u32, } @@ -45,11 +46,17 @@ impl Notifications { hints: HashMap>, timeout: i32, ) -> u32 { + let timeout = match timeout { + -1 => Some(5000), // default timeout + 0 => None, + t => Some(t), + }; + let contents = Contents { app_name: Some(app_name).filter(|s| !s.is_empty()), summary, body: Some(body).filter(|s| !s.is_empty()), - timeout: Some(timeout).filter(|t| *t == 0), + timeout, }; let id = self.next_id; @@ -62,8 +69,20 @@ impl Notifications { init_msg: serde_json::to_vec(&contents).unwrap(), }; - self.magpie - .send_async(&MagpieServerMsg::CreatePanel(msg)) + if let Some(delay_ms) = contents.timeout.clone() { + let delay = std::time::Duration::from_millis(delay_ms as _); + let magpie_sender = self.magpie_sender.to_owned(); + async_std::task::spawn(async move { + async_std::task::sleep(delay).await; + magpie_sender + .send(MagpieServerMsg::ClosePanel(ClosePanel { id })) + .await + .unwrap(); + }); + } + + self.magpie_sender + .send(MagpieServerMsg::CreatePanel(msg)) .await .unwrap(); @@ -84,7 +103,8 @@ impl Notifications { ) -> zbus::Result<()>; } -pub fn main() { +#[async_std::main] +async fn main() { let args: Vec = std::env::args().collect(); let module_path = args .get(1) @@ -92,27 +112,32 @@ pub fn main() { .to_owned() .into(); - smol::block_on(async { - let sock_path = find_socket(); - let socket = UnixStream::connect(sock_path).await.unwrap(); - let magpie = MagpieClient::new(socket); + let sock_path = find_socket(); + let socket = UnixStream::connect(sock_path).await.unwrap(); + let mut magpie = MagpieClient::new(socket); + let (magpie_sender, magpie_receiver) = unbounded(); - let notifications = Notifications { - magpie, - next_id: 0, - module_path, - }; + let notifications = Notifications { + magpie_sender, + next_id: 0, + module_path, + }; - let _ = ConnectionBuilder::session() - .unwrap() - .name("org.freedesktop.Notifications") - .unwrap() - .serve_at("/org/freedesktop/Notifications", notifications) - .unwrap() - .build() - .await - .unwrap(); + let _ = ConnectionBuilder::session() + .unwrap() + .name("org.freedesktop.Notifications") + .unwrap() + .serve_at("/org/freedesktop/Notifications", notifications) + .unwrap() + .build() + .await + .unwrap(); - pending::<()>().await; + async_std::task::spawn(async move { + while let Ok(msg) = magpie_receiver.recv().await { + magpie.send_async(&msg).await.unwrap(); + } }); + + pending::<()>().await; }