/
MacOSRelativePointer.cs
49 lines (42 loc) · 1.6 KB
/
MacOSRelativePointer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Linq;
using System.Numerics;
using OpenTabletDriver.Native.MacOS;
using OpenTabletDriver.Native.MacOS.Input;
using OpenTabletDriver.Platform.Display;
using OpenTabletDriver.Platform.Pointer;
namespace OpenTabletDriver.Desktop.Interop.Input.Relative
{
using static MacOS;
public class MacOSRelativePointer : MacOSVirtualMouse, IRelativePointer
{
private CGPoint offset;
public MacOSRelativePointer(IVirtualScreen virtualScreen)
{
var primary = virtualScreen.Displays.First();
offset = new CGPoint(primary.Position.X, primary.Position.Y);
}
public void SetPosition(Vector2 delta)
{
QueuePendingPosition(delta.X, delta.Y);
}
protected override void SetPendingPosition(IntPtr mouseEvent, float x, float y)
{
CGEventSetLocation(mouseEvent, GetCursorPosition() + new CGPoint(x, y));
CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, x);
CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, y);
}
protected override void ResetPendingPosition(IntPtr mouseEvent)
{
CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaX, 0);
CGEventSetDoubleValueField(mouseEvent, CGEventField.mouseEventDeltaY, 0);
}
private CGPoint GetCursorPosition()
{
var eventRef = CGEventCreate(IntPtr.Zero);
var pos = CGEventGetLocation(eventRef) + offset;
CFRelease(eventRef);
return pos;
}
}
}