Replace smol with async-std in music player + notification timeouts

This commit is contained in:
mars 2022-12-07 18:11:10 -07:00
parent 9660ebb4dd
commit 508abcb0cc
2 changed files with 52 additions and 27 deletions

View File

@ -9,11 +9,11 @@ path = "src/main.rs"
required-features = ["bin"] required-features = ["bin"]
[dependencies] [dependencies]
async-std = { version = "1.12", optional = true, features = ["attributes"] }
canary-magpie = { path = "../magpie", optional = true, features = ["async"] } canary-magpie = { path = "../magpie", optional = true, features = ["async"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
smol = { version = "1.2", optional = true }
zbus = { version = "3.5", optional = true } zbus = { version = "3.5", optional = true }
[features] [features]
bin = ["dep:canary-magpie", "dep:smol", "dep:zbus"] bin = ["dep:async-std", "dep:canary-magpie", "dep:zbus"]

View File

@ -2,16 +2,17 @@ use std::collections::HashMap;
use std::future::pending; use std::future::pending;
use std::path::PathBuf; use std::path::PathBuf;
use async_std::channel::{unbounded, Sender};
use async_std::os::unix::net::UnixStream;
use canary_magpie::protocol::*; use canary_magpie::protocol::*;
use canary_notifications::Contents; use canary_notifications::Contents;
use smol::net::unix::UnixStream;
use zbus::{dbus_interface, zvariant::Value, ConnectionBuilder, SignalContext}; use zbus::{dbus_interface, zvariant::Value, ConnectionBuilder, SignalContext};
pub type MagpieClient = ClientMessenger<UnixStream>; pub type MagpieClient = ClientMessenger<UnixStream>;
pub struct Notifications { pub struct Notifications {
module_path: PathBuf, module_path: PathBuf,
magpie: MagpieClient, magpie_sender: Sender<MagpieServerMsg>,
next_id: u32, next_id: u32,
} }
@ -45,11 +46,17 @@ impl Notifications {
hints: HashMap<String, Value<'_>>, hints: HashMap<String, Value<'_>>,
timeout: i32, timeout: i32,
) -> u32 { ) -> u32 {
let timeout = match timeout {
-1 => Some(5000), // default timeout
0 => None,
t => Some(t),
};
let contents = Contents { let contents = Contents {
app_name: Some(app_name).filter(|s| !s.is_empty()), app_name: Some(app_name).filter(|s| !s.is_empty()),
summary, summary,
body: Some(body).filter(|s| !s.is_empty()), body: Some(body).filter(|s| !s.is_empty()),
timeout: Some(timeout).filter(|t| *t == 0), timeout,
}; };
let id = self.next_id; let id = self.next_id;
@ -62,8 +69,20 @@ impl Notifications {
init_msg: serde_json::to_vec(&contents).unwrap(), init_msg: serde_json::to_vec(&contents).unwrap(),
}; };
self.magpie if let Some(delay_ms) = contents.timeout.clone() {
.send_async(&MagpieServerMsg::CreatePanel(msg)) 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 .await
.unwrap(); .unwrap();
@ -84,7 +103,8 @@ impl Notifications {
) -> zbus::Result<()>; ) -> zbus::Result<()>;
} }
pub fn main() { #[async_std::main]
async fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
let module_path = args let module_path = args
.get(1) .get(1)
@ -92,27 +112,32 @@ pub fn main() {
.to_owned() .to_owned()
.into(); .into();
smol::block_on(async { let sock_path = find_socket();
let sock_path = find_socket(); let socket = UnixStream::connect(sock_path).await.unwrap();
let socket = UnixStream::connect(sock_path).await.unwrap(); let mut magpie = MagpieClient::new(socket);
let magpie = MagpieClient::new(socket); let (magpie_sender, magpie_receiver) = unbounded();
let notifications = Notifications { let notifications = Notifications {
magpie, magpie_sender,
next_id: 0, next_id: 0,
module_path, module_path,
}; };
let _ = ConnectionBuilder::session() let _ = ConnectionBuilder::session()
.unwrap() .unwrap()
.name("org.freedesktop.Notifications") .name("org.freedesktop.Notifications")
.unwrap() .unwrap()
.serve_at("/org/freedesktop/Notifications", notifications) .serve_at("/org/freedesktop/Notifications", notifications)
.unwrap() .unwrap()
.build() .build()
.await .await
.unwrap(); .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;
} }