Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions src/FrankLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@
#import "NSApplication+FrankAutomation.h"
#endif


BOOL frankLogEnabled = NO;

static void * loadDylib(NSString *path)
{
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *simulatorRoot = [environment objectForKey:@"IPHONE_SIMULATOR_ROOT"];
if (simulatorRoot) {
path = [simulatorRoot stringByAppendingPathComponent:path];
}

NSLog(@"Attempting to open the library at '%@'", path);
return dlopen([path fileSystemRepresentation], RTLD_LOCAL | RTLD_LAZY );
}

@implementation FrankLoader

+ (void)applicationDidBecomeActive:(NSNotification *)notification{
Expand Down Expand Up @@ -53,15 +66,7 @@ + (void)load{
NSLog(@"Injecting Frank loader");

NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
NSString *appSupportLocation = @"/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport";

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *simulatorRoot = [environment objectForKey:@"IPHONE_SIMULATOR_ROOT"];
if (simulatorRoot) {
appSupportLocation = [simulatorRoot stringByAppendingString:appSupportLocation];
}

void *appSupportLibrary = dlopen([appSupportLocation fileSystemRepresentation], RTLD_LAZY);
void *appSupportLibrary = loadDylib(@"/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport");

if(!appSupportLibrary) {
NSLog(@"Unable to dlopen AppSupport. Cannot automatically enable accessibility.");
Expand All @@ -85,28 +90,50 @@ + (void)load{
NSLog(@"Unable to dlsym CPCopySharedResourcesPreferencesDomainForDomain. Cannot automatically enable accessibility.");
}

NSString* accessibilitySettingsBundleLocation = @"/System/Library/PreferenceBundles/AccessibilitySettings.bundle/AccessibilitySettings";

if (simulatorRoot) {
accessibilitySettingsBundleLocation = [simulatorRoot stringByAppendingString:accessibilitySettingsBundleLocation];
}
void* accessibilitySettingsBundle = loadDylib(@"/System/Library/PreferenceBundles/AccessibilitySettings.bundle/AccessibilitySettings");

const char *accessibilitySettingsBundlePath = [accessibilitySettingsBundleLocation fileSystemRepresentation];
void* accessibilitySettingsBundle = dlopen(accessibilitySettingsBundlePath, RTLD_LAZY);
BOOL couldEnableAccessibility = NO;

if (accessibilitySettingsBundle) {
Class axSettingsPrefControllerClass = NSClassFromString(@"AccessibilitySettingsController");
id axSettingPrefController = [[axSettingsPrefControllerClass alloc] init];

id initialAccessibilityInspectorSetting = [axSettingPrefController AXInspectorEnabled:nil];
[axSettingPrefController setAXInspectorEnabled:@(YES) specifier:nil];
if([axSettingPrefController respondsToSelector:@selector(AXInspectorEnabled:)])
{
id initialAccessibilityInspectorSetting = [axSettingPrefController AXInspectorEnabled:nil];
[axSettingPrefController setAXInspectorEnabled:@(YES) specifier:nil];

NSLog(@"Successfully enabled the AXInspector.");
NSLog(@"Successfully enabled the AXInspector using the setAXInspectorEnabled selector.");
couldEnableAccessibility = YES;
}
}
else {
NSLog(@"Unable to dlopen AccessibilitySettings. Cannout automatically enable accessibility.");
}

if(!couldEnableAccessibility)
{
NSLog(@"Could not enable accessibility using the legacy methods.");

// If we get to this point, the legacy method has not worked
void *handle = loadDylib(@"/usr/lib/libAccessibility.dylib");

if (!handle) {
NSLog(@"Unable to open libAccessibility. Cannout automatically enable accessibility.");
}

int (*_AXSAutomationEnabled)(void) = dlsym(handle, "_AXSAutomationEnabled");
void (*_AXSSetAutomationEnabled)(int) = dlsym(handle, "_AXSSetAutomationEnabled");

int initialValue = _AXSAutomationEnabled();
_AXSSetAutomationEnabled(YES);
atexit_b(^{
_AXSSetAutomationEnabled(initialValue);
});

NSLog(@"Enabled accessibility using libAccessibility.");
}

[autoreleasePool drain];

#if TARGET_OS_IPHONE
Expand Down