--- a/nbproject/project.xml Sun Aug 18 23:00:21 2019 +0200
+++ b/nbproject/project.xml Sun Aug 18 23:30:01 2019 +0200
@@ -45,7 +45,7 @@
<name>cadMousePro</name>
<c-extensions/>
<cpp-extensions>cpp</cpp-extensions>
- <header-extensions/>
+ <header-extensions>h</header-extensions>
<sourceEncoding>UTF-8</sourceEncoding>
<sourceRootList>
<sourceRootElem>.</sourceRootElem>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CadMouseConfig.h Sun Aug 18 23:30:01 2019 +0200
@@ -0,0 +1,82 @@
+#pragma once
+
+#include <type_traits>
+
+#include "HID.h"
+
+enum class Frequency : uint8_t {
+ Hz_0125 = 8,
+ Hz_0250 = 4,
+ Hz_0500 = 2,
+ Hz_1000 = 1
+};
+
+using FrequencyType = std::underlying_type<Frequency>::type;
+
+class CadMouseConfig {
+private:
+ bool liftOffDetection = true;
+ bool smartScrolling = false;
+ Frequency frequency = Frequency::Hz_1000;
+public:
+
+ void setFrequency(Frequency frequency) {
+ this->frequency = frequency;
+ }
+
+ void setLiftOffDetection(bool liftOffDetection) {
+ this->liftOffDetection = liftOffDetection;
+ }
+
+ void setSmartScrolling(bool smartScrolling) {
+ this->smartScrolling = smartScrolling;
+ }
+
+ Packet serialize() {
+ Packet data;
+ data.reserve(32);
+
+ data.push_back(0x10); // report ID
+ data.push_back(0x00); // option
+
+ data.push_back(0x1c); // speed
+
+ data.push_back(liftOffDetection ? 0x00 : 0x1f);
+
+ if (smartScrolling) {
+ data.push_back(0x00);
+ data.push_back(0x00);
+ data.push_back(0x00);
+ data.push_back(0x01);
+ } else {
+ data.push_back(0x01);
+ data.push_back(0xff);
+ data.push_back(0x00);
+ data.push_back(0x00);
+ }
+
+ for (int i = 0; i < 8; i++) data.push_back(0x00); // constant padding or magic
+
+ // magic constants or unknown fields
+ data.push_back(0x00);
+ data.push_back(0x03);
+ data.push_back(0x00);
+ data.push_back(0x0a);
+ data.push_back(0x0b);
+ data.push_back(0x0c);
+ data.push_back(0x0c);
+ data.push_back(0x0e);
+ data.push_back(0x0d);
+ data.push_back(0x2f);
+ data.push_back(0x00);
+ data.push_back(0x1e);
+
+ data.push_back(0x00);
+ data.push_back(0x00);
+ data.push_back(0x00);
+ data.push_back(static_cast<FrequencyType> (frequency));
+
+ return data;
+ }
+
+};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HID.h Sun Aug 18 23:30:01 2019 +0200
@@ -0,0 +1,74 @@
+#pragma once
+
+#include <array>
+#include <vector>
+#include <memory>
+#include <unistd.h>
+#include <cassert>
+
+#include <hidapi/hidapi.h>
+
+using Packet = std::vector<uint8_t>;
+static_assert(sizeof (uint8_t) == sizeof (unsigned char)); // unsigned char is used in the HID API library
+
+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:
+ std::shared_ptr<hid_device> 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.reset(hid_open(vendorId, productId, serialNumber), hid_close);
+ if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is the device present?");
+ }
+
+ virtual ~HIDDevice() {
+ 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.get(), 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.get(), 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.get(), buffer.data(), buffer.size());
+ if (error) throw HIDException(L"Unable to get serial number.");
+ return buffer.data();
+ }
+
+ void sendFeatureReport(Packet data) {
+ int written = hid_send_feature_report(handle.get(), data.data(), data.size());
+ if (written < 0) throw HIDException(L"Unable to send feature report.");
+ }
+
+};
--- a/src/cadMousePro.cpp Sun Aug 18 23:00:21 2019 +0200
+++ b/src/cadMousePro.cpp Sun Aug 18 23:30:01 2019 +0200
@@ -1,154 +1,7 @@
#include <iostream>
-#include <array>
-#include <vector>
-#include <memory>
-#include <unistd.h>
-#include <cassert>
-#include <type_traits>
-
-#include <hidapi/hidapi.h>
-
-using Packet = std::vector<uint8_t>;
-static_assert(sizeof (uint8_t) == sizeof (unsigned char)); // unsigned char is used in the HID API library
-
-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:
- std::shared_ptr<hid_device> 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.reset(hid_open(vendorId, productId, serialNumber), hid_close);
- if (handle == nullptr) throw HIDException(L"Unable to open HID device. Are you root? Is mouse present?");
- }
-
- virtual ~HIDDevice() {
- 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.get(), 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.get(), 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.get(), buffer.data(), buffer.size());
- if (error) throw HIDException(L"Unable to get serial number.");
- return buffer.data();
- }
-
- void sendFeatureReport(Packet data) {
- int written = hid_send_feature_report(handle.get(), data.data(), data.size());
- if (written < 0) throw HIDException(L"Unable to send feature report.");
- }
-
-};
-enum class Frequency : uint8_t {
- Hz_0125 = 8,
- Hz_0250 = 4,
- Hz_0500 = 2,
- Hz_1000 = 1
-};
-
-using FrequencyType = std::underlying_type<Frequency>::type;
-
-class CadMouseConfig {
-private:
- bool liftOffDetection = true;
- bool smartScrolling = false;
- Frequency frequency = Frequency::Hz_1000;
-public:
-
- void setFrequency(Frequency frequency) {
- this->frequency = frequency;
- }
-
- void setLiftOffDetection(bool liftOffDetection) {
- this->liftOffDetection = liftOffDetection;
- }
-
- void setSmartScrolling(bool smartScrolling) {
- this->smartScrolling = smartScrolling;
- }
-
- Packet serialize() {
- Packet data;
- data.reserve(32);
-
- data.push_back(0x10); // report ID
- data.push_back(0x00); // option
-
- data.push_back(0x1c); // speed
-
- data.push_back(liftOffDetection ? 0x00 : 0x1f);
-
- if (smartScrolling) {
- data.push_back(0x00);
- data.push_back(0x00);
- data.push_back(0x00);
- data.push_back(0x01);
- } else {
- data.push_back(0x01);
- data.push_back(0xff);
- data.push_back(0x00);
- data.push_back(0x00);
- }
-
- for (int i = 0; i < 8; i++) data.push_back(0x00); // constant padding or magic
-
- // magic constants or unknown fields
- data.push_back(0x00);
- data.push_back(0x03);
- data.push_back(0x00);
- data.push_back(0x0a);
- data.push_back(0x0b);
- data.push_back(0x0c);
- data.push_back(0x0c);
- data.push_back(0x0e);
- data.push_back(0x0d);
- data.push_back(0x2f);
- data.push_back(0x00);
- data.push_back(0x1e);
-
- data.push_back(0x00);
- data.push_back(0x00);
- data.push_back(0x00);
- data.push_back(static_cast<FrequencyType> (frequency));
-
- return data;
- }
-
-};
+#include "CadMouseConfig.h"
+#include "HID.h"
int main(int argc, char** argv) {
try {