Skip to content

Commit

Permalink
Several improvements. Might have broken something. Might have fixed w…
Browse files Browse the repository at this point in the history
…eird bug where dosplayLink would fail to enable.
  • Loading branch information
noah-nuebling committed May 5, 2020
1 parent 4c4d42e commit cc86c34
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Mouse Fix Helper/Remap/MouseInputReceiver.m
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static void Handle_DeviceMatchingCallbackHID (void *context, IOReturn result, vo
_relevantDevicesAreAttached = TRUE;
[MomentumScroll decide];
NSLog(@"MomentumScroll.isEnabled: %hhd", MomentumScroll.isEnabled);
NSLog(@"MomentumScroll.isRunning: %hhd", MomentumScroll.isRunning);
NSLog(@"MomentumScroll.hasStarted: %hhd", MomentumScroll.hasStarted);
Expand Down
28 changes: 19 additions & 9 deletions Mouse Fix Helper/Scroll/ScrollControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ static CGEventRef eventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEv
scrollPhase != 0) { // adding scrollphase here is untested
return event;
}

// Check if scrolling direction changed

[ScrollUtility updateScrollDirectionDidChange:scrollDeltaAxis1];

if (ScrollUtility.scrollDirectionDidChange) {
[ScrollUtility resetConsecutiveTicksAndSwipes];
}

// Create a copy, because the original event will become invalid and unusable in the new thread.
CGEventRef eventCopy = [ScrollUtility createScrollEventWithValuesFromEvent:event];

Expand All @@ -198,14 +207,7 @@ static CGEventRef eventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEv
// With multithreading enabled, scrolling sometimes - seemingly at random - stops working entirely. So the tap still works but sending events doesn't.
// - Switching to an app that doesn't have smothscroll enabled seems to fix it. -> Somethings in my code must be breaking
dispatch_async(_scrollQueue, ^{

// Check if scrolling direction changed

[ScrollUtility updateScrollDirectionDidChange:scrollDeltaAxis1];

if (ScrollUtility.scrollDirectionDidChange) {
[ScrollUtility resetConsecutiveTicksAndSwipes];
}


// Set application overrides

Expand All @@ -219,15 +221,23 @@ static CGEventRef eventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEv
}
if (ScrollUtility.mouseDidMove || ScrollUtility.frontMostAppDidChange) {
// set app overrides
[ConfigFileInterface_HelperApp updateInternalParameters_Force:NO];
BOOL configChanged = [ConfigFileInterface_HelperApp updateInternalParameters_Force:NO]; // TODO: `updateInternalParameters_Force:` should (probably) reset stuff itself, if it changes anything. This whole [SmoothScroll stop] stuff is kinda messy
if (configChanged) {
[SmoothScroll stop]; // Not sure if useful
[RoughScroll stop]; // Not sure if useful
}
}
}

// Process event

