cadMousePro-daemon/src/HID.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 19 Aug 2019 17:27:19 +0200
branchv_0
changeset 8 465572518625
parent 6 src/HID.h@975f38eb1e12
child 11 bb42abd9f510
permissions -rw-r--r--
move sources to a sub-project folder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     1
/**
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     2
 * cadMousePro
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     4
 *
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     8
 * (at your option) any later version.
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     9
 *
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    13
 * GNU General Public License for more details.
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    14
 *
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    15
 * You should have received a copy of the GNU General Public License
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    17
 */
5
6799cec5c2f8 refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    18
#pragma once
6799cec5c2f8 refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    19
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
    20
#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
    21
#include <vector>
4
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    22
#include <memory>
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    23
#include <unistd.h>
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    24
#include <cassert>
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <hidapi/hidapi.h>
4
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
using Packet = std::vector<uint8_t>;
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    29
static_assert(sizeof (uint8_t) == sizeof (unsigned char)); // unsigned char is used in the HID API library
0
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
class HIDException {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	std::wstring message;
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
public:
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
	HIDException(std::wstring message) : message(message) {
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
	virtual ~HIDException() {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	}
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
    41
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	const std::wstring getMessage() const {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		return message;
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
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
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
class HIDDevice {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
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
    50
	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
    51
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
public:
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	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
    55
		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
    56
		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
    57
		handle.reset(hid_open(vendorId, productId, serialNumber), hid_close);
5
6799cec5c2f8 refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    58
		if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is the device present?");
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	}
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	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
    62
		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
    63
	}
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
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
	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
    66
		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
    67
		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
    68
		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
    69
		return buffer.data();
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
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
    72
	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
    73
		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
    74
		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
    75
		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
    76
		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
    77
	}
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
	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
    80
		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
    81
		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
    82
		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
    83
		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
    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
4
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
	void sendFeatureReport(Packet 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
    87
		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
    88
		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
    89
	}
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
};