cadMousePro-daemon/src/Daemon.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 29 Aug 2019 19:21:45 +0200
branchv_0
changeset 13 52d92c1b340e
parent 12 cf77c218b0b1
child 26 fff8e9a86e85
permissions -rw-r--r--
generate C++ classes from D-Bus XML using cmake instead of a custom script
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * 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
    11
 * 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
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 */
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <QObject>
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#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
    23
#include <QDBusConnection>
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "HID.h"
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "CadMouseConfig.h"
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include "Common.h"
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class Daemon : public QObject, public QDBusContext {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	Q_OBJECT;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	HIDDevice getMouse() {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		return HIDDevice(VENDOR_ID, PRODUCT_ID, nullptr);
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	void logException(const HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	}
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
public:
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	static unsigned short const VENDOR_ID = 0x256f;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	static unsigned short const PRODUCT_ID = 0xc652;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	Daemon(QObject* parent = nullptr) : QObject(parent) {
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	Q_PROPERTY(bool devicePresent READ devicePresent)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	bool devicePresent() {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		std::wcout << L"checking mouse presence" << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
			getMouse();
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			return true;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		} catch (const HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			return false;
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	Q_PROPERTY(QString deviceName READ deviceName)
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	QString deviceName() {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		std::wcout << L"reading mouse name" << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			return QString::fromStdWString(getMouse().getProductName());
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		} catch (HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			return QString();
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	void configure(bool liftOffDetection, bool smartScrolling, int frequency) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		std::wcout << L"configuring mouse" << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		try {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			CadMouseConfig config;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
			config.setLiftOffDetection(liftOffDetection);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			config.setSmartScrolling(smartScrolling);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			config.setFrequency(Common::parseFrequency(std::to_wstring(frequency)));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			getMouse().sendFeatureReport(config.serialize());
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		} catch (HIDException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			logException(e);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		} catch (CLIException& e) {
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage()));
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
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
};