-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrthidlinux.cpp
More file actions
356 lines (305 loc) · 9.21 KB
/
rthidlinux.cpp
File metadata and controls
356 lines (305 loc) · 9.21 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <QtCore/QtGlobal>
#ifdef Q_OS_LINUX
#include "rthidlinux.h"
#include "rttypedefs.h"
#include <QThread>
#include <QMutexLocker>
#include <linux/ioctl.h>
#include <linux/hidraw.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
//#undef QT_DEBUG
#ifdef QT_DEBUG
static inline void debugReport(const char *where, quint32 rid, const quint8 *buffer, qsizetype length)
{
const QByteArray d((char *) buffer, length);
qDebug("[HIDDEV] %s: [RID:0x%02x LEN:%4lld] pl=%s", where, rid, length, qPrintable(d.toHex(' ')));
}
#endif
RTHidLinux::RTHidLinux(QObject *parent)
: RTAbstractDevice(parent)
, m_devices()
, m_handlers()
, m_monitor(nullptr)
, m_mutex()
, m_timer(this)
{
hid_init();
m_timer.setInterval(2000);
m_timer.setTimerType(Qt::CoarseTimer);
connect(&m_timer, &QTimer::timeout, this, [this](){ //
if (m_devices.isEmpty()) {
QList<quint32> products;
products.append(USB_DEVICE_ID_ROCCAT_TYON_BLACK);
products.append(USB_DEVICE_ID_ROCCAT_TYON_WHITE);
lookupDevices(USB_DEVICE_ID_VENDOR_ROCCAT, products);
}
});
}
RTHidLinux::~RTHidLinux()
{
releaseDevices();
hid_exit();
}
inline void RTHidLinux::releaseDevices()
{
if (m_monitor) {
m_monitor->requestInterruption();
m_monitor->wait();
m_monitor = 0L;
}
}
void RTHidLinux::registerHandlers(const TReportHandlers &handlers)
{
m_handlers = handlers;
}
bool RTHidLinux::hasDevice() const
{
return !m_devices.isEmpty();
}
static const uint kHIDUsageMouse = 0x01;
static const uint kHIDPageMouse = 0x01;
static const uint kHIDUsageMisc = 0x00;
static const uint kHIDPageMisc = 0x0a;
bool RTHidLinux::lookupDevices(quint32 vendorId, QList<quint32> products)
{
hid_device_info* devices;
emit lookupStarted();
// cleanup prior findings
releaseDevices();
// find ROCCAT Tyon device
foreach(quint32 product, products) {
if (!(devices = hid_enumerate(vendorId, product))) {
continue;
}
hid_device_info *info = devices;
while (info) {
qDebug("Device found --------------------------------");
qDebug("Device path..: %s", info->path);
qDebug("Manufacturer.: %s", qPrintable(QString::fromStdWString(info->manufacturer_string)));
qDebug("Product......: %s", qPrintable(QString::fromStdWString(info->product_string)));
qDebug("Serialnumber.: %s", qPrintable(QString::fromStdWString(info->serial_number)));
qDebug("Releasenumber: %d", info->release_number);
qDebug("Interface....: 0x%02x", info->interface_number);
qDebug("Primary usage: 0x%02x", info->usage);
qDebug("Usage page...: 0x%02x", info->usage_page);
/* Control interface */
if (info->usage == kHIDUsageMouse && info->usage_page == kHIDPageMouse) {
THidDevice d = {};
d.path = QString::fromLatin1(info->path);
d.interface = info->interface_number;
m_devices[HidMouseControl] = d;
}
/* X-Celerator input interface */
else if (info->usage == kHIDUsageMisc && info->usage_page == kHIDPageMisc) {
THidDevice d = {};
d.path = QString::fromLatin1(info->path);
d.interface = info->interface_number;
m_devices[HidMouseInput] = d;
hidMonitor(d);
}
// next entry
info = info->next;
}
// cleanup
hid_free_enumeration(devices);
}
// notify if control interface found
foreach(auto key, m_devices.keys()) {
if (key == HidMouseControl) {
if (m_timer.isActive()) {
m_timer.stop();
}
emit deviceFound(key);
return true;
}
}
m_timer.start();
return false;
}
inline THidDevice RTHidLinux::toDevice(THidDeviceType type) const
{
if (!m_devices.contains(type)) {
return {};
}
return m_devices[type];
}
bool RTHidLinux::openDevice(THidDeviceType type)
{
Q_UNUSED(type)
return true;
}
bool RTHidLinux::closeDevice(THidDeviceType type)
{
Q_UNUSED(type)
return true;
}
inline void RTHidLinux::hidMonitor(const THidDevice& device)
{
const Qt::ConnectionType ct = Qt::DirectConnection;
m_monitor = new RTHidMonitor(device);
connect(m_monitor, &RTHidMonitor::errorOccured, this, [this](int error, const QString &message) { //
raiseError(error, message);
}, ct);
connect(m_monitor, &RTHidMonitor::inputReady, this, [this](quint32 rid, const QByteArray &data) { //
emit inputReady(rid, data);
}, ct);
connect(m_monitor, &RTHidMonitor::finished, this, [this]() { //
m_monitor->deleteLater();
m_monitor = nullptr;
}, ct);
connect(m_monitor, &RTHidMonitor::destroyed, this, [device](QObject*) { //
if (device.dev) {
hid_close(device.dev);
}
}, ct);
m_monitor->start(QThread::IdlePriority);
}
inline int RTHidLinux::hidReadRaw(THidDeviceType type, qsizetype length, quint8* buffer)
{
QMutexLocker lock(&m_mutex);
const THidDevice d = toDevice(type);
int retval;
int fd;
fd = ::open(d.path.toLatin1().constData(), O_RDWR);
if (fd == -1) {
return ENODEV;
}
retval = ioctl(fd, HIDIOCGFEATURE(length), buffer);
if (retval == -1) {
retval = EIO;
} else {
retval = 0;
}
#ifdef QT_DEBUG
if (buffer[0] && buffer[1] > 0) {
debugReport("hidReadRaw ", buffer[0], buffer, length);
}
#endif
::close(fd);
return retval;
}
inline int RTHidLinux::hidWriteRaw(THidDeviceType type, qsizetype length, const quint8* buffer)
{
QMutexLocker lock(&m_mutex);
const THidDevice d = toDevice(type);
int retval;
int fd;
#ifdef QT_DEBUG
debugReport("hidWriteRaw", buffer[0], buffer, length);
#endif
fd = ::open(d.path.toLatin1().constData(), O_RDWR);
if (fd == -1) {
return ENODEV;
}
retval = ioctl(fd, HIDIOCSFEATURE(length), buffer);
if (retval == -1) {
retval = EIO;
} else {
retval = 0;
}
::close(fd);
return retval;
}
bool RTHidLinux::readHidMessage(THidDeviceType type, quint32 rid, qsizetype length)
{
quint8* buffer = (quint8*) malloc(length);
int ret;
// set report identifier
buffer[0] = rid;
if ((ret = hidReadRaw(type, length, buffer)) != 0) {
raiseError(ret, tr("readHidMessage: Error RID=0x%1").arg(rid, 2, 16, QChar('0')));
free(buffer);
return false;
}
if (m_handlers.contains(rid)) {
m_handlers[rid](buffer, length);
}
free(buffer);
return true;
}
bool RTHidLinux::readHidMessage(THidDeviceType type, quint32 rid, quint8 *buffer, qsizetype length)
{
int ret;
if ((ret = hidReadRaw(type, length, buffer)) != 0) {
raiseError(ret, tr("readHidMessage: Error RID=0x%1").arg(rid, 2, 16, QChar('0')));
return false;
}
return true;
}
bool RTHidLinux::writeHidMessage(THidDeviceType type, quint32 rid, const quint8 *buffer, qsizetype length)
{
int ret;
if ((ret = hidWriteRaw(type, length, buffer)) != 0) {
raiseError(ret, tr("writeHidMessage: Error RID=0x%1").arg(rid, 2, 16, QChar('0')));
return false;
}
return true;
}
bool RTHidLinux::writeHidAsync(THidDeviceType type, quint32 rid, const quint8 *buffer, qsizetype length)
{
int ret;
if ((ret = hidWriteRaw(type, length, buffer)) != 0) {
raiseError(ret, tr("writeHidAsync: Error RID=0x%1").arg(rid, 2, 16, QChar('0')));
return false;
}
return true;
}
RTHidMonitor::RTHidMonitor(const THidDevice& device)
: QThread(0L)
, m_device(device)
{
//--
}
void RTHidMonitor::run()
{
const qsizetype length = 64;
quint8 buffer[length] = {};
int ret, fd;
fd = ::open(m_device.path.toLatin1().constData(), O_RDONLY);
if (fd < 0) {
emit errorOccured(ENODEV, "Unable to open HID input interface.");
this->exit(ENODEV);
return;
}
fd_set fdSet, readSet;
FD_ZERO(&fdSet);
FD_SET(fd, &fdSet);
// for select() timeout
struct timeval tv = {};
tv.tv_usec = 10000;
while(!isInterruptionRequested()) {
readSet = fdSet;
ret = ::select(fd + 1, &readSet, NULL, NULL, &tv);
if (ret < 0) { // Error handling
emit errorOccured(EIO, "Unable to monitor HID input interface.");
break;
} else if (ret == 0) { // Timeout, loop again
this->yieldCurrentThread();
continue;
} else {
ret = ::read(fd, buffer, length);
if (ret < 0) {
emit errorOccured(EIO, "Unable to read HID input.");
break;
}
else if(ret && buffer[0]) { // report Id must exist
#ifdef QT_DEBUG
debugReport("RTHidMonitor", buffer[0], buffer, ret);
#endif
const QByteArray data((char*)buffer, ret);
emit inputReady(buffer[0], data);
}
}
}
::close(fd);
this->exit(0);
}
#endif // Q_OS_LINUX