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

[CGEvent] Use CGEventCreateScrollWheelEvent2 instead of CGEventCreateScrollWheelEvent. Fixes #13121. #20361

Merged
merged 4 commits into from
Apr 5, 2024
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
22 changes: 10 additions & 12 deletions src/CoreGraphics/CGEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,8 @@ public CGEvent (CGEventSource? source, ushort virtualKey, bool keyDown)
}

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary)]
extern static IntPtr CGEventCreateScrollWheelEvent (IntPtr source, CGScrollEventUnit units, uint /* uint32_t */ wheelCount, int /* uint32_t */ wheel1);
extern static IntPtr CGEventCreateScrollWheelEvent2 (IntPtr source, CGScrollEventUnit units, uint /* uint32_t */ wheelCount, int /* uint32_t */ wheel1, int /* uint32_t */ wheel2, int /* uint32_t */ wheel3);

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary)]
extern static IntPtr CGEventCreateScrollWheelEvent (IntPtr source, CGScrollEventUnit units, uint /* uint32_t */ wheelCount, int /* uint32_t */ wheel1, int /* uint32_t */ wheel2);

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary)]
extern static IntPtr CGEventCreateScrollWheelEvent (IntPtr source, CGScrollEventUnit units, uint /* uint32_t */ wheelCount, int /* uint32_t */ wheel1, int /* uint32_t */ wheel2, int /* uint32_t */ wheel3);

// This implementation doesn't work correctly on ARM64: https://github.com/xamarin/xamarin-macios/issues/13121
static IntPtr Create (CGEventSource? source, CGScrollEventUnit units, params int [] wheel)
{
IntPtr handle;
Expand All @@ -205,21 +198,26 @@ static IntPtr Create (CGEventSource? source, CGScrollEventUnit units, params int
case 0:
throw new ArgumentException ("At least one wheel must be provided");
case 1:
handle = CGEventCreateScrollWheelEvent (shandle, units, 1, wheel [0]);
handle = CGEventCreateScrollWheelEvent2 (shandle, units, 1, wheel [0], 0, 0);
break;
case 2:
handle = CGEventCreateScrollWheelEvent (shandle, units, 2, wheel [0], wheel [1]);
handle = CGEventCreateScrollWheelEvent2 (shandle, units, 2, wheel [0], wheel [1], 0);
break;
case 3:
handle = CGEventCreateScrollWheelEvent (shandle, units, 3, wheel [0], wheel [1], wheel [2]);
handle = CGEventCreateScrollWheelEvent2 (shandle, units, 3, wheel [0], wheel [1], wheel [2]);
break;
default:
throw new ArgumentException ("Only one to three wheels are supported on this constructor");
}
return handle;
}

public CGEvent (CGEventSource source, CGScrollEventUnit units, params int [] wheel)
/// <summary>Create a new scrolling event.</summary>
/// <returns>A new scrolling event.</returns>
/// <param name="source">An optional source for the new scrolling event.</param>
/// <param name="units">The unit of measurement for the new scrolling event.</param>
/// <param name="wheel">The wheel(s) the scrolling event refer to.</param>
public CGEvent (CGEventSource? source, CGScrollEventUnit units, params int [] wheel)
: base (Create (source, units, wheel), true)
{
}
Expand Down
1 change: 0 additions & 1 deletion tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49662,7 +49662,6 @@ M:CoreGraphics.CGDisplayStreamYCbCrMatrixOptionKeys.get_Itu_R_601_4
M:CoreGraphics.CGDisplayStreamYCbCrMatrixOptionKeys.get_Itu_R_709_2
M:CoreGraphics.CGDisplayStreamYCbCrMatrixOptionKeys.get_Smpte_240M_1995
M:CoreGraphics.CGEvent.#ctor(CoreGraphics.CGEventSource,CoreGraphics.CGEventType,CoreGraphics.CGPoint,CoreGraphics.CGMouseButton)
M:CoreGraphics.CGEvent.#ctor(CoreGraphics.CGEventSource,CoreGraphics.CGScrollEventUnit,System.Int32[])
M:CoreGraphics.CGEvent.#ctor(CoreGraphics.CGEventSource,System.UInt16,System.Boolean)
M:CoreGraphics.CGEvent.#ctor(CoreGraphics.CGEventSource)
M:CoreGraphics.CGEvent.#ctor(Foundation.NSData)
Expand Down
54 changes: 54 additions & 0 deletions tests/monotouch-test/CoreGraphics/CGEventTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

using Foundation;
#if MONOMAC
using AppKit;
Expand Down Expand Up @@ -45,6 +47,58 @@ public void CreateTapPSN ()

