src/cadMousePro.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 14 Aug 2019 16:16:58 +0200
branchv_0
changeset 0 6ff501639c23
child 1 29cbe171cd43
permissions -rw-r--r--
project skeleton, open USB HID device
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#include <iostream>
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <hidapi/hidapi.h>
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
class HIDException {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
private:
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
	std::wstring message;
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
public:
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
	HIDException(std::wstring message) : message(message) {
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
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
	virtual ~HIDException() {
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
	const std::wstring getMessage() const {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
		return message;
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
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
class HIDDevice {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
private:
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
	hid_device* handle;
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
public:
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
	HIDDevice(unsigned short vendorId, unsigned short productId, const wchar_t *serialNumber) {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
		handle = hid_open(vendorId, productId, serialNumber);
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
		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
    30
	}
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	virtual ~HIDDevice() {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
		hid_close(handle);
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
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
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
int main(int argc, char** argv) {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	try {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		std::wcout << L"cadMousePro" << std::endl;
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		HIDDevice mouse(0x256f, 0xc652, nullptr);
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		std::wcout << L"mouse opened" << std::endl;
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	} catch (const HIDException& e) {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
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
	}
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
}