Skip to content

Commit 747e257

Browse files
authored
(#126) Add rounding to fix mouse drift (#156)
1 parent b7dc37b commit 747e257

File tree

1 file changed

+76
-79
lines changed

1 file changed

+76
-79
lines changed

Diff for: src/win32/mouse.c

+76-79
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <math.h> /* For floor() */
77

88
#if !defined(M_SQRT2)
9-
#define M_SQRT2 1.4142135623730950488016887 /* Fix for MSVC. */
9+
#define M_SQRT2 1.4142135623730950488016887 /* Fix for MSVC. */
1010
#endif
1111

1212
/**
@@ -15,121 +15,118 @@
1515
* irrespective of resolution
1616
* https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput#remarks
1717
*/
18-
#define ABSOLUTE_COORD_CONST 65536
18+
#define ABSOLUTE_COORD_CONST 65536
1919

2020
#define MMMouseToMEventF(down, button) \
21-
(down ? MMMouseDownToMEventF(button) : MMMouseUpToMEventF(button))
21+
(down ? MMMouseDownToMEventF(button) : MMMouseUpToMEventF(button))
2222

2323
#define MMMouseUpToMEventF(button) \
24-
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTUP \
25-
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTUP \
26-
: MOUSEEVENTF_MIDDLEUP))
24+
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTUP \
25+
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTUP \
26+
: MOUSEEVENTF_MIDDLEUP))
2727

2828
#define MMMouseDownToMEventF(button) \
29-
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN \
30-
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \
31-
: MOUSEEVENTF_MIDDLEDOWN))
29+
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN \
30+
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \
31+
: MOUSEEVENTF_MIDDLEDOWN))
3232

3333
static int32_t DEFAULT_DOUBLE_CLICK_INTERVAL_MS = 200;
3434

3535
MMPoint CalculateAbsoluteCoordinates(MMPoint point) {
36-
MMSize displaySize = getMainDisplaySize();
37-
return MMPointMake(((float) point.x / displaySize.width) * ABSOLUTE_COORD_CONST, ((float) point.y / displaySize.height) * ABSOLUTE_COORD_CONST);
36+
MMSize displaySize = getMainDisplaySize();
37+
return MMPointMake(
38+
ceil(point.x * ABSOLUTE_COORD_CONST / displaySize.width),
39+
ceil(point.y * ABSOLUTE_COORD_CONST / displaySize.height)
40+
);
3841
}
3942

4043
/**
4144
* Move the mouse to a specific point.
4245
* @param point The coordinates to move the mouse to (x, y).
4346
*/
4447
void moveMouse(MMPoint point) {
45-
INPUT mouseInput;
46-
MMPoint pointAbsolute = CalculateAbsoluteCoordinates(point);
47-
mouseInput.type = INPUT_MOUSE;
48-
mouseInput.mi.dx = pointAbsolute.x;
49-
mouseInput.mi.dy = pointAbsolute.y;
50-
mouseInput.mi.mouseData = 0;
51-
mouseInput.mi.time = 0;
52-
mouseInput.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
53-
mouseInput.mi.dwExtraInfo = 0;
54-
SendInput(1, & mouseInput, sizeof(mouseInput));
48+
INPUT mouseInput;
49+
MMPoint pointAbsolute = CalculateAbsoluteCoordinates(point);
50+
mouseInput.type = INPUT_MOUSE;
51+
mouseInput.mi.dx = pointAbsolute.x;
52+
mouseInput.mi.dy = pointAbsolute.y;
53+
mouseInput.mi.mouseData = 0;
54+
mouseInput.mi.time = 0;
55+
mouseInput.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
56+
mouseInput.mi.dwExtraInfo = 0;
57+
SendInput(1, &mouseInput, sizeof(mouseInput));
5558
}
5659

