# HG changeset patch # User František Kučera # Date 1567094250 -7200 # Node ID cf77c218b0b11ed0849e7f14dfe476ab23ccda52 # Parent bb42abd9f5109a53d2d6af224a74a34118fc5a4c D-Bus interface diff -r bb42abd9f510 -r cf77c218b0b1 .hgignore --- a/.hgignore Tue Aug 20 18:59:39 2019 +0200 +++ b/.hgignore Thu Aug 29 17:57:30 2019 +0200 @@ -10,3 +10,4 @@ ^[^/]+/dist/ ^[^/]+/build/ ^[^/]+/nbproject/private/ +^[^/]+/src/generated-sources/ diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/nbproject/configurations.xml --- a/cadMousePro-daemon/nbproject/configurations.xml Tue Aug 20 18:59:39 2019 +0200 +++ b/cadMousePro-daemon/nbproject/configurations.xml Thu Aug 29 17:57:30 2019 +0200 @@ -41,10 +41,54 @@ + + + + + DBusAdaptor.cpp + + DBusAdaptor.cpp + + + + + + mocs_compilation.cpp + + + + + + DBusAdaptor.cpp + + Daemon.h cadMousePro.cpp + + + build/Debug/src/cadMousePro_autogen/7JU4VIX55Q/moc_DBusAdaptor.cpp + + + build/Debug/src/cadMousePro_autogen/EWIEGA46WW/moc_DBusAdaptor.cpp + build/Debug/src/cadMousePro_autogen/EWIEGA46WW/moc_Daemon.cpp + + + build/Debug/src/cadMousePro_autogen/S5VNLJ26FI/moc_DBusAdaptor.cpp + + build/Debug/src/cadMousePro_autogen/mocs_compilation.cpp + + + /usr/share/cmake-3.10/Modules/CMakeCXXCompilerABI.cpp + /usr/share/cmake-3.10/Modules/CMakeCompilerABI.h + false - + + @@ -77,12 +123,6 @@ ${MAKE} -f Makefile ${MAKE} -f Makefile clean build/Debug/src/welcome - - - /usr/include/hidapi - build/Debug/src - - build/Debug @@ -90,7 +130,154 @@ true - + + + + + + + + + + build/Debug/src/cadMousePro_autogen/include + + + + + + + + + + + + + + + + + + + + + + + + + + + /usr/include/hidapi + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtDBus + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ + + + + + + + build/Debug/src + src + + + QT_CORE_LIB + QT_DBUS_LIB + + + + + + + src + build/qt/src/cadMousePro_autogen/EWIEGA46WW + build/qt/src/cadMousePro_autogen + build/qt/src + + + + + + + build/Debug/src + src + build/Debug/src/cadMousePro_autogen/include + /usr/include/hidapi + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtDBus + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ + + + QT_CORE_LIB + QT_DBUS_LIB + + + + + + + build/Debug/src + src + build/Debug/src/cadMousePro_autogen/include + /usr/include/hidapi + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtDBus + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ + + + QT_CORE_LIB + QT_DBUS_LIB + + + + + + + build/Debug/src + src + build/Debug/src/cadMousePro_autogen/include + /usr/include/hidapi + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtDBus + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ + + + QT_CORE_LIB + QT_DBUS_LIB + + + + @@ -127,6 +314,8 @@ true + + diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/src/CLIException.h --- a/cadMousePro-daemon/src/CLIException.h Tue Aug 20 18:59:39 2019 +0200 +++ b/cadMousePro-daemon/src/CLIException.h Thu Aug 29 17:57:30 2019 +0200 @@ -30,7 +30,7 @@ CLIException(std::wstring message, int exitCode) : message(message), exitCode(exitCode) { } - std::wstring getMessge() const { + std::wstring getMessage() const { return message; } diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/src/CLIParser.h --- a/cadMousePro-daemon/src/CLIParser.h Tue Aug 20 18:59:39 2019 +0200 +++ b/cadMousePro-daemon/src/CLIParser.h Thu Aug 29 17:57:30 2019 +0200 @@ -37,7 +37,9 @@ else throw CLIException(L"Unable to parse boolean value „" + value + L"“ – expecting „true“ or „false“", CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); } - Frequency parseFrequency(std::wstring value) { +public: + + static Frequency parseFrequency(std::wstring value) { if (value == L"125") return Frequency::Hz_0125; else if (value == L"250") return Frequency::Hz_0250; else if (value == L"500") return Frequency::Hz_0500; @@ -45,8 +47,6 @@ else throw CLIException(L"Unable to parse frequency value „" + value + L"“ – expecting „125“ or „250“ or „500“ or „1000“", CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); } -public: - static const std::wstring OPTION_FREQUENCY; static const std::wstring OPTION_SMART_SCROLLING; static const std::wstring OPTION_LIFT_OFF_DETECTION; diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/src/CMakeLists.txt --- a/cadMousePro-daemon/src/CMakeLists.txt Tue Aug 20 18:59:39 2019 +0200 +++ b/cadMousePro-daemon/src/CMakeLists.txt Thu Aug 29 17:57:30 2019 +0200 @@ -2,6 +2,13 @@ # USB libraries: INCLUDE(FindPkgConfig) + +# Qt libraries: +set(CMAKE_INCLUDE_CURRENT_DIR ON) # Find includes in corresponding build directories +set(CMAKE_AUTOMOC ON) # Instruct CMake to run moc automatically when needed +set(CMAKE_AUTOUIC ON) # Create code from a list of Qt designer ui files +find_package(Qt5 CONFIG REQUIRED Core DBus) + pkg_check_modules (USB_LIBS hidapi-hidraw) include_directories(${USB_LIBS_INCLUDE_DIRS}) link_directories(${USB_LIBS_LIBRARY_DIRS}) @@ -13,9 +20,12 @@ # Executable output: add_executable( ${EXECUTABLE_FILE} + Daemon.h + generated-sources/DBusAdaptor.h + generated-sources/DBusAdaptor.cpp cadMousePro.cpp ) # Link libraries: -target_link_libraries(${EXECUTABLE_FILE} ${USB_LIBS_LIBRARIES}) +target_link_libraries(${EXECUTABLE_FILE} ${USB_LIBS_LIBRARIES} Qt5::DBus) set_property(TARGET ${EXECUTABLE_FILE} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/src/Common.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cadMousePro-daemon/src/Common.h Thu Aug 29 17:57:30 2019 +0200 @@ -0,0 +1,37 @@ +/** + * cadMousePro + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include "CadMouseConfig.h" +#include "CLI.h" +#include "CLIException.h" + +class Common { +public: + + static Frequency parseFrequency(std::wstring value) { + if (value == L"125") return Frequency::Hz_0125; + else if (value == L"250") return Frequency::Hz_0250; + else if (value == L"500") return Frequency::Hz_0500; + else if (value == L"1000") return Frequency::Hz_1000; + else throw CLIException(L"Unable to parse frequency value „" + value + L"“ – expecting „125“ or „250“ or „500“ or „1000“", CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + // TODO: refactor exceptions + +}; diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/src/Daemon.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cadMousePro-daemon/src/Daemon.h Thu Aug 29 17:57:30 2019 +0200 @@ -0,0 +1,88 @@ +/** + * cadMousePro + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include +#include +#include + +#include "HID.h" +#include "CadMouseConfig.h" +#include "Common.h" + +class Daemon : public QObject, public QDBusContext { + Q_OBJECT; + +private: + + HIDDevice getMouse() { + return HIDDevice(VENDOR_ID, PRODUCT_ID, nullptr); + } + + void logException(const HIDException& e) { + std::wcout << L"HIDException: " << e.getMessage() << std::endl; + } +public: + + static unsigned short const VENDOR_ID = 0x256f; + static unsigned short const PRODUCT_ID = 0xc652; + + Daemon(QObject* parent = nullptr) : QObject(parent) { + } + + Q_PROPERTY(bool devicePresent READ devicePresent) + bool devicePresent() { + std::wcout << L"checking mouse presence" << std::endl; + try { + getMouse(); + return true; + } catch (const HIDException& e) { + logException(e); + return false; + } + } + + Q_PROPERTY(QString deviceName READ deviceName) + QString deviceName() { + std::wcout << L"reading mouse name" << std::endl; + try { + return QString::fromStdWString(getMouse().getProductName()); + } catch (HIDException& e) { + logException(e); + return QString(); + } + } + + void configure(bool liftOffDetection, bool smartScrolling, int frequency) { + std::wcout << L"configuring mouse" << std::endl; + try { + CadMouseConfig config; + config.setLiftOffDetection(liftOffDetection); + config.setSmartScrolling(smartScrolling); + config.setFrequency(Common::parseFrequency(std::to_wstring(frequency))); + getMouse().sendFeatureReport(config.serialize()); + } catch (HIDException& e) { + logException(e); + sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage())); + } catch (CLIException& e) { + sendErrorReply(QDBusError::ErrorType::Failed, QString::fromStdWString(e.getMessage())); + } + } + +}; diff -r bb42abd9f510 -r cf77c218b0b1 cadMousePro-daemon/src/cadMousePro.cpp --- a/cadMousePro-daemon/src/cadMousePro.cpp Tue Aug 20 18:59:39 2019 +0200 +++ b/cadMousePro-daemon/src/cadMousePro.cpp Thu Aug 29 17:57:30 2019 +0200 @@ -17,10 +17,15 @@ */ #include +#include +#include + #include "CadMouseConfig.h" #include "CLI.h" #include "CLIParser.h" #include "HID.h" +#include "Daemon.h" +#include "generated-sources/DBusAdaptor.h" // to generate this file run: d-bus/generate.sh adaptor int main(int argc, char** argv) { try { @@ -34,13 +39,29 @@ if (configuration.daemon) { // TODO: run D-Bus service std::wcout << L"will start daemon" << std::endl; + std::wcout << L"uid = " << getuid() << std::endl; + QCoreApplication qtApplication(argc, argv); + + Daemon* daemon = new Daemon(&qtApplication); + DBusAdaptor* dbusAdaptor = new DBusAdaptor(daemon); + + QDBusConnection connection = getuid() ? QDBusConnection::sessionBus() : QDBusConnection::systemBus(); + //QDBusConnection connection = QDBusConnection::sessionBus(); + std::wcout << (connection.isConnected() ? L"připojeno" : L"nepřipojeno") << std::endl; + std::wcout << connection.name().toStdWString() << std::endl; + std::wcout << connection.lastError().message().toStdWString() << std::endl; + connection.registerObject("/info/globalcode/mouse/cadMousePro", daemon); + connection.registerService("info.globalcode.mouse.cadMousePro"); + + return qtApplication.exec(); + } else { - HIDDevice mouse(0x256f, 0xc652, nullptr); + HIDDevice mouse(Daemon::VENDOR_ID, Daemon::PRODUCT_ID, nullptr); std::wcout << L"mouse opened" << std::endl; std::wcout << L"manufacturer: " << mouse.getManufacturerName() << std::endl; std::wcout << L"product: " << mouse.getProductName() << std::endl; // std::wcout << L"serial number: " << mouse.getSerialNumber() << std::endl; // throws exception - + mouse.sendFeatureReport(configuration.cadMouseConfig.serialize()); } @@ -49,7 +70,7 @@ std::wcout << L"HIDException: " << e.getMessage() << std::endl; return CLI::EXIT_CODE_UNEXPECTED_ERROR; } catch (const CLIException& e) { - std::wcout << L"CLIException: " << e.getMessge() << std::endl; + std::wcout << L"CLIException: " << e.getMessage() << std::endl; return e.getExitCode(); } diff -r bb42abd9f510 -r cf77c218b0b1 d-bus/generate.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/d-bus/generate.sh Thu Aug 29 17:57:30 2019 +0200 @@ -0,0 +1,22 @@ +#!/bin/bash + +cd "$(dirname "$0")" +ADAPTOR_DIR="../cadMousePro-daemon/src/generated-sources" +PROXY_DIR="../cadMousePro-gui/src/generated-sources" +XML="info.globalcode.mouse.cadMousePro.xml" + +case "$1" in + adaptor) + qdbusxml2cpp -a DBusAdaptor.h:DBusAdaptor.cpp -c DBusAdaptor -l Daemon -i ../Daemon.h "$XML" + mkdir -p "$ADAPTOR_DIR" + mv DBusAdaptor.h DBusAdaptor.cpp "$ADAPTOR_DIR" + ;; + proxy) + qdbusxml2cpp -l Daemon -i ../Daemon.h -p Proxy.h "$XML" + mkdir -p "$PROXY_DIR" + mv Proxy.h "$PROXY_DIR" + ;; + *) + echo $"Usage: $0 (adaptor|proxy)" + exit 1 +esac diff -r bb42abd9f510 -r cf77c218b0b1 d-bus/info.globalcode.mouse.cadMousePro.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/d-bus/info.globalcode.mouse.cadMousePro.xml Thu Aug 29 17:57:30 2019 +0200 @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + +