From b55db6777ee7104a188ec518ad6e63176035a5ed Mon Sep 17 00:00:00 2001 From: PixlOne <8843371+PixlOne@users.noreply.github.com> Date: Fri, 9 Aug 2019 14:25:40 -0400 Subject: [PATCH] Add logid service & install target --- CMakeLists.txt | 6 +----- README.md | 2 +- src/logid/CMakeLists.txt | 22 +++++++++++++++++++--- src/logid/Configuration.cpp | 2 +- src/logid/Configuration.h | 1 + src/logid/DeviceFinder.cpp | 1 + src/logid/logid.cpp | 9 +++++---- src/logid/logid.service.cmake | 11 +++++++++++ 8 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/logid/logid.service.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c77cbac..8a8235b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,4 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -Wall") set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_subdirectory(src/logid) - -if(NOT EXISTS "${PROJECT_BINARY_DIR}/logid.cfg") - configure_file("logid.example.cfg" "logid.cfg" COPYONLY) -endif() +add_subdirectory(src/logid) \ No newline at end of file diff --git a/README.md b/README.md index ba4bd4b..8c4e1b5 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ cmake .. make ``` -Installation is currently not implemented. +To install, run `sudo make install` after building. You can set the daemon to start at boot by running `sudo systemctl start logid`. ## Compatible Devices diff --git a/src/logid/CMakeLists.txt b/src/logid/CMakeLists.txt index 6b8a5ab..103efe9 100644 --- a/src/logid/CMakeLists.txt +++ b/src/logid/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.10) project(logid) set(CMAKE_CXX_STANDARD 14) @@ -25,8 +25,7 @@ add_executable(logid set_target_properties(logid PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) pkg_check_modules(PC_EVDEV libevdev) -pkg_check_modules(libhidpp REQUIRED) -pkg_check_modules(libconfig++ REQUIRED) +pkg_check_modules(SYSTEMD "systemd") find_path(HIDPP_INCLUDE_DIR hidpp) find_library(HIDPP_LIBRARY libhidpp.so) @@ -39,3 +38,20 @@ find_library(EVDEV_LIBRARY include_directories(${HIDPP_INCLUDE_DIR}/hidpp ${EVDEV_INCLUDE_DIR}) target_link_libraries(logid ${CMAKE_THREAD_LIBS_INIT} ${EVDEV_LIBRARY} config++ ${HIDPP_LIBRARY}) + +install(TARGETS logid DESTINATION bin) + +if (SYSTEMD_FOUND AND "${SYSTEMD_SERVICES_INSTALL_DIR}" STREQUAL "") + execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} + --variable=systemdsystemunitdir systemd + OUTPUT_VARIABLE SYSTEMD_SERVICES_INSTALL_DIR) + string(REGEX REPLACE "[ \t\n]+" "" SYSTEMD_SERVICES_INSTALL_DIR + "${SYSTEMD_SERVICES_INSTALL_DIR}") + configure_file(logid.service.cmake ${CMAKE_BINARY_DIR}/logid.service) + message(STATUS "systemd units will be installed at ${SYSTEMD_SERVICES_INSTALL_DIR}") + install(FILES ${CMAKE_BINARY_DIR}/logid.service + DESTINATION ${SYSTEMD_SERVICES_INSTALL_DIR} + COMPONENT cp) +elseif(NOT SYSTEMD_FOUND AND SYSTEMD_SERVICES_INSTALL_DIR) + message(FATAL_ERROR "systemd is not found w/ pkg-config but SYSTEMD_SERVICES_INSTALL_DIR is defined.") +endif() diff --git a/src/logid/Configuration.cpp b/src/logid/Configuration.cpp index 00ddeda..86a63cd 100644 --- a/src/logid/Configuration.cpp +++ b/src/logid/Configuration.cpp @@ -21,7 +21,7 @@ Configuration::Configuration(const char *config_file) } catch(const FileIOException &e) { - log_printf(ERROR, "I/O Error while reading configuration file: %s", e.what()); + log_printf(ERROR, "I/O Error while reading %s: %s", config_file, e.what()); throw e; } catch(const ParseException &e) diff --git a/src/logid/Configuration.h b/src/logid/Configuration.h index 0a45a3a..e1b7e1d 100644 --- a/src/logid/Configuration.h +++ b/src/logid/Configuration.h @@ -27,6 +27,7 @@ class Configuration { public: Configuration(const char* config_file); + Configuration() {} std::map devices; private: libconfig::Config cfg; diff --git a/src/logid/DeviceFinder.cpp b/src/logid/DeviceFinder.cpp index 6047955..4d8de19 100644 --- a/src/logid/DeviceFinder.cpp +++ b/src/logid/DeviceFinder.cpp @@ -115,6 +115,7 @@ void DeviceFinder::addDevice(const char *path) } } } + catch(HIDPP::Dispatcher::NoHIDPPReportException &e) { } catch(std::system_error &e) { log_printf(WARN, "Failed to open %s: %s", string_path.c_str(), e.what()); } for(auto dev : _devs) diff --git a/src/logid/logid.cpp b/src/logid/logid.cpp index 72f3bd3..9ddc773 100644 --- a/src/logid/logid.cpp +++ b/src/logid/logid.cpp @@ -17,6 +17,7 @@ #include "DeviceFinder.h" #define evdev_name "logid" +#define DEFAULT_CONFIG_FILE "/etc/logid.cfg" LogLevel global_verbosity = INFO; Configuration* global_config; @@ -33,7 +34,7 @@ enum class Option int main(int argc, char** argv) { - char* config_file = "/etc/logid.cfg"; + std::string config_file = DEFAULT_CONFIG_FILE; for(int i = 1; i < argc; i++) { Option option = Option::None; @@ -101,7 +102,7 @@ Possible options are: -v,--verbose [level] Set log level to debug/info/warn/error (leave blank for debug) -c,--config [file path] Change config file from default at %s -h,--help Print this message. -)", argv[0], config_file); +)", argv[0], DEFAULT_CONFIG_FILE); return EXIT_SUCCESS; case Option::None: @@ -111,8 +112,8 @@ Possible options are: } // Read config - try { global_config = new Configuration(config_file); } - catch (std::exception &e) { return EXIT_FAILURE; } + try { global_config = new Configuration(config_file.c_str()); } + catch (std::exception &e) { global_config = new Configuration(); } //Create an evdev device called 'logid' try { global_evdev = new EvdevDevice(evdev_name); } diff --git a/src/logid/logid.service.cmake b/src/logid/logid.service.cmake new file mode 100644 index 0000000..fb975df --- /dev/null +++ b/src/logid/logid.service.cmake @@ -0,0 +1,11 @@ +[Unit] +Description=Logitech Configuration Daemon + +[Service] +Type=simple +ExecStart=${CMAKE_INSTALL_PREFIX}/bin/logid +User=root +ExecReload=/bin/kill -HUP $MAINPID + +[Install] +WantedBy=multi-user.target