author | František Kučera <franta-hg@frantovo.cz> |
Sun, 18 Aug 2019 23:30:01 +0200 | |
branch | v_0 |
changeset 5 | 6799cec5c2f8 |
parent 4 | src/cadMousePro.cpp@405aa9de65d2 |
child 6 | 975f38eb1e12 |
permissions | -rw-r--r-- |
5
6799cec5c2f8
refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
1 |
#pragma once |
6799cec5c2f8
refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
2 |
|
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
3 |
#include <array> |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
4 |
#include <vector> |
4
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
5 |
#include <memory> |
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
6 |
#include <unistd.h> |
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
7 |
#include <cassert> |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
#include <hidapi/hidapi.h> |
4
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
10 |
|
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
11 |
using Packet = std::vector<uint8_t>; |
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
12 |
static_assert(sizeof (uint8_t) == sizeof (unsigned char)); // unsigned char is used in the HID API library |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
class HIDException { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
private: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
std::wstring message; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
public: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
HIDException(std::wstring message) : message(message) { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
virtual ~HIDException() { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
} |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
const std::wstring getMessage() const { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
return message; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
}; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
class HIDDevice { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
private: |
3
1197b42e8b2e
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
33 |
std::shared_ptr<hid_device> handle; |
2
abeba77ec581
private copy constructor (to avoid unwanted close() in copie's destructor)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
34 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
public: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) { |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
int initError = hid_init(); // TODO: move to HIDContext class? |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
if (initError) throw HIDException(L"Unable to init HID API."); |
3
1197b42e8b2e
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
40 |
handle.reset(hid_open(vendorId, productId, serialNumber), hid_close); |
5
6799cec5c2f8
refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
41 |
if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is the device present?"); |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
virtual ~HIDDevice() { |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
hid_exit(); // TODO: move to HIDContext class? |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
} |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
|
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
const std::wstring getManufacturerName() const { |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
std::array<wchar_t, 200 > buffer; |
3
1197b42e8b2e
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
50 |
int error = hid_get_manufacturer_string(handle.get(), buffer.data(), buffer.size()); |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
if (error) throw HIDException(L"Unable to get manufacturer name."); |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
return buffer.data(); |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
|
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
const std::wstring getProductName() const { |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
std::array<wchar_t, 200 > buffer; |
3
1197b42e8b2e
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
int error = hid_get_product_string(handle.get(), buffer.data(), buffer.size()); |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
if (error) throw HIDException(L"Unable to get product name."); |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
return buffer.data(); |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
} |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
|
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
const std::wstring getSerialNumber() const { |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
std::array<wchar_t, 200 > buffer; |
3
1197b42e8b2e
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
int error = hid_get_serial_number_string(handle.get(), buffer.data(), buffer.size()); |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
if (error) throw HIDException(L"Unable to get serial number."); |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
return buffer.data(); |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
} |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
|
4
405aa9de65d2
CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
69 |
void sendFeatureReport(Packet data) { |
3
1197b42e8b2e
use a smart pointer with custom deleter inside HIDDevice, so instances can be copied
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
int written = hid_send_feature_report(handle.get(), data.data(), data.size()); |
1
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
if (written < 0) throw HIDException(L"Unable to send feature report."); |
29cbe171cd43
first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
} |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
}; |