cadMousePro-daemon/src/Daemon.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 03 Apr 2021 17:39:35 +0200
branchv_0
changeset 33 0dc4e2942840
parent 31 3cbac1956f05
permissions -rw-r--r--
support also the wired version of CadMouse Pro currently, there is a simple autodetection: we enumerate HID devices and look for the wireless version, if not found, we look for the wired version

/**
 * 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:

	static unsigned short const VENDOR_ID = 0x256f;
	static unsigned short const PRODUCT_ID_WIRELESS = 0xc652;
	static unsigned short const PRODUCT_ID_WIRED = 0xc656;

	void logException(const HIDException& e) {
		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
	}
public:

	Daemon(QObject* parent = nullptr) : QObject(parent) {
	}

	static HIDDevice getMouse() {
		// TODO: multiple mouse support, better mouse selection, specific settings for particular type…
		// (probably after future redesign/rewrite – this is just a simple tool written for 3DConnexion CadMouse Pro)
		std::vector<HIDDeviceInfo> devices = HIDDeviceInfo::enumerate();
		for (auto pid :{PRODUCT_ID_WIRELESS, PRODUCT_ID_WIRED}) {
			for (HIDDeviceInfo device : devices) {
				if (device.matches(VENDOR_ID, pid)) return device.open();
			}
		}
		throw HIDException(L"No suitable mouse found.");
	}

	Q_PROPERTY(bool devicePresent READ devicePresent)
	bool devicePresent() {
		std::wcout << L"checking mouse presence: ";
		try {
			getMouse();
			std::wcout << L"present" << std::endl;
			return true;
		} catch (const HIDException& e) {
			std::wcout << std::endl;
			logException(e);
			return false;
		}
	}

	Q_PROPERTY(QString deviceName READ deviceName)
	QString deviceName() {
		std::wcout << L"reading mouse name: ";
		try {
			auto productName = getMouse().getProductName();
			std::wcout << productName << std::endl;
			return QString::fromStdWString(productName);
		} catch (HIDException& e) {
			std::wcout << std::endl;
			logException(e);
			return QString();
		}
	}

	void configure(bool liftOffDetection, bool smartScrolling, bool remapWheelPress, bool remapGestureButton, int frequency) {
		std::wcout << L"configuring mouse:"
				<< L" liftOffDetection=" << liftOffDetection
				<< L" smartScrolling=" << smartScrolling
				<< L" remapWheelPress=" << remapWheelPress
				<< L" remapGestureButton=" << remapGestureButton
				<< L" frequency=" << frequency
				<< 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()));
		}
	}

};