Skip to content

Commit ad713f0

Browse files
committed
Create calculateDeltas function and use it in dragMouse.
1 parent e3f084e commit ad713f0

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/mouse.c

+26-13
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,12 @@
5757
#endif
5858

5959
/**
60-
* Move the mouse to a specific point.
61-
* @param point The coordinates to move the mouse to (x, y).
60+
* Calculate the delta for a mouse move and add them to the event.
61+
* @param event The mouse move event (by ref).
62+
* @param point The new mouse x and y.
6263
*/
63-
void moveMouse(MMPoint point)
64+
void calculateDeltas(CGEventRef *event, MMPoint point)
6465
{
65-
#if defined(IS_MACOSX)
66-
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
67-
CGPointFromMMPoint(point),
68-
kCGMouseButtonLeft);
69-
70-
7166
/**
7267
* The next few lines are a workaround for games not detecting mouse moves.
7368
* See this issue for more information:
@@ -80,11 +75,27 @@ void moveMouse(MMPoint point)
8075
int64_t deltaX = point.x - mouse.x;
8176
int64_t deltaY = point.y - mouse.y;
8277

83-
CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, deltaX);
84-
CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, deltaY);
78+
CGEventSetIntegerValueField(*event, kCGMouseEventDeltaX, deltaX);
79+
CGEventSetIntegerValueField(*event, kCGMouseEventDeltaY, deltaY);
8580

86-
CGEventPost(kCGSessionEventTap, move);
8781
CFRelease(get);
82+
}
83+
84+
85+
/**
86+
* Move the mouse to a specific point.
87+
* @param point The coordinates to move the mouse to (x, y).
88+
*/
89+
void moveMouse(MMPoint point)
90+
{
91+
#if defined(IS_MACOSX)
92+
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
93+
CGPointFromMMPoint(point),
94+
kCGMouseButtonLeft);
95+
96+
calculateDeltas(&move, point);
97+
98+
CGEventPost(kCGSessionEventTap, move);
8899
CFRelease(move);
89100
#elif defined(USE_X11)
90101
Display *display = XGetMainDisplay();
@@ -104,9 +115,11 @@ void dragMouse(MMPoint point, const MMMouseButton button)
104115
{
105116
#if defined(IS_MACOSX)
106117
const CGEventType dragType = MMMouseDragToCGEventType(button);
107-
const CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
118+
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
108119
CGPointFromMMPoint(point),
109120
(CGMouseButton)button);
121+
calculateDeltas(&drag, point);
122+
110123
CGEventPost(kCGSessionEventTap, drag);
111124
CFRelease(drag);
112125
#else

0 commit comments

Comments
 (0)