//! macOS platform effects. Process names come from libproc; no helper process is spawned.
use crate::{Result, download};
use std::{ffi::c_void, path::Path};
#[link(name = "proc")]
unsafe extern "C" {
fn proc_listallpids(buffer: *mut c_void, size: i32) -> i32;
fn proc_name(pid: i32, buffer: *mut c_void, size: u32) -> i32;
fn proc_pidpath(pid: i32, buffer: *mut c_void, size: u32) -> i32;
}
/// The short command name and the executable basename of every process that exposes them.
/// `None` means the process table itself could not be listed.
pub fn process_names() -> Option<Vec<Vec<u8>>> {
// SAFETY: a null buffer asks libproc for the current process count.
let count = unsafe { proc_listallpids(std::ptr::null_mut(), 0) };
if count <= 0 {
return None;
}
// Processes may start between the two calls.
let mut pids = vec![0i32; count as usize + 64];
let bytes = i32::try_from(pids.len() * size_of::<i32>()).ok()?;
// SAFETY: the buffer holds `bytes` bytes of i32 slots.
let listed = unsafe { proc_listallpids(pids.as_mut_ptr().cast(), bytes) };
if listed <= 0 {
return None;
}
pids.truncate((listed as usize).min(pids.len()));
let mut names = Vec::new();
let mut buffer = vec![0u8; 4096]; // PROC_PIDPATHINFO_MAXSIZE
for pid in pids.into_iter().filter(|pid| *pid > 0) {
// SAFETY: each call writes at most `buffer.len()` bytes and returns the length.
let len = unsafe { proc_name(pid, buffer.as_mut_ptr().cast(), buffer.len() as u32) };
if len > 0 {
names.push(buffer[..len as usize].to_vec());
}
let len = unsafe { proc_pidpath(pid, buffer.as_mut_ptr().cast(), buffer.len() as u32) };
if len > 0 {
let path = &buffer[..len as usize];
names.push(
path.rsplit(|b| *b == b'/')
.next()
.unwrap_or_default()
.to_vec(),
);
}
}
Some(names)
}
/// Fails open like Linux: an unlistable process table warns and reports not running.
pub fn factorio_running_among(names: Option<Vec<Vec<u8>>>) -> bool {
let Some(names) = names else {
eprintln!("Warning: cannot list processes; treating Factorio as not running");
return false;
};
names.iter().any(|name| {
name.eq_ignore_ascii_case(b"factorio") || name.eq_ignore_ascii_case(b"factorio.exe")
})
}
pub fn factorio_running() -> bool {
factorio_running_among(process_names())
}
/// The crate defaults to Finder AppleScript (Automation prompt). NSFileManager trashItemAtURL
/// moves to the item's volume Trash without a prompt and never deletes.
pub fn trash_context() -> trash::TrashContext {
use trash::macos::{DeleteMethod, TrashContextExtMacos};
let mut context = trash::TrashContext::default();
context.set_delete_method(DeleteMethod::NsFileManager);
context
}
pub fn trash(path: &Path) -> Result<()> {
download::ordinary(path)?;
trash_context().delete(path)?;
Ok(())
}
/// Synchronous worker-side opener with an injected process launcher for tests.
pub fn open_browser_with(
url: &str,
launch: impl FnOnce(&str, &[&str]) -> Result<()>,
) -> Result<()> {
if !crate::markdown::browser_url(url) {
return Err("Only HTTP(S) browser URLs are supported".into());
}
launch("/usr/bin/open", &[url])
}
pub fn open_browser(url: &str) -> Result<()> {
open_browser_with(url, |program, args| {
let status = std::process::Command::new(program).args(args).status()?;
if !status.success() {
return Err(format!("open failed: {status}").into());
}
Ok(())
})
}