Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix for Issue #5: Only invert scroll events if our CGSession is active.

  • Loading branch information...
commit 17007bdd22e110b2cb49d8b11b00630df4e6488e 1 parent 1e8b4fc
Jim Correia authored
2  Source/ScrollingInverterEventTap.h
@@ -10,6 +10,7 @@
10 10 @interface ScrollingInverterEventTap : NSObject {
11 11 @private
12 12 BOOL _enabled;
  13 + BOOL _sessionActive;
13 14 CFMachPortRef _eventTapMachPort;
14 15 CFRunLoopSourceRef _eventTapRunLoopSource;
15 16 }
@@ -19,5 +20,6 @@
19 20 - (void)invalidate;
20 21
21 22 @property (nonatomic, getter=isEnabled) BOOL enabled;
  23 +@property (nonatomic, readonly, getter=isSessionActive) BOOL sessionActive;
22 24
23 25 @end
26 Source/ScrollingInverterEventTap.m
@@ -13,6 +13,9 @@ @interface ScrollingInverterEventTap ()
13 13
14 14 - (CGEventRef)_eventTapCallbackWithProxy:(CGEventTapProxy)proxy type:(CGEventType)type event:(CGEventRef)event;
15 15
  16 +- (void)_workspaceSessionDidBecomeActive:(NSNotification *)notification;
  17 +- (void)_workspaceSessionDidResignActive:(NSNotification *)notification;
  18 +
16 19 @end
17 20
18 21 @implementation ScrollingInverterEventTap
@@ -24,9 +27,14 @@ - (id)init;
24 27 return nil;
25 28
26 29 _enabled = YES;
  30 + _sessionActive = YES;
27 31 _eventTapMachPort = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMaskBit(kCGEventScrollWheel), _eventTap, self);
28 32 _eventTapRunLoopSource = CFMachPortCreateRunLoopSource(NULL, _eventTapMachPort, 0);
29 33 CFRunLoopAddSource(CFRunLoopGetCurrent(), _eventTapRunLoopSource, kCFRunLoopCommonModes);
  34 +
  35 + NSNotificationCenter *notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
  36 + [notificationCenter addObserver:self selector:@selector(_workspaceSessionDidBecomeActive:) name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];
  37 + [notificationCenter addObserver:self selector:@selector(_workspaceSessionDidResignActive:) name:NSWorkspaceSessionDidResignActiveNotification object:nil];
30 38
31 39 return self;
32 40 }
@@ -39,6 +47,9 @@ - (void)dealloc;
39 47
40 48 - (void)invalidate;
41 49 {
  50 + NSNotificationCenter *notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
  51 + [notificationCenter removeObserver:self];
  52 +
42 53 if (_eventTapMachPort) {
43 54 CFMachPortInvalidate(_eventTapMachPort);
44 55 CFRelease(_eventTapMachPort);
@@ -53,6 +64,7 @@ - (void)invalidate;
53 64 }
54 65
55 66 @synthesize enabled = _enabled;
  67 +@synthesize sessionActive = _sessionActive;
56 68
57 69 #pragma mark -
58 70 #pragma mark Private
@@ -67,7 +79,7 @@ - (CGEventRef)_eventTapCallbackWithProxy:(CGEventTapProxy)proxy type:(CGEventTyp
67 79 {
68 80 switch (type) {
69 81 case kCGEventScrollWheel: {
70   - if ([self isEnabled]) {
  82 + if ([self isEnabled] && [self isSessionActive]) {
71 83 int64_t deltaAxis1 = CGEventGetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1);;
72 84 int64_t deltaAxis2 = CGEventGetIntegerValueField(event, kCGScrollWheelEventDeltaAxis2);;
73 85 double fixedPtDeltaAxis1 = CGEventGetDoubleValueField(event, kCGScrollWheelEventFixedPtDeltaAxis1);
@@ -107,4 +119,16 @@ - (CGEventRef)_eventTapCallbackWithProxy:(CGEventTapProxy)proxy type:(CGEventTyp
107 119 return event;
108 120 }
109 121
  122 +- (void)_workspaceSessionDidBecomeActive:(NSNotification *)notification;
  123 +{
  124 + _sessionActive = YES;
  125 +}
  126 +
  127 +- (void)_workspaceSessionDidResignActive:(NSNotification *)notification;
  128 +{
  129 + _sessionActive = NO;
  130 +}
  131 +
110 132 @end
  133 +
  134 +

0 comments on commit 17007bd

Please sign in to comment.
Something went wrong with that request. Please try again.