From 6aadc5a0aa1e0ed01a7ce384dfd79bde56c36f9a Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 13:08:37 +0100 Subject: [PATCH 1/8] Add Helper Methods for calculating the required absolute position on the screen --- src/win32/mouse.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index 0fb7eed..df101da 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -20,6 +20,18 @@ : ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \ : MOUSEEVENTF_MIDDLEDOWN)) +int CalculateAbsoluteCoordinateX(int x) +{ + MMSize displaySize = getMainDisplaySize(); + return (x * 65536) / displaySize.width; +} + +int CalculateAbsoluteCoordinateY(int y) +{ + MMSize displaySize = getMainDisplaySize(); + return (y * 65536) / displaySize.height; +} + /** * Move the mouse to a specific point. * @param point The coordinates to move the mouse to (x, y). From 5cf4c8fb04a43bc0b7bfc000a3aaa87aff648a58 Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 13:09:09 +0100 Subject: [PATCH 2/8] Refactor moveMouse to use SendInput for absolute position movements on windows --- src/win32/mouse.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index df101da..89180f0 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -38,7 +38,15 @@ int CalculateAbsoluteCoordinateY(int y) */ void moveMouse(MMPoint point) { - SetCursorPos ((int)point.x, (int)point.y); + INPUT mouseInput; + mouseInput.type = INPUT_MOUSE; + mouseInput.mi.dx = CalculateAbsoluteCoordinateX(point.x); + mouseInput.mi.dy = CalculateAbsoluteCoordinateY(point.y); + mouseInput.mi.mouseData = 0; + mouseInput.mi.time = 0; + mouseInput.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_ABSOLUTE; + mouseInput.mi.dwExtraInfo = 0; + SendInput(1, &mouseInput, sizeof(mouseInput)); } void dragMouse(MMPoint point, const MMMouseButton button) From d3634ba4015c03feb5f31f353b62fd41b0798adf Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 13:11:59 +0100 Subject: [PATCH 3/8] Indentation --- src/win32/mouse.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index 89180f0..c38af34 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -20,33 +20,30 @@ : ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \ : MOUSEEVENTF_MIDDLEDOWN)) -int CalculateAbsoluteCoordinateX(int x) -{ +int CalculateAbsoluteCoordinateX(int x) { MMSize displaySize = getMainDisplaySize(); - return (x * 65536) / displaySize.width; + return (x * 65536) / displaySize.width; } -int CalculateAbsoluteCoordinateY(int y) -{ +int CalculateAbsoluteCoordinateY(int y) { MMSize displaySize = getMainDisplaySize(); - return (y * 65536) / displaySize.height; + return (y * 65536) / displaySize.height; } /** * Move the mouse to a specific point. * @param point The coordinates to move the mouse to (x, y). */ -void moveMouse(MMPoint point) -{ - INPUT mouseInput; +void moveMouse(MMPoint point) { + INPUT mouseInput; mouseInput.type = INPUT_MOUSE; mouseInput.mi.dx = CalculateAbsoluteCoordinateX(point.x); mouseInput.mi.dy = CalculateAbsoluteCoordinateY(point.y); - mouseInput.mi.mouseData = 0; + mouseInput.mi.mouseData = 0; mouseInput.mi.time = 0; - mouseInput.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_ABSOLUTE; - mouseInput.mi.dwExtraInfo = 0; - SendInput(1, &mouseInput, sizeof(mouseInput)); + mouseInput.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_ABSOLUTE; + mouseInput.mi.dwExtraInfo = 0; + SendInput(1, & mouseInput, sizeof(mouseInput)); } void dragMouse(MMPoint point, const MMMouseButton button) From b3e18e4fbff777ca49cef0af4c9a0490c07652dd Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 22:31:41 +0100 Subject: [PATCH 4/8] Add Coord constant --- src/win32/mouse.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index c38af34..88c05cd 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -7,6 +7,15 @@ #define M_SQRT2 1.4142135623730950488016887 /* Fix for MSVC. */ #endif +/** + * This constant is required as Windows divides the entire + * screen space into 65536 segments in both X and Y axes + * irrespective of resolution + * https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput#remarks + */ + #define ABSOLUTE_COORD_CONST 65536 + + #define MMMouseToMEventF(down, button) \ (down ? MMMouseDownToMEventF(button) : MMMouseUpToMEventF(button)) From e4a8b4fd6da93dbf05ab52bfdd5f5bcd43c3c28c Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 22:32:18 +0100 Subject: [PATCH 5/8] Squash x and y coord methods Make Absolute coord calculation return MMPoint --- src/win32/mouse.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index 88c05cd..95e1158 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -29,14 +29,9 @@ : ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \ : MOUSEEVENTF_MIDDLEDOWN)) -int CalculateAbsoluteCoordinateX(int x) { +int CalculateAbsoluteCoordinates(MMPoint point) { MMSize displaySize = getMainDisplaySize(); - return (x * 65536) / displaySize.width; -} - -int CalculateAbsoluteCoordinateY(int y) { - MMSize displaySize = getMainDisplaySize(); - return (y * 65536) / displaySize.height; + return MMPointMake((x / displaySize.width) * ABSOLUTE_COORD_CONST, (y / displaySize.height) * ABSOLUTE_COORD_CONST); } /** From 097c254918c6bb14e86290bcca407181ec360a54 Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 22:32:41 +0100 Subject: [PATCH 6/8] Call coord calculator with MMPoint --- src/win32/mouse.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index 95e1158..571717c 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -40,9 +40,10 @@ int CalculateAbsoluteCoordinates(MMPoint point) { */ void moveMouse(MMPoint point) { INPUT mouseInput; + POINT pointAbsolute = CalculateAbsoluteCoordinates(point); mouseInput.type = INPUT_MOUSE; - mouseInput.mi.dx = CalculateAbsoluteCoordinateX(point.x); - mouseInput.mi.dy = CalculateAbsoluteCoordinateY(point.y); + mouseInput.mi.dx = pointAbsolute.x; + mouseInput.mi.dy = pointAbsolute.y; mouseInput.mi.mouseData = 0; mouseInput.mi.time = 0; mouseInput.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_ABSOLUTE; From 4ebf808b2ae4db9199b1a5eef0d5561cc49c5664 Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 22:46:06 +0100 Subject: [PATCH 7/8] Correct Types and Return values --- src/win32/mouse.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index 571717c..a529f32 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -29,9 +29,9 @@ : ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \ : MOUSEEVENTF_MIDDLEDOWN)) -int CalculateAbsoluteCoordinates(MMPoint point) { +MMPoint CalculateAbsoluteCoordinates(MMPoint point) { MMSize displaySize = getMainDisplaySize(); - return MMPointMake((x / displaySize.width) * ABSOLUTE_COORD_CONST, (y / displaySize.height) * ABSOLUTE_COORD_CONST); + return MMPointMake((point.x / displaySize.width) * ABSOLUTE_COORD_CONST, (point.y / displaySize.height) * ABSOLUTE_COORD_CONST); } /** @@ -40,13 +40,13 @@ int CalculateAbsoluteCoordinates(MMPoint point) { */ void moveMouse(MMPoint point) { INPUT mouseInput; - POINT pointAbsolute = CalculateAbsoluteCoordinates(point); + MMPoint pointAbsolute = CalculateAbsoluteCoordinates(point); mouseInput.type = INPUT_MOUSE; mouseInput.mi.dx = pointAbsolute.x; mouseInput.mi.dy = pointAbsolute.y; mouseInput.mi.mouseData = 0; mouseInput.mi.time = 0; - mouseInput.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_ABSOLUTE; + mouseInput.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; mouseInput.mi.dwExtraInfo = 0; SendInput(1, & mouseInput, sizeof(mouseInput)); } From ea13e1195ff94d7a57ad7b4e8a52950a4e558344 Mon Sep 17 00:00:00 2001 From: Reiss Cashmore Date: Sun, 10 Oct 2021 22:52:09 +0100 Subject: [PATCH 8/8] Import for getMainDisplaySize --- src/win32/mouse.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/win32/mouse.c b/src/win32/mouse.c index a529f32..a2fc937 100644 --- a/src/win32/mouse.c +++ b/src/win32/mouse.c @@ -1,4 +1,5 @@ #include "../mouse.h" +#include "../screen.h" #include "../microsleep.h" #include /* For floor() */