diff --git a/native/libmacbridge/AppDelegate.mm b/native/libmacbridge/AppDelegate.mm index b077954..c06fd28 100644 --- a/native/libmacbridge/AppDelegate.mm +++ b/native/libmacbridge/AppDelegate.mm @@ -2,22 +2,8 @@ @implementation AppDelegate -// 10.9+ only, see this url for compatibility: -// http://stackoverflow.com/questions/17693408/enable-access-for-assistive-devices-programmatically-on-10-9 -BOOL checkAccessibility() -{ - NSDictionary* opts = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES}; - return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts); -} - - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - if (checkAccessibility()) { - NSLog(@"Accessibility Enabled"); - }else { - NSLog(@"Accessibility Disabled"); - } - // Setup status icon myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain]; @@ -31,7 +17,6 @@ BOOL checkAccessibility() [myStatusItem.button setTarget:self]; // Setup key listener - NSLog(@"registering keydown mask"); [NSEvent addGlobalMonitorForEventsMatchingMask:(NSEventMaskKeyDown | NSEventMaskFlagsChanged) handler:^(NSEvent *event){ if (event.type == NSEventTypeKeyDown diff --git a/native/libmacbridge/bridge.h b/native/libmacbridge/bridge.h index bc9813d..6e8107a 100644 --- a/native/libmacbridge/bridge.h +++ b/native/libmacbridge/bridge.h @@ -79,6 +79,12 @@ extern "C" void register_context_menu_click_callback(ContextMenuClickCallback ca // SYSTEM +/* + * Check if espanso is authorized to control accessibility features, needed to detect key presses. + * @return + */ +int32_t check_accessibility(); + /* * Return the active NSRunningApplication path */ diff --git a/native/libmacbridge/bridge.mm b/native/libmacbridge/bridge.mm index b21eda7..fc10895 100644 --- a/native/libmacbridge/bridge.mm +++ b/native/libmacbridge/bridge.mm @@ -227,4 +227,11 @@ int32_t show_context_menu(MenuItem * items, int32_t count) { [delegate_ptr->myStatusItem popUpStatusItemMenu:espansoMenu]; }); -} \ No newline at end of file +} + +// 10.9+ only, see this url for compatibility: +// http://stackoverflow.com/questions/17693408/enable-access-for-assistive-devices-programmatically-on-10-9 +int32_t check_accessibility() { + NSDictionary* opts = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES}; + return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts); +} diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index 25299a8..3ea76f8 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -14,6 +14,7 @@ extern { pub fn eventloop(); // System + pub fn check_accessibility() -> i32; pub fn get_active_app_bundle(buffer: *mut c_char, size: i32) -> i32; pub fn get_active_app_identifier(buffer: *mut c_char, size: i32) -> i32; diff --git a/src/context/macos.rs b/src/context/macos.rs index dd97efa..7d8bfd7 100644 --- a/src/context/macos.rs +++ b/src/context/macos.rs @@ -8,6 +8,7 @@ use std::ffi::CString; use std::fs; use log::{info, error}; use std::path::PathBuf; +use std::process::exit; const STATUS_ICON_BINARY : &'static [u8] = include_bytes!("../res/mac/icon.png"); @@ -17,6 +18,18 @@ pub struct MacContext { impl MacContext { pub fn new(send_channel: Sender) -> Box { + // Check accessibility + unsafe { + let res = check_accessibility(); + + if res == 0 { + error!("Accessibility must be enabled to make espanso work on MacOS."); + error!("Please allow espanso in the Security & Privacy panel, then restart espanso."); + error!("For more information: "); // TODO: add documentation link + exit(1); + } + } + let context = Box::new(MacContext { send_channel });