66static const CFStringRef AX_NOTIFICATION = CFSTR ("com.apple.accessibility.api" );
77static bool TRUSTED ;
88
9- static CFMachPortRef TAP ;
10- static CFRunLoopSourceRef SOURCE ;
11-
129static int LINES ;
1310
1411static CGEventRef tapCallback (CGEventTapProxy proxy ,
@@ -37,23 +34,14 @@ static void notificationCallback(CFNotificationCenterRef center, void *observer,
3734 CFNotificationName name , const void * object ,
3835 CFDictionaryRef userInfo )
3936{
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+ );
5745}
5846
5947static bool getIntPreference (CFStringRef key , int * valuePtr )
@@ -73,9 +61,10 @@ static bool getIntPreference(CFStringRef key, int *valuePtr)
7361
7462int main (void )
7563{
64+ CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter ();
65+ char observer ;
7666 CFNotificationCenterAddObserver (
77- CFNotificationCenterGetDistributedCenter (), NULL ,
78- notificationCallback , AX_NOTIFICATION , NULL ,
67+ center , & observer , notificationCallback , AX_NOTIFICATION , NULL ,
7968 CFNotificationSuspensionBehaviorDeliverImmediately
8069 );
8170 CFDictionaryRef options = CFDictionaryCreate (
@@ -87,20 +76,21 @@ int main(void)
8776 CFRelease (options );
8877 if (!TRUSTED )
8978 CFRunLoopRun ();
79+ CFNotificationCenterRemoveObserver (center , & observer , AX_NOTIFICATION , NULL );
9080
9181 if (!getIntPreference (CFSTR ("lines" ), & LINES ))
9282 LINES = DEFAULT_LINES ;
9383
94- TAP = CGEventTapCreate (
84+ CFMachPortRef tap = CGEventTapCreate (
9585 kCGSessionEventTap , kCGHeadInsertEventTap , kCGEventTapOptionDefault ,
9686 CGEventMaskBit (kCGEventScrollWheel ), tapCallback , NULL
9787 );
98- if (!TAP )
88+ if (!tap )
9989 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 )
10292 displayNoticeAndExit (CFSTR ("DiscreteScroll could not create a run loop source." ));
103- CFRunLoopAddSource (CFRunLoopGetCurrent (), SOURCE , kCFRunLoopDefaultMode );
93+ CFRunLoopAddSource (CFRunLoopGetCurrent (), source , kCFRunLoopDefaultMode );
10494 CFRunLoopRun ();
10595
10696 return EXIT_SUCCESS ;
0 commit comments