|
| 1 | +/// Taken from: <https://github.com/acsandmann/rift/blob/main/src/sys/accessibility.rs> |
| 2 | +use std::ffi::c_void; |
| 3 | +use std::thread; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | + |
| 6 | +use log::info; |
| 7 | +use objc2::rc::autoreleasepool; |
| 8 | +use objc2::runtime::AnyObject; |
| 9 | +use objc2::{class, msg_send}; |
| 10 | + |
| 11 | +#[link(name = "ApplicationServices", kind = "framework")] |
| 12 | +unsafe extern "C" { |
| 13 | + fn AXIsProcessTrustedWithOptions(options: *const c_void) -> bool; |
| 14 | + |
| 15 | + static kAXTrustedCheckOptionPrompt: *const c_void; |
| 16 | +} |
| 17 | + |
| 18 | +#[link(name = "CoreFoundation", kind = "framework")] |
| 19 | +unsafe extern "C" { |
| 20 | + static kCFBooleanTrue: *const c_void; |
| 21 | + static kCFBooleanFalse: *const c_void; |
| 22 | +} |
| 23 | + |
| 24 | +const AX_POLL_INTERVAL: Duration = Duration::from_millis(250); |
| 25 | +const AX_POLL_TIMEOUT: Duration = Duration::from_secs(30); |
| 26 | + |
| 27 | +#[inline] |
| 28 | +fn ax_is_trusted() -> bool { |
| 29 | + unsafe { |
| 30 | + autoreleasepool(|_| { |
| 31 | + let keys: [*mut AnyObject; 1] = [kAXTrustedCheckOptionPrompt as *mut AnyObject]; |
| 32 | + let vals: [*mut AnyObject; 1] = [kCFBooleanFalse as *mut AnyObject]; |
| 33 | + let dict: *mut AnyObject = msg_send![ |
| 34 | + class!(NSDictionary), |
| 35 | + dictionaryWithObjects: vals.as_ptr(), |
| 36 | + forKeys: keys.as_ptr(), |
| 37 | + count: 1usize |
| 38 | + ]; |
| 39 | + |
| 40 | + AXIsProcessTrustedWithOptions(dict.cast()) |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[allow(unsafe_op_in_unsafe_fn)] |
| 46 | +unsafe fn prompt_ax_trust_dialog() { |
| 47 | + autoreleasepool(|_| { |
| 48 | + let keys: [*mut AnyObject; 1] = [kAXTrustedCheckOptionPrompt as *mut AnyObject]; |
| 49 | + let vals: [*mut AnyObject; 1] = [kCFBooleanTrue as *mut AnyObject]; |
| 50 | + |
| 51 | + let dict: *mut AnyObject = msg_send![ |
| 52 | + class!(NSDictionary), |
| 53 | + dictionaryWithObjects: vals.as_ptr(), |
| 54 | + forKeys: keys.as_ptr(), |
| 55 | + count: 1usize |
| 56 | + ]; |
| 57 | + |
| 58 | + let _ = AXIsProcessTrustedWithOptions(dict.cast()); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +pub fn ensure_accessibility_permission() { |
| 63 | + if ax_is_trusted() { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + info!("Accessibility permission is not granted; prompting user for permission now."); |
| 68 | + |
| 69 | + unsafe { prompt_ax_trust_dialog() }; |
| 70 | + |
| 71 | + let start = Instant::now(); |
| 72 | + loop { |
| 73 | + if ax_is_trusted() { |
| 74 | + info!("Accessibility permission granted"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if start.elapsed() >= AX_POLL_TIMEOUT { |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + thread::sleep(AX_POLL_INTERVAL); |
| 83 | + } |
| 84 | + |
| 85 | + println!( |
| 86 | + "Rustcast still does not have accessibility permission. Enable it in System Settings > Privacy & Security > Accessibility, then restart Rustcast." |
| 87 | + ); |
| 88 | + |
| 89 | + std::process::exit(1); |
| 90 | +} |
0 commit comments