From d4b2d2f403037af43c6251aa4412917019ba60c4 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Mon, 6 Jun 2022 13:56:33 +1200 Subject: [PATCH] Use smol async for asus-notify --- Cargo.lock | 98 +++++++++++++++++++++++++- asus-notify/Cargo.toml | 2 +- asus-notify/src/main.rs | 150 ++++++++++++++++++---------------------- 3 files changed, 166 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df361139..e9b38cf9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,13 +21,13 @@ dependencies = [ name = "asus-notify" version = "3.0.2" dependencies = [ - "futures", "notify-rust", "rog_aura", "rog_dbus", "rog_profiles", "rog_supported", "serde_json", + "smol", "zbus", ] @@ -87,6 +87,17 @@ dependencies = [ "slab", ] +[[package]] +name = "async-fs" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b3ca4f8ff117c37c278a2f7415ce9be55560b846b5bc4412aaa5d29c1c3dae2" +dependencies = [ + "async-lock", + "blocking", + "futures-lite", +] + [[package]] name = "async-io" version = "1.7.0" @@ -115,6 +126,34 @@ dependencies = [ "event-listener", ] +[[package]] +name = "async-net" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5373304df79b9b4395068fb080369ec7178608827306ce4d081cba51cac551df" +dependencies = [ + "async-io", + "blocking", + "futures-lite", +] + +[[package]] +name = "async-process" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf2c06e30a24e8c78a3987d07f0930edf76ef35e027e7bdb063fccafdad1f60c" +dependencies = [ + "async-io", + "blocking", + "cfg-if", + "event-listener", + "futures-lite", + "libc", + "once_cell", + "signal-hook", + "winapi", +] + [[package]] name = "async-recursion" version = "0.3.2" @@ -143,6 +182,12 @@ dependencies = [ "syn", ] +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + [[package]] name = "atty" version = "0.2.14" @@ -178,6 +223,20 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" +[[package]] +name = "blocking" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6ccb65d468978a086b69884437ded69a90faab3bbe6e67f242173ea728acccc" +dependencies = [ + "async-channel", + "async-task", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -1156,6 +1215,25 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" +[[package]] +name = "signal-hook" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.6" @@ -1168,6 +1246,24 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" +[[package]] +name = "smol" +version = "1.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cf3b5351f3e783c1d79ab5fc604eeed8b8ae9abd36b166e8b87a089efd85e4" +dependencies = [ + "async-channel", + "async-executor", + "async-fs", + "async-io", + "async-lock", + "async-net", + "async-process", + "blocking", + "futures-lite", + "once_cell", +] + [[package]] name = "socket2" version = "0.4.4" diff --git a/asus-notify/Cargo.toml b/asus-notify/Cargo.toml index 0c507ac9..8ab60c58 100644 --- a/asus-notify/Cargo.toml +++ b/asus-notify/Cargo.toml @@ -14,7 +14,7 @@ rog_dbus = { path = "../rog-dbus" } rog_aura = { path = "../rog-aura" } rog_supported = { path = "../rog-supported" } rog_profiles = { path = "../rog-profiles" } -futures = { version = "0.3.19", features = ["executor", "thread-pool"] } +smol = "^1.2" [dependencies.notify-rust] version = "^4.3" diff --git a/asus-notify/src/main.rs b/asus-notify/src/main.rs index 65b1c214..ee575d5e 100644 --- a/asus-notify/src/main.rs +++ b/asus-notify/src/main.rs @@ -1,4 +1,3 @@ -use futures::{executor::ThreadPool, StreamExt}; use notify_rust::{Hint, Notification, NotificationHandle}; use rog_aura::AuraEffect; use rog_dbus::{ @@ -6,11 +5,12 @@ use rog_dbus::{ zbus_rogbios::RogBiosProxy, }; use rog_profiles::Profile; +use smol::{future, Executor}; use std::{ error::Error, - future, sync::{Arc, Mutex}, }; +use zbus::export::futures_util::StreamExt; const NOTIF_HEADER: &str = "ROG Control"; @@ -43,102 +43,88 @@ fn main() -> Result<(), Box> { let last_notification: SharedHandle = Arc::new(Mutex::new(None)); - let thread_pool = ThreadPool::new()?; + let executor = Executor::new(); // BIOS notif let x = last_notification.clone(); - thread_pool.spawn_ok(async move { - let conn = zbus::Connection::system().await.unwrap(); - let proxy = RogBiosProxy::new(&conn).await.unwrap(); - if let Ok(p) = proxy.receive_notify_post_boot_sound().await { - p.for_each(|e| { - if let Ok(out) = e.args() { - if let Ok(ref mut lock) = x.try_lock() { - notify!(do_post_sound_notif, lock, &out.sound()); + executor + .spawn(async move { + let conn = zbus::Connection::system().await.unwrap(); + let proxy = RogBiosProxy::new(&conn).await.unwrap(); + if let Ok(p) = proxy.receive_notify_post_boot_sound().await { + p.for_each(|e| { + if let Ok(out) = e.args() { + if let Ok(ref mut lock) = x.try_lock() { + notify!(do_post_sound_notif, lock, &out.sound()); + } } - } - future::ready(()) - }) - .await; - }; - }); + future::ready(()) + }) + .await; + }; + }) + .detach(); // Charge notif let x = last_notification.clone(); - thread_pool.spawn_ok(async move { - let conn = zbus::Connection::system().await.unwrap(); - let proxy = ChargeProxy::new(&conn).await.unwrap(); - if let Ok(p) = proxy.receive_notify_charge().await { - p.for_each(|e| { - if let Ok(out) = e.args() { - if let Ok(ref mut lock) = x.try_lock() { - notify!(do_charge_notif, lock, &out.limit); + executor + .spawn(async move { + let conn = zbus::Connection::system().await.unwrap(); + let proxy = ChargeProxy::new(&conn).await.unwrap(); + if let Ok(p) = proxy.receive_notify_charge().await { + p.for_each(|e| { + if let Ok(out) = e.args() { + if let Ok(ref mut lock) = x.try_lock() { + notify!(do_charge_notif, lock, &out.limit); + } } - } - future::ready(()) - }) - .await; - }; - }); + future::ready(()) + }) + .await; + }; + }) + .detach(); // Profile notif let x = last_notification.clone(); - thread_pool.spawn_ok(async move { - let conn = zbus::Connection::system().await.unwrap(); - let proxy = ProfileProxy::new(&conn).await.unwrap(); - if let Ok(p) = proxy.receive_notify_profile().await { - p.for_each(|e| { - if let Ok(out) = e.args() { - if let Ok(ref mut lock) = x.try_lock() { - notify!(do_thermal_notif, lock, &out.profile); + executor + .spawn(async move { + let conn = zbus::Connection::system().await.unwrap(); + let proxy = ProfileProxy::new(&conn).await.unwrap(); + if let Ok(p) = proxy.receive_notify_profile().await { + p.for_each(|e| { + if let Ok(out) = e.args() { + if let Ok(ref mut lock) = x.try_lock() { + notify!(do_thermal_notif, lock, &out.profile); + } } - } - future::ready(()) - }) - .await; - }; - }); + future::ready(()) + }) + .await; + }; + }) + .detach(); // LED notif - thread_pool.spawn_ok(async move { - let conn = zbus::Connection::system().await.unwrap(); - let proxy = LedProxy::new(&conn).await.unwrap(); - if let Ok(p) = proxy.receive_notify_led().await { - p.for_each(|e| { - if let Ok(out) = e.args() { - if let Ok(ref mut lock) = last_notification.try_lock() { - notify!(do_led_notif, lock, &out.data); + executor + .spawn(async move { + let conn = zbus::Connection::system().await.unwrap(); + let proxy = LedProxy::new(&conn).await.unwrap(); + if let Ok(p) = proxy.receive_notify_led().await { + p.for_each(|e| { + if let Ok(out) = e.args() { + if let Ok(ref mut lock) = last_notification.try_lock() { + notify!(do_led_notif, lock, &out.data); + } } - } - future::ready(()) - }) - .await; - }; - }); + future::ready(()) + }) + .await; + }; + }) + .detach(); loop { - std::thread::sleep(std::time::Duration::from_millis(1000)); - // if err_count < 3 { - // err_count += 1; - // println!("{}", err); - // } - // if err_count == 3 { - // err_count += 1; - // println!("Max error count reached. Spooling silently."); - // } - // sleep(Duration::from_millis(2000)); - // continue; - // } - // err_count = 0; - - // if let Ok(data) = signals.led_mode.try_recv() { - // notify!(do_led_notif, last_notification, &data); - // } - // if let Ok(data) = signals.profile.try_recv() { - // notify!(do_thermal_notif, last_notification, &data); - // } - // if let Ok(data) = signals.charge.try_recv() { - // notify!(do_charge_notif, last_notification, &data); - // } + future::block_on(executor.tick()); } }