use std::ffi::c_void;
#[link(name = "ApplicationServices", kind = "framework")]
unsafe extern "C" {
fn AXIsProcessTrusted() -> bool;
fn AXIsProcessTrustedWithOptions(options: *const c_void) -> bool;
static kAXTrustedCheckOptionPrompt: *const c_void;
}
#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C" {
static kCFAllocatorDefault: *const c_void;
static kCFBooleanTrue: *const c_void;
static kCFTypeDictionaryKeyCallBacks: c_void;
static kCFTypeDictionaryValueCallBacks: c_void;
fn CFDictionaryCreate(
allocator: *const c_void,
keys: *const *const c_void,
values: *const *const c_void,
num_values: isize,
key_callbacks: *const c_void,
value_callbacks: *const c_void,
) -> *const c_void;
fn CFRelease(cf: *const c_void);
}
pub fn is_trusted() -> bool {
unsafe { AXIsProcessTrusted() }
}
pub fn request_trust() -> bool {
unsafe {
let keys = [kAXTrustedCheckOptionPrompt];
let values = [kCFBooleanTrue];
let options = CFDictionaryCreate(
kCFAllocatorDefault,
keys.as_ptr(),
values.as_ptr(),
1,
std::ptr::addr_of!(kCFTypeDictionaryKeyCallBacks),
std::ptr::addr_of!(kCFTypeDictionaryValueCallBacks),
);
let trusted = AXIsProcessTrustedWithOptions(options);
if !options.is_null() {
CFRelease(options);
}
trusted
}
}