cadMousePro-daemon/src/Daemon.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 23 Oct 2019 22:43:29 +0200
branchv_0
changeset 26 fff8e9a86e85
parent 13 52d92c1b340e
child 29 361687fe303a
permissions -rw-r--r--
fix license version: GNU GPLv3
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	HIDDevice getMouse() {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		return HIDDevice(VENDOR_ID, PRODUCT_ID, nullptr);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	}
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	static unsigned short const VENDOR_ID = 0x256f;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	static unsigned short const PRODUCT_ID = 0xc652;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	Daemon(QObject* parent = nullptr) : QObject(parent) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	Q_PROPERTY(bool devicePresent READ devicePresent)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	bool devicePresent() {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		std::wcout << L"checking mouse presence" << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
			getMouse();
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
			return true;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		} catch (const HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			return false;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	Q_PROPERTY(QString deviceName READ deviceName)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	QString deviceName() {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		std::wcout << L"reading mouse name" << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			return QString::fromStdWString(getMouse().getProductName());
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		} catch (HIDException& e) {
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 QString();
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
	void configure(bool liftOffDetection, bool smartScrolling, int frequency) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		std::wcout << L"configuring mouse" << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			CadMouseConfig config;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			config.setLiftOffDetection(liftOffDetection);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
			config.setSmartScrolling(smartScrolling);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			config.setFrequency(Common::parseFrequency(std::to_wstring(frequency)));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			getMouse().sendFeatureReport(config.serialize());
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		} catch (HIDException& e) {
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
			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		} catch (CLIException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
};