cadMousePro-daemon/src/cadMousePro.cpp
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 33 0dc4e2942840
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     1
/**
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     2
 * cadMousePro
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     4
 *
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
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.
6
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     8
 *
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    12
 * GNU General Public License for more details.
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    13
 *
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    14
 * You should have received a copy of the GNU General Public License
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
975f38eb1e12 license: GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    16
 */
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <iostream>
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    19
#include <QCoreApplication>
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    20
#include <QtDBus/QDBusConnection>
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    21
5
6799cec5c2f8 refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    22
#include "CadMouseConfig.h"
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    23
#include "CLI.h"
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    24
#include "CLIParser.h"
5
6799cec5c2f8 refactoring: separate into several files
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    25
#include "HID.h"
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    26
#include "Daemon.h"
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
    27
#include "generated-sources/DBusAdaptor.h"
4
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
int main(int argc, char** argv) {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	try {
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    31
		CLI cli(argc, argv);
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
		std::wcout << L"cadMousePro" << std::endl;
1
29cbe171cd43 first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    35
		CLIParser cliParser;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    36
		CLIConfiguration configuration = cliParser.parse(cli.arguments());
1
29cbe171cd43 first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    37
10
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    38
		if (configuration.daemon) {
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    39
			// TODO: run D-Bus service
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    40
			std::wcout << L"will start daemon" << std::endl;
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    41
			std::wcout << L"uid = " << getuid() << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    42
			QCoreApplication qtApplication(argc, argv);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    43
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    44
			Daemon* daemon = new Daemon(&qtApplication);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    45
			DBusAdaptor* dbusAdaptor = new DBusAdaptor(daemon);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    46
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    47
			QDBusConnection connection = getuid() ? QDBusConnection::sessionBus() : QDBusConnection::systemBus();
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    48
			//QDBusConnection connection = QDBusConnection::sessionBus();
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    49
			std::wcout << (connection.isConnected() ? L"připojeno" : L"nepřipojeno") << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    50
			std::wcout << connection.name().toStdWString() << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    51
			std::wcout << connection.lastError().message().toStdWString() << std::endl;
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    52
			connection.registerObject("/info/globalcode/mouse/cadMousePro", daemon);
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    53
			connection.registerService("info.globalcode.mouse.cadMousePro");
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    54
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    55
			return qtApplication.exec();
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    56
10
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    57
		} else {
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    58
			HIDDevice mouse(Daemon::VENDOR_ID, Daemon::PRODUCT_ID, nullptr);
10
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    59
			std::wcout << L"mouse opened" << std::endl;
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    60
			std::wcout << L"manufacturer:  " << mouse.getManufacturerName() << std::endl;
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    61
			std::wcout << L"product:       " << mouse.getProductName() << std::endl;
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    62
			// std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    63
10
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    64
			mouse.sendFeatureReport(configuration.cadMouseConfig.serialize());
05dbed834852 prepare daemon config
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    65
		}
1
29cbe171cd43 first working version: disables „free wheel“ a.k.a. „smart scrolling“ feature
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    67
		return CLI::EXIT_CODE_SUCCESS;
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	} catch (const HIDException& e) {
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		std::wcout << L"HIDException: " << e.getMessage() << std::endl;
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    70
		return CLI::EXIT_CODE_UNEXPECTED_ERROR;
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    71
	} catch (const CLIException& e) {
12
cf77c218b0b1 D-Bus interface
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    72
		std::wcout << L"CLIException: " << e.getMessage() << std::endl;
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    73
		return e.getExitCode();
0
6ff501639c23 project skeleton, open USB HID device
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
7
148f8dd077e8 CLI options and parser of configuration parameters instead of hardcoded values
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    75
4
405aa9de65d2 CadMouseConfig class, frequency, lift-off detection, smart scrolling
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
}