Skip to content

Commit

Permalink
get the icon from the NSRunningApp
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele-em committed Oct 26, 2023
1 parent b239349 commit 142b550
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 29 deletions.
115 changes: 111 additions & 4 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ tokio-util = { version = "0.7.8", features = ["codec"] }
futures-util = { version = "0.3.28", features = ["sink"] }

sysinfo = "0.29.10"
cacao = "0.3.2"
cocoa = "0.25"
objc = "0.2"
base64 = "0.21.5"

# [patch.crates-io]
# tokio = { path = "../tokio/tokio" }
# smoltcp = { git = 'https://github.com/mhils/smoltcp', rev = 'f65351adfa92db5193f368368cb668bac721fe43' }
Expand Down
60 changes: 37 additions & 23 deletions src/macos/processes.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
use anyhow::Result;
use crate::processes::{ProcessList, ProcessInfo};
use sysinfo::{PidExt, ProcessRefreshKind, System, SystemExt, ProcessExt};
use cacao::foundation::{id, NSString};
use crate::processes::{ProcessInfo, ProcessList};
use anyhow::{bail, Result};
use base64::engine::general_purpose;
use base64::Engine;
use cocoa::base::id;
use objc::{class, msg_send, sel, sel_impl};
use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};

pub fn active_executables() -> Result<ProcessList> {
dbg!("active_executables");
let mut sys = System::new();
let mut list: ProcessList = vec![];
sys.refresh_processes_specifics(ProcessRefreshKind::new());
for proc in sys.processes() {
let app: id = unsafe{msg_send![class!(NSRunningApplication), runningApplicationWithProcessIdentifier: proc.0.as_u32()]};
let display_name = if !app.is_null() {
let localized_name: id = unsafe{msg_send![app, localizedName]};
NSString::retain(localized_name).to_string()
} else {
proc.1.name().to_string()
};
let display_name = proc.1.name().to_string();
let mut activation_policy = None;
let mut _icon: Option<i32> = None;
let executable = proc.1.exe();
let activation_policy: u8 = unsafe {msg_send![app, activationPolicy]};

list.push(
ProcessInfo {
executable: executable.to_path_buf(),
display_name,
activation_policy,
is_system: executable.starts_with("/System/"),
let mut icon = vec![];
let app: id = unsafe {
msg_send![class!(NSRunningApplication), runningApplicationWithProcessIdentifier: proc.0.as_u32()]
};
if !app.is_null() {
activation_policy = Some(unsafe { msg_send![app, activationPolicy] });
let img: id = unsafe { msg_send![app, icon] };
let tif: id = unsafe { msg_send![img, TIFFRepresentation] };
let bitmap: id = unsafe { msg_send![class!(NSBitmapImageRep), imageRepWithData: tif] };
let png: id = unsafe { msg_send![bitmap, representationUsingType: 4 properties: 0] };
let length: usize = unsafe { msg_send![png, length] };
if let Some(len) = base64::encoded_len(length, true) {
let bytes: *const u8 = unsafe { msg_send![png, bytes] };
let s = unsafe { std::slice::from_raw_parts(bytes, length) }.to_vec();
icon.resize(len, 0);
match general_purpose::STANDARD.encode_slice(s, &mut icon) {
Ok(len) => icon.truncate(len),
Err(e) => bail!(e),
}
}
);
}
list.push(ProcessInfo {
executable: executable.to_path_buf(),
display_name,
activation_policy,
is_system: executable.starts_with("/System/"),
icon,
});
}

Ok(list)
}

Expand All @@ -43,7 +57,7 @@ mod tests {
assert!(!lst.is_empty());

for proc in &lst {
dbg!(proc);
dbg!(proc);
}
dbg!(lst.len());
}
Expand Down
3 changes: 2 additions & 1 deletion src/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use std::path::PathBuf;
pub struct ProcessInfo {
pub executable: PathBuf,
pub display_name: String,
pub activation_policy: u8,
pub activation_policy: Option<u8>,
pub is_system: bool,
pub icon: Vec<u8>,
}

pub type ProcessList = Vec<ProcessInfo>;

0 comments on commit 142b550

Please sign in to comment.