cadMousePro-daemon/src/Daemon.h
branchv_0
changeset 12 cf77c218b0b1
child 13 52d92c1b340e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cadMousePro-daemon/src/Daemon.h	Thu Aug 29 17:57:30 2019 +0200
@@ -0,0 +1,88 @@
+/**
+ * cadMousePro
+ * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <iostream>
+#include <QObject>
+#include <QDBusContext>
+#include <qt5/QtDBus/qdbusconnection.h>
+
+#include "HID.h"
+#include "CadMouseConfig.h"
+#include "Common.h"
+
+class Daemon : public QObject, public QDBusContext {
+	Q_OBJECT;
+
+private:
+
+	HIDDevice getMouse() {
+		return HIDDevice(VENDOR_ID, PRODUCT_ID, nullptr);
+	}
+
+	void logException(const HIDException& e) {
+		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
+	}
+public:
+
+	static unsigned short const VENDOR_ID = 0x256f;
+	static unsigned short const PRODUCT_ID = 0xc652;
+
+	Daemon(QObject* parent = nullptr) : QObject(parent) {
+	}
+
+	Q_PROPERTY(bool devicePresent READ devicePresent)
+	bool devicePresent() {
+		std::wcout << L"checking mouse presence" << std::endl;
+		try {
+			getMouse();
+			return true;
+		} catch (const HIDException& e) {
+			logException(e);
+			return false;
+		}
+	}
+
+	Q_PROPERTY(QString deviceName READ deviceName)
+	QString deviceName() {
+		std::wcout << L"reading mouse name" << std::endl;
+		try {
+			return QString::fromStdWString(getMouse().getProductName());
+		} catch (HIDException& e) {
+			logException(e);
+			return QString();
+		}
+	}
+
+	void configure(bool liftOffDetection, bool smartScrolling, int frequency) {
+		std::wcout << L"configuring mouse" << std::endl;
+		try {
+			CadMouseConfig config;
+			config.setLiftOffDetection(liftOffDetection);
+			config.setSmartScrolling(smartScrolling);
+			config.setFrequency(Common::parseFrequency(std::to_wstring(frequency)));
+			getMouse().sendFeatureReport(config.serialize());
+		} catch (HIDException& e) {
+			logException(e);
+			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
+		} catch (CLIException& e) {
+			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
+		}
+	}
+
+};