if (_isSmoothEnabled) {
[SmoothScroll start]; // Not sure if useful
[RoughScroll stop]; // Not sure if useful
[SmoothScroll handleInput:eventCopy info:NULL];
} else {
[SmoothScroll stop]; // Not sure if useful
[RoughScroll stop]; // Not sure if useful
[RoughScroll handleInput:eventCopy info:NULL];
}
CFRelease(eventCopy);
Expand Down
2 changes: 1 addition & 1 deletion Mouse Fix Helper/Scroll/ScrollUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ + (BOOL)scrollDirectionDidChange {
static long long _previousScrollValue;
/// Checks whether the sign of input number is different from when this function was last called. Writes result into `_frontMostAppDidChange`.
+ (void)updateScrollDirectionDidChange:(long long)thisScrollValue {
BOOL _scrollDirectionDidChange = NO;
_scrollDirectionDidChange = NO;
if (![ScrollUtility sameSign:thisScrollValue and:_previousScrollValue]) {
_scrollDirectionDidChange = YES;
}
Expand Down
2 changes: 1 addition & 1 deletion Mouse Fix Helper/Scroll/SmoothScroll.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

+ (void)start;
+ (void)stop;
+ (BOOL)isRunning;
+ (BOOL)hasStarted;
+ (BOOL)isScrolling;

+ (void)handleInput:(CGEventRef _Nonnull)event info:(NSDictionary * _Nullable)info;
Expand Down
56 changes: 30 additions & 26 deletions Mouse Fix Helper/Scroll/SmoothScroll.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,28 @@ + (void)configureWithParameters:(NSDictionary *)params {
+ (BOOL)isScrolling {
return _isScrolling;
}
static BOOL _isRunning;
+ (BOOL)isRunning {
return _isRunning;
static BOOL _hasStarted;
+ (BOOL)hasStarted {
return _hasStarted;
}
+ (void)start {

if (_isRunning) {
if (_hasStarted) {
return;
}
NSLog(@"SmoothScroll started");

_isRunning = YES;
_hasStarted = YES;
[SmoothScroll resetDynamicGlobals];
CGDisplayRemoveReconfigurationCallback(Handle_displayReconfiguration, NULL); // don't know if necesssary
CGDisplayRegisterReconfigurationCallback(Handle_displayReconfiguration, NULL);
}
+ (void)stop {

if (!_hasStarted) {
return;
}
NSLog(@"SmoothScroll stopped");

_isRunning = NO;
_hasStarted = NO;
_isScrolling = NO;
CVDisplayLinkStop(_displayLink);
CGDisplayRemoveReconfigurationCallback(Handle_displayReconfiguration, NULL);
Expand All @@ -122,22 +123,12 @@ + (void)stop {

+ (void)handleInput:(CGEventRef)event info:(NSDictionary * _Nullable)info {

long long scrollDeltaAxis1 = CGEventGetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1);
// NSLog(@"CONSECCC: %d", ScrollUtility.consecutiveScrollTickCounter);
// NSLog(@"DLINK ON???: %d", CVDisplayLinkIsRunning(_displayLink));
// NSLog(@"DLINK PHASEEE: %d", _displayLinkPhase);
// NSLog(@"STARTEDD??: %d", _hasStarted);

// Stuff you wanna do on the first tick of each series of consecutive scroll ticks.
if (ScrollUtility.consecutiveScrollTickCounter == 0) {
if (ScrollUtility.mouseDidMove) {
// set diplaylink to the display that is actally being scrolled - not sure if this is necessary, because having the displaylink at 30fps on a 30fps display looks just as horrible as having the display link on 60fps, if not worse
@try {
setDisplayLinkToDisplayUnderMousePointer(event);
} @catch (NSException *e) {
NSLog(@"Error while trying to set display link to display under mouse pointer: %@", [e reason]);
}
}
if (CVDisplayLinkIsRunning(_displayLink) == FALSE) {
CVDisplayLinkStart(_displayLink);
}
}
long long scrollDeltaAxis1 = CGEventGetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1);

// Update global vars

Expand Down Expand Up @@ -176,11 +167,26 @@ + (void)handleInput:(CGEventRef)event info:(NSDictionary * _Nullable)info {
if (ScrollUtility.consecutiveScrollSwipeCounter > ScrollControl.fastScrollThreshold_inSwipes) {
_pxScrollBuffer = _pxScrollBuffer * pow(ScrollControl.fastScrollExponentialBase, (int32_t)ScrollUtility.consecutiveScrollSwipeCounter - ScrollControl.fastScrollThreshold_inSwipes);
}

// Start displaylink and stuff
if (ScrollUtility.consecutiveScrollTickCounter == 0) {
if (ScrollUtility.mouseDidMove) {
// set diplaylink to the display that is actally being scrolled - not sure if this is necessary, because having the displaylink at 30fps on a 30fps display looks just as horrible as having the display link on 60fps, if not worse
@try {
setDisplayLinkToDisplayUnderMousePointer(event);
} @catch (NSException *e) {
NSLog(@"Error while trying to set display link to display under mouse pointer: %@", [e reason]);
}
}
if (CVDisplayLinkIsRunning(_displayLink) == FALSE) {
CVDisplayLinkStart(_displayLink);
}
}
}

static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {


// NSLog(@"DPL");

double msSinceLastFrame = CVDisplayLinkGetActualOutputVideoRefreshPeriod(_displayLink) * 1000;
if (msSinceLastFrame != 16.674562) {
Expand Down Expand Up @@ -348,9 +354,7 @@ static void setDisplayLinkToDisplayUnderMousePointer(CGEventRef event) {
NSException *e = [NSException exceptionWithName:NSInternalInconsistencyException reason:@"there are 0 diplays under the mouse pointer" userInfo:NULL];
@throw e;
}

free(newDisplaysUnderMousePointer);

}

@end
Expand Down
4 changes: 3 additions & 1 deletion Mouse Fix/SupportFiles/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>0.9.2 Beta 2</string>
<key>CFBundleVersion</key>
<string>1403</string>
<string>1404</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2019 Noah Nuebling (MIT License)</string>
<key>NSMainNibFile</key>
Expand All @@ -30,5 +30,7 @@
<false/>
<key>NSPrincipalClass</key>
<string>PrefPaneDelegate</string>
<key>NSRequiresAquaSystemAppearance</key>
<false/>
</dict>
</plist>

0 comments on commit cc86c34

Please sign in to comment.