diff --git a/Cargo.lock b/Cargo.lock index f43c6ed..e6de683 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "alphanumeric-sort" +version = "1.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77e9c9abb82613923ec78d7a461595d52491ba7240f3c64c0bbe0e6d98e0fce0" + [[package]] name = "anyhow" version = "1.0.66" @@ -83,13 +89,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 +411,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 +478,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" @@ -443,8 +541,11 @@ dependencies = [ name = "parrotd" version = "0.1.0" dependencies = [ + "alphanumeric-sort", + "glib", "gtk", "libappindicator", + "sysinfo", ] [[package]] @@ -528,6 +629,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" @@ -537,6 +661,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" @@ -587,6 +717,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 567480e..11f771c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,6 @@ edition = "2021" [dependencies] libappindicator = "0.7" gtk = "0.15" +glib = "0.15" +sysinfo = "0.26" +alphanumeric-sort = "1.4" diff --git a/res/cat/dark_cat_0.png b/res/cat/dark_cat_0.png new file mode 100644 index 0000000..814d895 Binary files /dev/null and b/res/cat/dark_cat_0.png differ diff --git a/res/cat/dark_cat_1.png b/res/cat/dark_cat_1.png new file mode 100644 index 0000000..08afd92 Binary files /dev/null and b/res/cat/dark_cat_1.png differ diff --git a/res/cat/dark_cat_2.png b/res/cat/dark_cat_2.png new file mode 100644 index 0000000..e4c124e Binary files /dev/null and b/res/cat/dark_cat_2.png differ diff --git a/res/cat/dark_cat_3.png b/res/cat/dark_cat_3.png new file mode 100644 index 0000000..68e5886 Binary files /dev/null and b/res/cat/dark_cat_3.png differ diff --git a/res/cat/dark_cat_4.png b/res/cat/dark_cat_4.png new file mode 100644 index 0000000..38bfe80 Binary files /dev/null and b/res/cat/dark_cat_4.png differ diff --git a/res/cat/light_cat_0.png b/res/cat/light_cat_0.png new file mode 100644 index 0000000..e2090ff Binary files /dev/null and b/res/cat/light_cat_0.png differ diff --git a/res/cat/light_cat_1.png b/res/cat/light_cat_1.png new file mode 100644 index 0000000..d267c16 Binary files /dev/null and b/res/cat/light_cat_1.png differ diff --git a/res/cat/light_cat_2.png b/res/cat/light_cat_2.png new file mode 100644 index 0000000..04675f9 Binary files /dev/null and b/res/cat/light_cat_2.png differ diff --git a/res/cat/light_cat_3.png b/res/cat/light_cat_3.png new file mode 100644 index 0000000..901e69c Binary files /dev/null and b/res/cat/light_cat_3.png differ diff --git a/res/cat/light_cat_4.png b/res/cat/light_cat_4.png new file mode 100644 index 0000000..93b70a9 Binary files /dev/null and b/res/cat/light_cat_4.png differ diff --git a/res/horse/dark_horse_0.png b/res/horse/dark_horse_0.png new file mode 100644 index 0000000..c82fde1 Binary files /dev/null and b/res/horse/dark_horse_0.png differ diff --git a/res/horse/dark_horse_1.png b/res/horse/dark_horse_1.png new file mode 100644 index 0000000..336c5c3 Binary files /dev/null and b/res/horse/dark_horse_1.png differ diff --git a/res/horse/dark_horse_10.png b/res/horse/dark_horse_10.png new file mode 100644 index 0000000..06a03e7 Binary files /dev/null and b/res/horse/dark_horse_10.png differ diff --git a/res/horse/dark_horse_11.png b/res/horse/dark_horse_11.png new file mode 100644 index 0000000..81eb91c Binary files /dev/null and b/res/horse/dark_horse_11.png differ diff --git a/res/horse/dark_horse_12.png b/res/horse/dark_horse_12.png new file mode 100644 index 0000000..c0eaa5b Binary files /dev/null and b/res/horse/dark_horse_12.png differ diff --git a/res/horse/dark_horse_13.png b/res/horse/dark_horse_13.png new file mode 100644 index 0000000..6476897 Binary files /dev/null and b/res/horse/dark_horse_13.png differ diff --git a/res/horse/dark_horse_2.png b/res/horse/dark_horse_2.png new file mode 100644 index 0000000..a04f4c9 Binary files /dev/null and b/res/horse/dark_horse_2.png differ diff --git a/res/horse/dark_horse_3.png b/res/horse/dark_horse_3.png new file mode 100644 index 0000000..8402d07 Binary files /dev/null and b/res/horse/dark_horse_3.png differ diff --git a/res/horse/dark_horse_4.png b/res/horse/dark_horse_4.png new file mode 100644 index 0000000..3d2311a Binary files /dev/null and b/res/horse/dark_horse_4.png differ diff --git a/res/horse/dark_horse_5.png b/res/horse/dark_horse_5.png new file mode 100644 index 0000000..57ab46d Binary files /dev/null and b/res/horse/dark_horse_5.png differ diff --git a/res/horse/dark_horse_6.png b/res/horse/dark_horse_6.png new file mode 100644 index 0000000..7581027 Binary files /dev/null and b/res/horse/dark_horse_6.png differ diff --git a/res/horse/dark_horse_7.png b/res/horse/dark_horse_7.png new file mode 100644 index 0000000..7aeabab Binary files /dev/null and b/res/horse/dark_horse_7.png differ diff --git a/res/horse/dark_horse_8.png b/res/horse/dark_horse_8.png new file mode 100644 index 0000000..dd84632 Binary files /dev/null and b/res/horse/dark_horse_8.png differ diff --git a/res/horse/dark_horse_9.png b/res/horse/dark_horse_9.png new file mode 100644 index 0000000..17df297 Binary files /dev/null and b/res/horse/dark_horse_9.png differ diff --git a/res/horse/light_horse_0.png b/res/horse/light_horse_0.png new file mode 100644 index 0000000..d12aff8 Binary files /dev/null and b/res/horse/light_horse_0.png differ diff --git a/res/horse/light_horse_1.png b/res/horse/light_horse_1.png new file mode 100644 index 0000000..ced221e Binary files /dev/null and b/res/horse/light_horse_1.png differ diff --git a/res/horse/light_horse_10.png b/res/horse/light_horse_10.png new file mode 100644 index 0000000..5727f5c Binary files /dev/null and b/res/horse/light_horse_10.png differ diff --git a/res/horse/light_horse_11.png b/res/horse/light_horse_11.png new file mode 100644 index 0000000..14e3672 Binary files /dev/null and b/res/horse/light_horse_11.png differ diff --git a/res/horse/light_horse_12.png b/res/horse/light_horse_12.png new file mode 100644 index 0000000..1405384 Binary files /dev/null and b/res/horse/light_horse_12.png differ diff --git a/res/horse/light_horse_13.png b/res/horse/light_horse_13.png new file mode 100644 index 0000000..776445d Binary files /dev/null and b/res/horse/light_horse_13.png differ diff --git a/res/horse/light_horse_2.png b/res/horse/light_horse_2.png new file mode 100644 index 0000000..a53f115 Binary files /dev/null and b/res/horse/light_horse_2.png differ diff --git a/res/horse/light_horse_3.png b/res/horse/light_horse_3.png new file mode 100644 index 0000000..10fb170 Binary files /dev/null and b/res/horse/light_horse_3.png differ diff --git a/res/horse/light_horse_4.png b/res/horse/light_horse_4.png new file mode 100644 index 0000000..42f94cf Binary files /dev/null and b/res/horse/light_horse_4.png differ diff --git a/res/horse/light_horse_5.png b/res/horse/light_horse_5.png new file mode 100644 index 0000000..13eab34 Binary files /dev/null and b/res/horse/light_horse_5.png differ diff --git a/res/horse/light_horse_6.png b/res/horse/light_horse_6.png new file mode 100644 index 0000000..c0bdc07 Binary files /dev/null and b/res/horse/light_horse_6.png differ diff --git a/res/horse/light_horse_7.png b/res/horse/light_horse_7.png new file mode 100644 index 0000000..17bf73f Binary files /dev/null and b/res/horse/light_horse_7.png differ diff --git a/res/horse/light_horse_8.png b/res/horse/light_horse_8.png new file mode 100644 index 0000000..3a0a6d0 Binary files /dev/null and b/res/horse/light_horse_8.png differ diff --git a/res/horse/light_horse_9.png b/res/horse/light_horse_9.png new file mode 100644 index 0000000..f66abf1 Binary files /dev/null and b/res/horse/light_horse_9.png differ diff --git a/src/main.rs b/src/main.rs index f3bf7e1..15aac16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,29 @@ pub use err::*; use libappindicator::*; use gtk::prelude::*; use pack::*; +use std::cell::RefCell; +use std::time::{Duration, Instant}; +use sysinfo::*; -fn main() { - gtk::init().unwrap(); - let mut icon_pack = IconPack::new("./res", "parrot", PackVariant::Dark).unwrap(); +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", ""); indicator.set_icon_theme_path(icon_pack.pack_path()); - indicator.set_icon_full(icon_pack.next_icon_path(), "parrotd"); indicator.set_status(AppIndicatorStatus::Active); let mut m = gtk::Menu::new(); let mi = gtk::CheckMenuItem::with_label("I'm a parrot"); @@ -23,5 +39,40 @@ fn main() { m.append(&mi); indicator.set_menu(&mut m); m.show_all(); + return indicator; +} + +fn main() { + gtk::init().unwrap(); + + let res_dir = std::env::var("PARROTD_RESOURCE_DIR").unwrap_or("./res".to_owned()); + let pack_name = std::env::var("PARROTD_ICON_PACK").unwrap_or("parrot".to_owned()); + let variant = PackVariant::parse( + &std::env::var("PARROTD_VARIANT").unwrap_or("dark".to_owned())).expect("Invalid variant name"); + + let mut icon_pack = IconPack::new(res_dir, &pack_name, variant).unwrap(); + let mut indicator = init_indicator(&icon_pack); + + // Animation task + let mut last_update = Instant::now(); + 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_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; + } + Continue(true) + }); + gtk::main(); } diff --git a/src/pack.rs b/src/pack.rs index c591787..7875859 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -2,6 +2,7 @@ use crate::*; use std::path::{Path, PathBuf}; #[allow(dead_code)] +#[derive(Debug)] pub enum PackVariant { Light, Dark, @@ -14,6 +15,14 @@ impl PackVariant { PackVariant::Dark => "dark", } } + + pub fn parse(s: &str) -> Option { + match s { + "light" => Some(PackVariant::Light), + "dark" => Some(PackVariant::Dark), + _ => None + } + } } #[allow(unused)] @@ -60,7 +69,7 @@ impl IconPack { icons.push(name.split_at(name.len() - ".png".len()).0.to_string()); } - icons.sort(); // Alphanumerical ordering + alphanumeric_sort::sort_str_slice(&mut icons); // Alphanumerical ordering let num_icons = icons.len();