Skip to content

Commit

Permalink
I think I fixed the bug where displayLink wouldn't start sometimes. D…
Browse files Browse the repository at this point in the history
…ispatching

the starting code on the main queue seems to prevent the issue from occurring.
  • Loading branch information
noah-nuebling committed May 5, 2020
1 parent 7f48ad0 commit 2252578
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Mouse Fix Helper/Config/config.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<dict>
<key>click</key>
<array>
<string>symbolicHotKey</string>
<integer>81</integer>
<string>swipeEvent</string>
<string>right</string>
</array>
</dict>
</dict>
Expand All @@ -30,8 +30,8 @@
<dict>
<key>click</key>
<array>
<string>symbolicHotKey</string>
<integer>79</integer>
<string>swipeEvent</string>
<string>left</string>
</array>
</dict>
</dict>
Expand Down
32 changes: 26 additions & 6 deletions Mouse Fix Helper/Scroll/SmoothScroll.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ + (void)stop {

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


// _pxStepSize = 20;
// double _pxPerMSBaseSpeed = 0.45;

// NSLog(@"1.");
// NSLog(@"CONSECCC: %d", ScrollUtility.consecutiveScrollTickCounter);
// NSLog(@"DLINK ON???: %d", CVDisplayLinkIsRunning(_displayLink));
// NSLog(@"DLINK PHASEEE: %d", _displayLinkPhase);
// NSLog(@"STARTEDD??: %d", _hasStarted);
// NSLog(@"SMOTHSTARTEDD??: %d", _hasStarted);

long long scrollDeltaAxis1 = CGEventGetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1);

Expand All @@ -152,6 +157,7 @@ + (void)handleInput:(CGEventRef)event info:(NSDictionary * _Nullable)info {

// Apply scroll wheel input to _pxScrollBuffer
_msLeftForScroll = _msPerStep;
// _msLeftForScroll = 1 / (_pxPerMSBaseSpeed / _pxStepSize);
if (scrollDeltaAxis1 > 0) {
_pxScrollBuffer += _pxStepSize * ScrollControl.scrollDirection;
} else if (scrollDeltaAxis1 < 0) {
Expand All @@ -171,23 +177,37 @@ + (void)handleInput:(CGEventRef)event info:(NSDictionary * _Nullable)info {
// 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
// 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);
while (CVDisplayLinkIsRunning(_displayLink) == NO) {
// Executing this on _scrollQueue (like the rest of this function) leads to `CVDisplayLinkStart()` failing sometimes. Once it has failed it will fail over and over again, taking a few minutes or so to start working again, if at all.
// Solution: I have no idea why this helps, but executing on the main queue does the trick! ^^
dispatch_sync(dispatch_get_main_queue(), ^{
CVReturn rt = CVDisplayLinkStart(_displayLink);
if (rt != kCVReturnSuccess) {
NSLog(@"Failed to start displayLink. Trying again.");
NSLog(@"Error code: %d", rt);
}
});
}
}

// NSLog(@"2.");
// NSLog(@"2 CONSECCC: %d", ScrollUtility.consecutiveScrollTickCounter);
// NSLog(@"2 DLINK ON???: %d", CVDisplayLinkIsRunning(_displayLink));
// NSLog(@"2 DLINK PHASEEE: %d", _displayLinkPhase);
// NSLog(@"2 STARTEDD??: %d", _hasStarted);
// NSLog(@" ");

}

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) {
NSLog(@"frameTimeSpike: %fms", msSinceLastFrame);
Expand Down
2 changes: 1 addition & 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>1405</string>
<string>1407</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2019 Noah Nuebling (MIT License)</string>
<key>NSMainNibFile</key>
Expand Down

0 comments on commit 2252578

Please sign in to comment.