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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * cadMousePro
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
26
fff8e9a86e85 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <iostream>
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <QObject>
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <QDBusContext>
13
52d92c1b340e generate C++ classes from D-Bus XML using cmake instead of a custom script
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    22
#include <QDBusConnection>
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include "HID.h"
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "CadMouseConfig.h"
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Common.h"
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
class Daemon : public QObject, public QDBusContext {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	Q_OBJECT;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
private:
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
33
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    33
	static unsigned short const VENDOR_ID = 0x256f;
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    34
	static unsigned short const PRODUCT_ID_WIRELESS = 0xc652;
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    35
	static unsigned short const PRODUCT_ID_WIRED = 0xc656;
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	void logException(const HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
public:
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
33
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    42
	Daemon(QObject* parent = nullptr) : QObject(parent) {
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    43
	}
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
33
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    45
	static HIDDevice getMouse() {
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    46
		// TODO: multiple mouse support, better mouse selection, specific settings for particular type…
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    47
		// (probably after future redesign/rewrite – this is just a simple tool written for 3DConnexion CadMouse Pro)
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    48
		std::vector<HIDDeviceInfo> devices = HIDDeviceInfo::enumerate();
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    49
		for (auto pid :{PRODUCT_ID_WIRELESS, PRODUCT_ID_WIRED}) {
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    50
			for (HIDDeviceInfo device : devices) {
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    51
				if (device.matches(VENDOR_ID, pid)) return device.open();
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    52
			}
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    53
		}
0dc4e2942840 support also the wired version of CadMouse Pro
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    54
		throw HIDException(L"No suitable mouse found.");
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	Q_PROPERTY(bool devicePresent READ devicePresent)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	bool devicePresent() {
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    59
		std::wcout << L"checking mouse presence: ";
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			getMouse();
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    62
			std::wcout << L"present" << std::endl;
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			return true;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		} catch (const HIDException& e) {
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    65
			std::wcout << std::endl;
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			return false;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	Q_PROPERTY(QString deviceName READ deviceName)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	QString deviceName() {
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    73
		std::wcout << L"reading mouse name: ";
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		try {
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    75
			auto productName = getMouse().getProductName();
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    76
			std::wcout << productName << std::endl;
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    77
			return QString::fromStdWString(productName);
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		} catch (HIDException& e) {
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    79
			std::wcout << std::endl;
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			return QString();
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
29
361687fe303a add two new options: 1) remap wheel button 2) remap gesture button
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    85
	void configure(bool liftOffDetection, bool smartScrolling, bool remapWheelPress, bool remapGestureButton, int frequency) {
31
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    86
		std::wcout << L"configuring mouse:"
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    87
				<< L" liftOffDetection=" << liftOffDetection
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    88
				<< L" smartScrolling=" << smartScrolling
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    89
				<< L" remapWheelPress=" << remapWheelPress
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    90
				<< L" remapGestureButton=" << remapGestureButton
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    91
				<< L" frequency=" << frequency
3cbac1956f05 log values while configuring mouse, checking mouse presence or reading mouse name
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    92
				<< std::endl;
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
			CadMouseConfig config;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
			config.setLiftOffDetection(liftOffDetection);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
			config.setSmartScrolling(smartScrolling);
29
361687fe303a add two new options: 1) remap wheel button 2) remap gesture button
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    97
			config.setWheelPressRemapped(remapWheelPress);
361687fe303a add two new options: 1) remap wheel button 2) remap gesture button
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    98
			config.setRemapGestureButton(remapGestureButton);
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
			config.setFrequency(Common::parseFrequency(std::to_wstring(frequency)));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
			getMouse().sendFeatureReport(config.serialize());
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		} catch (HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		} catch (CLIException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
};