author | František Kučera <franta-hg@frantovo.cz> |
Sun, 18 Aug 2019 00:10:30 +0200 | |
branch | v_0 |
changeset 3 | 1197b42e8b2e |
parent 2 | abeba77ec581 |
child 4 | 405aa9de65d2 |
permissions | -rw-r--r-- |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
#include <iostream> |
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
|
2 |
#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
|
3 |
#include <vector> |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
#include <hidapi/hidapi.h> |
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
|
6 |
#include <memory> |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
class HIDException { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
private: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
std::wstring message; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
public: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
HIDException(std::wstring message) : message(message) { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
virtual ~HIDException() { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
} |
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
|
18 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
const std::wstring getMessage() const { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
return message; |
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 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
}; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
class HIDDevice { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
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
|
27 |
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
|
28 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
public: |
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 |
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
|
32 |
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
|
33 |
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
|
34 |
handle.reset(hid_open(vendorId, productId, serialNumber), hid_close); |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?"); |
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 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
} |
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
|
41 |
|
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
return buffer.data(); |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
|
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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
} |
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 |
|
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 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
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 |
|
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 |
void sendFeatureReport(std::vector<unsigned char> 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
|
64 |
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
|
65 |
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
|
66 |
} |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
}; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
int main(int argc, char** argv) { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
try { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
std::wcout << L"cadMousePro" << std::endl; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
HIDDevice mouse(0x256f, 0xc652, nullptr); |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
std::wcout << L"mouse opened" << std::endl; |
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
|
76 |
std::wcout << L"manufacturer: " << mouse.getManufacturerName() << std::endl; |
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
|
77 |
std::wcout << L"product: " << mouse.getProductName() << std::endl; |
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
|
78 |
// std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception |
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
|
79 |
|
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
|
80 |
std::vector<unsigned char> enableFreeWheel{0x10, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0a, 0x0b, 0x0c, 0x0c, 0x0e, 0x0d, 0x2f, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01}; |
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
|
81 |
std::vector<unsigned char> disableFreeWheel{0x10, 0x00, 0x1c, 0x00, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0a, 0x0b, 0x0c, 0x0c, 0x0e, 0x0d, 0x2f, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01}; |
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
|
82 |
// mouse.sendFeatureReport(enableFreeWheel); |
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
|
83 |
mouse.sendFeatureReport(disableFreeWheel); |
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
|
84 |
|
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
|
85 |
// TODO: enable acceleration |
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
|
86 |
// TODO: enable lift-off detection |
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
|
87 |
// TODO: sensof polling rate – frequency |
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
|
88 |
// TODO: wheel scrolling size – lines, page |
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
|
89 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
} catch (const HIDException& e) { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
std::wcout << L"HIDException: " << e.getMessage() << std::endl; |
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
|
92 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
} |