From 1dd543ddf3d02d06c00f1000d452d4d257f3071f Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Thu, 27 May 2021 13:22:00 +1200 Subject: [PATCH] Simplify the notifier --- Cargo.lock | 4 +- asus-notify/Cargo.toml | 6 +-- asus-notify/src/main.rs | 95 ++++++++++++++++++++++------------------- daemon/src/config.rs | 2 +- 4 files changed, 56 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0320c0a6..bc7b5ad9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,10 +31,10 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "asus-notify" -version = "3.0.0" +version = "3.0.1" dependencies = [ - "daemon", "notify-rust", + "rog_aura", "rog_dbus", "rog_profiles", "rog_types", diff --git a/asus-notify/Cargo.toml b/asus-notify/Cargo.toml index e361dfa8..251708a2 100644 --- a/asus-notify/Cargo.toml +++ b/asus-notify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "asus-notify" -version = "3.0.0" +version = "3.0.1" authors = ["Luke D Jones "] edition = "2018" @@ -10,9 +10,9 @@ edition = "2018" # serialisation serde_json = "^1.0" rog_dbus = { path = "../rog-dbus" } -rog_profiles = { path = "../rog-profiles" } +rog_aura = { path = "../rog-aura" } rog_types = { path = "../rog-types" } -daemon = { path = "../daemon" } +rog_profiles = { path = "../rog-profiles" } [dependencies.notify-rust] version = "^4.3" diff --git a/asus-notify/src/main.rs b/asus-notify/src/main.rs index 574ce467..dc6cade6 100644 --- a/asus-notify/src/main.rs +++ b/asus-notify/src/main.rs @@ -1,22 +1,33 @@ use notify_rust::{Hint, Notification, NotificationHandle}; +use rog_aura::AuraEffect; use rog_dbus::{DbusProxies, Signals}; use rog_profiles::profiles::{FanLevel, Profile}; +use rog_types::gfx_vendors::GfxVendors; use std::error::Error; use std::thread::sleep; use std::time::Duration; +const NOTIF_HEADER: &str = "ROG Control"; + +macro_rules! notify { + ($notifier:ident, $last_notif:ident, $data:expr) => { + if let Some(notif) = $last_notif.take() { + notif.close(); + } + if let Ok(x) = $notifier($data) { + $last_notif = Some(x); + } + }; +} + fn main() -> Result<(), Box> { println!("asus-notify version {}", env!("CARGO_PKG_VERSION")); - println!(" daemon version {}", daemon::VERSION); println!(" rog-dbus version {}", rog_dbus::VERSION); let (proxies, conn) = DbusProxies::new()?; let signals = Signals::new(&proxies)?; - let mut last_profile_notif: Option = None; - let mut last_led_notif: Option = None; - let mut last_gfx_notif: Option = None; - let mut last_chrg_notif: Option = None; + let mut last_notification: Option = None; let recv = proxies.setup_recv(conn); let mut err_count = 0; @@ -36,42 +47,17 @@ fn main() -> Result<(), Box> { } err_count = 0; - if let Ok(vendor) = signals.gfx_vendor.try_recv() { - if let Some(notif) = last_gfx_notif.take() { - notif.close(); - } - let x = do_notif(&format!( - "Graphics mode changed to {}", - <&str>::from(vendor) - ))?; - last_gfx_notif = Some(x); + if let Ok(data) = signals.led_mode.try_recv() { + notify!(do_led_notif, last_notification, &data); } - - if let Ok(limit) = signals.charge.try_recv() { - if let Some(notif) = last_chrg_notif.take() { - notif.close(); - } - let x = do_notif(&format!("Battery charge limit changed to {}", limit))?; - last_chrg_notif = Some(x); + if let Ok(data) = signals.profile.try_recv() { + notify!(do_thermal_notif, last_notification, &data); } - - if let Ok(profile) = signals.profile.try_recv() { - if let Some(notif) = last_profile_notif.take() { - notif.close(); - } - let x = do_thermal_notif(&profile)?; - last_profile_notif = Some(x); + if let Ok(data) = signals.charge.try_recv() { + notify!(do_charge_notif, last_notification, &data); } - - if let Ok(ledmode) = signals.led_mode.try_recv() { - if let Some(notif) = last_led_notif.take() { - notif.close(); - } - let x = do_notif(&format!( - "Keyboard LED mode changed to {}", - ledmode.mode_name() - ))?; - last_led_notif = Some(x); + if let Ok(data) = signals.gfx_vendor.try_recv() { + notify!(do_gfx_notif, last_notification, &data); } } } @@ -100,11 +86,30 @@ fn do_thermal_notif(profile: &Profile) -> Result Result> { - let x = Notification::new() - .summary("ASUS ROG") - .body(body) - .timeout(2000) - .show()?; - Ok(x) +macro_rules! base_notification { + ($body:expr) => { + Notification::new() + .summary(NOTIF_HEADER) + .body($body) + .timeout(2000) + .show() + }; +} + +fn do_led_notif(ledmode: &AuraEffect) -> Result { + base_notification!(&format!( + "Keyboard LED mode changed to {}", + ledmode.mode_name() + )) +} + +fn do_charge_notif(limit: &u8) -> Result { + base_notification!(&format!("Battery charge limit changed to {}", limit)) +} + +fn do_gfx_notif(vendor: &GfxVendors) -> Result { + base_notification!(&format!( + "Graphics mode changed to {}", + <&str>::from(vendor) + )) } diff --git a/daemon/src/config.rs b/daemon/src/config.rs index 8b34693d..c5ccbf2a 100644 --- a/daemon/src/config.rs +++ b/daemon/src/config.rs @@ -59,7 +59,7 @@ impl Default for Config { "silent".into(), 0, 100, - true, + false, FanLevel::Silent, "".to_string(), ),