author | František Kučera <franta-hg@frantovo.cz> |
Wed, 14 Aug 2019 18:32:35 +0200 | |
branch | v_0 |
changeset 1 | 29cbe171cd43 |
parent 0 | 6ff501639c23 |
child 2 | abeba77ec581 |
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> |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
class HIDException { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
private: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
std::wstring message; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
public: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
HIDException(std::wstring message) : message(message) { |
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 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
virtual ~HIDException() { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
} |
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
|
17 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
const std::wstring getMessage() const { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
return 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 |
}; |
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 |
class HIDDevice { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
private: |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
hid_device* handle; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
public: |
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 |
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
|
30 |
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
|
31 |
if (initError) throw HIDException(L"Unable to init HID API."); |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
handle = hid_open(vendorId, productId, serialNumber); |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
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
|
34 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
virtual ~HIDDevice() { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
hid_close(handle); |
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 |
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
|
39 |
} |
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 |
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
|
42 |
std::array<wchar_t, 200 > buffer; |
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 |
int error = hid_get_manufacturer_string(handle, buffer.data(), buffer.size()); |
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
|
44 |
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
|
45 |
return buffer.data(); |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
|
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
|
48 |
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
|
49 |
std::array<wchar_t, 200 > buffer; |
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 |
int error = hid_get_product_string(handle, buffer.data(), buffer.size()); |
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 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
|
52 |
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
|
53 |
} |
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 |
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
|
56 |
std::array<wchar_t, 200 > buffer; |
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 |
int error = hid_get_serial_number_string(handle, buffer.data(), buffer.size()); |
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 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
|
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 |
void sendFeatureReport(std::vector<unsigned char> 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
|
63 |
int written = hid_send_feature_report(handle, data.data(), data.size()); |
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
|
64 |
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
|
65 |
} |
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
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 |
int main(int argc, char** argv) { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
try { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
std::wcout << L"cadMousePro" << std::endl; |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
HIDDevice mouse(0x256f, 0xc652, nullptr); |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
// 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
|
78 |
|
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 |
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
|
80 |
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
|
81 |
// 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
|
82 |
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
|
83 |
|
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 |
// 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
|
85 |
// 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
|
86 |
// 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
|
87 |
// 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
|
88 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
} catch (const HIDException& e) { |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
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
|
91 |
|
0
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
} |
6ff501639c23
project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
} |