Skip to content

Commit

Permalink
Bump sysinfo on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
errorxyz committed Dec 9, 2024
1 parent 5f7480a commit 25e25a5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
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.

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

[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};

Check failure on line 11 in src/processes/macos_icons.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.80, --exclude windows-redirector)

unresolved import `sysinfo`

#[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};

Check failure on line 18 in src/processes/macos_list.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.80, --exclude windows-redirector)

unresolved import `sysinfo`

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

0 comments on commit 25e25a5

Please sign in to comment.