|
1 /** |
|
2 * cadMousePro |
|
3 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #pragma once |
|
19 |
|
20 #include <iostream> |
|
21 #include <QObject> |
|
22 #include <QDBusContext> |
|
23 #include <qt5/QtDBus/qdbusconnection.h> |
|
24 |
|
25 #include "HID.h" |
|
26 #include "CadMouseConfig.h" |
|
27 #include "Common.h" |
|
28 |
|
29 class Daemon : public QObject, public QDBusContext { |
|
30 Q_OBJECT; |
|
31 |
|
32 private: |
|
33 |
|
34 HIDDevice getMouse() { |
|
35 return HIDDevice(VENDOR_ID, PRODUCT_ID, nullptr); |
|
36 } |
|
37 |
|
38 void logException(const HIDException& e) { |
|
39 std::wcout << L"HIDException: " << e.getMessage() << std::endl; |
|
40 } |
|
41 public: |
|
42 |
|
43 static unsigned short const VENDOR_ID = 0x256f; |
|
44 static unsigned short const PRODUCT_ID = 0xc652; |
|
45 |
|
46 Daemon(QObject* parent = nullptr) : QObject(parent) { |
|
47 } |
|
48 |
|
49 Q_PROPERTY(bool devicePresent READ devicePresent) |
|
50 bool devicePresent() { |
|
51 std::wcout << L"checking mouse presence" << std::endl; |
|
52 try { |
|
53 getMouse(); |
|
54 return true; |
|
55 } catch (const HIDException& e) { |
|
56 logException(e); |
|
57 return false; |
|
58 } |
|
59 } |
|
60 |
|
61 Q_PROPERTY(QString deviceName READ deviceName) |
|
62 QString deviceName() { |
|
63 std::wcout << L"reading mouse name" << std::endl; |
|
64 try { |
|
65 return QString::fromStdWString(getMouse().getProductName()); |
|
66 } catch (HIDException& e) { |
|
67 logException(e); |
|
68 return QString(); |
|
69 } |
|
70 } |
|
71 |
|
72 void configure(bool liftOffDetection, bool smartScrolling, int frequency) { |
|
73 std::wcout << L"configuring mouse" << std::endl; |
|
74 try { |
|
75 CadMouseConfig config; |
|
76 config.setLiftOffDetection(liftOffDetection); |
|
77 config.setSmartScrolling(smartScrolling); |
|
78 config.setFrequency(Common::parseFrequency(std::to_wstring(frequency))); |
|
79 getMouse().sendFeatureReport(config.serialize()); |
|
80 } catch (HIDException& e) { |
|
81 logException(e); |
|
82 sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage())); |
|
83 } catch (CLIException& e) { |
|
84 sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage())); |
|
85 } |
|
86 } |
|
87 |
|
88 }; |