-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathhidcontroller.cpp
209 lines (171 loc) · 7.3 KB
/
hidcontroller.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "controllers/hid/hidcontroller.h"
#include <hidapi.h>
#include "controllers/defs_controllers.h"
#include "moc_hidcontroller.cpp"
class LegacyControllerMapping;
HidController::HidController(
mixxx::hid::DeviceInfo&& deviceInfo)
: Controller(deviceInfo.formatName()),
m_deviceInfo(std::move(deviceInfo)) {
// We assume, that all HID controllers are full-duplex,
// but a HID can also be input only (e.g. a USB HID mouse)
setInputDevice(true);
setOutputDevice(true);
}
HidController::~HidController() {
if (isOpen()) {
close();
}
}
QString HidController::mappingExtension() {
return HID_MAPPING_EXTENSION;
}
void HidController::setMapping(std::shared_ptr<LegacyControllerMapping> pMapping) {
m_pMapping = downcastAndTakeOwnership<LegacyHidControllerMapping>(std::move(pMapping));
}
std::shared_ptr<LegacyControllerMapping> HidController::cloneMapping() {
if (!m_pMapping) {
return nullptr;
}
return m_pMapping->clone();
}
bool HidController::matchMapping(const MappingInfo& mapping) {
const QList<ProductInfo>& products = mapping.getProducts();
for (const auto& product : products) {
if (m_deviceInfo.matchProductInfo(product)) {
return true;
}
}
return false;
}
int HidController::open() {
if (isOpen()) {
qDebug() << "HID device" << getName() << "already open";
return -1;
}
VERIFY_OR_DEBUG_ASSERT(!m_pHidIoThread) {
qWarning() << "HidIoThread already present for" << getName();
return -1;
}
// Open device by path
qCInfo(m_logBase) << "Opening HID device" << getName() << "by HID path"
<< m_deviceInfo.pathRaw();
hid_device* pHidDevice = hid_open_path(m_deviceInfo.pathRaw());
// If that fails, try to open device with vendor/product/serial #
if (!pHidDevice) {
qCWarning(m_logBase) << "Failed. Trying to open with make, model & serial no:"
<< m_deviceInfo.getVendorId() << m_deviceInfo.getProductId()
<< m_deviceInfo.getSerialNumber();
pHidDevice = hid_open(
m_deviceInfo.getVendorId(),
m_deviceInfo.getProductId(),
m_deviceInfo.serialNumberRaw());
}
// If it does fail, try without serial number WARNING: This will only open
// one of multiple identical devices
if (!pHidDevice) {
qCWarning(m_logBase) << "Unable to open specific HID device" << getName()
<< "Trying now with just make and model."
<< "(This may only open the first of multiple identical devices.)";
pHidDevice = hid_open(m_deviceInfo.getVendorId(),
m_deviceInfo.getProductId(),
nullptr);
}
// If that fails, we give up!
if (!pHidDevice) {
qCWarning(m_logBase) << "Unable to open HID device" << getName();
return -1;
}
// Set hid controller to non-blocking
if (hid_set_nonblocking(pHidDevice, 1) != 0) {
qCWarning(m_logBase) << "Unable to set HID device " << getName() << " to non-blocking";
hid_close(pHidDevice);
return -1;
}
setOpen(true);
m_pHidIoThread = std::make_unique<HidIoThread>(pHidDevice, m_deviceInfo);
m_pHidIoThread->setObjectName(QStringLiteral("HidIoThread ") + getName());
connect(m_pHidIoThread.get(),
&HidIoThread::receive,
this,
&HidController::receive,
Qt::QueuedConnection);
// Controller input needs to be prioritized since it can affect the
// audio directly, like when scratching
// The effect of the priority parameter is dependent on the operating system's scheduling policy.
// In particular, the priority will be ignored on systems that do not support thread priorities (as Linux).
m_pHidIoThread->start(QThread::HighPriority);
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread->testAndSetThreadState(
HidIoThreadState::Initialized, HidIoThreadState::OutputActive)) {
qWarning() << "HidIoThread wasn't in expected Initialized state";
}
// This executes the init function of the JavaScript mapping
startEngine();
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread->testAndSetThreadState(
HidIoThreadState::OutputActive,
HidIoThreadState::InputOutputActive)) {
qWarning() << "HidIoThread wasn't in expected OutputActive state";
}
return 0;
}
int HidController::close() {
if (!isOpen()) {
qCWarning(m_logBase) << "HID device" << getName() << "already closed";
return -1;
}
qCInfo(m_logBase) << "Shutting down HID device" << getName();
// Stop the InputReport polling, but allow sending OutputReports in JavaScript mapping shutdown procedure
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread) {
qWarning() << "HidIoThread not present for" << getName()
<< "while closing the device !";
}
else {
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread->testAndSetThreadState(
HidIoThreadState::InputOutputActive,
HidIoThreadState::OutputActive)) {
qWarning() << "HidIoThread wasn't in expected InputOutputActive state";
}
}
// Stop controller engine here to ensure it's done before the device is closed
// in case it has any final parting messages
// This executes the shutdown function of the JavaScript mapping
stopEngine();
if (m_pHidIoThread) {
disconnect(m_pHidIoThread.get());
// Request stop after sending the last cached OutputReport
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread->testAndSetThreadState(
HidIoThreadState::OutputActive,
HidIoThreadState::StopWhenAllReportsSent)) {
qWarning() << "HidIoThread wasn't in expected OutputActive state";
m_pHidIoThread->setThreadState(HidIoThreadState::StopWhenAllReportsSent);
}
qCInfo(m_logBase) << "Waiting on HID IO thread to send the last remaining OutputReports";
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread->waitUntilRunLoopIsStopped(50)) {
qWarning() << "Sending the last remaining OutputReports "
"reached timeout!";
qCInfo(m_logBase) << "Enforce stop of HID IO thread run loop!";
m_pHidIoThread->setThreadState(HidIoThreadState::StopRequested);
VERIFY_OR_DEBUG_ASSERT(m_pHidIoThread->waitUntilRunLoopIsStopped(100)) {
qWarning() << "Stopping run loop reached timeout!";
}
}
// After completion of all HID communication deconstruct m_pHidIoThread
m_pHidIoThread.reset();
}
// Close device
setOpen(false);
qCInfo(m_logBase) << "Device closed";
return 0;
}
/// This function is only for class compatibility with the (midi)controller
/// and will not do the same as for MIDI devices,
/// because sending of raw bytes is not a supported HIDAPI feature.
bool HidController::sendBytes(const QByteArray& data) {
// Some HIDAPI backends will fail if the device uses ReportIDs (as practical all DJ controllers),
// because 0 is no valid ReportID for these devices.
m_pHidIoThread->updateCachedOutputReportData(0, data, false);
return true;
}
ControllerJSProxy* HidController::jsProxy() {
return new HidControllerJSProxy(this);
}