Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scrolling in WebKit #141

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CoreGraphicsSPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is part of Scroll Reverser <https://pilotmoon.com/scrollreverser/>
// Licensed under Apache License v2.0 <http://www.apache.org/licenses/LICENSE-2.0>

#ifndef CoreGraphicsSPI_h
#define CoreGraphicsSPI_h

IOHIDEventRef CGEventCopyIOHIDEvent(CGEventRef);

#endif /* CoreGraphicsSPI_h */
46 changes: 46 additions & 0 deletions IOKitSPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of Scroll Reverser <https://pilotmoon.com/scrollreverser/>
// Licensed under Apache License v2.0 <http://www.apache.org/licenses/LICENSE-2.0>

#ifndef IOKitSPI_h
#define IOKitSPI_h

typedef struct __IOHIDEvent * IOHIDEventRef;

enum {
kIOHIDEventTypeNULL,
kIOHIDEventTypeVendorDefined,
kIOHIDEventTypeKeyboard = 3,
kIOHIDEventTypeRotation = 5,
kIOHIDEventTypeScroll = 6,
kIOHIDEventTypeZoom = 8,
kIOHIDEventTypeDigitizer = 11,
kIOHIDEventTypeNavigationSwipe = 16,
kIOHIDEventTypeForce = 32,
};
typedef uint32_t IOHIDEventType;

typedef uint32_t IOHIDEventField;
typedef uint64_t IOHIDEventSenderID;


enum {
kIOHIDEventScrollMomentumInterrupted = (1 << 4),
};
typedef uint8_t IOHIDEventScrollMomentumBits;

#ifdef __LP64__
typedef double IOHIDFloat;
#else
typedef float IOHIDFloat;
#endif

#define IOHIDEventFieldBase(type) (type << 16)

#define kIOHIDEventFieldScrollBase IOHIDEventFieldBase(kIOHIDEventTypeScroll)
static const IOHIDEventField kIOHIDEventFieldScrollX = (kIOHIDEventFieldScrollBase | 0);
static const IOHIDEventField kIOHIDEventFieldScrollY = (kIOHIDEventFieldScrollBase | 1);

IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef, IOHIDEventField);
void IOHIDEventSetFloatValue(IOHIDEventRef, IOHIDEventField, IOHIDFloat);

#endif /* IOKitSPI_h */
20 changes: 20 additions & 0 deletions MouseTap.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ static CGEventRef _callback(CGEventTapProxy proxy,
}
else if (type==(CGEventType)NSEventTypeScrollWheel)
{
IOHIDEventRef const ioHidEventRef=CGEventCopyIOHIDEvent(eventRef);

// is inverted from device? (1=natural scrolling, 0=classic scrolling)
const BOOL invertedFromDevice=!![event isDirectionInvertedFromDevice];
[tap->logger logBool:invertedFromDevice forKey:@"ifd"];
Expand All @@ -93,12 +95,20 @@ static CGEventRef _callback(CGEventTapProxy proxy,
const int64_t point_axis2=CGEventGetIntegerValueField(eventRef, kCGScrollWheelEventPointDeltaAxis2);
const double fixedpt_axis1=CGEventGetDoubleValueField(eventRef, kCGScrollWheelEventFixedPtDeltaAxis1);
const double fixedpt_axis2=CGEventGetDoubleValueField(eventRef, kCGScrollWheelEventFixedPtDeltaAxis2);
IOHIDFloat iohid_axis1=0;
IOHIDFloat iohid_axis2=0;
if (ioHidEventRef) {
iohid_axis1=IOHIDEventGetFloatValue(ioHidEventRef, kIOHIDEventFieldScrollY);
iohid_axis2=IOHIDEventGetFloatValue(ioHidEventRef, kIOHIDEventFieldScrollX);
}
[tap->logger logSignedInteger:axis1 forKey:@"y"];
[tap->logger logSignedInteger:axis2 forKey:@"x"];
[tap->logger logSignedInteger:point_axis1 forKey:@"y_pt"];
[tap->logger logSignedInteger:point_axis2 forKey:@"x_pt"];
[tap->logger logDouble:fixedpt_axis1 forKey:@"y_fp"];
[tap->logger logDouble:fixedpt_axis2 forKey:@"x_fp"];
[tap->logger logDouble:iohid_axis1 forKey:@"y_iohid"];
[tap->logger logDouble:iohid_axis2 forKey:@"x_iohid"];

// get source pid
const uint64_t pid=CGEventGetIntegerValueField(eventRef, kCGEventSourceUnixProcessID);
Expand Down Expand Up @@ -197,11 +207,21 @@ This is because setting DeltaAxis causes macos to internally modify PointDeltaAx
if (!discreteAdjust&&vmul!=1) { // vertical - only set these if not doing discrete adjust
CGEventSetDoubleValueField(eventRef, kCGScrollWheelEventFixedPtDeltaAxis1, fixedpt_axis1*vmul);
CGEventSetIntegerValueField(eventRef, kCGScrollWheelEventPointDeltaAxis1, point_axis1*vmul);
if (ioHidEventRef) {
IOHIDEventSetFloatValue(ioHidEventRef, kIOHIDEventFieldScrollY, iohid_axis1*vmul);
}
}
if (hmul!=1) { // horizontal
CGEventSetIntegerValueField(eventRef, kCGScrollWheelEventDeltaAxis2, axis2*hmul);
CGEventSetDoubleValueField(eventRef, kCGScrollWheelEventFixedPtDeltaAxis2, fixedpt_axis2*hmul);
CGEventSetIntegerValueField(eventRef, kCGScrollWheelEventPointDeltaAxis2, point_axis2*hmul);
if (ioHidEventRef) {
IOHIDEventSetFloatValue(ioHidEventRef, kIOHIDEventFieldScrollX, iohid_axis2*vmul);
}
}

if (ioHidEventRef) {
CFRelease(ioHidEventRef);
}
}
else
Expand Down
4 changes: 4 additions & 0 deletions ScrollReverser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@
88AC80572405185D00063181 /* es */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = es; path = resources/es.lproj/Localizable.strings; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* ScrollReverser-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ScrollReverser-Info.plist"; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* Scroll Reverser (Dev).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Scroll Reverser (Dev).app"; sourceTree = BUILT_PRODUCTS_DIR; };
B7CF5D6627BFAB8400E09485 /* IOKitSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOKitSPI.h; sourceTree = "<group>"; };
B7CF5D6727BFACD800E09485 /* CoreGraphicsSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CoreGraphicsSPI.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -302,6 +304,8 @@
isa = PBXGroup;
children = (
256AC3F00F4B6AF500CF3369 /* Scroll_Inverter_Prefix.pch */,
B7CF5D6627BFAB8400E09485 /* IOKitSPI.h */,
B7CF5D6727BFACD800E09485 /* CoreGraphicsSPI.h */,
29B97316FDCFA39411CA2CEA /* main.m */,
);
name = "Other Sources";
Expand Down
2 changes: 2 additions & 0 deletions Scroll_Inverter_Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#import "IOKitSPI.h"
#import "CoreGraphicsSPI.h"

#if !defined(DEBUG)
#define NSLog(...)
Expand Down