#include <iostream>
#include <array>
#include <vector>
#include <hidapi/hidapi.h>
class HIDException {
private:
std::wstring message;
public:
HIDException(std::wstring message) : message(message) {
}
virtual ~HIDException() {
}
const std::wstring getMessage() const {
return message;
}
};
class HIDDevice {
private:
hid_device* handle;
HIDDevice(const HIDDevice& other) : handle(other.handle) {
}
public:
HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
int initError = hid_init(); // TODO: move to HIDContext class?
if (initError) throw HIDException(L"Unable to init HID API.");
handle = hid_open(vendorId, productId, serialNumber);
if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
}
virtual ~HIDDevice() {
hid_close(handle);
hid_exit(); // TODO: move to HIDContext class?
}
const std::wstring getManufacturerName() const {
std::array<wchar_t, 200 > buffer;
int error = hid_get_manufacturer_string(handle, buffer.data(), buffer.size());
if (error) throw HIDException(L"Unable to get manufacturer name.");
return buffer.data();
}
const std::wstring getProductName() const {
std::array<wchar_t, 200 > buffer;
int error = hid_get_product_string(handle, buffer.data(), buffer.size());
if (error) throw HIDException(L"Unable to get product name.");
return buffer.data();
}
const std::wstring getSerialNumber() const {
std::array<wchar_t, 200 > buffer;
int error = hid_get_serial_number_string(handle, buffer.data(), buffer.size());
if (error) throw HIDException(L"Unable to get serial number.");
return buffer.data();
}
void sendFeatureReport(std::vector<unsigned char> data) {
int written = hid_send_feature_report(handle, data.data(), data.size());
if (written < 0) throw HIDException(L"Unable to send feature report.");
}
};
int main(int argc, char** argv) {
try {
std::wcout << L"cadMousePro" << std::endl;
HIDDevice mouse(0x256f, 0xc652, nullptr);
std::wcout << L"mouse opened" << std::endl;
std::wcout << L"manufacturer: " << mouse.getManufacturerName() << std::endl;
std::wcout << L"product: " << mouse.getProductName() << std::endl;
// std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception
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};
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};
// mouse.sendFeatureReport(enableFreeWheel);
mouse.sendFeatureReport(disableFreeWheel);
// TODO: enable acceleration
// TODO: enable lift-off detection
// TODO: sensof polling rate – frequency
// TODO: wheel scrolling size – lines, page
} catch (const HIDException& e) {
std::wcout << L"HIDException: " << e.getMessage() << std::endl;
}
}