From 9f905fbd36f4c03ae3473a326c2b836b4dc1ed60 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 26 Jun 2021 21:32:55 +0200 Subject: [PATCH] feat(mac-utils): implement accessibility methods --- espanso-mac-utils/src/ffi.rs | 2 ++ espanso-mac-utils/src/lib.rs | 22 ++++++++++++++++++++++ espanso-mac-utils/src/native.h | 6 ++++++ espanso-mac-utils/src/native.mm | 10 ++++++++++ 4 files changed, 40 insertions(+) diff --git a/espanso-mac-utils/src/ffi.rs b/espanso-mac-utils/src/ffi.rs index 3491298..770e98d 100644 --- a/espanso-mac-utils/src/ffi.rs +++ b/espanso-mac-utils/src/ffi.rs @@ -25,4 +25,6 @@ use std::os::raw::c_char; extern "C" { pub fn mac_utils_get_secure_input_process(pid: *mut i64) -> i32; pub fn mac_utils_get_path_from_pid(pid: i64, buffer: *mut c_char, size: i32) -> i32; + pub fn mac_utils_check_accessibility() -> i32; + pub fn mac_utils_prompt_accessibility() -> i32; } diff --git a/espanso-mac-utils/src/lib.rs b/espanso-mac-utils/src/lib.rs index 8e5b57c..742034d 100644 --- a/espanso-mac-utils/src/lib.rs +++ b/espanso-mac-utils/src/lib.rs @@ -93,6 +93,28 @@ fn get_app_name_from_path(path: &str) -> Option { } } +#[cfg(target_os = "macos")] +pub fn check_accessibility() -> bool { + unsafe { + if ffi::mac_utils_check_accessibility() > 0 { + true + } else { + false + } + } +} + +#[cfg(target_os = "macos")] +pub fn prompt_accessibility() -> bool { + unsafe { + if ffi::mac_utils_prompt_accessibility() > 0 { + true + } else { + false + } + } +} + #[cfg(test)] #[cfg(target_os = "macos")] mod tests { diff --git a/espanso-mac-utils/src/native.h b/espanso-mac-utils/src/native.h index 8d045d0..1d2a70d 100644 --- a/espanso-mac-utils/src/native.h +++ b/espanso-mac-utils/src/native.h @@ -28,4 +28,10 @@ extern "C" int32_t mac_utils_get_secure_input_process(int64_t *pid); // Find the executable path corresponding to the given PID, return 0 if no process was found. extern "C" int32_t mac_utils_get_path_from_pid(int64_t pid, char *buff, int buff_size); +// Return 1 if the accessibility permissions have been granted, 0 otherwise +extern "C" int32_t mac_utils_check_accessibility(); + +// Return 1 if the accessibility permissions have been granted, 0 otherwise +extern "C" int32_t mac_utils_prompt_accessibility(); + #endif //ESPANSO_MAC_UTILS_H \ No newline at end of file diff --git a/espanso-mac-utils/src/native.mm b/espanso-mac-utils/src/native.mm index 47cf28a..11c0770 100644 --- a/espanso-mac-utils/src/native.mm +++ b/espanso-mac-utils/src/native.mm @@ -65,4 +65,14 @@ int32_t mac_utils_get_path_from_pid(int64_t pid, char *buff, int buff_size) { } else { return 1; } +} + +int32_t mac_utils_check_accessibility() { + NSDictionary* opts = @{(__bridge id)kAXTrustedCheckOptionPrompt: @NO}; + return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts); +} + +int32_t mac_utils_prompt_accessibility() { + NSDictionary* opts = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES}; + return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts); } \ No newline at end of file