From 47c4afd6b4e474e3b8c2c2040e3bf9542c400ec4 Mon Sep 17 00:00:00 2001 From: chutneyio Date: Sun, 26 Nov 2023 20:07:45 +0700 Subject: [PATCH 1/3] Set mouse delta to move and drag event (MacOS) --- .../Interop/Input/MacOSVirtualMouse.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs b/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs index 391c4f3bf..2722de26a 100644 --- a/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs +++ b/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs @@ -13,6 +13,8 @@ public abstract class MacOSVirtualMouse : IMouseButtonHandler, ISynchronousPoint private int prevButtonStates; private float? pendingX; private float? pendingY; + private float? _lastX; + private float? _lastY; private CGMouseButton lastButton; private IntPtr mouseEvent = CGEventCreate(IntPtr.Zero); @@ -39,6 +41,16 @@ public void Flush() // can send drag here var lastButtonSet = IsButtonSet(currButtonStates, lastButton); var cgEventType = ToDragCGEventType(lastButton, lastButtonSet); + + if (pendingX.HasValue && _lastX.HasValue && pendingY.HasValue && _lastY.HasValue) + { + // set mouse delta between current and previous mouse position + var deltaX = pendingX.Value - _lastX.Value; + var deltaY = pendingY.Value - _lastY.Value; + CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, deltaX); + CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, deltaY); + } + CGEventSetType(mouseEvent, cgEventType); CGEventPost(CGEventTapLocation.kCGHIDEventTap, mouseEvent); } @@ -52,6 +64,10 @@ public void Reset() ProcessKeyStates(currButtonStates, 0); prevButtonStates = 0; currButtonStates = 0; + pendingX = null; + pendingY = null; + _lastX = null; + _lastY = null; } } @@ -60,6 +76,8 @@ public void Reset() protected void QueuePendingPosition(float x, float y) { + _lastX = pendingX; + _lastY = pendingY; pendingX = x; pendingY = y; } @@ -69,8 +87,6 @@ private bool DrainPendingPosition() if (pendingX.HasValue) { SetPendingPosition(mouseEvent, pendingX.Value, pendingY.Value); - pendingX = null; - pendingY = null; return true; } From 5d3d8e207a20c152e06b669ab40844a2afa79600 Mon Sep 17 00:00:00 2001 From: chutneyio Date: Sun, 26 Nov 2023 21:29:24 +0700 Subject: [PATCH 2/3] Set delta for mouse event in absolute pointer mode --- .../Input/Absolute/MacOSAbsolutePointer.cs | 8 ++++++++ .../Interop/Input/MacOSVirtualMouse.cs | 20 ++----------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs b/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs index ed27f4360..5e93eb806 100644 --- a/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs +++ b/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Numerics; using OpenTabletDriver.Native.OSX; +using OpenTabletDriver.Native.OSX.Input; using OpenTabletDriver.Plugin.Platform.Pointer; namespace OpenTabletDriver.Desktop.Interop.Input.Absolute @@ -11,6 +12,8 @@ namespace OpenTabletDriver.Desktop.Interop.Input.Absolute public class MacOSAbsolutePointer : MacOSVirtualMouse, IAbsolutePointer { private Vector2 offset; + private Vector2 lastPos; + private Vector2 delta; public MacOSAbsolutePointer() { @@ -21,12 +24,17 @@ public MacOSAbsolutePointer() public void SetPosition(Vector2 pos) { var newPos = pos - offset; + delta = newPos - lastPos; + lastPos = newPos; + QueuePendingPosition(newPos.X, newPos.Y); } protected override void SetPendingPosition(IntPtr mouseEvent, float x, float y) { CGEventSetLocation(mouseEvent, new CGPoint(x, y)); + CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, delta.X); + CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, delta.Y); } protected override void ResetPendingPosition(IntPtr mouseEvent) diff --git a/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs b/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs index 2722de26a..391c4f3bf 100644 --- a/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs +++ b/OpenTabletDriver.Desktop/Interop/Input/MacOSVirtualMouse.cs @@ -13,8 +13,6 @@ public abstract class MacOSVirtualMouse : IMouseButtonHandler, ISynchronousPoint private int prevButtonStates; private float? pendingX; private float? pendingY; - private float? _lastX; - private float? _lastY; private CGMouseButton lastButton; private IntPtr mouseEvent = CGEventCreate(IntPtr.Zero); @@ -41,16 +39,6 @@ public void Flush() // can send drag here var lastButtonSet = IsButtonSet(currButtonStates, lastButton); var cgEventType = ToDragCGEventType(lastButton, lastButtonSet); - - if (pendingX.HasValue && _lastX.HasValue && pendingY.HasValue && _lastY.HasValue) - { - // set mouse delta between current and previous mouse position - var deltaX = pendingX.Value - _lastX.Value; - var deltaY = pendingY.Value - _lastY.Value; - CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, deltaX); - CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, deltaY); - } - CGEventSetType(mouseEvent, cgEventType); CGEventPost(CGEventTapLocation.kCGHIDEventTap, mouseEvent); } @@ -64,10 +52,6 @@ public void Reset() ProcessKeyStates(currButtonStates, 0); prevButtonStates = 0; currButtonStates = 0; - pendingX = null; - pendingY = null; - _lastX = null; - _lastY = null; } } @@ -76,8 +60,6 @@ public void Reset() protected void QueuePendingPosition(float x, float y) { - _lastX = pendingX; - _lastY = pendingY; pendingX = x; pendingY = y; } @@ -87,6 +69,8 @@ private bool DrainPendingPosition() if (pendingX.HasValue) { SetPendingPosition(mouseEvent, pendingX.Value, pendingY.Value); + pendingX = null; + pendingY = null; return true; } From 55d24f67218d727e0e476be789405a85cfa18d29 Mon Sep 17 00:00:00 2001 From: X9VoiD Date: Sun, 26 Nov 2023 23:06:32 +0800 Subject: [PATCH 3/3] Emit delta only when it makes sense --- .../Interop/Input/Absolute/MacOSAbsolutePointer.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs b/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs index 5e93eb806..54f0a62bc 100644 --- a/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs +++ b/OpenTabletDriver.Desktop/Interop/Input/Absolute/MacOSAbsolutePointer.cs @@ -12,8 +12,8 @@ namespace OpenTabletDriver.Desktop.Interop.Input.Absolute public class MacOSAbsolutePointer : MacOSVirtualMouse, IAbsolutePointer { private Vector2 offset; - private Vector2 lastPos; - private Vector2 delta; + private Vector2? lastPos; + private Vector2? delta; public MacOSAbsolutePointer() { @@ -33,12 +33,17 @@ public void SetPosition(Vector2 pos) protected override void SetPendingPosition(IntPtr mouseEvent, float x, float y) { CGEventSetLocation(mouseEvent, new CGPoint(x, y)); - CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, delta.X); - CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, delta.Y); + if (delta is not null) + { + CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, delta.Value.X); + CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, delta.Value.Y); + } } protected override void ResetPendingPosition(IntPtr mouseEvent) { + lastPos = null; + delta = null; } } }