6
6
static const CFStringRef AX_NOTIFICATION = CFSTR ("com.apple.accessibility.api" );
7
7
static bool TRUSTED ;
8
8
9
- static CFMachPortRef TAP ;
10
- static CFRunLoopSourceRef SOURCE ;
11
-
12
9
static int LINES ;
13
10
14
11
static CGEventRef tapCallback (CGEventTapProxy proxy ,
@@ -37,23 +34,14 @@ static void notificationCallback(CFNotificationCenterRef center, void *observer,
37
34
CFNotificationName name , const void * object ,
38
35
CFDictionaryRef userInfo )
39
36
{
40
- if (CFStringCompare (name , AX_NOTIFICATION , 0 ) == kCFCompareEqualTo ) {
41
- CFRunLoopRef runLoop = CFRunLoopGetCurrent ();
42
- CFRunLoopPerformBlock (
43
- runLoop , kCFRunLoopDefaultMode , ^{
44
- bool previouslyTrusted = TRUSTED ;
45
- if ((TRUSTED = AXIsProcessTrusted ()) != previouslyTrusted ) {
46
- CFRunLoopStop (runLoop );
47
- if (SOURCE && CFRunLoopContainsSource (runLoop , SOURCE , kCFRunLoopDefaultMode )) {
48
- CGEventTapEnable (TAP , TRUSTED );
49
- CFRunLoopRun ();
50
- } else if (!TRUSTED ) {
51
- CFRunLoopRun ();
52
- }
53
- }
54
- }
55
- );
56
- }
37
+ CFRunLoopRef runLoop = CFRunLoopGetCurrent ();
38
+ CFRunLoopPerformBlock (
39
+ runLoop , kCFRunLoopDefaultMode , ^{
40
+ bool previouslyTrusted = TRUSTED ;
41
+ if ((TRUSTED = AXIsProcessTrusted ()) && !previouslyTrusted )
42
+ CFRunLoopStop (runLoop );
43
+ }
44
+ );
57
45
}
58
46
59
47
static bool getIntPreference (CFStringRef key , int * valuePtr )
@@ -73,9 +61,10 @@ static bool getIntPreference(CFStringRef key, int *valuePtr)
73
61
74
62
int main (void )
75
63
{
64
+ CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter ();
65
+ char observer ;
76
66
CFNotificationCenterAddObserver (
77
- CFNotificationCenterGetDistributedCenter (), NULL ,
78
- notificationCallback , AX_NOTIFICATION , NULL ,
67
+ center , & observer , notificationCallback , AX_NOTIFICATION , NULL ,
79
68
CFNotificationSuspensionBehaviorDeliverImmediately
80
69
);
81
70
CFDictionaryRef options = CFDictionaryCreate (
@@ -87,20 +76,21 @@ int main(void)
87
76
CFRelease (options );
88
77
if (!TRUSTED )
89
78
CFRunLoopRun ();
79
+ CFNotificationCenterRemoveObserver (center , & observer , AX_NOTIFICATION , NULL );
90
80
91
81
if (!getIntPreference (CFSTR ("lines" ), & LINES ))
92
82
LINES = DEFAULT_LINES ;
93
83
94
- TAP = CGEventTapCreate (
84
+ CFMachPortRef tap = CGEventTapCreate (
95
85
kCGSessionEventTap , kCGHeadInsertEventTap , kCGEventTapOptionDefault ,
96
86
CGEventMaskBit (kCGEventScrollWheel ), tapCallback , NULL
97
87
);
98
- if (!TAP )
88
+ if (!tap )
99
89
displayNoticeAndExit (CFSTR ("DiscreteScroll could not create an event tap." ));
100
- SOURCE = CFMachPortCreateRunLoopSource (kCFAllocatorDefault , TAP , 0 );
101
- if (!SOURCE )
90
+ CFRunLoopSourceRef source = CFMachPortCreateRunLoopSource (kCFAllocatorDefault , tap , 0 );
91
+ if (!source )
102
92
displayNoticeAndExit (CFSTR ("DiscreteScroll could not create a run loop source." ));
103
- CFRunLoopAddSource (CFRunLoopGetCurrent (), SOURCE , kCFRunLoopDefaultMode );
93
+ CFRunLoopAddSource (CFRunLoopGetCurrent (), source , kCFRunLoopDefaultMode );
104
94
CFRunLoopRun ();
105
95
106
96
return EXIT_SUCCESS ;
0 commit comments