forked from pilotmoon/Scroll-Reverser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermissionsManager.m
158 lines (138 loc) · 4.61 KB
/
PermissionsManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// PermissionsManager.m
// Scroll Reverser
//
// Created by Nicholas Moore on 21/11/2019.
//
#import "PermissionsManager.h"
#import <IOKit/hidsystem/IOHIDLib.h>
NSString *const PermissionsManagerKeyAccessibilityEnabled=@"accessibilityEnabled";
NSString *const PermissionsManagerKeyInputMonitoringEnabled=@"inputMonitoringEnabled";
NSString *const PermissionsManagerKeyHasAllRequiredPermissions=@"hasAllRequiredPermissions";
@interface PermissionsManager ()
@property (getter=isAccessibilityEnabled) BOOL accessibilityEnabled;
@property (getter=isInputMonitoringEnabled) BOOL inputMonitoringEnabled;
@property NSTimer *refreshTimer;
@property NSDate *refreshStarted;
@end
@implementation PermissionsManager
- (instancetype)init
{
self = [super init];
if (self) {
[self checkState];
// refresh on user activity
const NSEventMask mask=NSEventMaskLeftMouseDown;
[NSEvent addGlobalMonitorForEventsMatchingMask:mask handler:^(NSEvent *event) {
[self refresh];
}];
[NSEvent addLocalMonitorForEventsMatchingMask:mask handler:^NSEvent *(NSEvent *event) {
[self refresh];
return event;
}];
}
return self;
}
# pragma mark Private Methods
- (BOOL)checkAccessibilityWithPrompt:(BOOL)prompt
{
// this is a 10.9 API but is only needed on 10.14.
// with no prompt, this check is very fast. otherwise it blocks.
NSDictionary *const options=@{(__bridge NSString *)kAXTrustedCheckOptionPrompt: @(prompt)};
return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
}
- (BOOL)checkInputMonitoringWithPrompt:(BOOL)prompt
{
if (@available(macOS 10.15, *)) {
static const IOHIDRequestType accessType=kIOHIDRequestTypePostEvent; // ??
if (prompt) {
// this will block
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
IOHIDRequestAccess(accessType);
});
return NO;
}
else {
// this check is very fast
return kIOHIDAccessTypeGranted==IOHIDCheckAccess(accessType);
}
}
else {
return YES;
}
}
+ (NSSet *)keyPathsForValuesAffectingHasAllRequiredPermissions
{
return [NSSet setWithArray:@[PermissionsManagerKeyAccessibilityEnabled, PermissionsManagerKeyInputMonitoringEnabled]];
}
- (void)checkState
{
BOOL axState=[self checkAccessibilityWithPrompt:NO];
if(axState!=self.accessibilityEnabled) {
self.accessibilityEnabled=axState;
}
BOOL imState=[self checkInputMonitoringWithPrompt:NO];
if(imState!=self.inputMonitoringEnabled) {
self.inputMonitoringEnabled=imState;
}
NSLog(@"permissions: ax %@, im %@", @(axState), @(imState));
}
#pragma mark Public Interface
/* Every time this is called, a repeat timer will repeatedly poll. */
- (void)refresh
{
// always do timer stuff on main thread
dispatch_async(dispatch_get_main_queue(), ^{
self.refreshStarted=[NSDate date];
if (!self.refreshTimer.valid) {
NSLog(@"Starting refresh timer");
self.refreshTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"refresh");
[self checkState];
if ([[NSDate date] timeIntervalSinceDate:self.refreshStarted]>5) {
NSLog(@"time elapsed, stopping refresh loop");
[self.refreshTimer invalidate];
self.refreshTimer=nil;
}
}];
}
});
}
- (BOOL)hasAllRequiredPermissions
{
return self.accessibilityEnabled && self.inputMonitoringEnabled;
}
+ (NSURL *)securitySettingsUrlForKey:(NSString *)key
{
return [NSURL URLWithString:[NSString stringWithFormat:@"x-apple.systempreferences:com.apple.preference.security?%@", key]];
}
- (void)requestAccessibilityPermission
{
[[NSWorkspace sharedWorkspace] openURL:[[self class] securitySettingsUrlForKey:@"Privacy_Accessibility"]];
[self checkAccessibilityWithPrompt:YES];
}
- (void)requestInputMonitoringPermission
{
[[NSWorkspace sharedWorkspace] openURL:[[self class] securitySettingsUrlForKey:@"Privacy_ListenEvent"]];
[self checkInputMonitoringWithPrompt:YES];
}
// Accessibility permission is needed on Mojave and above
- (BOOL)isAccessibilityRequired {
if (@available(macOS 10.14, *)) {
return YES;
}
else {
return NO;
}
}
// Input Monitoring permission is needed on Catalina and above
- (BOOL)isInputMonitoringRequired
{
if (@available(macOS 10.15, *)) {
return YES;
}
else {
return NO;
}
}
@end