forked from lemmingDev/ESP32-BLE-Gamepad
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MouseDevice.cpp
167 lines (141 loc) · 3.65 KB
/
MouseDevice.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
#include "MouseDevice.h"
#include "BleCompositeHID.h"
MouseDevice::MouseDevice():
_config(MouseConfiguration()), // Use default config
_mouseButtons(),
_mouseX(0),
_mouseY(0),
_mouseWheel(0),
_mouseHWheel(0)
{
this->resetButtons();
}
MouseDevice::MouseDevice(const MouseConfiguration& config):
_config(config), // Copy config to avoid modification
_mouseButtons(),
_mouseX(0),
_mouseY(0),
_mouseWheel(0),
_mouseHWheel(0)
{
this->resetButtons();
}
void MouseDevice::init(NimBLEHIDDevice* hid)
{
setCharacteristics(hid->inputReport(_config.getReportId()), nullptr);
}
const BaseCompositeDeviceConfiguration* MouseDevice::getDeviceConfig() const
{
return &_config;
}
void MouseDevice::resetButtons()
{
std::lock_guard<std::mutex> lock(_mutex);
memset(&_mouseButtons, 0, sizeof(_mouseButtons));
}
// Mouse
void MouseDevice::mouseClick(uint8_t button)
{
// No-op
// TODO: Send two reports one after the other for convience? Can't be both pressed and not pressed in a bitflag
}
void MouseDevice::mousePress(uint8_t button)
{
uint8_t index = (button - 1) / 8;
uint8_t bit = (button - 1) % 8;
uint8_t bitmask = (1 << bit);
uint8_t result = _mouseButtons[index] | bitmask;
if (result != _mouseButtons[index])
{
std::lock_guard<std::mutex> lock(_mutex);
_mouseButtons[index] = result;
}
if (_config.getAutoReport())
{
sendMouseReport();
}
}
void MouseDevice::mouseRelease(uint8_t button)
{
uint8_t index = (button - 1) / 8;
uint8_t bit = (button - 1) % 8;
uint8_t bitmask = (1 << bit);
uint64_t result = _mouseButtons[index] & ~bitmask;
if (result != _mouseButtons[index])
{
std::lock_guard<std::mutex> lock(_mutex);
_mouseButtons[index] = result;
}
if (_config.getAutoReport())
{
sendMouseReport();
}
}
void MouseDevice::mouseMove(signed char x, signed char y, signed char scrollX, signed char scrollY)
{
if (x == -127)
{
x = -126;
}
if (y == -127)
{
y = -126;
}
if (scrollX == -127)
{
scrollX = -126;
}
if (scrollY == -127)
{
scrollY = -126;
}
{
std::lock_guard<std::mutex> lock(_mutex);
_mouseX = x;
_mouseY = y;
_mouseWheel = scrollY;
_mouseHWheel = scrollX;
}
if (_config.getAutoReport())
{
sendMouseReport();
}
}
void MouseDevice::sendMouseReport(bool defer)
{
if (defer || _config.getAutoDefer())
{
queueDeferredReport(std::bind(&MouseDevice::sendMouseReportImpl, this));
}
else
{
sendMouseReportImpl();
}
}
void MouseDevice::sendMouseReportImpl()
{
auto input = getInput();
auto parentDevice = this->getParent();
if (!input || !parentDevice)
return;
if(!parentDevice->isConnected())
return;
uint8_t mouse_report[_config.getDeviceReportSize()];
uint8_t currentReportIndex = 0;
{
std::lock_guard<std::mutex> lock(_mutex);
memset(&mouse_report, 0, sizeof(mouse_report));
memcpy(&mouse_report, &_mouseButtons, sizeof(_mouseButtons));
currentReportIndex += _config.getMouseButtonNumBytes();
// TODO: Make dynamic based on axis counts
if (_config.getMouseAxisCount() > 0)
{
mouse_report[currentReportIndex++] = _mouseX;
mouse_report[currentReportIndex++] = _mouseY;
mouse_report[currentReportIndex++] = _mouseWheel;
mouse_report[currentReportIndex++] = _mouseHWheel;
}
}
input->setValue(mouse_report, sizeof(mouse_report));
input->notify();
}