-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmouse.cpp
180 lines (152 loc) · 4.85 KB
/
mouse.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "mouse.h"
#include <stdexcept>
#include "registry.h"
Mouse::Mouse()
: Device{L"\\\\?\\HID#VARIABLE_6&Col02#1"}
{}
void Mouse::initialize()
{
if (isInitialized()) throw std::runtime_error{"ERROR_DOUBLE_INITIALIZATION"};
populateRangeSpeedVector();
Device::initialize();
}
void Mouse::leftButtonDown()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
m_buttons |= BUTTON_LEFT;
sendMouseReport(0, 0);
}
void Mouse::leftButtonUp()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
m_buttons &= ~BUTTON_LEFT;
sendMouseReport(0, 0);
}
void Mouse::leftButtonClick()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
leftButtonDown();
leftButtonUp();
}
void Mouse::rightButtonDown()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
m_buttons |= BUTTON_RIGHT;
sendMouseReport(0, 0);
}
void Mouse::rightButtonUp()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
m_buttons &= ~BUTTON_RIGHT;
sendMouseReport(0, 0);
}
void Mouse::rightButtonClick()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
rightButtonDown();
rightButtonUp();
}
void Mouse::middleButtonDown()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
m_buttons |= BUTTON_MIDDLE;
sendMouseReport(0, 0);
}
void Mouse::middleButtonUp()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
m_buttons &= ~BUTTON_MIDDLE;
sendMouseReport(0, 0);
}
void Mouse::middleButtonClick()
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
middleButtonDown();
middleButtonUp();
}
void Mouse::moveCursor(POINT point)
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
moveCursor(point.x, point.y);
}
void Mouse::moveCursor(LONG x, LONG y)
{
if (!isInitialized()) throw std::runtime_error{"ERROR_NOT_INITIALIZED"};
while (!isAborted()) {
DWORD getCurrentCursorPositionError = ERROR_SUCCESS;
POINT currentPoint = getCurrentCursorPosition(&getCurrentCursorPositionError);
if (ERROR_SUCCESS != getCurrentCursorPositionError) {
throw std::runtime_error{"ERROR_CURSOR_PROCESSING"};
}
LONG distance = (LONG) sqrt(pow(x - currentPoint.x, 2) + pow(y - currentPoint.y, 2));
if (distance <= DISTANCE_TOLERANCE) {
return;
}
CHAR xSpeed = static_cast<CHAR>(getSpeedByRange(abs(x - currentPoint.x)));
xSpeed = (x > currentPoint.x ? xSpeed : -xSpeed);
CHAR ySpeed = static_cast<CHAR>(getSpeedByRange(abs(y - currentPoint.y)));
ySpeed = (y > currentPoint.y ? ySpeed : -ySpeed);
sendMouseReport(xSpeed, ySpeed);
Sleep(1);
}
}
void Mouse::abort()
{
Device::abort();
}
void Mouse::populateRangeSpeedVector()
{
m_rangeSpeedVector.clear();
int rangeIndex = 0;
for (int speed = 0; speed < MAX_ABSOLUTE_SPEED + 1; ++speed) {
int range = getRangeBySpeed(speed);
while (rangeIndex <= range) {
m_rangeSpeedVector.push_back(speed);
++rangeIndex;
}
}
}
int Mouse::getSpeedByRange(int range) const
{
if (m_rangeSpeedVector.size() == 0) {
return static_cast<int>(sqrt(range));
}
if (range > m_rangeSpeedVector.size() - 1) {
range = static_cast<int>(m_rangeSpeedVector.size()) - 1;
}
return m_rangeSpeedVector.at(range);
}
int Mouse::getRangeBySpeed(int speed)
{
double sensitivityFactors[21] = {0, 0.03125, 0.625, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5};
int sensitivity = RegistryService::get().getMouseSensivity();
int eppSpeed = RegistryService::get().getMouseSpeed();
if ( sensitivity < SENSITIVITY_MIN
|| sensitivity > SENSITIVITY_MAX
|| eppSpeed < EPP_DISABLED
|| eppSpeed > EPP_ENABLED)
{
return speed;
}
double sensitivityFactor = eppSpeed == EPP_ENABLED ? 1 : sensitivityFactors[sensitivity];
double eppFactor = eppSpeed == EPP_ENABLED ? 0.25 * sensitivity : 1;
double range = static_cast<double>(speed) * sensitivityFactor * eppFactor;
return static_cast<int>(range);
}
POINT Mouse::getCurrentCursorPosition(LPDWORD error)
{
POINT currentPoint{0, 0};
BOOL getCursorPosResult = GetCursorPos(¤tPoint);
if (!getCursorPosResult && nullptr != error) {
*error = GetLastError();
}
return currentPoint;
}
void Mouse::sendMouseReport(CHAR xSpeed, CHAR ySpeed) {
Report report;
report.reportId = REPORT_ID;
report.buttons = m_buttons;
report.x = xSpeed;
report.y = ySpeed;
setOutputReport(&report, static_cast<DWORD>(sizeof(Report)));
}