#if __MACOS__ || __MACCATALYST__
[Test]
public void Constructor_CGEventSourceStateID_0 ()
{
var ex = Assert.Throws<ArgumentException> (() => new CGEvent (null, CGScrollEventUnit.Pixel), "ArgumentException");
Assert.AreEqual ("At least one wheel must be provided", ex.Message, "Message");
}

[Test]
public void Constructor_CGEventSourceStateID_1 ()
{
using var evt = new CGEvent (null, CGScrollEventUnit.Pixel, 0);
Assert.AreEqual (CGEventType.ScrollWheel, evt.EventType, "EventType");
Assert.AreEqual (0, evt.Timestamp, "Timestamp");
// There doesn't seem to be any way to validate any creation
// arguments, except using CGEvent.ToData which returns an opaque
// byte array. Unfortunately the byte array changes randomly
// (timestamps in it maybe?), so it's not reliable enough for a
// test.
}

[Test]
public void Constructor_CGEventSourceStateID_2 ()
{
using var evt = new CGEvent (null, CGScrollEventUnit.Pixel, 0, 3);
Assert.AreEqual (CGEventType.ScrollWheel, evt.EventType, "EventType");
Assert.AreEqual (0, evt.Timestamp, "Timestamp");
// There doesn't seem to be any way to validate any creation
// arguments, except using CGEvent.ToData which returns an opaque
// byte array. Unfortunately the byte array changes randomly
// (timestamps in it maybe?), so it's not reliable enough for a
// test.
}

[Test]
public void Constructor_CGEventSourceStateID_3 ()
{
using var evt = new CGEvent (null, CGScrollEventUnit.Pixel, 0, 3, 9);
Assert.AreEqual (CGEventType.ScrollWheel, evt.EventType, "EventType");
Assert.AreEqual (0, evt.Timestamp, "Timestamp");
// There doesn't seem to be any way to validate any creation
// arguments, except using CGEvent.ToData which returns an opaque
// byte array. Unfortunately the byte array changes randomly
// (timestamps in it maybe?), so it's not reliable enough for a
// test.
}

[Test]
public void Constructor_CGEventSourceStateID_4 ()
{
var ex = Assert.Throws<ArgumentException> (() => new CGEvent (null, CGScrollEventUnit.Pixel, 0, 3, 9, 42), "ArgumentException");
Assert.AreEqual ("Only one to three wheels are supported on this constructor", ex.Message, "Message");
}

public void PostToPid ()
{
var pid = Process.GetCurrentProcess ().Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
!missing-pinvoke! CGDisplayUnitNumber is not bound
!missing-pinvoke! CGDisplayUsesOpenGLAcceleration is not bound
!missing-pinvoke! CGDisplayVendorNumber is not bound
!missing-pinvoke! CGEventCreateScrollWheelEvent2 is not bound
!missing-pinvoke! CGEventGetTypeID is not bound
!missing-pinvoke! CGEventSetDoubleValueField is not bound
!missing-pinvoke! CGEventSetIntegerValueField is not bound
Expand Down Expand Up @@ -104,3 +103,6 @@
!missing-pinvoke! CGWindowServerCreateServerPort is not bound
!missing-pinvoke! CGPreflightScreenCaptureAccess is not bound
!missing-pinvoke! CGRequestScreenCaptureAccess is not bound

# We're using CGEventCreateScrollWheelEvent2, which is identical, except not variadic.
!missing-pinvoke! CGEventCreateScrollWheelEvent is not bound
4 changes: 3 additions & 1 deletion tests/xtro-sharpie/macOS-CoreGraphics.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
!missing-pinvoke! CGDisplayUnitNumber is not bound
!missing-pinvoke! CGDisplayUsesOpenGLAcceleration is not bound
!missing-pinvoke! CGDisplayVendorNumber is not bound
!missing-pinvoke! CGEventCreateScrollWheelEvent2 is not bound
!missing-pinvoke! CGEventGetTypeID is not bound
!missing-pinvoke! CGEventSetDoubleValueField is not bound
!missing-pinvoke! CGEventSetIntegerValueField is not bound
Expand Down Expand Up @@ -104,3 +103,6 @@
!missing-pinvoke! CGWindowServerCreateServerPort is not bound
!missing-pinvoke! CGPreflightScreenCaptureAccess is not bound
!missing-pinvoke! CGRequestScreenCaptureAccess is not bound

# We're using CGEventCreateScrollWheelEvent2, which is identical, except not variadic.
!missing-pinvoke! CGEventCreateScrollWheelEvent is not bound
Loading