57-
void dragMouse(MMPoint point, const MMMouseButton button)
58-
{
59-
(void)button;
60-
moveMouse(point);
60+
void dragMouse(MMPoint point, const MMMouseButton button) {
61+
(void) button;
62+
moveMouse(point);
6163
}
6264

63-
MMPoint getMousePos()
64-
{
65-
POINT point;
66-
GetCursorPos(&point);
65+
MMPoint getMousePos() {
66+
POINT point;
67+
GetCursorPos(&point);
6768

68-
return MMPointFromPOINT(point);
69+
return MMPointFromPOINT(point);
6970
}
7071

7172
/**
7273
* Press down a button, or release it.
7374
* @param down True for down, false for up.
7475
* @param button The button to press down or release.
7576
*/
76-
void toggleMouse(bool down, MMMouseButton button)
77-
{
78-
INPUT mouseInput;
79-
mouseInput.type = INPUT_MOUSE;
80-
mouseInput.mi.mouseData = 0;
81-
mouseInput.mi.dx = 0;
82-
mouseInput.mi.dy = 0;
83-
mouseInput.mi.time = 0;
84-
mouseInput.mi.dwFlags = MMMouseToMEventF(down, button);
85-
SendInput(1, &mouseInput, sizeof(mouseInput));
77+
void toggleMouse(bool down, MMMouseButton button) {
78+
INPUT mouseInput;
79+
mouseInput.type = INPUT_MOUSE;
80+
mouseInput.mi.mouseData = 0;
81+
mouseInput.mi.dx = 0;
82+
mouseInput.mi.dy = 0;
83+
mouseInput.mi.time = 0;
84+
mouseInput.mi.dwFlags = MMMouseToMEventF(down, button);
85+
SendInput(1, &mouseInput, sizeof(mouseInput));
8686
}
8787

88-
void clickMouse(MMMouseButton button)
89-
{
90-
toggleMouse(true, button);
91-
toggleMouse(false, button);
88+
void clickMouse(MMMouseButton button) {
89+
toggleMouse(true, button);
90+
toggleMouse(false, button);
9291
}
9392

9493
/**
9594
* Special function for sending double clicks, needed for Mac OS X.
9695
* @param button Button to click.
9796
*/
98-
void doubleClick(MMMouseButton button)
99-
{
100-
UINT maxDoubleClickTime = GetDoubleClickTime();
101-
/* Double click for everything else. */
102-
clickMouse(button);
103-
if (maxDoubleClickTime > DEFAULT_DOUBLE_CLICK_INTERVAL_MS) {
104-
microsleep(DEFAULT_DOUBLE_CLICK_INTERVAL_MS);
105-
} else {
106-
microsleep(DEADBEEF_RANDRANGE(1, maxDoubleClickTime));
107-
}
108-
clickMouse(button);
97+
void doubleClick(MMMouseButton button) {
98+
UINT maxDoubleClickTime = GetDoubleClickTime();
99+
/* Double click for everything else. */
100+
clickMouse(button);
101+
if (maxDoubleClickTime > DEFAULT_DOUBLE_CLICK_INTERVAL_MS) {
102+
microsleep(DEFAULT_DOUBLE_CLICK_INTERVAL_MS);
103+
} else {
104+
microsleep(DEADBEEF_RANDRANGE(1, maxDoubleClickTime));
105+
}
106+
clickMouse(button);
109107
}
110108

111-
void scrollMouse(int x, int y)
112-
{
113-
INPUT mouseScrollInputH;
114-
INPUT mouseScrollInputV;
115-
116-
mouseScrollInputH.type = INPUT_MOUSE;
117-
mouseScrollInputH.mi.dx = 0;
118-
mouseScrollInputH.mi.dy = 0;
119-
mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_HWHEEL;
120-
mouseScrollInputH.mi.time = 0;
121-
mouseScrollInputH.mi.dwExtraInfo = 0;
122-
// Flip x to match other platforms.
123-
mouseScrollInputH.mi.mouseData = -x;
124-
125-
mouseScrollInputV.type = INPUT_MOUSE;
126-
mouseScrollInputV.mi.dx = 0;
127-
mouseScrollInputV.mi.dy = 0;
128-
mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_WHEEL;
129-
mouseScrollInputV.mi.time = 0;
130-
mouseScrollInputV.mi.dwExtraInfo = 0;
131-
mouseScrollInputV.mi.mouseData = y;
132-
133-
SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH));
134-
SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV));
109+
void scrollMouse(int x, int y) {
110+
INPUT mouseScrollInputH;
111+
INPUT mouseScrollInputV;
112+
113+
mouseScrollInputH.type = INPUT_MOUSE;
114+
mouseScrollInputH.mi.dx = 0;
115+
mouseScrollInputH.mi.dy = 0;
116+
mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_HWHEEL;
117+
mouseScrollInputH.mi.time = 0;
118+
mouseScrollInputH.mi.dwExtraInfo = 0;
119+
// Flip x to match other platforms.
120+
mouseScrollInputH.mi.mouseData = -x;
121+
122+
mouseScrollInputV.type = INPUT_MOUSE;
123+
mouseScrollInputV.mi.dx = 0;
124+
mouseScrollInputV.mi.dy = 0;
125+
mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_WHEEL;
126+
mouseScrollInputV.mi.time = 0;
127+
mouseScrollInputV.mi.dwExtraInfo = 0;
128+
mouseScrollInputV.mi.mouseData = y;
129+
130+
SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH));
131+
SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV));
135132
}

0 commit comments

Comments
 (0)