From 777237b19b76985a156ebfadb92cdd9606370bf4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 21 Apr 2024 23:40:14 +0200 Subject: [PATCH] Update to sysinfo 0.30 --- Cargo.lock | 25 ++++++++++++++++++++--- Cargo.toml | 2 +- src/display_disk.rs | 26 +++++++++--------------- src/display_network.rs | 28 +++++++++++++------------- src/display_procs.rs | 6 +++--- src/display_sysinfo.rs | 18 ++++++++++------- src/main.rs | 45 ++++++++++++++++++++++++------------------ src/network_dialog.rs | 2 -- src/process_dialog.rs | 25 +++++++++++++++++------ 9 files changed, 105 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef9c20d..7212e1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -744,9 +744,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.28.4" +version = "0.30.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c2f3ca6693feb29a89724516f016488e9aafc7f37264f898593ee4b942f31b" +checksum = "87341a165d73787554941cd5ef55ad728011566fe714e987d1b976c15dbc3a83" dependencies = [ "cfg-if", "core-foundation-sys", @@ -754,7 +754,7 @@ dependencies = [ "ntapi", "once_cell", "rayon", - "winapi", + "windows", ] [[package]] @@ -881,6 +881,25 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index be6a531..02bd242 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ keywords = ["GUI", "process", "viewer", "gtk"] [dependencies] gtk = { version = "0.8", package = "gtk4" } -sysinfo = "0.28.2" +sysinfo = "0.30" libc = "0.2" serde = "1.0" serde_derive = "1.0" diff --git a/src/display_disk.rs b/src/display_disk.rs index 0aa1efc..9dad4fc 100644 --- a/src/display_disk.rs +++ b/src/display_disk.rs @@ -1,14 +1,11 @@ use std::cell::RefCell; use std::rc::Rc; -use std::sync::{Arc, Mutex}; use crate::utils::format_number; use gtk::glib; use gtk::prelude::*; -use sysinfo::{DiskExt, SystemExt}; - struct DiskInfo { label: gtk::Label, progress: gtk::ProgressBar, @@ -76,7 +73,7 @@ fn refresh_disks(container: >k::Box, disks: &[sysinfo::Disk], elems: &mut Vec< } } -pub fn create_disk_info(sys: &Arc>, stack: >k::Stack) { +pub fn create_disk_info(stack: >k::Stack) { let elems: Rc>> = Rc::new(RefCell::new(Vec::new())); let vertical_layout = gtk::Box::new(gtk::Orientation::Vertical, 0); let container = gtk::Box::new(gtk::Orientation::Vertical, 0); @@ -86,24 +83,19 @@ pub fn create_disk_info(sys: &Arc>, stack: >k::Stack) { .child(&container) .build(); + let disks = RefCell::new(sysinfo::Disks::new_with_refreshed_list()); + + refresh_disks(&container, &disks.borrow(), &mut elems.borrow_mut()); + let refresh_but = gtk::Button::with_label("Refresh disks"); - refresh_but.connect_clicked( - glib::clone!(@weak sys, @weak container, @strong elems => move |_| { - let mut sys = sys.lock().expect("failed to lock to refresh disks"); - sys.refresh_disks(); - refresh_disks(&container, sys.disks(), &mut elems.borrow_mut()); - }), - ); + refresh_but.connect_clicked(glib::clone!(@weak container, @strong elems => move |_| { + disks.borrow_mut().refresh_list(); + refresh_disks(&container, &disks.borrow(), &mut elems.borrow_mut()); + })); vertical_layout.append(&scroll); vertical_layout.append(&refresh_but); stack.add_titled(&vertical_layout, Some("Disks"), "Disks"); - - refresh_disks( - &container, - sys.lock().expect("failed to lock to get disks").disks(), - &mut elems.borrow_mut(), - ); } diff --git a/src/display_network.rs b/src/display_network.rs index 6639047..835a683 100644 --- a/src/display_network.rs +++ b/src/display_network.rs @@ -3,7 +3,7 @@ use crate::network_dialog::{self, NetworkDialog}; use crate::utils::{format_number, format_number_full}; use gtk::glib; use gtk::prelude::*; -use sysinfo::{NetworkExt, NetworksExt, System, SystemExt}; +use sysinfo::Networks; use std::cell::RefCell; use std::collections::HashSet; @@ -41,13 +41,12 @@ fn append_column( pub struct Network { list_store: gtk::ListStore, - pub filter_entry: gtk::SearchEntry, pub search_bar: gtk::SearchBar, dialogs: Rc>>, } impl Network { - pub fn new(stack: >k::Stack, sys: &Arc>) -> Network { + pub fn new(stack: >k::Stack, networks: &Arc>) -> Self { let tree = gtk::TreeView::builder().headers_visible(true).build(); let scroll = gtk::ScrolledWindow::builder().child(&tree).build(); let info_button = gtk::Button::builder() @@ -99,7 +98,7 @@ impl Network { // The filter model let filter_model = gtk::TreeModelFilter::new(&list_store, None); filter_model.set_visible_func( - glib::clone!(@weak filter_entry => @default-return false, move |model, iter| { + glib::clone!(@strong filter_entry => @default-return false, move |model, iter| { if !WidgetExt::is_visible(&filter_entry) { return true; } @@ -173,19 +172,19 @@ impl Network { let dialogs = Rc::new(RefCell::new(Vec::new())); - info_button.connect_clicked(glib::clone!(@weak dialogs, @weak sys => move |_| { + info_button.connect_clicked(glib::clone!(@weak dialogs, @weak networks => move |_| { let current_network = current_network.borrow(); if let Some(ref interface_name) = *current_network { create_network_dialog( &mut dialogs.borrow_mut(), interface_name, - &sys.lock().expect("failed to lock for new network dialog"), + &networks.lock().expect("failed to lock for new network dialog"), ); } })); tree.connect_row_activated( - glib::clone!(@weak sys, @weak dialogs => move |tree_view, path, _| { + glib::clone!(@weak networks, @weak dialogs => move |tree_view, path, _| { let model = tree_view.model().expect("couldn't get model"); let iter = model.iter(path).expect("couldn't get iter"); let interface_name = model.get_value(&iter, 0) @@ -194,26 +193,24 @@ impl Network { create_network_dialog( &mut dialogs.borrow_mut(), &interface_name, - &sys.lock().expect("failed to lock for new network dialog (from tree)"), + &networks.lock().expect("failed to lock for new network dialog (from tree)"), ); }), ); Network { list_store, - filter_entry, search_bar, dialogs, } } - pub fn update_networks(&mut self, sys: &System) { + pub fn update_networks(&mut self, networks: &Networks) { // first part, deactivate sorting let sorted = TreeSortableExtManual::sort_column_id(&self.list_store); self.list_store.set_unsorted(); let mut seen: HashSet = HashSet::new(); - let networks = sys.networks(); if let Some(iter) = self.list_store.iter_first() { let mut valid = true; @@ -319,15 +316,18 @@ fn create_and_fill_model( ); } -fn create_network_dialog(dialogs: &mut Vec, interface_name: &str, sys: &System) { +fn create_network_dialog( + dialogs: &mut Vec, + interface_name: &str, + networks: &Networks, +) { for dialog in dialogs.iter() { if dialog.name == interface_name { dialog.show(); return; } } - if let Some((_, data)) = sys - .networks() + if let Some((_, data)) = networks .iter() .find(|(name, _)| name.as_str() == interface_name) { diff --git a/src/display_procs.rs b/src/display_procs.rs index 86c55fe..e2223f6 100644 --- a/src/display_procs.rs +++ b/src/display_procs.rs @@ -1,7 +1,7 @@ use gtk::glib; use gtk::prelude::*; -use sysinfo::{Pid, PidExt, Process, ProcessExt}; +use sysinfo::{Pid, Process}; use crate::utils::format_number; @@ -42,6 +42,7 @@ impl Procs { .margin_top(6) .margin_bottom(6) .margin_end(6) + .margin_start(6) .sensitive(false) .build(); @@ -80,8 +81,7 @@ impl Procs { for pro in proc_list.values() { if let Some(exe) = pro .exe() - .file_name() - .and_then(|f| f.to_str()) + .and_then(|exe| exe.file_name().and_then(|f| f.to_str())) .or_else(|| Some(pro.name())) { create_and_fill_model( diff --git a/src/display_sysinfo.rs b/src/display_sysinfo.rs index 9e69de9..809b3ca 100644 --- a/src/display_sysinfo.rs +++ b/src/display_sysinfo.rs @@ -1,6 +1,5 @@ use gtk::glib; use gtk::prelude::*; -use sysinfo::{ComponentExt, CpuExt, SystemExt}; use std::cell::RefCell; use std::iter; @@ -71,6 +70,7 @@ pub struct DisplaySysInfo { impl DisplaySysInfo { pub fn new( sys: &Arc>, + sys_components: &sysinfo::Components, stack: >k::Stack, settings: &Settings, ) -> DisplaySysInfo { @@ -192,13 +192,13 @@ impl DisplaySysInfo { // // TEMPERATURES PART // - if !sys.components().is_empty() { + if !sys_components.is_empty() { check_box3 = Some(create_header( "Components' temperature", &vertical_layout, settings.display_graph, )); - for component in sys.components() { + for component in sys_components { let horizontal_layout = gtk::Box::new(gtk::Orientation::Horizontal, 10); // TODO: add max and critical temperatures as well let temp = gtk::Label::new(Some(&format!("{:.1} °C", component.temperature()))); @@ -273,7 +273,7 @@ impl DisplaySysInfo { temperature_usage_history, temperature_check_box: check_box3, }; - tmp.update_system_info(&sys, settings.display_fahrenheit); + tmp.update_system_info(&sys, sys_components, settings.display_fahrenheit); tmp } @@ -285,7 +285,12 @@ impl DisplaySysInfo { } } - pub fn update_system_info(&mut self, sys: &sysinfo::System, display_fahrenheit: bool) { + pub fn update_system_info( + &mut self, + sys: &sysinfo::System, + sys_components: &sysinfo::Components, + display_fahrenheit: bool, + ) { let disp = |total, used| { format!( "{} / {}", @@ -337,8 +342,7 @@ impl DisplaySysInfo { // temperature part let t = self.temperature_usage_history.borrow_mut(); - for (pos, (component, label)) in sys - .components() + for (pos, (component, label)) in sys_components .iter() .zip(self.components.iter()) .enumerate() diff --git a/src/main.rs b/src/main.rs index 344c848..bdfb345 100755 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,6 @@ #![crate_type = "bin"] -use sysinfo::*; - use gdk::Texture; use gdk_pixbuf::Pixbuf; use gio::MemoryInputStream; @@ -16,6 +14,8 @@ use gtk::prelude::*; use gtk::{gdk, gdk_pixbuf, gio, glib}; use gtk::{AboutDialog, Dialog, Entry, MessageDialog}; +use sysinfo::{Networks, Pid, RefreshKind}; + use std::cell::RefCell; use std::collections::{HashMap, HashSet}; #[cfg(unix)] @@ -288,26 +288,25 @@ fn setup_timeout(rfs: &RequiredForSettings) { ); } -fn setup_network_timeout(rfs: &RequiredForSettings) { +fn setup_network_timeout(rfs: &RequiredForSettings, networks: Arc>) { let (sender, receiver) = async_channel::bounded(1); let network_refresh_timeout = &rfs.network_refresh_timeout; let network_tab = &rfs.network_tab; - let sys = &rfs.sys; thread::spawn( - glib::clone!(@weak sys, @weak network_refresh_timeout => move || { + glib::clone!(@weak networks, @weak network_refresh_timeout => move || { loop { let sleep_dur = Duration::from_millis( *network_refresh_timeout.lock().expect("failed to lock networks refresh mutex") as _); thread::sleep(sleep_dur); - sys.lock().expect("failed to lock to refresh networks").refresh_networks(); + networks.lock().expect("failed to lock to refresh networks").refresh_list(); sender.send_blocking(()).expect("failed to send data through networks refresh channel"); } }), ); - glib::spawn_future_local(glib::clone!(@weak sys, @weak network_tab => async move { + glib::spawn_future_local(glib::clone!(@weak network_tab => async move { loop { match receiver.recv().await { Ok(_) => {}, @@ -316,25 +315,31 @@ fn setup_network_timeout(rfs: &RequiredForSettings) { return; } } - network_tab.borrow_mut().update_networks(&sys.lock().expect("failed to lock to update networks")); + network_tab.borrow_mut().update_networks(&networks.lock().expect("failed to lock to update networks")); } })); } -fn setup_system_timeout(rfs: &RequiredForSettings, settings: &Rc>) { +fn setup_system_timeout( + rfs: &RequiredForSettings, + settings: &Rc>, + components: sysinfo::Components, +) { let (sender, receiver) = async_channel::bounded(1); let system_refresh_timeout = &rfs.system_refresh_timeout; let sys = &rfs.sys; let display_tab = &rfs.display_tab; + let components = Arc::new(Mutex::new(components)); thread::spawn( - glib::clone!(@weak sys, @weak system_refresh_timeout => move || { + glib::clone!(@weak sys, @weak system_refresh_timeout, @weak components => move || { loop { let sleep_dur = Duration::from_millis( *system_refresh_timeout.lock().expect("failed to lock system refresh mutex") as _); thread::sleep(sleep_dur); - sys.lock().expect("failed to lock to refresh system").refresh_system(); + sys.lock().expect("failed to lock to refresh system").refresh_all(); + components.lock().expect("failed to lock components").refresh_list(); sender.send_blocking(()).expect("failed to send data through system refresh channel"); } }), @@ -352,9 +357,10 @@ fn setup_system_timeout(rfs: &RequiredForSettings, settings: &Rc move |_, _| { diff --git a/src/network_dialog.rs b/src/network_dialog.rs index b87c88a..0256eca 100644 --- a/src/network_dialog.rs +++ b/src/network_dialog.rs @@ -1,8 +1,6 @@ use gtk::prelude::*; use gtk::{glib, EventControllerKey}; -use sysinfo::NetworkExt; - use crate::graph::GraphWidget; use crate::notebook::NoteBook; use crate::utils::{ diff --git a/src/process_dialog.rs b/src/process_dialog.rs index 8d7db10..039358b 100644 --- a/src/process_dialog.rs +++ b/src/process_dialog.rs @@ -1,6 +1,6 @@ use gtk::prelude::*; use gtk::{glib, pango, EventControllerKey}; -use sysinfo::{self, Pid, ProcessExt}; +use sysinfo::Pid; use std::cell::{Cell, RefCell}; use std::fmt; @@ -43,8 +43,12 @@ impl ProcDialog { if self.is_dead { return; } - self.working_directory - .set_text(&process.cwd().display().to_string()); + self.working_directory.set_text( + &process + .cwd() + .map(|path| path.display().to_string()) + .unwrap_or_default(), + ); let memory = process.memory(); let memory_s = format_number(memory); self.memory_usage.set_text(&memory_s); @@ -236,17 +240,26 @@ pub fn create_process_dialog(process: &sysinfo::Process, total_memory: u64) -> P create_and_add_new_label( &labels, "executable path", - &process.exe().display().to_string(), + &process + .exe() + .map(|path| path.display().to_string()) + .unwrap_or_default(), ); let working_directory = create_and_add_new_label( &labels, "current working directory", - &process.cwd().display().to_string(), + &process + .cwd() + .map(|path| path.display().to_string()) + .unwrap_or_default(), ); create_and_add_new_label( &labels, "root directory", - &process.root().display().to_string(), + &process + .root() + .map(|path| path.display().to_string()) + .unwrap_or_default(), ); let env_tree = gtk::TreeView::new();