Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump sysinfo on macos #201

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ core-graphics = "0.24"
core-foundation = "0.10"
cocoa = "0.26"
objc = "0.2"
sysinfo = "0.29.10"
sysinfo = "0.33.0"

[target.'cfg(target_os = "linux")'.dependencies]
tun = { version = "0.7.5", features = ["async"] }
Expand Down
39 changes: 23 additions & 16 deletions src/processes/macos_icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::hash::{Hash, Hasher};
use std::io::Cursor;
use std::path::Path;
use std::path::PathBuf;
use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};

#[derive(Default)]
pub struct IconCache {
Expand Down Expand Up @@ -54,22 +54,29 @@ pub fn tiff_to_png(tiff: &[u8]) -> Vec<u8> {

pub fn tiff_data_for_executable(executable: &Path) -> Result<Vec<u8>> {
let mut sys = System::new();
sys.refresh_processes_specifics(ProcessRefreshKind::new());
sys.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::nothing().with_exe(UpdateKind::OnlyIfNotSet),
);
for (pid, process) in sys.processes() {
let pid = pid.as_u32();
if executable == process.exe().to_path_buf() {
unsafe {
let app: id = msg_send![
class!(NSRunningApplication),
runningApplicationWithProcessIdentifier: pid
];
if !app.is_null() {
let img: id = msg_send![app, icon];
let tiff: id = msg_send![img, TIFFRepresentation];
let length: usize = msg_send![tiff, length];
let bytes: *const u8 = msg_send![tiff, bytes];
let data = std::slice::from_raw_parts(bytes, length).to_vec();
return Ok(data);
// process.exe() will return empty path if there was an error while trying to read /proc/<pid>/exe.
if let Some(path) = process.exe() {
if executable == path.to_path_buf() {
let pid = pid.as_u32();
unsafe {
let app: id = msg_send![
class!(NSRunningApplication),
runningApplicationWithProcessIdentifier: pid
];
if !app.is_null() {
let img: id = msg_send![app, icon];
let tiff: id = msg_send![img, TIFFRepresentation];
let length: usize = msg_send![tiff, length];
let bytes: *const u8 = msg_send![tiff, bytes];
let data = std::slice::from_raw_parts(bytes, length).to_vec();
return Ok(data);
}
}
}
}
Expand Down
54 changes: 33 additions & 21 deletions src/processes/macos_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,46 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::ffi::c_void;
use std::path::PathBuf;
use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};

pub fn active_executables() -> Result<ProcessList> {
let mut executables: HashMap<PathBuf, ProcessInfo> = HashMap::new();
let visible = visible_windows()?;
let mut sys = System::new();
sys.refresh_processes_specifics(ProcessRefreshKind::new());
sys.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::nothing().with_exe(UpdateKind::OnlyIfNotSet),
);
for (pid, process) in sys.processes() {
let pid = pid.as_u32();
let display_name = process.name().to_string();
let executable = process.exe().to_path_buf();
let is_system = executable.starts_with("/System/");
match executables.entry(executable) {
Entry::Occupied(mut e) => {
let process_info = e.get();
if !process_info.is_visible && visible.contains(&pid) {
e.get_mut().is_visible = true;
// process.exe() will return empty path if there was an error while trying to read /proc/<pid>/exe.
if let Some(path) = process.exe() {
let pid = pid.as_u32();
let executable = path.to_path_buf();
match executables.entry(executable) {
Entry::Occupied(mut e) => {
let process_info = e.get();
if !process_info.is_visible && visible.contains(&pid) {
e.get_mut().is_visible = true;
}
}
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 = match path.file_name() {
Some(s) => s.to_string_lossy().to_string(),
None => path.to_string_lossy().to_string(),
};
let is_visible = visible.contains(&pid);
let is_system = executable.starts_with("/System/");
e.insert(ProcessInfo {
executable,
display_name,
is_visible,
is_system,
});
}
}
Entry::Vacant(e) => {
let executable = e.key().clone();
let is_visible = visible.contains(&pid);
e.insert(ProcessInfo {
executable,
display_name,
is_visible,
is_system,
});
}
}
}
Expand Down
Loading