From ed419140280553b070943b7ba539120a26ff5686 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 11 Dec 2018 07:42:05 +0100 Subject: wanda: restructure directory hierarchy --- CMakeLists.txt | 159 ++++++++++------------ cmake/Modules/WandaTargets.cmake | 135 ++++++++++++++++++ include/wanda/command.hpp | 46 +++++++ include/wanda/commander.hpp | 66 +++++++++ include/wanda/control_connection.hpp | 114 ++++++++++++++++ include/wanda/control_interface.hpp | 96 +++++++++++++ include/wanda/deferred_failure.hpp | 21 +++ include/wanda/environment.hpp | 61 +++++++++ include/wanda/expected.hpp | 256 +++++++++++++++++++++++++++++++++++ include/wanda/filesystem.hpp | 39 ++++++ include/wanda/keyed.hpp | 28 ++++ include/wanda/logging.hpp | 35 +++++ include/wanda/message.hpp | 83 ++++++++++++ include/wanda/optional.hpp | 65 +++++++++ include/wanda/setting.hpp | 160 ++++++++++++++++++++++ include/wanda/type_wrapper.hpp | 47 +++++++ include/wanda/wallpaper.hpp | 24 ++++ include/wanda/xdg.hpp | 40 ++++++ src/command.cpp | 47 ------- src/command.hpp | 46 ------- src/commander.cpp | 77 ----------- src/commander.hpp | 66 --------- src/control_connection.cpp | 120 ---------------- src/control_connection.hpp | 114 ---------------- src/control_interface.cpp | 154 --------------------- src/control_interface.hpp | 96 ------------- src/deferred_failure.hpp | 21 --- src/environment.cpp | 71 ---------- src/environment.hpp | 61 --------- src/expected.hpp | 256 ----------------------------------- src/filesystem.cpp | 29 ---- src/filesystem.hpp | 39 ------ src/keyed.hpp | 28 ---- src/logging.cpp | 21 --- src/logging.hpp | 35 ----- src/message.cpp | 75 ---------- src/message.hpp | 83 ------------ src/optional.hpp | 65 --------- src/setting.cpp | 102 -------------- src/setting.hpp | 160 ---------------------- src/type_wrapper.hpp | 47 ------- src/wallpaper.cpp | 23 ---- src/wallpaper.hpp | 24 ---- src/wanda/command.cpp | 47 +++++++ src/wanda/commander.cpp | 77 +++++++++++ src/wanda/control_connection.cpp | 120 ++++++++++++++++ src/wanda/control_interface.cpp | 154 +++++++++++++++++++++ src/wanda/environment.cpp | 71 ++++++++++ src/wanda/filesystem.cpp | 29 ++++ src/wanda/logging.cpp | 21 +++ src/wanda/message.cpp | 75 ++++++++++ src/wanda/setting.cpp | 102 ++++++++++++++ src/wanda/wallpaper.cpp | 23 ++++ src/wanda/wandac.cpp | 97 +++++++++++++ src/wanda/wandad.cpp | 103 ++++++++++++++ src/wanda/xdg.cpp | 46 +++++++ src/wandac.cpp | 97 ------------- src/wandad.cpp | 103 -------------- src/xdg.cpp | 46 ------- src/xdg.hpp | 40 ------ tests/core_driver.cpp | 18 --- tests/test_suite_xdg.cpp | 95 ------------- tests/test_suite_xdg.hpp | 16 --- tests/wanda/core_driver.cpp | 18 +++ tests/wanda/test_suite_xdg.cpp | 95 +++++++++++++ tests/wanda/test_suite_xdg.hpp | 16 +++ 66 files changed, 2481 insertions(+), 2363 deletions(-) create mode 100644 cmake/Modules/WandaTargets.cmake create mode 100644 include/wanda/command.hpp create mode 100644 include/wanda/commander.hpp create mode 100644 include/wanda/control_connection.hpp create mode 100644 include/wanda/control_interface.hpp create mode 100644 include/wanda/deferred_failure.hpp create mode 100644 include/wanda/environment.hpp create mode 100644 include/wanda/expected.hpp create mode 100644 include/wanda/filesystem.hpp create mode 100644 include/wanda/keyed.hpp create mode 100644 include/wanda/logging.hpp create mode 100644 include/wanda/message.hpp create mode 100644 include/wanda/optional.hpp create mode 100644 include/wanda/setting.hpp create mode 100644 include/wanda/type_wrapper.hpp create mode 100644 include/wanda/wallpaper.hpp create mode 100644 include/wanda/xdg.hpp delete mode 100644 src/command.cpp delete mode 100644 src/command.hpp delete mode 100644 src/commander.cpp delete mode 100644 src/commander.hpp delete mode 100644 src/control_connection.cpp delete mode 100644 src/control_connection.hpp delete mode 100644 src/control_interface.cpp delete mode 100644 src/control_interface.hpp delete mode 100644 src/deferred_failure.hpp delete mode 100644 src/environment.cpp delete mode 100644 src/environment.hpp delete mode 100644 src/expected.hpp delete mode 100644 src/filesystem.cpp delete mode 100644 src/filesystem.hpp delete mode 100644 src/keyed.hpp delete mode 100644 src/logging.cpp delete mode 100644 src/logging.hpp delete mode 100644 src/message.cpp delete mode 100644 src/message.hpp delete mode 100644 src/optional.hpp delete mode 100644 src/setting.cpp delete mode 100644 src/setting.hpp delete mode 100644 src/type_wrapper.hpp delete mode 100644 src/wallpaper.cpp delete mode 100644 src/wallpaper.hpp create mode 100644 src/wanda/command.cpp create mode 100644 src/wanda/commander.cpp create mode 100644 src/wanda/control_connection.cpp create mode 100644 src/wanda/control_interface.cpp create mode 100644 src/wanda/environment.cpp create mode 100644 src/wanda/filesystem.cpp create mode 100644 src/wanda/logging.cpp create mode 100644 src/wanda/message.cpp create mode 100644 src/wanda/setting.cpp create mode 100644 src/wanda/wallpaper.cpp create mode 100644 src/wanda/wandac.cpp create mode 100644 src/wanda/wandad.cpp create mode 100644 src/wanda/xdg.cpp delete mode 100644 src/wandac.cpp delete mode 100644 src/wandad.cpp delete mode 100644 src/xdg.cpp delete mode 100644 src/xdg.hpp delete mode 100644 tests/core_driver.cpp delete mode 100644 tests/test_suite_xdg.cpp delete mode 100644 tests/test_suite_xdg.hpp create mode 100644 tests/wanda/core_driver.cpp create mode 100644 tests/wanda/test_suite_xdg.cpp create mode 100644 tests/wanda/test_suite_xdg.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d805943..3f9ce60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,111 +16,94 @@ endif() list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules") -include("ConanDependencies") -include("SystemDependencies") -include("CUTE") - -##### Wanda CORE ##### +if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin") +endif() -set(WANDA_CORE_SOURCES - "src/command.cpp" - "src/control_connection.cpp" - "src/environment.cpp" - "src/logging.cpp" - "src/message.cpp" - "src/xdg.cpp" -) +if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib") +endif() -set(WANDA_CORE_HEADERS - "src/command.hpp" - "src/control_connection.hpp" - "src/deferred_failure.hpp" - "src/environment.hpp" - "src/expected.hpp" - "src/keyed.hpp" - "src/logging.hpp" - "src/message.hpp" - "src/optional.hpp" - "src/type_wrapper.hpp" - "src/xdg.hpp" -) +if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib") +endif() -set(WANDA_CORE_LIBRARIES +include("ConanDependencies") +include("SystemDependencies") +include("CUTE") +include("WandaTargets") + +add_wanda_library(NAME "wanda" + SOURCES + "command.cpp" + "control_connection.cpp" + "environment.cpp" + "logging.cpp" + "message.cpp" + "xdg.cpp" + + HEADERS + "command.hpp" + "control_connection.hpp" + "deferred_failure.hpp" + "environment.hpp" + "expected.hpp" + "keyed.hpp" + "logging.hpp" + "message.hpp" + "optional.hpp" + "type_wrapper.hpp" + "xdg.hpp" + + LIBRARIES "CONAN_PKG::asio" "CONAN_PKG::spdlog" "SYSTEM::C++FS" "Threads::Threads" ) -add_library("wanda" ${WANDA_CORE_SOURCES} ${WANDA_CORE_HEADERS}) -target_link_libraries("wanda" ${WANDA_CORE_LIBRARIES}) -target_include_directories("wanda" INTERFACE $) -set_target_properties("wanda" PROPERTIES PUBLIC_HEADER "${WANDA_CORE_HEADERS}") -install(TARGETS "wanda" ARCHIVE DESTINATION "lib" PUBLIC_HEADER DESTINATION "include") - -##### Wanda Daemon ##### - -set(WANDA_DAEMON_SOURCES - "src/control_interface.cpp" - "src/filesystem.cpp" - "src/setting.cpp" - "src/wallpaper.cpp" - "src/wandad.cpp" -) - -set(WANDA_DAEMON_HEADERS - "src/control_interface.hpp" - "src/filesystem.hpp" - "src/setting.hpp" - "src/wallpaper.hpp" -) - -set(WANDA_DAEMON_LIBRARIES +add_wanda_executable(NAME "wandad" + SOURCES + "control_interface.cpp" + "filesystem.cpp" + "setting.cpp" + "wallpaper.cpp" + "wandad.cpp" + + HEADERS + "control_interface.hpp" + "filesystem.hpp" + "setting.hpp" + "wallpaper.hpp" + + LIBRARIES "wanda" "CONAN_PKG::clara" "SYSTEM::GIO" ) -add_executable("wandad" ${WANDA_DAEMON_SOURCES} ${WANDA_DAEMON_HEADERS}) -target_link_libraries("wandad" ${WANDA_DAEMON_LIBRARIES}) -install(TARGETS "wandad" RUNTIME DESTINATION "bin") - -##### Wanda Controller ##### +add_wanda_executable(NAME "wandac" + SOURCES + "commander.cpp" + "wandac.cpp" -set(WANDA_CONTROLLER_SOURCES - "src/commander.cpp" - "src/wandac.cpp" -) - -set(WANDA_CONTROLLER_HEADERS - "src/commander.hpp" -) + HEADERS + "commander.hpp" -set(WANDA_CONTROLLER_LIBRARIES + LIBRARIES "wanda" "CONAN_PKG::clara" ) -add_executable("wandac" ${WANDA_CONTROLLER_SOURCES} ${WANDA_CONTROLLER_HEADERS}) -target_link_libraries("wandac" ${WANDA_CONTROLLER_LIBRARIES}) -install(TARGETS "wandac" RUNTIME DESTINATION "bin") - -##### Wanda Core Tests ##### -if(CUTE_FOUND) - set(WANDA_CORE_TESTS_SUITE_SOURCES - "tests/test_suite_xdg.cpp" - ) - - set(WANDA_CORE_TESTS_SUITE_HEADERS - "tests/test_suite_xdg.hpp" - ) - - set(WANDA_CORE_TESTS_LIBRARIES - "wanda" - "LIB::CUTE" - ) - - add_executable("wanda_core_tests" "tests/core_driver.cpp" ${WANDA_CORE_TESTS_SUITE_SOURCES} ${WANDA_CORE_TESTS_SUITE_HEADERS}) - target_link_libraries("wanda_core_tests" ${WANDA_CORE_TESTS_LIBRARIES}) - add_test(NAME "wanda_core_tests" COMMAND "wanda_core_tests") -endif() \ No newline at end of file +add_wanda_test(NAME "wanda_core" + SOURCES + "core_driver.cpp" + "test_suite_xdg.cpp" + + HEADERS + "test_suite_xdg.hpp" + + LIBRARIES + "wanda" + "LIB::CUTE" +) \ No newline at end of file diff --git a/cmake/Modules/WandaTargets.cmake b/cmake/Modules/WandaTargets.cmake new file mode 100644 index 0000000..38fc169 --- /dev/null +++ b/cmake/Modules/WandaTargets.cmake @@ -0,0 +1,135 @@ +include(CMakeParseArguments) + +function(add_wanda_library) + set(OPTIONS) + set(SV_ARGS "NAME") + set(MV_ARGS "SOURCES;HEADERS;LIBRARIES") + cmake_parse_arguments(WANDA_LIBRARY "${OPTIONS}" "${SV_ARGS}" "${MV_ARGS}" ${ARGN}) + + if(NOT WANDA_LIBRARY_NAME) + message(FATAL_ERROR "Missing argument 'NAME' to call of 'add_wanda_library'") + endif() + + if(NOT WANDA_LIBRARY_SOURCES) + message(FATAL_ERROR "Missing argument 'SOURCES' to call of 'add_wanda_library'") + endif() + + set(RESOLVED_SOURCES) + foreach(SOURCE IN LISTS WANDA_LIBRARY_SOURCES) + list(APPEND RESOLVED_SOURCES "src/wanda/${SOURCE}") + endforeach() + + set(RESOLVED_HEADERS) + foreach(HEADER IN LISTS WANDA_LIBRARY_HEADERS) + list(APPEND RESOLVED_HEADERS "include/wanda/${HEADER}") + endforeach() + + add_library("${WANDA_LIBRARY_NAME}" + ${RESOLVED_SOURCES} # Source files + ${RESOLVED_HEADERS} # Header files + ) + + target_include_directories("${WANDA_LIBRARY_NAME}" SYSTEM PUBLIC + $ + $ + ) + + target_link_libraries("${WANDA_LIBRARY_NAME}" + ${WANDA_LIBRARY_LIBRARIES} + ) + + set_target_properties("${WANDA_LIBRARY_NAME}" + PROPERTIES PUBLIC_HEADER "${RESOLVED_HEADERS}" + ) + + install(TARGETS "${WANDA_LIBRARY_NAME}" + ARCHIVE DESTINATION "lib" PUBLIC_HEADER DESTINATION "include/wanda" + ) +endfunction() + +function(add_wanda_executable) + set(OPTIONS) + set(SV_ARGS "NAME") + set(MV_ARGS "SOURCES;HEADERS;LIBRARIES") + cmake_parse_arguments(WANDA_EXECUTABLE "${OPTIONS}" "${SV_ARGS}" "${MV_ARGS}" ${ARGN}) + + if(NOT WANDA_EXECUTABLE_NAME) + message(FATAL_ERROR "Missing argument 'NAME' to call of 'add_wanda_executable'") + endif() + + if(NOT WANDA_EXECUTABLE_SOURCES) + message(FATAL_ERROR "Missing argument 'SOURCES' to call of 'add_wanda_executable'") + endif() + + set(RESOLVED_SOURCES) + foreach(SOURCE IN LISTS WANDA_EXECUTABLE_SOURCES) + list(APPEND RESOLVED_SOURCES "src/wanda/${SOURCE}") + endforeach() + + set(RESOLVED_HEADERS) + foreach(HEADER IN LISTS WANDA_EXECUTABLE_HEADERS) + list(APPEND RESOLVED_HEADERS "include/wanda/${HEADER}") + endforeach() + + add_executable("${WANDA_EXECUTABLE_NAME}" + ${RESOLVED_SOURCES} # Source files + ${RESOLVED_HEADERS} # Header files + ) + + target_include_directories("${WANDA_EXECUTABLE_NAME}" SYSTEM PRIVATE + $ + ) + + target_link_libraries("${WANDA_EXECUTABLE_NAME}" + ${WANDA_EXECUTABLE_LIBRARIES} + ) + + install(TARGETS "${WANDA_EXECUTABLE_NAME}" + ARCHIVE DESTINATION "lib" RUNTIME DESTINATION "bin" + ) +endfunction() + +function(add_wanda_test) + set(OPTIONS) + set(SV_ARGS "NAME") + set(MV_ARGS "SOURCES;HEADERS;LIBRARIES") + cmake_parse_arguments(WANDA_TEST "${OPTIONS}" "${SV_ARGS}" "${MV_ARGS}" ${ARGN}) + + if(NOT WANDA_TEST_NAME) + message(FATAL_ERROR "Missing argument 'NAME' to call of 'add_wanda_test'") + endif() + + if(NOT WANDA_TEST_SOURCES) + message(FATAL_ERROR "Missing argument 'SOURCES' to call of 'add_wanda_test'") + endif() + + set(RESOLVED_SOURCES) + foreach(SOURCE IN LISTS WANDA_TEST_SOURCES) + list(APPEND RESOLVED_SOURCES "tests/wanda/${SOURCE}") + endforeach() + + set(RESOLVED_HEADERS) + foreach(HEADER IN LISTS WANDA_TEST_HEADERS) + list(APPEND RESOLVED_HEADERS "tests/wanda/${HEADER}") + endforeach() + + if(NOT CUTE_FOUND) + message(STATUS "CUTE not found. Skipping test '${WANDA_TEST_NAME}'") + return() + endif() + + add_executable("${WANDA_TEST_NAME}_driver" + ${RESOLVED_SOURCES} # Source files + ${RESOLVED_HEADERS} # Header files + ) + + target_include_directories("${WANDA_TEST_NAME}_driver" SYSTEM PRIVATE + $ + ) + + target_link_libraries("${WANDA_TEST_NAME}_driver" + ${WANDA_TEST_LIBRARIES} + ) + + add_test(NAME "${WANDA_TEST_NAME}" COMMAND "${WANDA_TEST_NAME}_driver") +endfunction() \ No newline at end of file diff --git a/include/wanda/command.hpp b/include/wanda/command.hpp new file mode 100644 index 0000000..5ea1a08 --- /dev/null +++ b/include/wanda/command.hpp @@ -0,0 +1,46 @@ +#ifndef WANDA_COMMAND_HPP +#define WANDA_COMMAND_HPP + +#include + +#include +#include +#include + +namespace wanda +{ + /** + * @brief An enum to describe different command IDs + */ + enum struct command_id : char + { + change, //< Change the wallpaper + }; + + /** + * @brief A simple type to represent commands transported through the control connection + */ + struct command + { + command_id const id; + std::vector const arguments; + + /** + * @brief Convert the command to a message for transmission to a remote endpoint + */ + std::optional message() const; + }; + + /** + * @brief Extract a command from a message + */ + std::optional make_command(message message); + + /** + * @brief A simple factory to create a "Change wallpaper" command + */ + command make_change_command(); + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/commander.hpp b/include/wanda/commander.hpp new file mode 100644 index 0000000..1c76c6d --- /dev/null +++ b/include/wanda/commander.hpp @@ -0,0 +1,66 @@ +#ifndef WANDA_COMMANDER_HPP +#define WANDA_COMMANDER_HPP + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace wanda +{ + /** + * @brief The remote control client + * + */ + struct commander : wanda::control_connection::listener + { + /** + * @brief The interface to be implemented by remote control listeners + */ + struct listener + { + virtual void on_connected(commander & commander){}; + virtual void on_response(commander & commander, std::string response){}; + virtual void on_error(commander & commander, std::string error){}; + }; + + /** + * @brief Construct a new commander + */ + commander(asio::io_service & service, std::filesystem::path socket, listener & listener); + + /** + * @brief Start communication with the remote daemon endpoint + */ + void start(); + + /** + * @brief Stop communication with the remote daemon endpoint + */ + void stop(); + + /** + * @brief Send a command to the remote daemon endpoint + */ + void send(command command); + + void on_error(control_connection::pointer connection, std::error_code error) override; + void on_received(control_connection::pointer connection, message message) override; + + private: + asio::io_service & m_service; + wanda::control_connection::protocol::endpoint m_endpoint; + wanda::control_connection::protocol::socket m_socket; + wanda::control_connection::pointer m_connection; + listener & m_listener; + }; + +} // namespace wanda +#endif \ No newline at end of file diff --git a/include/wanda/control_connection.hpp b/include/wanda/control_connection.hpp new file mode 100644 index 0000000..b692d37 --- /dev/null +++ b/include/wanda/control_connection.hpp @@ -0,0 +1,114 @@ +#ifndef WANDA_CONTROL_CONNECTION_HPP +#define WANDA_CONTROL_CONNECTION_HPP + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace wanda +{ + /** + * @brief A connection to a remote control endpoint + */ + struct control_connection : keyed, std::enable_shared_from_this + { + using protocol = asio::local::stream_protocol; + using pointer = std::shared_ptr; + + /** + * @brief The interface to be implemented by the control interface listener + */ + struct listener + { + virtual void on_close(pointer connection) {} + virtual void on_received(pointer connection, message message) {} + virtual void on_error(pointer connection, std::error_code) {} + }; + + /** + * @brief A enum to describe different connection states + */ + enum struct state : std::underlying_type_t + { + unknown, //< Connection is in an unknown state + fresh, //< Connection is freshly created but not established + established, //< Connection has been established + }; + + /** + * @internal + * @brief Construct a new control connection object + * + * @note This constructor is keyed on a private key type so it can only be constructed using the #wanda::make_connection factory + */ + control_connection(key, protocol::socket socket); + + /** + * @brief Add the given listener to this control connection's listener set + * + * @returns true iff. the listener was not already in the listener set + */ + bool add(listener * listener); + + /** + * @brief Remove the given listener from this control connection's listener set + * + * @return true iff. the listener was previously registered with this control connection + */ + bool remove(listener * listener); + + /** + * @brief Start I/O processing for this control connection + */ + void start(); + + /** + * @brief Close this control connection + */ + void close(); + + /** + * @brief Send the given message to the remote endpoint + */ + void send(message message); + + /** + * @brief Set the connection state to the provided state + */ + void update(state state); + + /** + * @brief Get the current connection state + */ + state current_state() const; + + private: + friend pointer make_control_connection(protocol::socket && socket); + + void perform_read(); + + protocol::socket m_socket; + asio::streambuf m_in{}; + asio::streambuf m_out{}; + std::istream m_input{&m_in}; + std::ostream m_output{&m_out}; + std::set m_listeners{}; + state m_state{}; + }; + + /** + * @brief Create a new control connection + */ + control_connection::pointer make_control_connection(control_connection::protocol::socket && socket); + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/control_interface.hpp b/include/wanda/control_interface.hpp new file mode 100644 index 0000000..73ef2cf --- /dev/null +++ b/include/wanda/control_interface.hpp @@ -0,0 +1,96 @@ +/** + * @file environment.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_CONTROL_INTERFACE_HPP +#define WANDA_CONTROL_INTERFACE_HPP + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace wanda +{ + /** + * @brief An RAII type to delete a socket file upon destruction + */ + struct socket_deleter + { + ~socket_deleter(); + + std::filesystem::path path; + }; + + /** + * @brief The daemon control interface + */ + struct control_interface : control_connection::listener, keyed, std::enable_shared_from_this + { + using protocol = asio::local::stream_protocol; + using pointer = std::shared_ptr; + + /** + * @brief The interface to be implemented by the control interface listener + */ + struct listener + { + virtual void on_received(control_interface & interface, command command){}; + }; + + /** + * @internal + * @brief Construct a new control interface object + * + * @note This constructor is keyed on a private key type so it can only be constructed using the #wanda::make_interface factory + */ + control_interface(key, asio::io_service & service, protocol::endpoint endpoint, listener & listener); + + /** + * @brief Start handling of controller connections + */ + std::error_code start(); + + /** + * @brief Stop the control interface + */ + std::error_code shutdown(); + + void on_close(control_connection::pointer connection) override; + void on_received(control_connection::pointer connection, message message) override; + + private: + void perform_accept(); + + friend pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener); + + asio::io_service & m_service; + protocol::endpoint m_endpoint; + protocol::socket m_socket; + protocol::acceptor m_acceptor; + listener & m_listener; + socket_deleter m_deleter{m_endpoint.path()}; + std::set m_connections; + }; + + /** + * @brief A factory to create new #control_interface instances + */ + control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path socket, control_interface::listener & listener); + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/deferred_failure.hpp b/include/wanda/deferred_failure.hpp new file mode 100644 index 0000000..5db26f6 --- /dev/null +++ b/include/wanda/deferred_failure.hpp @@ -0,0 +1,21 @@ +/** + * @file deferred_failure.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_DEFERRED_FAILURE_HPP +#define WANDA_DEFERRED_FAILURE_HPP + +#include + +namespace +{ + /** + * @brief A helper type to defer static_assert failures + */ + template + using deferred_failure = std::false_type; +} // namespace + +#endif \ No newline at end of file diff --git a/include/wanda/environment.hpp b/include/wanda/environment.hpp new file mode 100644 index 0000000..5a702a8 --- /dev/null +++ b/include/wanda/environment.hpp @@ -0,0 +1,61 @@ +/** + * @file environment.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_ENVIRONMENT_HPP +#define WANDA_ENVIRONMENT_HPP + +#include + +#include +#include + +namespace wanda +{ + /** + * @brief A type to provide access to the runtime environment + */ + struct environment + { + using map_type = std::map; + using iterator = map_type::iterator; + using const_iterator = map_type::const_iterator; + using reference = map_type::reference; + using const_reference = map_type::const_reference; + + /** + * @brief Construct a new environment from the given string array + */ + explicit environment(char const * const * env = ::environ); + + /** + * @brief Get the value of the given variable + * + * @return A mutable reference to the value of the given environment variable + */ + std::string & operator[](std::string const & variable); + + /** + * @brief Get the value of the given variable + * + * @return An immutable reference to the value of the given environment variable + */ + std::string const & operator[](std::string const & variable) const; + + iterator begin(); + const_iterator begin() const; + const_iterator cbegin() const; + + iterator end(); + const_iterator end() const; + const_iterator cend() const; + + private: + map_type m_cache{}; + }; + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/expected.hpp b/include/wanda/expected.hpp new file mode 100644 index 0000000..fff0d81 --- /dev/null +++ b/include/wanda/expected.hpp @@ -0,0 +1,256 @@ +/** + * @file expected.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_EXPECTED_HPP +#define WANDA_EXPECTED_HPP + +#include +#include +#include + +namespace wanda +{ + /** + * @brief A type to represent the error case of a computation based on #wanda::expected + */ + template + struct unexpected + { + static_assert(!std::is_same_v, "ErrorType can not be 'void'!"); + static_assert(!std::is_array_v, "ErrorType can not be an array type!"); + + /** + * @brief Copy construct a new @p unexpected from another @p unexpected + */ + constexpr unexpected(unexpected const &) = default; + + /** + * @brief Move construct a new @p unexpected from another @p unexpected + */ + constexpr unexpected(unexpected &&) = default; + + /** + * @brief Construct a new @p unexpected by direct initializing the error object from @p args + */ + template + constexpr explicit unexpected(std::in_place_t, Args &&... args) + : m_error(std::forward(args)...) + { + } + + /** + * @brief Construct a new @p unexpected by direct initializing the error object from @p il and @p args + */ + template< + typename U, + typename... Args, + std::enable_if_t, Args...>> * = nullptr> + constexpr explicit unexpected(std::in_place_t, std::initializer_list il, Args &&... args) + : m_error(il, std::forward(args)...) + { + } + + /** + * @brief Construct a new @p unexpected by direct initializing the error object from @p error + */ + template< + typename Err = ErrorType, + std::enable_if_t && + !std::is_same_v>, std::in_place_t> && + !std::is_same_v>, unexpected>> * = nullptr> + constexpr explicit unexpected(Err && error) + : m_error(std::forward(error)) + { + } + + /** + * @brief Construct a new @p unexpected by copying the value of another @p unexpected of different error type + */ + template< + typename Err, + std::enable_if_t && + !std::is_constructible_v &> && + !std::is_constructible_v> && + !std::is_constructible_v const &> && + !std::is_constructible_v const> && + !std::is_convertible_v &, ErrorType> && + !std::is_convertible_v, ErrorType> && + !std::is_convertible_v const &, ErrorType> && + !std::is_convertible_v const, ErrorType>)> * = nullptr, + std::enable_if_t> * = nullptr> + constexpr explicit unexpected(unexpected const & error) + : m_error(error.m_error) + { + } + + /** + * @brief Construct a new @p unexpected by copying the value of another @p unexpected of different error type + */ + template< + typename Err, + std::enable_if_t && + !std::is_constructible_v &> && + !std::is_constructible_v> && + !std::is_constructible_v const &> && + !std::is_constructible_v const> && + !std::is_convertible_v &, ErrorType> && + !std::is_convertible_v, ErrorType> && + !std::is_convertible_v const &, ErrorType> && + !std::is_convertible_v const, ErrorType>)> * = nullptr, + std::enable_if_t> * = nullptr> + constexpr unexpected(unexpected const & error) + : m_error(error.m_error) + { + } + + /** + * @brief Construct a new @p unexpected by moving the value of another @p unexpected of different error type + */ + template< + typename Err, + std::enable_if_t && + !std::is_constructible_v &> && + !std::is_constructible_v> && + !std::is_constructible_v const &> && + !std::is_constructible_v const> && + !std::is_convertible_v &, ErrorType> && + !std::is_convertible_v, ErrorType> && + !std::is_convertible_v const &, ErrorType> && + !std::is_convertible_v const, ErrorType>)> * = nullptr, + std::enable_if_t> * = nullptr> + constexpr explicit unexpected(unexpected && error) + : m_error(std::move(error.m_error)) + { + } + + /** + * @brief Construct a new @p unexpected by moving the value of another @p unexpected of different error type + */ + template< + typename Err, + std::enable_if_t && + !std::is_constructible_v &> && + !std::is_constructible_v> && + !std::is_constructible_v const &> && + !std::is_constructible_v const> && + !std::is_convertible_v &, ErrorType> && + !std::is_convertible_v, ErrorType> && + !std::is_convertible_v const &, ErrorType> && + !std::is_convertible_v const, ErrorType>)> * = nullptr, + std::enable_if_t> * = nullptr> + constexpr unexpected(unexpected && error) + : m_error(std::move(error.m_error)) + { + } + + /** + * @brief Get the error value contained in this @p unexpected instance + */ + constexpr ErrorType const & value() const & + { + return m_error; + } + + /** + * @brief Get the error value contained in this @p unexpected instance + */ + constexpr ErrorType & value() & + { + return m_error; + } + + /** + * @brief Get the error value contained in this @p unexpected instance + */ + constexpr ErrorType && value() && + { + return std::move(m_error); + } + + /** + * @brief Get the error value contained in this @p unexpected instance + */ + constexpr ErrorType const && value() const && + { + return std::move(m_error); + } + + /** + * @brief Swap the error value of this @p unexpected instance with the one of @p other + */ + void swap(unexpected & other) noexcept(std::is_nothrow_swappable_v) + { + using std::swap; + swap(m_error, other.m_error); + } + + template + friend constexpr bool operator==(unexpected const & lhs, unexpected const & rhs); + + template + friend constexpr bool operator!=(unexpected const & lhs, unexpected const & rhs); + + template< + typename Err, + std::enable_if_t> *> + friend void swap(unexpected & lhs, unexpected & rhs); + + private: + ErrorType m_error; + }; + + template + unexpected(ErrorType)->unexpected; + + /** + * @brief Compare two @p unexpected instances for equality + */ + template + constexpr bool operator==(unexpected const & lhs, unexpected const & rhs) + { + return lhs.m_error == rhs.m_error; + } + + /** + * @brief Compare two @p unexpected instances for inequality + */ + template + constexpr bool operator!=(unexpected const & lhs, unexpected const & rhs) + { + return lhs.m_error != rhs.m_error; + } + + /** + * @brief Swap the error values of two @p unexpected instances + */ + template< + typename Err, + std::enable_if_t> * = nullptr> + void swap(unexpected & lhs, unexpected & rhs) + { + lhs.swap(rhs); + } + + /** + * @brief A tag type for @p unexpected + */ + struct unexpect_t + { + explicit unexpect_t() = default; + }; + + /** + * @brief A tap for @p unexpected + */ + inline constexpr unexpect_t unexpect{}; + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/filesystem.hpp b/include/wanda/filesystem.hpp new file mode 100644 index 0000000..1975bc5 --- /dev/null +++ b/include/wanda/filesystem.hpp @@ -0,0 +1,39 @@ +/** + * @file filesystem.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_FILESYSTEM_HPP +#define WANDA_FILESYSTEM_HPP + +#include +#include +#include + +namespace wanda +{ + /** + * @brief Covenience alias for path lists + */ + using path_list = std::vector; + + /** + * @brief The default scan filter, allowing only regular files to pass + */ + constexpr inline auto default_filter = [](std::filesystem::path const & path) { + return is_regular_file(path); + }; + + /** + * @brief Scan the given folder for files + */ + std::optional scan(std::filesystem::path folder, bool(filter)(std::filesystem::path const &) = default_filter); + + /** + * @brief Pick a random path from the given list + */ + std::filesystem::path random_pick(path_list const & paths); +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/keyed.hpp b/include/wanda/keyed.hpp new file mode 100644 index 0000000..58f17ad --- /dev/null +++ b/include/wanda/keyed.hpp @@ -0,0 +1,28 @@ +/** + * @file keyed.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_KEYED_HPP +#define WANDA_KEYED_HPP + +namespace wanda +{ + /** + * @brief A tag type to prevent construction of a type without a factory + */ + template + struct keyed + { + protected: + struct key + { + }; + + explicit keyed(key) {} + }; + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/logging.hpp b/include/wanda/logging.hpp new file mode 100644 index 0000000..b3c1665 --- /dev/null +++ b/include/wanda/logging.hpp @@ -0,0 +1,35 @@ +/** + * @file logging.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_LOGGING_HPP +#define WANDA_LOGGING_HPP + +#include +#include + +#include + +namespace wanda +{ + /** + * @brief A covenience alias to represent a handle for a logger + */ + using logger_ptr = std::shared_ptr; + + /** + * @brief Initialize the shared logger + * + * @note The logger will only ever be initialized once, even if this function is called multiple times + */ + void initialize_logger(spdlog::sink_ptr sink = std::make_shared()); + + /** + * @brief Get the shared logger + */ + logger_ptr get_logger(); +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/message.hpp b/include/wanda/message.hpp new file mode 100644 index 0000000..866408f --- /dev/null +++ b/include/wanda/message.hpp @@ -0,0 +1,83 @@ +/** + * @file message.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_MESSAGE_HPP +#define WANDA_MESSAGE_HPP + +#include +#include +#include +#include + +namespace wanda +{ + inline namespace v1 + { + /** + * @brief The version argument for the hello message reflecting the current version + */ + auto constexpr message_argument_hello = "1.0.0"; + } // namespace v1 + + /** + * @brief A tag to mark messages originating from the controller + */ + auto constexpr message_source_controller = "C"; + + /** + * @brief A tag to mark messages originating from the daemon + */ + auto constexpr message_source_daemon = "D"; + + /** + * @brief The command of the hello message + */ + auto constexpr message_command_hello = "HELLO"; + + /** + * @brief A control protocol message, consisting of a @p source, @p command, and @p arguments + */ + struct message + { + /** + * @brief Serialize this message into a string + */ + explicit operator std::string() const; + + /** + * @brief Get the size of the message as if it was serialized + */ + std::size_t size() const; + + /** + * @brief The source of the message + */ + std::string source; + + /** + * @brief The command of the message + */ + std::string command; + + /** + * @brief The arguments of the message command + */ + std::optional argument; + }; + + /** + * @brief Deserialize a message from the given stream + */ + std::istream & operator>>(std::istream & in, message & message); + + /** + * @brief Serialize a message to the given stream + */ + std::ostream & operator<<(std::ostream & out, message const & message); + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/optional.hpp b/include/wanda/optional.hpp new file mode 100644 index 0000000..da3774c --- /dev/null +++ b/include/wanda/optional.hpp @@ -0,0 +1,65 @@ +/** + * @file optional.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_OPTIONAL_HPP +#define WANDA_OPTIONAL_HPP + +#include + +namespace wanda::std_ext +{ + /** + * @brief A type to represent a computation that could fail + */ + struct failable + { + /** + * @brief A factory to create a successful computation + */ + constexpr static auto success() { return failable{false}; } + + /** + * @brief A factory to create a failed computation + */ + constexpr static auto failure() { return failable{true}; } + + /** + * @brief Execute the given handler if the computation failed + */ + template + constexpr void operator||(Handler handler) const + { + if (m_failed) + { + handler(); + } + } + + private: + constexpr explicit failable(bool failed) + : m_failed{failed} {}; + bool const m_failed; + }; + + /** + * @brief Unwrap the given optional object, if present, and pass it to the handler + * + * @return A successful computation iff. the object was present, a failed computation otherwise. + */ + template + auto with(std::optional && object, HandlerType handler) + { + if (object) + { + handler(object.value()); + return failable::success(); + } + return failable::failure(); + } + +} // namespace wanda::std_ext + +#endif \ No newline at end of file diff --git a/include/wanda/setting.hpp b/include/wanda/setting.hpp new file mode 100644 index 0000000..98b5771 --- /dev/null +++ b/include/wanda/setting.hpp @@ -0,0 +1,160 @@ +/** + * @file setting.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_setting_HPP +#define WANDA_setting_HPP + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace wanda +{ + struct setting; + + /** + * @brief A convenience type to represent setting keys + */ + using key = type_wrapper; + + namespace literals + { + /** + * @brief UDL to create setting keys + */ + key operator""_key(char const * str, std::size_t len); + + /** + * @brief UDL to create setting schemas + */ + std::optional operator""_setting(char const * str, std::size_t lent); + } // namespace literals + + /** + * @brief A simple wrapper for GSettings Schemas + */ + struct setting + { + struct entry + { + using value_type = std::variant>; + + /** + * @brief Get the value of the settings entry + */ + value_type operator*() const; + + /** + * @brief Assign the given @p value to the settings entry + * + * @returns @p true iff. the value could be successfully assigned + */ + template + bool operator=(Type value) + { + struct setting_applier + { + setting_applier(GSettings * setting, gchar const * key, Type value) noexcept + : m_result{[&] { + if constexpr (std::is_same_v) + { + return g_settings_set_boolean(setting, key, value); + } + else if constexpr (std::is_same_v) + { + return g_settings_set_int(setting, key, value); + } + else if constexpr (std::is_same_v) + { + return g_settings_set_int64(setting, key, value); + } + else if constexpr (std::is_same_v) + { + return g_settings_set_uint(setting, key, value); + } + else if constexpr (std::is_same_v) + { + return g_settings_set_uint64(setting, key, value); + } + else if constexpr (std::is_same_v) + { + return g_settings_set_double(setting, key, value); + } + else if constexpr (std::is_same_v) + { + return g_settings_set_string(setting, key, value.c_str()); + } + else if constexpr (std::is_same_v>) + { + auto temp = std::vector{value.size() + 1}; + std::transform(value.begin(), value.end(), temp.begin(), [](auto const & str) { return str.c_str(); }); + return g_settings_set_strv(setting, key, temp.data()); + } + else + { + static_assert(deferred_failure{}, "Invalid argument type!"); + } + }()} + { + } + + ~setting_applier() + { + g_settings_sync(); + } + + operator bool() const + { + return m_result; + } + + private: + gboolean const m_result; + }; + + return setting_applier{m_settings.get(), m_key.get().c_str(), value}; + } + + private: + entry(setting const & schema, key key); + + std::unique_ptr m_settings; + + key m_key; + + friend setting; + }; + + /** + * @brief Get the entry for the given key + * + * @return An std::optional wrapping the entry associated with + * the given key, or an empty std::optional if the desired key + * does not exist in the setting's schema. + */ + std::optional operator[](key key) const; + + private: + explicit setting(GSettingsSchema * schema); + + std::unique_ptr m_schema; + + friend std::optional literals::operator""_setting(char const *, std::size_t); + }; + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/type_wrapper.hpp b/include/wanda/type_wrapper.hpp new file mode 100644 index 0000000..12684cb --- /dev/null +++ b/include/wanda/type_wrapper.hpp @@ -0,0 +1,47 @@ +/** + * @file type_wrapper.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_TYPE_WRAPPER_HPP +#define WANDA_TYPE_WRAPPER_HPP + +#include + +namespace wanda +{ + /** + * @brief A type to create a distinct type based on an existing type + * + * @tparam InnerType The type to wrap + * @tparam TagType A tag type to identify the distinct type + */ + template + struct type_wrapper + { + /** + * @brief Construct a new type wrapper object + */ + explicit type_wrapper(InnerType value) + : m_value{std::move(value)} + { + } + + /** + * @brief Retrieve the wrapped value with its original type + */ + constexpr explicit operator InnerType const &() const { return get(); } + + /** + * @brief Retrieve the wrapped value with its original type + */ + constexpr InnerType const & get() const { return m_value; } + + private: + InnerType m_value; + }; + +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/wallpaper.hpp b/include/wanda/wallpaper.hpp new file mode 100644 index 0000000..0cad473 --- /dev/null +++ b/include/wanda/wallpaper.hpp @@ -0,0 +1,24 @@ +/** + * @file wallpaper.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_WALLPAPER_HPP +#define WANDA_WALLPAPER_HPP + +#include +#include + +#include +#include + +namespace wanda +{ + /** + * @brief Set the wallpaper to the file specified by the given path + */ + void set_wallpaper(std::filesystem::path wallpaper); +} // namespace wanda + +#endif \ No newline at end of file diff --git a/include/wanda/xdg.hpp b/include/wanda/xdg.hpp new file mode 100644 index 0000000..bc138fa --- /dev/null +++ b/include/wanda/xdg.hpp @@ -0,0 +1,40 @@ +/** + * @file xdg.hpp + * @author Felix Morgner (felix.morgner@gmail.com) + * @since 1.0.0 + */ + +#ifndef WANDA_XDG_HPP +#define WANDA_XDG_HPP + +#include + +#include +#include +#include + +namespace wanda +{ + /** + * @brief An @p enum to represet the standardized XDG directories + */ + enum struct xdg_directory : std::underlying_type_t + { + data_home, + config_home, + cache_home, + runtime_dir, + }; + + /** + * @brief Get the name of the environment variable associated with the given XDG directory + */ + std::string xdg_variable(xdg_directory directory); + + /** + * @brief Get the path to the given @p directory given the provided @p environment + */ + std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment); +} // namespace wanda + +#endif \ No newline at end of file diff --git a/src/command.cpp b/src/command.cpp deleted file mode 100644 index 76cdee7..0000000 --- a/src/command.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "command.hpp" - -namespace wanda -{ - std::optional command::message() const - { - using namespace std::string_literals; - auto const command = [this] { - switch (id) - { - case command_id::change: - return "CHANGE"s; - default: - return ""s; - } - }(); - - auto argument_string = std::string{}; - for (int index = 0ul; index < arguments.size(); ++index) - { - argument_string += (index) ? "," + arguments[index] : arguments[index]; - } - - if (command.empty()) - { - return std::nullopt; - } - - return wanda::message{"C", command, argument_string}; - } - - std::optional make_command(message message) - { - if (message.command == "CHANGE") - { - return {{command_id::change}}; - } - - return std::nullopt; - } - - command make_change_command() - { - return {command_id::change}; - } - -} // namespace wanda \ No newline at end of file diff --git a/src/command.hpp b/src/command.hpp deleted file mode 100644 index afc6a6f..0000000 --- a/src/command.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef WANDA_COMMAND_HPP -#define WANDA_COMMAND_HPP - -#include "message.hpp" - -#include -#include -#include - -namespace wanda -{ - /** - * @brief An enum to describe different command IDs - */ - enum struct command_id : char - { - change, //< Change the wallpaper - }; - - /** - * @brief A simple type to represent commands transported through the control connection - */ - struct command - { - command_id const id; - std::vector const arguments; - - /** - * @brief Convert the command to a message for transmission to a remote endpoint - */ - std::optional message() const; - }; - - /** - * @brief Extract a command from a message - */ - std::optional make_command(message message); - - /** - * @brief A simple factory to create a "Change wallpaper" command - */ - command make_change_command(); - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/commander.cpp b/src/commander.cpp deleted file mode 100644 index a67d06a..0000000 --- a/src/commander.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "commander.hpp" -#include "logging.hpp" -#include "message.hpp" -#include "optional.hpp" - -#include - -namespace wanda -{ - commander::commander(asio::io_service & service, std::filesystem::path socket, listener & listener) - : m_service{service} - , m_endpoint{socket.string()} - , m_socket{service} - , m_listener{listener} - { - } - - void commander::start() - { - m_socket.async_connect(m_endpoint, [&](auto const & error) { - if (error) - { - get_logger()->error("error while connecting to control interface: '{}'", error.message()); - } - else - { - get_logger()->info("establishing connection to wanda deamon"); - m_connection = wanda::make_control_connection(std::move(m_socket)); - m_connection->add(this); - m_connection->start(); - m_connection->send({message_source_controller, message_command_hello, message_argument_hello}); - } - }); - } - - void commander::stop() - { - get_logger()->info("closing control connection"); - m_connection->close(); - } - - void commander::send(command command) - { - using namespace wanda::std_ext; - - if (!m_connection || m_connection->current_state() != control_connection::state::established) - { - get_logger()->error("tried to send command without an established connection"); - m_listener.on_error(*this, "tried to send command without an established connection"); - return; - } - - with(command.message(), [&](auto const & message) { m_connection->send(message); }) || - [&] { get_logger()->error("unknown command"); }; - } - - void commander::on_error(control_connection::pointer connection, std::error_code error) - { - get_logger()->error("control interface communication error: '{}'", error.message()); - } - - void commander::on_received(wanda::control_connection::pointer connection, message message) - { - if (auto state = connection->current_state(); message.command == "HELLO" && state == control_connection::state::fresh) - { - get_logger()->info("connection to wanda deamon successfully established"); - connection->update(control_connection::state::established); - m_listener.on_connected(*this); - } - else - { - get_logger()->error("unexpected message: '{}'", message); - m_listener.on_error(*this, "unexpected message '" + static_cast(message) + '\''); - } - } - -} // namespace wanda \ No newline at end of file diff --git a/src/commander.hpp b/src/commander.hpp deleted file mode 100644 index 42a2ab6..0000000 --- a/src/commander.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef WANDA_COMMANDER_HPP -#define WANDA_COMMANDER_HPP - -#include "command.hpp" -#include "control_connection.hpp" -#include "message.hpp" - -#include - -#include -#include -#include -#include -#include - -namespace wanda -{ - /** - * @brief The remote control client - * - */ - struct commander : wanda::control_connection::listener - { - /** - * @brief The interface to be implemented by remote control listeners - */ - struct listener - { - virtual void on_connected(commander & commander){}; - virtual void on_response(commander & commander, std::string response){}; - virtual void on_error(commander & commander, std::string error){}; - }; - - /** - * @brief Construct a new commander - */ - commander(asio::io_service & service, std::filesystem::path socket, listener & listener); - - /** - * @brief Start communication with the remote daemon endpoint - */ - void start(); - - /** - * @brief Stop communication with the remote daemon endpoint - */ - void stop(); - - /** - * @brief Send a command to the remote daemon endpoint - */ - void send(command command); - - void on_error(control_connection::pointer connection, std::error_code error) override; - void on_received(control_connection::pointer connection, message message) override; - - private: - asio::io_service & m_service; - wanda::control_connection::protocol::endpoint m_endpoint; - wanda::control_connection::protocol::socket m_socket; - wanda::control_connection::pointer m_connection; - listener & m_listener; - }; - -} // namespace wanda -#endif \ No newline at end of file diff --git a/src/control_connection.cpp b/src/control_connection.cpp deleted file mode 100644 index 13e0e5e..0000000 --- a/src/control_connection.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include "control_connection.hpp" - -#include - -namespace wanda -{ - control_connection::pointer make_control_connection(control_connection::protocol::socket && socket) - { - return std::make_shared(control_connection::key{}, std::move(socket)); - } - - control_connection::control_connection(control_connection::key key, control_connection::protocol::socket socket) - : keyed{key} - , m_socket{std::move(socket)} - { - } - - bool control_connection::add(listener * listener) - { - auto [_, inserted] = m_listeners.insert(listener); - return inserted; - } - - bool control_connection::remove(listener * listener) - { - return m_listeners.erase(listener); - } - - void control_connection::start() - { - if (m_state == state::unknown) - { - m_state = state::fresh; - perform_read(); - } - } - - void control_connection::send(message message) - { - m_output << message << '\n'; - asio::async_write(m_socket, m_out, asio::transfer_exactly(message.size() + 1), [that = shared_from_this(), this](auto const & error, auto const length) { - if (error) - { - // TODO: Handle error - } - else - { - m_out.consume(length); - } - }); - } - - void control_connection::close() - { - if (auto error = std::error_code{}; m_socket.cancel(error)) - { - for (auto & listener : m_listeners) - { - listener->on_error(shared_from_this(), error); - } - } - - if (auto error = std::error_code{}; m_socket.close(error)) - { - for (auto & listener : m_listeners) - { - listener->on_error(shared_from_this(), error); - } - } - - for (auto & listener : m_listeners) - { - listener->on_close(shared_from_this()); - } - m_listeners.clear(); - } - - void control_connection::update(state state) - { - m_state = state; - } - - control_connection::state control_connection::current_state() const - { - return m_state; - } - - void control_connection::perform_read() - { - asio::async_read_until(m_socket, m_in, '\n', [that = shared_from_this(), this](auto const & error, auto const length) { - if (error) - { - for (auto & listener : m_listeners) - { - listener->on_error(shared_from_this(), error); - } - close(); - } - else - { - auto msg = message{}; - m_input >> msg; - if (!m_input) - { - m_input.ignore(std::numeric_limits::max()); - m_input.clear(); - } - else - { - for (auto & listener : m_listeners) - { - listener->on_received(shared_from_this(), msg); - } - } - perform_read(); - } - }); - } - -} // namespace wanda \ No newline at end of file diff --git a/src/control_connection.hpp b/src/control_connection.hpp deleted file mode 100644 index 9e3ad62..0000000 --- a/src/control_connection.hpp +++ /dev/null @@ -1,114 +0,0 @@ -#ifndef WANDA_CONTROL_CONNECTION_HPP -#define WANDA_CONTROL_CONNECTION_HPP - -#include "keyed.hpp" -#include "message.hpp" - -#include - -#include -#include -#include -#include -#include -#include - -namespace wanda -{ - /** - * @brief A connection to a remote control endpoint - */ - struct control_connection : keyed, std::enable_shared_from_this - { - using protocol = asio::local::stream_protocol; - using pointer = std::shared_ptr; - - /** - * @brief The interface to be implemented by the control interface listener - */ - struct listener - { - virtual void on_close(pointer connection) {} - virtual void on_received(pointer connection, message message) {} - virtual void on_error(pointer connection, std::error_code) {} - }; - - /** - * @brief A enum to describe different connection states - */ - enum struct state : std::underlying_type_t - { - unknown, //< Connection is in an unknown state - fresh, //< Connection is freshly created but not established - established, //< Connection has been established - }; - - /** - * @internal - * @brief Construct a new control connection object - * - * @note This constructor is keyed on a private key type so it can only be constructed using the #wanda::make_connection factory - */ - control_connection(key, protocol::socket socket); - - /** - * @brief Add the given listener to this control connection's listener set - * - * @returns true iff. the listener was not already in the listener set - */ - bool add(listener * listener); - - /** - * @brief Remove the given listener from this control connection's listener set - * - * @return true iff. the listener was previously registered with this control connection - */ - bool remove(listener * listener); - - /** - * @brief Start I/O processing for this control connection - */ - void start(); - - /** - * @brief Close this control connection - */ - void close(); - - /** - * @brief Send the given message to the remote endpoint - */ - void send(message message); - - /** - * @brief Set the connection state to the provided state - */ - void update(state state); - - /** - * @brief Get the current connection state - */ - state current_state() const; - - private: - friend pointer make_control_connection(protocol::socket && socket); - - void perform_read(); - - protocol::socket m_socket; - asio::streambuf m_in{}; - asio::streambuf m_out{}; - std::istream m_input{&m_in}; - std::ostream m_output{&m_out}; - std::set m_listeners{}; - state m_state{}; - }; - - /** - * @brief Create a new control connection - */ - control_connection::pointer make_control_connection(control_connection::protocol::socket && socket); - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/control_interface.cpp b/src/control_interface.cpp deleted file mode 100644 index 81e10cf..0000000 --- a/src/control_interface.cpp +++ /dev/null @@ -1,154 +0,0 @@ -#include "control_interface.hpp" -#include "logging.hpp" -#include "optional.hpp" - -#include - -#include - -#include -#include -#include -#include -#include - -namespace wanda -{ - // 'socket_deleter' implementation - - socket_deleter::~socket_deleter() - { - if (std::filesystem::exists(path)) - { - std::filesystem::remove(path); - } - } - - // 'control_interface' implementation - - control_interface::control_interface(control_interface::key key, asio::io_service & service, control_interface::protocol::endpoint endpoint, listener & listener) - : keyed{key} - , m_service{service} - , m_endpoint{std::move(endpoint)} - , m_socket{m_service} - , m_acceptor{m_service} - , m_listener{listener} - { - } - - std::error_code control_interface::start() - { - if (auto error = std::error_code{}; m_acceptor.open(m_endpoint.protocol(), error)) - { - return error; - } - - if (auto error = std::error_code{}; m_acceptor.bind(m_endpoint, error)) - { - return error; - } - - if (auto error = std::error_code{}; m_acceptor.listen(128, error)) - { - return error; - } - else - { - perform_accept(); - return error; - } - } - - std::error_code control_interface::shutdown() - { - for (auto & connection : m_connections) - { - connection->close(); - } - - auto error = std::error_code{}; - return m_acceptor.close(error); - } - - void control_interface::perform_accept() - { - m_acceptor.async_accept(m_socket, [that = shared_from_this(), this](auto const & error) { - if (error && error != asio::error::operation_aborted) - { - get_logger()->error("failed to accept connection because '{}'", error); - } - else - { - get_logger()->info("new incoming controller connection"); - auto [connection, inserted] = m_connections.insert(make_control_connection(std::move(m_socket))); - if (inserted) - { - (*connection)->add(this); - (*connection)->start(); - } - perform_accept(); - } - }); - } - - void control_interface::on_close(control_connection::pointer connection) - { - if (static_cast(connection->current_state()) >= static_cast(control_connection::state::established)) - { - get_logger()->info("controller connection closed"); - } - else - { - get_logger()->info("controller connection aborted before it could be established"); - } - m_connections.erase(connection); - } - - void control_interface::on_received(control_connection::pointer connection, message message) - { - using namespace wanda::std_ext; - - if (m_connections.find(connection) == m_connections.cend()) - { - get_logger()->error("received message from an unknown connection"); - return; - } - - if (message.source != message_source_controller) - { - get_logger()->error("received a deamon message"); - return; - } - - if (auto state = connection->current_state(); message.command == message_command_hello && state == control_connection::state::fresh) - { - get_logger()->info("controller connection established"); - if (message.argument.has_value()) - { - get_logger()->info("remote controller version '{}'", *message.argument); - } - connection->send({message_source_daemon, message_command_hello, message_argument_hello}); - connection->update(control_connection::state::established); - } - else - { - with(make_command(message), [&](auto const & command) { - m_listener.on_received(*this, command); - }) || - [&] { get_logger()->warn("ignoring unknown message '{}'", message); }; - } - } - - control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path socket, control_interface::listener & listener) - { - if (std::filesystem::exists(socket)) - { - get_logger()->error("socket '{}' exists", socket.native()); - return {}; - } - - control_interface::protocol::endpoint endpoint{socket.string()}; - return std::make_shared(control_interface::key{}, service, std::move(endpoint), listener); - } - -} // namespace wanda \ No newline at end of file diff --git a/src/control_interface.hpp b/src/control_interface.hpp deleted file mode 100644 index 3d11fdf..0000000 --- a/src/control_interface.hpp +++ /dev/null @@ -1,96 +0,0 @@ -/** - * @file environment.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_CONTROL_INTERFACE_HPP -#define WANDA_CONTROL_INTERFACE_HPP - -#include "command.hpp" -#include "control_connection.hpp" -#include "keyed.hpp" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace wanda -{ - /** - * @brief An RAII type to delete a socket file upon destruction - */ - struct socket_deleter - { - ~socket_deleter(); - - std::filesystem::path path; - }; - - /** - * @brief The daemon control interface - */ - struct control_interface : control_connection::listener, keyed, std::enable_shared_from_this - { - using protocol = asio::local::stream_protocol; - using pointer = std::shared_ptr; - - /** - * @brief The interface to be implemented by the control interface listener - */ - struct listener - { - virtual void on_received(control_interface & interface, command command){}; - }; - - /** - * @internal - * @brief Construct a new control interface object - * - * @note This constructor is keyed on a private key type so it can only be constructed using the #wanda::make_interface factory - */ - control_interface(key, asio::io_service & service, protocol::endpoint endpoint, listener & listener); - - /** - * @brief Start handling of controller connections - */ - std::error_code start(); - - /** - * @brief Stop the control interface - */ - std::error_code shutdown(); - - void on_close(control_connection::pointer connection) override; - void on_received(control_connection::pointer connection, message message) override; - - private: - void perform_accept(); - - friend pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener); - - asio::io_service & m_service; - protocol::endpoint m_endpoint; - protocol::socket m_socket; - protocol::acceptor m_acceptor; - listener & m_listener; - socket_deleter m_deleter{m_endpoint.path()}; - std::set m_connections; - }; - - /** - * @brief A factory to create new #control_interface instances - */ - control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path socket, control_interface::listener & listener); - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/deferred_failure.hpp b/src/deferred_failure.hpp deleted file mode 100644 index 5db26f6..0000000 --- a/src/deferred_failure.hpp +++ /dev/null @@ -1,21 +0,0 @@ -/** - * @file deferred_failure.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_DEFERRED_FAILURE_HPP -#define WANDA_DEFERRED_FAILURE_HPP - -#include - -namespace -{ - /** - * @brief A helper type to defer static_assert failures - */ - template - using deferred_failure = std::false_type; -} // namespace - -#endif \ No newline at end of file diff --git a/src/environment.cpp b/src/environment.cpp deleted file mode 100644 index abcf62a..0000000 --- a/src/environment.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "environment.hpp" - -#include - -namespace wanda -{ - environment::environment(char const * const * env) - { - if (!env) - { - return; - } - - std::string buffer{}; - for (; *env != nullptr; ++env) - { - buffer = *env; - int split_point = buffer.find('='); - if (split_point != std::string::npos) - { - m_cache[buffer.substr(0, split_point)] = buffer.substr(split_point + 1); - } - } - } - - std::string & environment::operator[](std::string const & variable) - { - return m_cache[variable]; - } - - std::string const & environment::operator[](std::string const & variable) const - { - static std::string const empty{}; - if (auto needle = m_cache.find(variable); needle != cend()) - { - return needle->second; - } - return empty; - } - - environment::iterator environment::begin() - { - return m_cache.begin(); - } - - environment::const_iterator environment::begin() const - { - return m_cache.begin(); - } - - environment::const_iterator environment::cbegin() const - { - return m_cache.cbegin(); - } - - environment::iterator environment::end() - { - return m_cache.end(); - } - - environment::const_iterator environment::end() const - { - return m_cache.end(); - } - - environment::const_iterator environment::cend() const - { - return m_cache.cend(); - } - -} // namespace wanda \ No newline at end of file diff --git a/src/environment.hpp b/src/environment.hpp deleted file mode 100644 index 5a702a8..0000000 --- a/src/environment.hpp +++ /dev/null @@ -1,61 +0,0 @@ -/** - * @file environment.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_ENVIRONMENT_HPP -#define WANDA_ENVIRONMENT_HPP - -#include - -#include -#include - -namespace wanda -{ - /** - * @brief A type to provide access to the runtime environment - */ - struct environment - { - using map_type = std::map; - using iterator = map_type::iterator; - using const_iterator = map_type::const_iterator; - using reference = map_type::reference; - using const_reference = map_type::const_reference; - - /** - * @brief Construct a new environment from the given string array - */ - explicit environment(char const * const * env = ::environ); - - /** - * @brief Get the value of the given variable - * - * @return A mutable reference to the value of the given environment variable - */ - std::string & operator[](std::string const & variable); - - /** - * @brief Get the value of the given variable - * - * @return An immutable reference to the value of the given environment variable - */ - std::string const & operator[](std::string const & variable) const; - - iterator begin(); - const_iterator begin() const; - const_iterator cbegin() const; - - iterator end(); - const_iterator end() const; - const_iterator cend() const; - - private: - map_type m_cache{}; - }; - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/expected.hpp b/src/expected.hpp deleted file mode 100644 index fff0d81..0000000 --- a/src/expected.hpp +++ /dev/null @@ -1,256 +0,0 @@ -/** - * @file expected.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_EXPECTED_HPP -#define WANDA_EXPECTED_HPP - -#include -#include -#include - -namespace wanda -{ - /** - * @brief A type to represent the error case of a computation based on #wanda::expected - */ - template - struct unexpected - { - static_assert(!std::is_same_v, "ErrorType can not be 'void'!"); - static_assert(!std::is_array_v, "ErrorType can not be an array type!"); - - /** - * @brief Copy construct a new @p unexpected from another @p unexpected - */ - constexpr unexpected(unexpected const &) = default; - - /** - * @brief Move construct a new @p unexpected from another @p unexpected - */ - constexpr unexpected(unexpected &&) = default; - - /** - * @brief Construct a new @p unexpected by direct initializing the error object from @p args - */ - template - constexpr explicit unexpected(std::in_place_t, Args &&... args) - : m_error(std::forward(args)...) - { - } - - /** - * @brief Construct a new @p unexpected by direct initializing the error object from @p il and @p args - */ - template< - typename U, - typename... Args, - std::enable_if_t, Args...>> * = nullptr> - constexpr explicit unexpected(std::in_place_t, std::initializer_list il, Args &&... args) - : m_error(il, std::forward(args)...) - { - } - - /** - * @brief Construct a new @p unexpected by direct initializing the error object from @p error - */ - template< - typename Err = ErrorType, - std::enable_if_t && - !std::is_same_v>, std::in_place_t> && - !std::is_same_v>, unexpected>> * = nullptr> - constexpr explicit unexpected(Err && error) - : m_error(std::forward(error)) - { - } - - /** - * @brief Construct a new @p unexpected by copying the value of another @p unexpected of different error type - */ - template< - typename Err, - std::enable_if_t && - !std::is_constructible_v &> && - !std::is_constructible_v> && - !std::is_constructible_v const &> && - !std::is_constructible_v const> && - !std::is_convertible_v &, ErrorType> && - !std::is_convertible_v, ErrorType> && - !std::is_convertible_v const &, ErrorType> && - !std::is_convertible_v const, ErrorType>)> * = nullptr, - std::enable_if_t> * = nullptr> - constexpr explicit unexpected(unexpected const & error) - : m_error(error.m_error) - { - } - - /** - * @brief Construct a new @p unexpected by copying the value of another @p unexpected of different error type - */ - template< - typename Err, - std::enable_if_t && - !std::is_constructible_v &> && - !std::is_constructible_v> && - !std::is_constructible_v const &> && - !std::is_constructible_v const> && - !std::is_convertible_v &, ErrorType> && - !std::is_convertible_v, ErrorType> && - !std::is_convertible_v const &, ErrorType> && - !std::is_convertible_v const, ErrorType>)> * = nullptr, - std::enable_if_t> * = nullptr> - constexpr unexpected(unexpected const & error) - : m_error(error.m_error) - { - } - - /** - * @brief Construct a new @p unexpected by moving the value of another @p unexpected of different error type - */ - template< - typename Err, - std::enable_if_t && - !std::is_constructible_v &> && - !std::is_constructible_v> && - !std::is_constructible_v const &> && - !std::is_constructible_v const> && - !std::is_convertible_v &, ErrorType> && - !std::is_convertible_v, ErrorType> && - !std::is_convertible_v const &, ErrorType> && - !std::is_convertible_v const, ErrorType>)> * = nullptr, - std::enable_if_t> * = nullptr> - constexpr explicit unexpected(unexpected && error) - : m_error(std::move(error.m_error)) - { - } - - /** - * @brief Construct a new @p unexpected by moving the value of another @p unexpected of different error type - */ - template< - typename Err, - std::enable_if_t && - !std::is_constructible_v &> && - !std::is_constructible_v> && - !std::is_constructible_v const &> && - !std::is_constructible_v const> && - !std::is_convertible_v &, ErrorType> && - !std::is_convertible_v, ErrorType> && - !std::is_convertible_v const &, ErrorType> && - !std::is_convertible_v const, ErrorType>)> * = nullptr, - std::enable_if_t> * = nullptr> - constexpr unexpected(unexpected && error) - : m_error(std::move(error.m_error)) - { - } - - /** - * @brief Get the error value contained in this @p unexpected instance - */ - constexpr ErrorType const & value() const & - { - return m_error; - } - - /** - * @brief Get the error value contained in this @p unexpected instance - */ - constexpr ErrorType & value() & - { - return m_error; - } - - /** - * @brief Get the error value contained in this @p unexpected instance - */ - constexpr ErrorType && value() && - { - return std::move(m_error); - } - - /** - * @brief Get the error value contained in this @p unexpected instance - */ - constexpr ErrorType const && value() const && - { - return std::move(m_error); - } - - /** - * @brief Swap the error value of this @p unexpected instance with the one of @p other - */ - void swap(unexpected & other) noexcept(std::is_nothrow_swappable_v) - { - using std::swap; - swap(m_error, other.m_error); - } - - template - friend constexpr bool operator==(unexpected const & lhs, unexpected const & rhs); - - template - friend constexpr bool operator!=(unexpected const & lhs, unexpected const & rhs); - - template< - typename Err, - std::enable_if_t> *> - friend void swap(unexpected & lhs, unexpected & rhs); - - private: - ErrorType m_error; - }; - - template - unexpected(ErrorType)->unexpected; - - /** - * @brief Compare two @p unexpected instances for equality - */ - template - constexpr bool operator==(unexpected const & lhs, unexpected const & rhs) - { - return lhs.m_error == rhs.m_error; - } - - /** - * @brief Compare two @p unexpected instances for inequality - */ - template - constexpr bool operator!=(unexpected const & lhs, unexpected const & rhs) - { - return lhs.m_error != rhs.m_error; - } - - /** - * @brief Swap the error values of two @p unexpected instances - */ - template< - typename Err, - std::enable_if_t> * = nullptr> - void swap(unexpected & lhs, unexpected & rhs) - { - lhs.swap(rhs); - } - - /** - * @brief A tag type for @p unexpected - */ - struct unexpect_t - { - explicit unexpect_t() = default; - }; - - /** - * @brief A tap for @p unexpected - */ - inline constexpr unexpect_t unexpect{}; - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/filesystem.cpp b/src/filesystem.cpp deleted file mode 100644 index bb724d1..0000000 --- a/src/filesystem.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "filesystem.hpp" - -#include - -#include - -namespace wanda -{ - std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) - { - if (!std::filesystem::is_directory(source)) - { - return std::nullopt; - } - - auto first = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{source}); - auto last = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{}); - return path_list{first, last}; - } - - std::filesystem::path random_pick(path_list const & paths) - { - static auto generator = std::mt19937{std::random_device{}()}; - auto distribution = std::uniform_int_distribution{0, paths.size() - 1}; - - return paths[distribution(generator)]; - } - -} // namespace wanda \ No newline at end of file diff --git a/src/filesystem.hpp b/src/filesystem.hpp deleted file mode 100644 index 1975bc5..0000000 --- a/src/filesystem.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file filesystem.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_FILESYSTEM_HPP -#define WANDA_FILESYSTEM_HPP - -#include -#include -#include - -namespace wanda -{ - /** - * @brief Covenience alias for path lists - */ - using path_list = std::vector; - - /** - * @brief The default scan filter, allowing only regular files to pass - */ - constexpr inline auto default_filter = [](std::filesystem::path const & path) { - return is_regular_file(path); - }; - - /** - * @brief Scan the given folder for files - */ - std::optional scan(std::filesystem::path folder, bool(filter)(std::filesystem::path const &) = default_filter); - - /** - * @brief Pick a random path from the given list - */ - std::filesystem::path random_pick(path_list const & paths); -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/keyed.hpp b/src/keyed.hpp deleted file mode 100644 index 58f17ad..0000000 --- a/src/keyed.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file keyed.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_KEYED_HPP -#define WANDA_KEYED_HPP - -namespace wanda -{ - /** - * @brief A tag type to prevent construction of a type without a factory - */ - template - struct keyed - { - protected: - struct key - { - }; - - explicit keyed(key) {} - }; - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/logging.cpp b/src/logging.cpp deleted file mode 100644 index fc8bf8b..0000000 --- a/src/logging.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "logging.hpp" - -namespace wanda -{ - std::function initializer = [](spdlog::sink_ptr sink) { - spdlog::register_logger(std::make_shared("wanda", sink)); - initializer = [](auto) {}; - }; - - void initialize_logger(spdlog::sink_ptr sink) - { - initializer(sink); - } - - logger_ptr get_logger() - { - initialize_logger(); - return spdlog::get("wanda"); - } - -} // namespace wanda \ No newline at end of file diff --git a/src/logging.hpp b/src/logging.hpp deleted file mode 100644 index b3c1665..0000000 --- a/src/logging.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/** - * @file logging.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_LOGGING_HPP -#define WANDA_LOGGING_HPP - -#include -#include - -#include - -namespace wanda -{ - /** - * @brief A covenience alias to represent a handle for a logger - */ - using logger_ptr = std::shared_ptr; - - /** - * @brief Initialize the shared logger - * - * @note The logger will only ever be initialized once, even if this function is called multiple times - */ - void initialize_logger(spdlog::sink_ptr sink = std::make_shared()); - - /** - * @brief Get the shared logger - */ - logger_ptr get_logger(); -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/message.cpp b/src/message.cpp deleted file mode 100644 index 4c152fa..0000000 --- a/src/message.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "message.hpp" - -#include -#include -#include - -namespace wanda -{ - message::operator std::string() const - { - std::ostringstream buffer{}; - buffer << source - << ':' - << command; - if (argument.has_value()) - { - buffer << ':' << *argument; - } - return buffer.str(); - } - - std::size_t message::size() const - { - return static_cast(*this).size(); - } - - template - OutputIt copy_until(InputIt first, InputIt last, OutputIt out, UnaryPredicate predicate) - { - while (first != last && !predicate(*first)) - { - *out++ = *first++; - } - return out; - } - - std::istream & operator>>(std::istream & in, message & message) - { - auto pos = std::istream_iterator{in}; - auto end = std::istream_iterator{}; - auto buffer = std::string{}; - - copy_until(pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; }); - if (in.eof() || buffer.size() != 1) - { - in.setstate(std::ios_base::failbit); - return in; - } - message.source = buffer; - - buffer.clear(); - copy_until(++pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; }); - if (in.eof()) - { - in.setstate(std::ios_base::failbit); - } - message.command = buffer; - - buffer.clear(); - copy(++pos, end, std::back_inserter(buffer)); - if (buffer.size()) - { - message.argument = std::optional{std::move(buffer)}; - } - - in.clear(in.rdstate() ^ std::ios_base::failbit); - return in; - } - - std::ostream & operator<<(std::ostream & out, message const & message) - { - return out << static_cast(message); - } - -} // namespace wanda \ No newline at end of file diff --git a/src/message.hpp b/src/message.hpp deleted file mode 100644 index 866408f..0000000 --- a/src/message.hpp +++ /dev/null @@ -1,83 +0,0 @@ -/** - * @file message.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_MESSAGE_HPP -#define WANDA_MESSAGE_HPP - -#include -#include -#include -#include - -namespace wanda -{ - inline namespace v1 - { - /** - * @brief The version argument for the hello message reflecting the current version - */ - auto constexpr message_argument_hello = "1.0.0"; - } // namespace v1 - - /** - * @brief A tag to mark messages originating from the controller - */ - auto constexpr message_source_controller = "C"; - - /** - * @brief A tag to mark messages originating from the daemon - */ - auto constexpr message_source_daemon = "D"; - - /** - * @brief The command of the hello message - */ - auto constexpr message_command_hello = "HELLO"; - - /** - * @brief A control protocol message, consisting of a @p source, @p command, and @p arguments - */ - struct message - { - /** - * @brief Serialize this message into a string - */ - explicit operator std::string() const; - - /** - * @brief Get the size of the message as if it was serialized - */ - std::size_t size() const; - - /** - * @brief The source of the message - */ - std::string source; - - /** - * @brief The command of the message - */ - std::string command; - - /** - * @brief The arguments of the message command - */ - std::optional argument; - }; - - /** - * @brief Deserialize a message from the given stream - */ - std::istream & operator>>(std::istream & in, message & message); - - /** - * @brief Serialize a message to the given stream - */ - std::ostream & operator<<(std::ostream & out, message const & message); - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/optional.hpp b/src/optional.hpp deleted file mode 100644 index da3774c..0000000 --- a/src/optional.hpp +++ /dev/null @@ -1,65 +0,0 @@ -/** - * @file optional.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_OPTIONAL_HPP -#define WANDA_OPTIONAL_HPP - -#include - -namespace wanda::std_ext -{ - /** - * @brief A type to represent a computation that could fail - */ - struct failable - { - /** - * @brief A factory to create a successful computation - */ - constexpr static auto success() { return failable{false}; } - - /** - * @brief A factory to create a failed computation - */ - constexpr static auto failure() { return failable{true}; } - - /** - * @brief Execute the given handler if the computation failed - */ - template - constexpr void operator||(Handler handler) const - { - if (m_failed) - { - handler(); - } - } - - private: - constexpr explicit failable(bool failed) - : m_failed{failed} {}; - bool const m_failed; - }; - - /** - * @brief Unwrap the given optional object, if present, and pass it to the handler - * - * @return A successful computation iff. the object was present, a failed computation otherwise. - */ - template - auto with(std::optional && object, HandlerType handler) - { - if (object) - { - handler(object.value()); - return failable::success(); - } - return failable::failure(); - } - -} // namespace wanda::std_ext - -#endif \ No newline at end of file diff --git a/src/setting.cpp b/src/setting.cpp deleted file mode 100644 index d9f8607..0000000 --- a/src/setting.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "setting.hpp" - -#include -#include - -namespace wanda -{ - // UDL implementations - - key literals::operator""_key(char const * str, std::size_t len) - { - return key{{str, len}}; - } - - std::optional literals::operator""_setting(char const * str, std::size_t len) - { - auto source = g_settings_schema_source_get_default(); - if (!source) - { - return std::nullopt; - } - - auto schema = g_settings_schema_source_lookup(source, str, true); - if (!schema) - { - return std::nullopt; - } - - return setting{schema}; - } - - // 'setting' implementation - - setting::setting(GSettingsSchema * schema) - : m_schema{schema, &g_settings_schema_unref} - { - } - - std::optional setting::operator[](key key) const - { - if (!g_settings_schema_has_key(m_schema.get(), key.get().c_str())) - { - return std::nullopt; - } - - return setting::entry{*this, std::move(key)}; - } - - // 'setting::entry' implementation - - setting::entry::entry(setting const & setting, key key) - : m_settings{g_settings_new(g_settings_schema_get_id(setting.m_schema.get())), &g_object_unref} - , m_key{key.get()} - { - } - - setting::entry::value_type setting::entry::operator*() const - { - auto value = std::unique_ptr{g_settings_get_value(m_settings.get(), m_key.get().c_str()), &g_variant_unref}; - auto raw = value.get(); - - if (g_variant_is_of_type(raw, G_VARIANT_TYPE_BOOLEAN)) - { - return static_cast(g_variant_get_boolean(raw)); - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_INT32)) - { - return static_cast(g_variant_get_int32(raw)); - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_INT64)) - { - return static_cast(g_variant_get_int64(raw)); - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_UINT32)) - { - return static_cast(g_variant_get_uint32(raw)); - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_UINT64)) - { - return static_cast(g_variant_get_uint64(raw)); - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_DOUBLE)) - { - return static_cast(g_variant_get_double(raw)); - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_STRING)) - { - auto size = gsize{}; - auto string = g_variant_get_string(raw, &size); - return std::string{string, size}; - } - else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_STRING_ARRAY)) - { - auto length = gsize{}; - auto data = g_variant_get_strv(raw, &length); - return std::vector{data, data + length}; - } - - return {}; - } - -} // namespace wanda \ No newline at end of file diff --git a/src/setting.hpp b/src/setting.hpp deleted file mode 100644 index b681e05..0000000 --- a/src/setting.hpp +++ /dev/null @@ -1,160 +0,0 @@ -/** - * @file setting.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_setting_HPP -#define WANDA_setting_HPP - -#include "deferred_failure.hpp" -#include "type_wrapper.hpp" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace wanda -{ - struct setting; - - /** - * @brief A convenience type to represent setting keys - */ - using key = type_wrapper; - - namespace literals - { - /** - * @brief UDL to create setting keys - */ - key operator""_key(char const * str, std::size_t len); - - /** - * @brief UDL to create setting schemas - */ - std::optional operator""_setting(char const * str, std::size_t lent); - } // namespace literals - - /** - * @brief A simple wrapper for GSettings Schemas - */ - struct setting - { - struct entry - { - using value_type = std::variant>; - - /** - * @brief Get the value of the settings entry - */ - value_type operator*() const; - - /** - * @brief Assign the given @p value to the settings entry - * - * @returns @p true iff. the value could be successfully assigned - */ - template - bool operator=(Type value) - { - struct setting_applier - { - setting_applier(GSettings * setting, gchar const * key, Type value) noexcept - : m_result{[&] { - if constexpr (std::is_same_v) - { - return g_settings_set_boolean(setting, key, value); - } - else if constexpr (std::is_same_v) - { - return g_settings_set_int(setting, key, value); - } - else if constexpr (std::is_same_v) - { - return g_settings_set_int64(setting, key, value); - } - else if constexpr (std::is_same_v) - { - return g_settings_set_uint(setting, key, value); - } - else if constexpr (std::is_same_v) - { - return g_settings_set_uint64(setting, key, value); - } - else if constexpr (std::is_same_v) - { - return g_settings_set_double(setting, key, value); - } - else if constexpr (std::is_same_v) - { - return g_settings_set_string(setting, key, value.c_str()); - } - else if constexpr (std::is_same_v>) - { - auto temp = std::vector{value.size() + 1}; - std::transform(value.begin(), value.end(), temp.begin(), [](auto const & str) { return str.c_str(); }); - return g_settings_set_strv(setting, key, temp.data()); - } - else - { - static_assert(deferred_failure{}, "Invalid argument type!"); - } - }()} - { - } - - ~setting_applier() - { - g_settings_sync(); - } - - operator bool() const - { - return m_result; - } - - private: - gboolean const m_result; - }; - - return setting_applier{m_settings.get(), m_key.get().c_str(), value}; - } - - private: - entry(setting const & schema, key key); - - std::unique_ptr m_settings; - - key m_key; - - friend setting; - }; - - /** - * @brief Get the entry for the given key - * - * @return An std::optional wrapping the entry associated with - * the given key, or an empty std::optional if the desired key - * does not exist in the setting's schema. - */ - std::optional operator[](key key) const; - - private: - explicit setting(GSettingsSchema * schema); - - std::unique_ptr m_schema; - - friend std::optional literals::operator""_setting(char const *, std::size_t); - }; - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/type_wrapper.hpp b/src/type_wrapper.hpp deleted file mode 100644 index 12684cb..0000000 --- a/src/type_wrapper.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file type_wrapper.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_TYPE_WRAPPER_HPP -#define WANDA_TYPE_WRAPPER_HPP - -#include - -namespace wanda -{ - /** - * @brief A type to create a distinct type based on an existing type - * - * @tparam InnerType The type to wrap - * @tparam TagType A tag type to identify the distinct type - */ - template - struct type_wrapper - { - /** - * @brief Construct a new type wrapper object - */ - explicit type_wrapper(InnerType value) - : m_value{std::move(value)} - { - } - - /** - * @brief Retrieve the wrapped value with its original type - */ - constexpr explicit operator InnerType const &() const { return get(); } - - /** - * @brief Retrieve the wrapped value with its original type - */ - constexpr InnerType const & get() const { return m_value; } - - private: - InnerType m_value; - }; - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/wallpaper.cpp b/src/wallpaper.cpp deleted file mode 100644 index b4e0b76..0000000 --- a/src/wallpaper.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "logging.hpp" -#include "optional.hpp" -#include "setting.hpp" -#include "wallpaper.hpp" - -namespace wanda -{ - void set_wallpaper(std::filesystem::path wallpaper) - { - using namespace wanda::literals; - using namespace wanda::std_ext; - using namespace std::string_literals; - - with("org.gnome.desktop.background"_setting, [&](auto & setting) { - with(setting["picture-uri"_key], [&](auto & value) { - value = "file://" + wallpaper.native(); - }) || - [&] { get_logger()->error("invalid settings key"); }; - }) || - [&] { get_logger()->error("invalid setting"); }; - } - -} // namespace wanda \ No newline at end of file diff --git a/src/wallpaper.hpp b/src/wallpaper.hpp deleted file mode 100644 index 0cad473..0000000 --- a/src/wallpaper.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @file wallpaper.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_WALLPAPER_HPP -#define WANDA_WALLPAPER_HPP - -#include -#include - -#include -#include - -namespace wanda -{ - /** - * @brief Set the wallpaper to the file specified by the given path - */ - void set_wallpaper(std::filesystem::path wallpaper); -} // namespace wanda - -#endif \ No newline at end of file diff --git a/src/wanda/command.cpp b/src/wanda/command.cpp new file mode 100644 index 0000000..960c52b --- /dev/null +++ b/src/wanda/command.cpp @@ -0,0 +1,47 @@ +#include + +namespace wanda +{ + std::optional command::message() const + { + using namespace std::string_literals; + auto const command = [this] { + switch (id) + { + case command_id::change: + return "CHANGE"s; + default: + return ""s; + } + }(); + + auto argument_string = std::string{}; + for (int index = 0ul; index < arguments.size(); ++index) + { + argument_string += (index) ? "," + arguments[index] : arguments[index]; + } + + if (command.empty()) + { + return std::nullopt; + } + + return wanda::message{"C", command, argument_string}; + } + + std::optional make_command(message message) + { + if (message.command == "CHANGE") + { + return {{command_id::change}}; + } + + return std::nullopt; + } + + command make_change_command() + { + return {command_id::change}; + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/commander.cpp b/src/wanda/commander.cpp new file mode 100644 index 0000000..85fc68a --- /dev/null +++ b/src/wanda/commander.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#include + +namespace wanda +{ + commander::commander(asio::io_service & service, std::filesystem::path socket, listener & listener) + : m_service{service} + , m_endpoint{socket.string()} + , m_socket{service} + , m_listener{listener} + { + } + + void commander::start() + { + m_socket.async_connect(m_endpoint, [&](auto const & error) { + if (error) + { + get_logger()->error("error while connecting to control interface: '{}'", error.message()); + } + else + { + get_logger()->info("establishing connection to wanda deamon"); + m_connection = wanda::make_control_connection(std::move(m_socket)); + m_connection->add(this); + m_connection->start(); + m_connection->send({message_source_controller, message_command_hello, message_argument_hello}); + } + }); + } + + void commander::stop() + { + get_logger()->info("closing control connection"); + m_connection->close(); + } + + void commander::send(command command) + { + using namespace wanda::std_ext; + + if (!m_connection || m_connection->current_state() != control_connection::state::established) + { + get_logger()->error("tried to send command without an established connection"); + m_listener.on_error(*this, "tried to send command without an established connection"); + return; + } + + with(command.message(), [&](auto const & message) { m_connection->send(message); }) || + [&] { get_logger()->error("unknown command"); }; + } + + void commander::on_error(control_connection::pointer connection, std::error_code error) + { + get_logger()->error("control interface communication error: '{}'", error.message()); + } + + void commander::on_received(wanda::control_connection::pointer connection, message message) + { + if (auto state = connection->current_state(); message.command == "HELLO" && state == control_connection::state::fresh) + { + get_logger()->info("connection to wanda deamon successfully established"); + connection->update(control_connection::state::established); + m_listener.on_connected(*this); + } + else + { + get_logger()->error("unexpected message: '{}'", message); + m_listener.on_error(*this, "unexpected message '" + static_cast(message) + '\''); + } + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/control_connection.cpp b/src/wanda/control_connection.cpp new file mode 100644 index 0000000..40e29f3 --- /dev/null +++ b/src/wanda/control_connection.cpp @@ -0,0 +1,120 @@ +#include + +#include + +namespace wanda +{ + control_connection::pointer make_control_connection(control_connection::protocol::socket && socket) + { + return std::make_shared(control_connection::key{}, std::move(socket)); + } + + control_connection::control_connection(control_connection::key key, control_connection::protocol::socket socket) + : keyed{key} + , m_socket{std::move(socket)} + { + } + + bool control_connection::add(listener * listener) + { + auto [_, inserted] = m_listeners.insert(listener); + return inserted; + } + + bool control_connection::remove(listener * listener) + { + return m_listeners.erase(listener); + } + + void control_connection::start() + { + if (m_state == state::unknown) + { + m_state = state::fresh; + perform_read(); + } + } + + void control_connection::send(message message) + { + m_output << message << '\n'; + asio::async_write(m_socket, m_out, asio::transfer_exactly(message.size() + 1), [that = shared_from_this(), this](auto const & error, auto const length) { + if (error) + { + // TODO: Handle error + } + else + { + m_out.consume(length); + } + }); + } + + void control_connection::close() + { + if (auto error = std::error_code{}; m_socket.cancel(error)) + { + for (auto & listener : m_listeners) + { + listener->on_error(shared_from_this(), error); + } + } + + if (auto error = std::error_code{}; m_socket.close(error)) + { + for (auto & listener : m_listeners) + { + listener->on_error(shared_from_this(), error); + } + } + + for (auto & listener : m_listeners) + { + listener->on_close(shared_from_this()); + } + m_listeners.clear(); + } + + void control_connection::update(state state) + { + m_state = state; + } + + control_connection::state control_connection::current_state() const + { + return m_state; + } + + void control_connection::perform_read() + { + asio::async_read_until(m_socket, m_in, '\n', [that = shared_from_this(), this](auto const & error, auto const length) { + if (error) + { + for (auto & listener : m_listeners) + { + listener->on_error(shared_from_this(), error); + } + close(); + } + else + { + auto msg = message{}; + m_input >> msg; + if (!m_input) + { + m_input.ignore(std::numeric_limits::max()); + m_input.clear(); + } + else + { + for (auto & listener : m_listeners) + { + listener->on_received(shared_from_this(), msg); + } + } + perform_read(); + } + }); + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/control_interface.cpp b/src/wanda/control_interface.cpp new file mode 100644 index 0000000..c008920 --- /dev/null +++ b/src/wanda/control_interface.cpp @@ -0,0 +1,154 @@ +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include + +namespace wanda +{ + // 'socket_deleter' implementation + + socket_deleter::~socket_deleter() + { + if (std::filesystem::exists(path)) + { + std::filesystem::remove(path); + } + } + + // 'control_interface' implementation + + control_interface::control_interface(control_interface::key key, asio::io_service & service, control_interface::protocol::endpoint endpoint, listener & listener) + : keyed{key} + , m_service{service} + , m_endpoint{std::move(endpoint)} + , m_socket{m_service} + , m_acceptor{m_service} + , m_listener{listener} + { + } + + std::error_code control_interface::start() + { + if (auto error = std::error_code{}; m_acceptor.open(m_endpoint.protocol(), error)) + { + return error; + } + + if (auto error = std::error_code{}; m_acceptor.bind(m_endpoint, error)) + { + return error; + } + + if (auto error = std::error_code{}; m_acceptor.listen(128, error)) + { + return error; + } + else + { + perform_accept(); + return error; + } + } + + std::error_code control_interface::shutdown() + { + for (auto & connection : m_connections) + { + connection->close(); + } + + auto error = std::error_code{}; + return m_acceptor.close(error); + } + + void control_interface::perform_accept() + { + m_acceptor.async_accept(m_socket, [that = shared_from_this(), this](auto const & error) { + if (error && error != asio::error::operation_aborted) + { + get_logger()->error("failed to accept connection because '{}'", error); + } + else + { + get_logger()->info("new incoming controller connection"); + auto [connection, inserted] = m_connections.insert(make_control_connection(std::move(m_socket))); + if (inserted) + { + (*connection)->add(this); + (*connection)->start(); + } + perform_accept(); + } + }); + } + + void control_interface::on_close(control_connection::pointer connection) + { + if (static_cast(connection->current_state()) >= static_cast(control_connection::state::established)) + { + get_logger()->info("controller connection closed"); + } + else + { + get_logger()->info("controller connection aborted before it could be established"); + } + m_connections.erase(connection); + } + + void control_interface::on_received(control_connection::pointer connection, message message) + { + using namespace wanda::std_ext; + + if (m_connections.find(connection) == m_connections.cend()) + { + get_logger()->error("received message from an unknown connection"); + return; + } + + if (message.source != message_source_controller) + { + get_logger()->error("received a deamon message"); + return; + } + + if (auto state = connection->current_state(); message.command == message_command_hello && state == control_connection::state::fresh) + { + get_logger()->info("controller connection established"); + if (message.argument.has_value()) + { + get_logger()->info("remote controller version '{}'", *message.argument); + } + connection->send({message_source_daemon, message_command_hello, message_argument_hello}); + connection->update(control_connection::state::established); + } + else + { + with(make_command(message), [&](auto const & command) { + m_listener.on_received(*this, command); + }) || + [&] { get_logger()->warn("ignoring unknown message '{}'", message); }; + } + } + + control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path socket, control_interface::listener & listener) + { + if (std::filesystem::exists(socket)) + { + get_logger()->error("socket '{}' exists", socket.native()); + return {}; + } + + control_interface::protocol::endpoint endpoint{socket.string()}; + return std::make_shared(control_interface::key{}, service, std::move(endpoint), listener); + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/environment.cpp b/src/wanda/environment.cpp new file mode 100644 index 0000000..533a5f5 --- /dev/null +++ b/src/wanda/environment.cpp @@ -0,0 +1,71 @@ +#include + +#include + +namespace wanda +{ + environment::environment(char const * const * env) + { + if (!env) + { + return; + } + + std::string buffer{}; + for (; *env != nullptr; ++env) + { + buffer = *env; + int split_point = buffer.find('='); + if (split_point != std::string::npos) + { + m_cache[buffer.substr(0, split_point)] = buffer.substr(split_point + 1); + } + } + } + + std::string & environment::operator[](std::string const & variable) + { + return m_cache[variable]; + } + + std::string const & environment::operator[](std::string const & variable) const + { + static std::string const empty{}; + if (auto needle = m_cache.find(variable); needle != cend()) + { + return needle->second; + } + return empty; + } + + environment::iterator environment::begin() + { + return m_cache.begin(); + } + + environment::const_iterator environment::begin() const + { + return m_cache.begin(); + } + + environment::const_iterator environment::cbegin() const + { + return m_cache.cbegin(); + } + + environment::iterator environment::end() + { + return m_cache.end(); + } + + environment::const_iterator environment::end() const + { + return m_cache.end(); + } + + environment::const_iterator environment::cend() const + { + return m_cache.cend(); + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/filesystem.cpp b/src/wanda/filesystem.cpp new file mode 100644 index 0000000..01dff43 --- /dev/null +++ b/src/wanda/filesystem.cpp @@ -0,0 +1,29 @@ +#include + +#include + +#include + +namespace wanda +{ + std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) + { + if (!std::filesystem::is_directory(source)) + { + return std::nullopt; + } + + auto first = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{source}); + auto last = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{}); + return path_list{first, last}; + } + + std::filesystem::path random_pick(path_list const & paths) + { + static auto generator = std::mt19937{std::random_device{}()}; + auto distribution = std::uniform_int_distribution{0, paths.size() - 1}; + + return paths[distribution(generator)]; + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/logging.cpp b/src/wanda/logging.cpp new file mode 100644 index 0000000..8c61953 --- /dev/null +++ b/src/wanda/logging.cpp @@ -0,0 +1,21 @@ +#include + +namespace wanda +{ + std::function initializer = [](spdlog::sink_ptr sink) { + spdlog::register_logger(std::make_shared("wanda", sink)); + initializer = [](auto) {}; + }; + + void initialize_logger(spdlog::sink_ptr sink) + { + initializer(sink); + } + + logger_ptr get_logger() + { + initialize_logger(); + return spdlog::get("wanda"); + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/message.cpp b/src/wanda/message.cpp new file mode 100644 index 0000000..978b7c4 --- /dev/null +++ b/src/wanda/message.cpp @@ -0,0 +1,75 @@ +#include + +#include +#include +#include + +namespace wanda +{ + message::operator std::string() const + { + std::ostringstream buffer{}; + buffer << source + << ':' + << command; + if (argument.has_value()) + { + buffer << ':' << *argument; + } + return buffer.str(); + } + + std::size_t message::size() const + { + return static_cast(*this).size(); + } + + template + OutputIt copy_until(InputIt first, InputIt last, OutputIt out, UnaryPredicate predicate) + { + while (first != last && !predicate(*first)) + { + *out++ = *first++; + } + return out; + } + + std::istream & operator>>(std::istream & in, message & message) + { + auto pos = std::istream_iterator{in}; + auto end = std::istream_iterator{}; + auto buffer = std::string{}; + + copy_until(pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; }); + if (in.eof() || buffer.size() != 1) + { + in.setstate(std::ios_base::failbit); + return in; + } + message.source = buffer; + + buffer.clear(); + copy_until(++pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; }); + if (in.eof()) + { + in.setstate(std::ios_base::failbit); + } + message.command = buffer; + + buffer.clear(); + copy(++pos, end, std::back_inserter(buffer)); + if (buffer.size()) + { + message.argument = std::optional{std::move(buffer)}; + } + + in.clear(in.rdstate() ^ std::ios_base::failbit); + return in; + } + + std::ostream & operator<<(std::ostream & out, message const & message) + { + return out << static_cast(message); + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/setting.cpp b/src/wanda/setting.cpp new file mode 100644 index 0000000..f0cf7a7 --- /dev/null +++ b/src/wanda/setting.cpp @@ -0,0 +1,102 @@ +#include + +#include +#include + +namespace wanda +{ + // UDL implementations + + key literals::operator""_key(char const * str, std::size_t len) + { + return key{{str, len}}; + } + + std::optional literals::operator""_setting(char const * str, std::size_t len) + { + auto source = g_settings_schema_source_get_default(); + if (!source) + { + return std::nullopt; + } + + auto schema = g_settings_schema_source_lookup(source, str, true); + if (!schema) + { + return std::nullopt; + } + + return setting{schema}; + } + + // 'setting' implementation + + setting::setting(GSettingsSchema * schema) + : m_schema{schema, &g_settings_schema_unref} + { + } + + std::optional setting::operator[](key key) const + { + if (!g_settings_schema_has_key(m_schema.get(), key.get().c_str())) + { + return std::nullopt; + } + + return setting::entry{*this, std::move(key)}; + } + + // 'setting::entry' implementation + + setting::entry::entry(setting const & setting, key key) + : m_settings{g_settings_new(g_settings_schema_get_id(setting.m_schema.get())), &g_object_unref} + , m_key{key.get()} + { + } + + setting::entry::value_type setting::entry::operator*() const + { + auto value = std::unique_ptr{g_settings_get_value(m_settings.get(), m_key.get().c_str()), &g_variant_unref}; + auto raw = value.get(); + + if (g_variant_is_of_type(raw, G_VARIANT_TYPE_BOOLEAN)) + { + return static_cast(g_variant_get_boolean(raw)); + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_INT32)) + { + return static_cast(g_variant_get_int32(raw)); + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_INT64)) + { + return static_cast(g_variant_get_int64(raw)); + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_UINT32)) + { + return static_cast(g_variant_get_uint32(raw)); + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_UINT64)) + { + return static_cast(g_variant_get_uint64(raw)); + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_DOUBLE)) + { + return static_cast(g_variant_get_double(raw)); + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_STRING)) + { + auto size = gsize{}; + auto string = g_variant_get_string(raw, &size); + return std::string{string, size}; + } + else if (g_variant_is_of_type(raw, G_VARIANT_TYPE_STRING_ARRAY)) + { + auto length = gsize{}; + auto data = g_variant_get_strv(raw, &length); + return std::vector{data, data + length}; + } + + return {}; + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/wallpaper.cpp b/src/wanda/wallpaper.cpp new file mode 100644 index 0000000..3fc93b3 --- /dev/null +++ b/src/wanda/wallpaper.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +namespace wanda +{ + void set_wallpaper(std::filesystem::path wallpaper) + { + using namespace wanda::literals; + using namespace wanda::std_ext; + using namespace std::string_literals; + + with("org.gnome.desktop.background"_setting, [&](auto & setting) { + with(setting["picture-uri"_key], [&](auto & value) { + value = "file://" + wallpaper.native(); + }) || + [&] { get_logger()->error("invalid settings key"); }; + }) || + [&] { get_logger()->error("invalid setting"); }; + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wanda/wandac.cpp b/src/wanda/wandac.cpp new file mode 100644 index 0000000..1d317db --- /dev/null +++ b/src/wanda/wandac.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +struct cli +{ + std::string command{}; + bool help{}; + + clara::Parser parser; + + auto parse(int argc, char const * const * argv, std::ostream & error) + { + parser = clara::Arg{command, "command"}("The command to send to the deamon").required() | + clara::Help(help); + + auto result = parser.parse(clara::Args{argc, argv}); + + if (!result) + { + error << "Error while processing command line arguments: " + << result.errorMessage() + << '\n' + << parser + << '\n'; + return false; + } + else if (command.empty()) + { + error << "Missing required argument 'command'\n" + << parser + << '\n'; + return false; + } + + return true; + } +}; + +struct listener : wanda::commander::listener +{ + listener(::cli & cli) + : m_cli{cli} + { + } + + void on_connected(wanda::commander & commander) override + { + if (m_cli.command == "change") + { + commander.send(wanda::make_change_command()); + commander.stop(); + } + } + +private: + ::cli & m_cli; +}; + +int main(int argc, char const * const * argv) +{ + auto cli = ::cli{}; + if (!cli.parse(argc, argv, std::cerr)) + { + return EXIT_FAILURE; + } + else if (cli.help) + { + std::cout << cli.parser << '\n'; + return EXIT_SUCCESS; + } + + wanda::initialize_logger(std::make_shared()); + + auto interface = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; + auto service = asio::io_service{}; + auto listener = ::listener{cli}; + + auto commander = wanda::commander{service, interface, listener}; + + wanda::get_logger()->info("trying to connect to wanda control interface on '{}'", interface.native()); + commander.start(); + + service.run(); +} \ No newline at end of file diff --git a/src/wanda/wandad.cpp b/src/wanda/wandad.cpp new file mode 100644 index 0000000..e39c116 --- /dev/null +++ b/src/wanda/wandad.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace +{ + constexpr auto image_filter = [](auto const & path) { + static auto const extensions = std::set{ + std::filesystem::path{".jpg"}, + std::filesystem::path{".png"}, + }; + + if (!std::filesystem::is_regular_file(path)) + { + return false; + } + + return extensions.find(path.extension()) != extensions.cend(); + }; + + struct listener : wanda::control_interface::listener + { + listener(std::vector const & wallpapers) + : m_wallpapers{wallpapers} + { + } + + void on_received(wanda::control_interface & interface, wanda::command command) override + { + switch (command.id) + { + case wanda::command_id::change: + { + auto wallpaper = wanda::random_pick(m_wallpapers); + wanda::get_logger()->info("changing wallpaper to '{}'", wallpaper.native()); + wanda::set_wallpaper(wallpaper); + break; + } + default: + wanda::get_logger()->error("received unknown command '{}'", static_cast(command.id)); + } + } + + private: + std::vector const & m_wallpapers; + }; + +} // namespace + +int main() +{ + using namespace wanda::std_ext; + + wanda::initialize_logger(std::make_shared()); + wanda::get_logger()->info("wanda is starting up"); + + with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [&](auto const & list) { + auto service = asio::io_service{}; + auto socket_path = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; + + wanda::get_logger()->info("starting control interface on '{}'", socket_path.native()); + auto listener = ::listener{list}; + auto interface = wanda::make_interface(service, socket_path, listener); + + if (!interface) + { + wanda::get_logger()->error("failed to start control interface"); + return; + } + + if (interface->start()) + { + return; + } + + auto signals = asio::signal_set{service, SIGINT}; + signals.async_wait([&](auto const & error, auto const signal) { + if (!error && signal == SIGINT) + { + interface->shutdown(); + service.stop(); + } + }); + + auto wallpaper = wanda::random_pick(list); + wanda::set_wallpaper(wallpaper); + + service.run(); + }) || [&] { wanda::get_logger()->error("wallpaper directory does not exist"); }; +} diff --git a/src/wanda/xdg.cpp b/src/wanda/xdg.cpp new file mode 100644 index 0000000..d49e53f --- /dev/null +++ b/src/wanda/xdg.cpp @@ -0,0 +1,46 @@ +#include + +#include + +namespace wanda +{ + std::string xdg_variable(xdg_directory directory) + { + switch (directory) + { + case xdg_directory::data_home: + return "XDG_DATA_HOME"; + case xdg_directory::config_home: + return "XDG_CONFIG_HOME"; + case xdg_directory::cache_home: + return "XDG_CACHE_HOME"; + case xdg_directory::runtime_dir: + return "XDG_RUNTIME_DIR"; + } + return "XDG_INVALID_PATH"; + } + + std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment) + { + if (auto path = environment[xdg_variable(directory)]; !path.empty()) + { + return path; + } + + auto home = std::filesystem::path{environment["HOME"]}; + switch (directory) + { + case xdg_directory::data_home: + return home / ".local/share"; + case xdg_directory::config_home: + return home / ".config"; + case xdg_directory::cache_home: + return home / ".cache"; + case xdg_directory::runtime_dir: + return std::filesystem::path{"/run/user"} / std::to_string(::getuid()); + } + + return ""; + } + +} // namespace wanda \ No newline at end of file diff --git a/src/wandac.cpp b/src/wandac.cpp deleted file mode 100644 index e8be1ef..0000000 --- a/src/wandac.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include "command.hpp" -#include "commander.hpp" -#include "environment.hpp" -#include "logging.hpp" -#include "xdg.hpp" - -#include -#include -#include -#include - -#include -#include -#include -#include - -struct cli -{ - std::string command{}; - bool help{}; - - clara::Parser parser; - - auto parse(int argc, char const * const * argv, std::ostream & error) - { - parser = clara::Arg{command, "command"}("The command to send to the deamon").required() | - clara::Help(help); - - auto result = parser.parse(clara::Args{argc, argv}); - - if (!result) - { - error << "Error while processing command line arguments: " - << result.errorMessage() - << '\n' - << parser - << '\n'; - return false; - } - else if (command.empty()) - { - error << "Missing required argument 'command'\n" - << parser - << '\n'; - return false; - } - - return true; - } -}; - -struct listener : wanda::commander::listener -{ - listener(::cli & cli) - : m_cli{cli} - { - } - - void on_connected(wanda::commander & commander) override - { - if (m_cli.command == "change") - { - commander.send(wanda::make_change_command()); - commander.stop(); - } - } - -private: - ::cli & m_cli; -}; - -int main(int argc, char const * const * argv) -{ - auto cli = ::cli{}; - if (!cli.parse(argc, argv, std::cerr)) - { - return EXIT_FAILURE; - } - else if (cli.help) - { - std::cout << cli.parser << '\n'; - return EXIT_SUCCESS; - } - - wanda::initialize_logger(std::make_shared()); - - auto interface = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; - auto service = asio::io_service{}; - auto listener = ::listener{cli}; - - auto commander = wanda::commander{service, interface, listener}; - - wanda::get_logger()->info("trying to connect to wanda control interface on '{}'", interface.native()); - commander.start(); - - service.run(); -} \ No newline at end of file diff --git a/src/wandad.cpp b/src/wandad.cpp deleted file mode 100644 index 7321f04..0000000 --- a/src/wandad.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "command.hpp" -#include "control_interface.hpp" -#include "environment.hpp" -#include "filesystem.hpp" -#include "logging.hpp" -#include "optional.hpp" -#include "setting.hpp" -#include "wallpaper.hpp" -#include "xdg.hpp" - -#include -#include - -#include -#include -#include - -namespace -{ - constexpr auto image_filter = [](auto const & path) { - static auto const extensions = std::set{ - std::filesystem::path{".jpg"}, - std::filesystem::path{".png"}, - }; - - if (!std::filesystem::is_regular_file(path)) - { - return false; - } - - return extensions.find(path.extension()) != extensions.cend(); - }; - - struct listener : wanda::control_interface::listener - { - listener(std::vector const & wallpapers) - : m_wallpapers{wallpapers} - { - } - - void on_received(wanda::control_interface & interface, wanda::command command) override - { - switch (command.id) - { - case wanda::command_id::change: - { - auto wallpaper = wanda::random_pick(m_wallpapers); - wanda::get_logger()->info("changing wallpaper to '{}'", wallpaper.native()); - wanda::set_wallpaper(wallpaper); - break; - } - default: - wanda::get_logger()->error("received unknown command '{}'", static_cast(command.id)); - } - } - - private: - std::vector const & m_wallpapers; - }; - -} // namespace - -int main() -{ - using namespace wanda::std_ext; - - wanda::initialize_logger(std::make_shared()); - wanda::get_logger()->info("wanda is starting up"); - - with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [&](auto const & list) { - auto service = asio::io_service{}; - auto socket_path = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; - - wanda::get_logger()->info("starting control interface on '{}'", socket_path.native()); - auto listener = ::listener{list}; - auto interface = wanda::make_interface(service, socket_path, listener); - - if (!interface) - { - wanda::get_logger()->error("failed to start control interface"); - return; - } - - if (interface->start()) - { - return; - } - - auto signals = asio::signal_set{service, SIGINT}; - signals.async_wait([&](auto const & error, auto const signal) { - if (!error && signal == SIGINT) - { - interface->shutdown(); - service.stop(); - } - }); - - auto wallpaper = wanda::random_pick(list); - wanda::set_wallpaper(wallpaper); - - service.run(); - }) || [&] { wanda::get_logger()->error("wallpaper directory does not exist"); }; -} diff --git a/src/xdg.cpp b/src/xdg.cpp deleted file mode 100644 index 10f3603..0000000 --- a/src/xdg.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "xdg.hpp" - -#include - -namespace wanda -{ - std::string xdg_variable(xdg_directory directory) - { - switch (directory) - { - case xdg_directory::data_home: - return "XDG_DATA_HOME"; - case xdg_directory::config_home: - return "XDG_CONFIG_HOME"; - case xdg_directory::cache_home: - return "XDG_CACHE_HOME"; - case xdg_directory::runtime_dir: - return "XDG_RUNTIME_DIR"; - } - return "XDG_INVALID_PATH"; - } - - std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment) - { - if (auto path = environment[xdg_variable(directory)]; !path.empty()) - { - return path; - } - - auto home = std::filesystem::path{environment["HOME"]}; - switch (directory) - { - case xdg_directory::data_home: - return home / ".local/share"; - case xdg_directory::config_home: - return home / ".config"; - case xdg_directory::cache_home: - return home / ".cache"; - case xdg_directory::runtime_dir: - return std::filesystem::path{"/run/user"} / std::to_string(::getuid()); - } - - return ""; - } - -} // namespace wanda \ No newline at end of file diff --git a/src/xdg.hpp b/src/xdg.hpp deleted file mode 100644 index 94dbf9b..0000000 --- a/src/xdg.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @file xdg.hpp - * @author Felix Morgner (felix.morgner@gmail.com) - * @since 1.0.0 - */ - -#ifndef WANDA_XDG_HPP -#define WANDA_XDG_HPP - -#include "environment.hpp" - -#include -#include -#include - -namespace wanda -{ - /** - * @brief An @p enum to represet the standardized XDG directories - */ - enum struct xdg_directory : std::underlying_type_t - { - data_home, - config_home, - cache_home, - runtime_dir, - }; - - /** - * @brief Get the name of the environment variable associated with the given XDG directory - */ - std::string xdg_variable(xdg_directory directory); - - /** - * @brief Get the path to the given @p directory given the provided @p environment - */ - std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment); -} // namespace wanda - -#endif \ No newline at end of file diff --git a/tests/core_driver.cpp b/tests/core_driver.cpp deleted file mode 100644 index 3643da0..0000000 --- a/tests/core_driver.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "test_suite_xdg.hpp" - -#include "cute/cute.h" -#include "cute/cute_runner.h" -#include "cute/tap_listener.h" - -#include -#include -#include -#include - -int main(int argc, char const *const *argv) -{ - auto listener = cute::tap_listener<>{std::cout}; - auto runner = cute::makeRunner(listener, argc, argv); - auto suites = std::vector>{wanda::test_suite_xdg()}; - return !all_of(cbegin(suites), cend(suites), [&](auto const &suite) { return runner(suite.first, suite.second.c_str()); }); -} \ No newline at end of file diff --git a/tests/test_suite_xdg.cpp b/tests/test_suite_xdg.cpp deleted file mode 100644 index 48f3eaa..0000000 --- a/tests/test_suite_xdg.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "test_suite_xdg.hpp" -#include "xdg.hpp" - -#include "cute/cute.h" - -#include - -#include - -namespace wanda -{ - -namespace -{ - char const * home_only_environment[2] = {"HOME=/home/cute"}; - char const * xdg_data_home_environment[2] = {"XDG_DATA_HOME=/home/cute/xdg_data_home"}; - char const * xdg_config_home_environment[2] = {"XDG_CONFIG_HOME=/home/cute/xdg_config_home"}; - char const * xdg_cache_home_environment[2] = {"XDG_CACHE_HOME=/home/cute/xdg_cache_home"}; - char const * xdg_runtime_dir_environment[2] = {"XDG_RUNTIME_DIR=/home/cute/xdg_runtime_dir"}; -} - -void test_xdg_variables() -{ - ASSERT_EQUAL("XDG_DATA_HOME", xdg_variable(xdg_directory::data_home)); - ASSERT_EQUAL("XDG_CONFIG_HOME", xdg_variable(xdg_directory::config_home)); - ASSERT_EQUAL("XDG_CACHE_HOME", xdg_variable(xdg_directory::cache_home)); - ASSERT_EQUAL("XDG_RUNTIME_DIR", xdg_variable(xdg_directory::runtime_dir)); -} - -void test_xdg_path_for_data_home_without_xdg_data_home_in_environment() -{ - auto env = environment{home_only_environment}; - ASSERT_EQUAL("/home/cute/.local/share", xdg_path_for(xdg_directory::data_home, env)); -} - -void test_xdg_path_for_data_home_with_xdg_data_home_in_environment() -{ - auto env = environment{xdg_data_home_environment}; - ASSERT_EQUAL("/home/cute/xdg_data_home", xdg_path_for(xdg_directory::data_home, env)); -} - -void test_xdg_path_for_config_home_without_xdg_config_home_in_environment() -{ - auto env = environment{home_only_environment}; - ASSERT_EQUAL("/home/cute/.config", xdg_path_for(xdg_directory::config_home, env)); -} - -void test_xdg_path_for_config_home_with_xdg_config_home_in_environment() -{ - auto env = environment{xdg_config_home_environment}; - ASSERT_EQUAL("/home/cute/xdg_config_home", xdg_path_for(xdg_directory::config_home, env)); -} - -void test_xdg_path_for_cache_home_without_xdg_cache_home_in_environment() -{ - auto env = environment{home_only_environment}; - ASSERT_EQUAL("/home/cute/.cache", xdg_path_for(xdg_directory::cache_home, env)); -} - -void test_xdg_path_for_cache_home_with_xdg_cache_home_in_environment() -{ - auto env = environment{xdg_cache_home_environment}; - ASSERT_EQUAL("/home/cute/xdg_cache_home", xdg_path_for(xdg_directory::cache_home, env)); -} - -void test_xdg_path_for_runtime_dir_without_xdg_runtime_dir_in_environment() -{ - auto env = environment{home_only_environment}; - auto expected = std::filesystem::path{"/run/user"} / std::to_string(::getuid()); - ASSERT_EQUAL(expected, xdg_path_for(xdg_directory::runtime_dir, env)); -} - -void test_xdg_path_for_runtime_dir_with_xdg_runtime_dir_in_environment() -{ - auto env = environment{xdg_runtime_dir_environment}; - ASSERT_EQUAL("/home/cute/xdg_runtime_dir", xdg_path_for(xdg_directory::runtime_dir, env)); -} - -std::pair test_suite_xdg() -{ - return std::make_pair(cute::suite{ - CUTE(test_xdg_variables), - CUTE(test_xdg_path_for_data_home_without_xdg_data_home_in_environment), - CUTE(test_xdg_path_for_data_home_with_xdg_data_home_in_environment), - CUTE(test_xdg_path_for_config_home_without_xdg_config_home_in_environment), - CUTE(test_xdg_path_for_config_home_with_xdg_config_home_in_environment), - CUTE(test_xdg_path_for_cache_home_without_xdg_cache_home_in_environment), - CUTE(test_xdg_path_for_cache_home_with_xdg_cache_home_in_environment), - CUTE(test_xdg_path_for_runtime_dir_without_xdg_runtime_dir_in_environment), - CUTE(test_xdg_path_for_runtime_dir_with_xdg_runtime_dir_in_environment), - }, - "XDG Utilities"); -} - -} // namespace wanda \ No newline at end of file diff --git a/tests/test_suite_xdg.hpp b/tests/test_suite_xdg.hpp deleted file mode 100644 index 698ad9a..0000000 --- a/tests/test_suite_xdg.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef WANDA_TEST_SUITE_XDG_HPP -#define WANDA_TEST_SUITE_XDG_HPP - -#include "cute/cute.h" - -#include -#include - -namespace wanda -{ - -std::pair test_suite_xdg(); - -} // namespace wanda - -#endif \ No newline at end of file diff --git a/tests/wanda/core_driver.cpp b/tests/wanda/core_driver.cpp new file mode 100644 index 0000000..3643da0 --- /dev/null +++ b/tests/wanda/core_driver.cpp @@ -0,0 +1,18 @@ +#include "test_suite_xdg.hpp" + +#include "cute/cute.h" +#include "cute/cute_runner.h" +#include "cute/tap_listener.h" + +#include +#include +#include +#include + +int main(int argc, char const *const *argv) +{ + auto listener = cute::tap_listener<>{std::cout}; + auto runner = cute::makeRunner(listener, argc, argv); + auto suites = std::vector>{wanda::test_suite_xdg()}; + return !all_of(cbegin(suites), cend(suites), [&](auto const &suite) { return runner(suite.first, suite.second.c_str()); }); +} \ No newline at end of file diff --git a/tests/wanda/test_suite_xdg.cpp b/tests/wanda/test_suite_xdg.cpp new file mode 100644 index 0000000..0338ee3 --- /dev/null +++ b/tests/wanda/test_suite_xdg.cpp @@ -0,0 +1,95 @@ +#include "test_suite_xdg.hpp" +#include + +#include "cute/cute.h" + +#include + +#include + +namespace wanda +{ + +namespace +{ + char const * home_only_environment[2] = {"HOME=/home/cute"}; + char const * xdg_data_home_environment[2] = {"XDG_DATA_HOME=/home/cute/xdg_data_home"}; + char const * xdg_config_home_environment[2] = {"XDG_CONFIG_HOME=/home/cute/xdg_config_home"}; + char const * xdg_cache_home_environment[2] = {"XDG_CACHE_HOME=/home/cute/xdg_cache_home"}; + char const * xdg_runtime_dir_environment[2] = {"XDG_RUNTIME_DIR=/home/cute/xdg_runtime_dir"}; +} + +void test_xdg_variables() +{ + ASSERT_EQUAL("XDG_DATA_HOME", xdg_variable(xdg_directory::data_home)); + ASSERT_EQUAL("XDG_CONFIG_HOME", xdg_variable(xdg_directory::config_home)); + ASSERT_EQUAL("XDG_CACHE_HOME", xdg_variable(xdg_directory::cache_home)); + ASSERT_EQUAL("XDG_RUNTIME_DIR", xdg_variable(xdg_directory::runtime_dir)); +} + +void test_xdg_path_for_data_home_without_xdg_data_home_in_environment() +{ + auto env = environment{home_only_environment}; + ASSERT_EQUAL("/home/cute/.local/share", xdg_path_for(xdg_directory::data_home, env)); +} + +void test_xdg_path_for_data_home_with_xdg_data_home_in_environment() +{ + auto env = environment{xdg_data_home_environment}; + ASSERT_EQUAL("/home/cute/xdg_data_home", xdg_path_for(xdg_directory::data_home, env)); +} + +void test_xdg_path_for_config_home_without_xdg_config_home_in_environment() +{ + auto env = environment{home_only_environment}; + ASSERT_EQUAL("/home/cute/.config", xdg_path_for(xdg_directory::config_home, env)); +} + +void test_xdg_path_for_config_home_with_xdg_config_home_in_environment() +{ + auto env = environment{xdg_config_home_environment}; + ASSERT_EQUAL("/home/cute/xdg_config_home", xdg_path_for(xdg_directory::config_home, env)); +} + +void test_xdg_path_for_cache_home_without_xdg_cache_home_in_environment() +{ + auto env = environment{home_only_environment}; + ASSERT_EQUAL("/home/cute/.cache", xdg_path_for(xdg_directory::cache_home, env)); +} + +void test_xdg_path_for_cache_home_with_xdg_cache_home_in_environment() +{ + auto env = environment{xdg_cache_home_environment}; + ASSERT_EQUAL("/home/cute/xdg_cache_home", xdg_path_for(xdg_directory::cache_home, env)); +} + +void test_xdg_path_for_runtime_dir_without_xdg_runtime_dir_in_environment() +{ + auto env = environment{home_only_environment}; + auto expected = std::filesystem::path{"/run/user"} / std::to_string(::getuid()); + ASSERT_EQUAL(expected, xdg_path_for(xdg_directory::runtime_dir, env)); +} + +void test_xdg_path_for_runtime_dir_with_xdg_runtime_dir_in_environment() +{ + auto env = environment{xdg_runtime_dir_environment}; + ASSERT_EQUAL("/home/cute/xdg_runtime_dir", xdg_path_for(xdg_directory::runtime_dir, env)); +} + +std::pair test_suite_xdg() +{ + return std::make_pair(cute::suite{ + CUTE(test_xdg_variables), + CUTE(test_xdg_path_for_data_home_without_xdg_data_home_in_environment), + CUTE(test_xdg_path_for_data_home_with_xdg_data_home_in_environment), + CUTE(test_xdg_path_for_config_home_without_xdg_config_home_in_environment), + CUTE(test_xdg_path_for_config_home_with_xdg_config_home_in_environment), + CUTE(test_xdg_path_for_cache_home_without_xdg_cache_home_in_environment), + CUTE(test_xdg_path_for_cache_home_with_xdg_cache_home_in_environment), + CUTE(test_xdg_path_for_runtime_dir_without_xdg_runtime_dir_in_environment), + CUTE(test_xdg_path_for_runtime_dir_with_xdg_runtime_dir_in_environment), + }, + "XDG Utilities"); +} + +} // namespace wanda \ No newline at end of file diff --git a/tests/wanda/test_suite_xdg.hpp b/tests/wanda/test_suite_xdg.hpp new file mode 100644 index 0000000..698ad9a --- /dev/null +++ b/tests/wanda/test_suite_xdg.hpp @@ -0,0 +1,16 @@ +#ifndef WANDA_TEST_SUITE_XDG_HPP +#define WANDA_TEST_SUITE_XDG_HPP + +#include "cute/cute.h" + +#include +#include + +namespace wanda +{ + +std::pair test_suite_xdg(); + +} // namespace wanda + +#endif \ No newline at end of file -- cgit v1.2.3