cadMousePro-daemon/src/Daemon.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 09 Jun 2020 16:12:00 +0200
branchv_0
changeset 29 361687fe303a
parent 26 fff8e9a86e85
child 31 3cbac1956f05
permissions -rw-r--r--
add two new options: 1) remap wheel button 2) remap gesture button thanks Paul Guertin for discovering the magic numbers

/**
 * 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, version 3 of the License.
 *
 * 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 <QDBusConnection>

#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, bool remapWheelPress, bool remapGestureButton, int frequency) {
		std::wcout << L"configuring mouse" << std::endl;
		try {
			CadMouseConfig config;
			config.setLiftOffDetection(liftOffDetection);
			config.setSmartScrolling(smartScrolling);
			config.setWheelPressRemapped(remapWheelPress);
			config.setRemapGestureButton(remapGestureButton);
			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()));
		}
	}

};