diff --git a/Cargo.lock b/Cargo.lock index 0f1e35f..8c54466 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,13 +83,68 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + +[[package]] +name = "crossbeam-channel" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96bf8df95e795db1a4aca2957ad884a2df35413b24bbeb3114422f3cc21498e8" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset 0.7.1", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "422f23e724af1240ec469ea1e834d87a4b59ce2efe2c6a96256b0c47e2fd86aa" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + [[package]] name = "field-offset" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e1c54951450cbd39f3dbcf1005ac413b49487dabf18a720ad2383eccfeffb92" dependencies = [ - "memoffset", + "memoffset 0.6.5", "rustc_version", ] @@ -350,6 +405,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "libappindicator" version = "0.7.1" @@ -408,6 +472,34 @@ dependencies = [ "autocfg", ] +[[package]] +name = "memoffset" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +dependencies = [ + "autocfg", +] + +[[package]] +name = "ntapi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc51db7b362b205941f71232e56c625156eb9a929f8cf74a428fd5bc094a4afc" +dependencies = [ + "winapi", +] + +[[package]] +name = "num_cpus" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.16.0" @@ -446,6 +538,7 @@ dependencies = [ "glib", "gtk", "libappindicator", + "sysinfo", ] [[package]] @@ -529,6 +622,29 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e060280438193c554f654141c9ea9417886713b7acd75974c85b18a69a88e0b" +dependencies = [ + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "rustc_version" version = "0.3.3" @@ -538,6 +654,12 @@ dependencies = [ "semver", ] +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "semver" version = "0.11.0" @@ -588,6 +710,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sysinfo" +version = "0.26.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c375d5fd899e32847b8566e10598d6e9f1d9b55ec6de3cdf9e7da4bdc51371bc" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "winapi", +] + [[package]] name = "system-deps" version = "6.0.3" diff --git a/Cargo.toml b/Cargo.toml index cfe4fab..780e668 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" libappindicator = "0.7" gtk = "0.15" glib = "0.15" +sysinfo = "0.26" diff --git a/src/main.rs b/src/main.rs index 8ff4c40..5e71019 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,25 @@ pub use err::*; use libappindicator::*; use gtk::prelude::*; use pack::*; +use std::cell::RefCell; use std::time::{Duration, Instant}; +use sysinfo::*; + +thread_local! { + static SYSTEM: RefCell = RefCell::new(System::new_all()); +} + +fn update_frame_time() -> Duration { + return SYSTEM.with(|system| { + let mut system = system.borrow_mut(); + system.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage()); + let num_cpus = system.cpus().len(); + let load: f32 = system.cpus().iter() + .map(|cpu| cpu.cpu_usage() / num_cpus as f32) + .sum(); + return Duration::from_millis(10 + 2 * load as u64); // min 10 (~100Hz), max 210 (~5Hz) + }) +} fn init_indicator(icon_pack: &IconPack) -> AppIndicator { let mut indicator = AppIndicator::new("parrotd", ""); @@ -32,9 +50,19 @@ fn main() { // Animation task let mut last_update = Instant::now(); - glib::timeout_add_local(Duration::from_millis(1000 / 60) /* maximum 60Hz */, move || { + let mut last_update_frame_time = Instant::now(); + let mut frame_time = update_frame_time(); + glib::timeout_add_local(Duration::from_millis(1) /* we need granularity */, move || { let now = Instant::now(); - if now - last_update >= Duration::from_millis(100) { + + if now - last_update_frame_time >= Duration::from_millis(100) { + // Update frame time + frame_time = update_frame_time(); + last_update_frame_time = now; + } + + if now - last_update >= frame_time { + // Update animation indicator.set_icon(icon_pack.next_icon_path()); last_update = now; }