diff --git a/src/processes/linux_list.rs b/src/processes/linux_list.rs deleted file mode 100644 index 53b9927c..00000000 --- a/src/processes/linux_list.rs +++ /dev/null @@ -1,60 +0,0 @@ -use crate::processes::{ProcessInfo, ProcessList}; -use anyhow::Result; -use std::collections::hash_map::Entry; -use std::collections::HashMap; -use std::path::PathBuf; -use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; - -pub fn active_executables() -> Result { - let mut executables: HashMap = HashMap::new(); - let mut sys = System::new(); - - sys.refresh_processes_specifics( - ProcessesToUpdate::All, - true, - ProcessRefreshKind::nothing().with_exe(UpdateKind::OnlyIfNotSet), - ); - - for process in sys.processes().values() { - // process.exe() will return an empty path if there was an error while trying to read /proc//exe. - if let Some(path) = process.exe() { - let executable = path.to_path_buf(); - - match executables.entry(executable) { - Entry::Occupied(_) => {} - Entry::Vacant(e) => { - let executable = e.key().clone(); - // .file_name() returns `None` if the path terminates in `..` - // We use the absolute path in such a case. - let display_name = path - .file_name() - .unwrap_or(path.as_os_str()) - .to_string_lossy() - .to_string(); - let is_system = match process.effective_user_id() { - Some(id) => id.to_string() == "0", - None => false, - }; - e.insert(ProcessInfo { - executable, - display_name, - is_visible: false, - is_system, - }); - } - } - } - } - Ok(executables.into_values().collect()) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn process_list() { - let lst = active_executables().unwrap(); - assert!(!lst.is_empty()); - } -} diff --git a/src/processes/mod.rs b/src/processes/mod.rs index ec8505b3..e7d8fe6a 100644 --- a/src/processes/mod.rs +++ b/src/processes/mod.rs @@ -2,9 +2,9 @@ pub use image; use std::path::PathBuf; #[cfg(target_os = "macos")] -mod macos_list; +mod nix_list; #[cfg(target_os = "macos")] -pub use self::macos_list::active_executables; +pub use self::nix_list::active_executables; #[cfg(windows)] mod windows_list; @@ -14,9 +14,9 @@ pub use self::windows_list::active_executables; pub use self::windows_list::get_process_name; #[cfg(target_os = "linux")] -mod linux_list; +mod nix_list; #[cfg(target_os = "linux")] -pub use self::linux_list::active_executables; +pub use self::nix_list::active_executables; #[cfg(target_os = "macos")] mod macos_icons; @@ -44,7 +44,7 @@ pub static ICON_CACHE: once_cell::sync::Lazy> = pub mod bench { #[cfg(target_os = "macos")] - pub use super::macos_list::visible_windows; + pub use super::nix_list::visible_windows; #[cfg(windows)] pub use super::windows_list::visible_windows; diff --git a/src/processes/macos_list.rs b/src/processes/nix_list.rs similarity index 90% rename from src/processes/macos_list.rs rename to src/processes/nix_list.rs index 863520ec..46ac285f 100644 --- a/src/processes/macos_list.rs +++ b/src/processes/nix_list.rs @@ -1,18 +1,23 @@ use crate::intercept_conf::PID; use crate::processes::{ProcessInfo, ProcessList}; use anyhow::Result; + +#[cfg(target_os = "macos")] use cocoa::base::nil; +#[cfg(target_os = "macos")] use cocoa::foundation::NSString; -use core_foundation::number::kCFNumberSInt32Type; -use core_foundation::number::CFNumberGetValue; -use core_foundation::number::CFNumberRef; +#[cfg(target_os = "macos")] +use core_foundation::number::{kCFNumberSInt32Type, CFNumberGetValue, CFNumberRef}; +#[cfg(target_os = "macos")] use core_graphics::display::{ kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef, CGWindowListCopyWindowInfo, }; + use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; +#[cfg(target_os = "macos")] use std::ffi::c_void; use std::path::PathBuf; use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; @@ -65,6 +70,7 @@ pub fn active_executables() -> Result { Ok(executables.into_values().collect()) } +#[cfg(target_os = "macos")] pub fn visible_windows() -> Result> { let mut pids: HashSet = HashSet::new(); unsafe { @@ -100,6 +106,12 @@ pub fn visible_windows() -> Result> { } } +#[cfg(target_os = "linux")] +pub fn visible_windows() -> Result> { + // Finding visible windows on linux is not worth the effort + Ok(HashSet::new()) +} + #[cfg(test)] mod tests { use super::*; @@ -117,6 +129,7 @@ mod tests { dbg!(lst.len()); } + #[cfg(target_os = "macos")] #[test] fn visible_windows_list() { let open_windows_pids = visible_windows().unwrap();