src/cadMousePro.cpp
branchv_0
changeset 3 1197b42e8b2e
parent 2 abeba77ec581
child 4 405aa9de65d2
equal deleted inserted replaced
2:abeba77ec581 3:1197b42e8b2e
     1 #include <iostream>
     1 #include <iostream>
     2 #include <array>
     2 #include <array>
     3 #include <vector>
     3 #include <vector>
     4 
     4 
     5 #include <hidapi/hidapi.h>
     5 #include <hidapi/hidapi.h>
       
     6 #include <memory>
     6 
     7 
     7 class HIDException {
     8 class HIDException {
     8 private:
     9 private:
     9 	std::wstring message;
    10 	std::wstring message;
    10 public:
    11 public:
    21 
    22 
    22 };
    23 };
    23 
    24 
    24 class HIDDevice {
    25 class HIDDevice {
    25 private:
    26 private:
    26 	hid_device* handle;
    27 	std::shared_ptr<hid_device> handle;
    27 
       
    28 	HIDDevice(const HIDDevice& other) : handle(other.handle) {
       
    29 	}
       
    30 
    28 
    31 public:
    29 public:
    32 
    30 
    33 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
    31 	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
    34 		int initError = hid_init(); // TODO: move to HIDContext class?
    32 		int initError = hid_init(); // TODO: move to HIDContext class?
    35 		if (initError) throw HIDException(L"Unable to init HID API.");
    33 		if (initError) throw HIDException(L"Unable to init HID API.");
    36 		handle = hid_open(vendorId, productId, serialNumber);
    34 		handle.reset(hid_open(vendorId, productId, serialNumber), hid_close);
    37 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
    35 		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
    38 	}
    36 	}
    39 
    37 
    40 	virtual ~HIDDevice() {
    38 	virtual ~HIDDevice() {
    41 		hid_close(handle);
       
    42 		hid_exit(); // TODO: move to HIDContext class?
    39 		hid_exit(); // TODO: move to HIDContext class?
    43 	}
    40 	}
    44 
    41 
    45 	const std::wstring getManufacturerName() const {
    42 	const std::wstring getManufacturerName() const {
    46 		std::array<wchar_t, 200 > buffer;
    43 		std::array<wchar_t, 200 > buffer;
    47 		int error = hid_get_manufacturer_string(handle, buffer.data(), buffer.size());
    44 		int error = hid_get_manufacturer_string(handle.get(), buffer.data(), buffer.size());
    48 		if (error) throw HIDException(L"Unable to get manufacturer name.");
    45 		if (error) throw HIDException(L"Unable to get manufacturer name.");
    49 		return buffer.data();
    46 		return buffer.data();
    50 	}
    47 	}
    51 
    48 
    52 	const std::wstring getProductName() const {
    49 	const std::wstring getProductName() const {
    53 		std::array<wchar_t, 200 > buffer;
    50 		std::array<wchar_t, 200 > buffer;
    54 		int error = hid_get_product_string(handle, buffer.data(), buffer.size());
    51 		int error = hid_get_product_string(handle.get(), buffer.data(), buffer.size());
    55 		if (error) throw HIDException(L"Unable to get product name.");
    52 		if (error) throw HIDException(L"Unable to get product name.");
    56 		return buffer.data();
    53 		return buffer.data();
    57 	}
    54 	}
    58 
    55 
    59 	const std::wstring getSerialNumber() const {
    56 	const std::wstring getSerialNumber() const {
    60 		std::array<wchar_t, 200 > buffer;
    57 		std::array<wchar_t, 200 > buffer;
    61 		int error = hid_get_serial_number_string(handle, buffer.data(), buffer.size());
    58 		int error = hid_get_serial_number_string(handle.get(), buffer.data(), buffer.size());
    62 		if (error) throw HIDException(L"Unable to get serial number.");
    59 		if (error) throw HIDException(L"Unable to get serial number.");
    63 		return buffer.data();
    60 		return buffer.data();
    64 	}
    61 	}
    65 
    62 
    66 	void sendFeatureReport(std::vector<unsigned char> data) {
    63 	void sendFeatureReport(std::vector<unsigned char> data) {
    67 		int written = hid_send_feature_report(handle, data.data(), data.size());
    64 		int written = hid_send_feature_report(handle.get(), data.data(), data.size());
    68 		if (written < 0) throw HIDException(L"Unable to send feature report.");
    65 		if (written < 0) throw HIDException(L"Unable to send feature report.");
    69 	}
    66 	}
    70 
    67 
    71 };
    68 };
    72 
    69