From 0c1d1a0f24655c22a373c19aac90a6a225a353e6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 8 Dec 2018 10:43:50 +0100 Subject: wanda: reformat source code --- src/command.cpp | 39 ++++++----- src/command.hpp | 18 +++--- src/commander.cpp | 98 ++++++++++++++-------------- src/commander.hpp | 51 +++++++-------- src/control_connection.cpp | 153 ++++++++++++++++++++++--------------------- src/control_connection.hpp | 29 ++++----- src/control_interface.cpp | 158 ++++++++++++++++++++++----------------------- src/control_interface.hpp | 28 ++++---- src/deferred_failure.hpp | 6 +- src/environment.cpp | 75 +++++++++++---------- src/environment.hpp | 15 ++--- src/expected.hpp | 144 +++++++++++++++++++++++------------------ src/filesystem.cpp | 17 +++-- src/filesystem.hpp | 21 +++--- src/keyed.hpp | 11 ++-- src/message.cpp | 55 ++++++++-------- src/message.hpp | 29 ++++----- src/optional.hpp | 34 +++++----- src/setting.cpp | 80 +++++++++++------------ src/setting.hpp | 134 +++++++++++++++++++------------------- src/type_wrapper.hpp | 19 +++--- src/wallpaper.cpp | 23 +++---- src/wallpaper.hpp | 9 +-- src/wandac.cpp | 130 ++++++++++++++++++------------------- src/wandad.cpp | 77 +++++++++++----------- src/xdg.cpp | 33 +++++----- src/xdg.hpp | 12 ++-- 27 files changed, 749 insertions(+), 749 deletions(-) (limited to 'src') diff --git a/src/command.cpp b/src/command.cpp index 6831d5e..76cdee7 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -2,47 +2,46 @@ namespace wanda { - -std::optional command::message() const -{ + std::optional command::message() const + { using namespace std::string_literals; auto const command = [this] { - switch (id) - { + switch (id) + { case command_id::change: - return "CHANGE"s; + return "CHANGE"s; default: - return ""s; - } + return ""s; + } }(); auto argument_string = std::string{}; for (int index = 0ul; index < arguments.size(); ++index) { - argument_string += (index) ? "," + arguments[index] : arguments[index]; + argument_string += (index) ? "," + arguments[index] : arguments[index]; } if (command.empty()) { - return std::nullopt; + return std::nullopt; } return wanda::message{"C", command, argument_string}; -} + } -std::optional make_command(message message) -{ - if(message.command == "CHANGE") + std::optional make_command(message message) + { + if (message.command == "CHANGE") { - return {{command_id::change}}; + return {{command_id::change}}; } return std::nullopt; -} + } -command make_change_command() -{ + command make_change_command() + { return {command_id::change}; -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/command.hpp b/src/command.hpp index 5fac065..37afaae 100644 --- a/src/command.hpp +++ b/src/command.hpp @@ -9,23 +9,23 @@ namespace wanda { -enum struct command_id : char -{ + enum struct command_id : char + { change, -}; + }; -struct command -{ + struct command + { command_id const id; std::vector const arguments; std::optional message() const; -}; + }; -std::optional make_command(message message); + std::optional make_command(message message); -command make_change_command(); + command make_change_command(); -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/commander.cpp b/src/commander.cpp index e2bf8c7..36d9fba 100644 --- a/src/commander.cpp +++ b/src/commander.cpp @@ -6,74 +6,72 @@ namespace wanda { + commander::commander(asio::io_service & service, std::filesystem::path socket, listener & listener, std::shared_ptr logger) + : m_service{service} + , m_endpoint{socket.string()} + , m_socket{service} + , m_listener{listener} + , m_logger{logger} + { + } -commander::commander(asio::io_service &service, std::filesystem::path socket, listener &listener, std::shared_ptr logger) - : m_service{service}, - m_endpoint{socket.string()}, - m_socket{service}, - m_listener{listener}, - m_logger{logger} -{ -} - -void commander::start() -{ - m_socket.async_connect(m_endpoint, [&](auto const &error) { - if (error) - { - m_logger->error("error while connecting to control interface: '{}'", error.message()); - } - else - { - m_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::start() + { + m_socket.async_connect(m_endpoint, [&](auto const & error) { + if (error) + { + m_logger->error("error while connecting to control interface: '{}'", error.message()); + } + else + { + m_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() -{ + void commander::stop() + { m_logger->info("closing control connection"); m_connection->close(); -} + } -void commander::send(command command) -{ + void commander::send(command command) + { using namespace wanda::std_ext; - + if (!m_connection || m_connection->current_state() != control_connection::state::established) { - m_logger->error("tried to send command without an established connection"); - m_listener.on_error(*this, "tried to send command without an established connection"); - return; + m_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); - }) || [&]{ m_logger->error("unknown command"); }; -} + with(command.message(), [&](auto const & message) { m_connection->send(message); }) || + [&] { m_logger->error("unknown command"); }; + } -void commander::on_error(control_connection::pointer connection, std::error_code error) -{ + void commander::on_error(control_connection::pointer connection, std::error_code error) + { m_logger->error("control interface communication error: '{}'", error.message()); -} + } -void commander::on_received(wanda::control_connection::pointer connection, message 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) { - m_logger->info("connection to wanda deamon successfully established"); - connection->update(control_connection::state::established); - m_listener.on_connected(*this); + m_logger->info("connection to wanda deamon successfully established"); + connection->update(control_connection::state::established); + m_listener.on_connected(*this); } else { - m_logger->error("unexpected message: '{}'", message); - m_listener.on_error(*this, "unexpected message '" + static_cast(message) + '\''); + m_logger->error("unexpected message: '{}'", message); + m_listener.on_error(*this, "unexpected message '" + static_cast(message) + '\''); } -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/commander.hpp b/src/commander.hpp index 980573f..122b07d 100644 --- a/src/commander.hpp +++ b/src/commander.hpp @@ -16,33 +16,32 @@ namespace wanda { - -struct commander : wanda::control_connection::listener -{ - struct listener + struct commander : wanda::control_connection::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) { }; + 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){}; + }; + + commander(asio::io_service & service, std::filesystem::path socket, listener & listener, std::shared_ptr logger); + + void start(); + void stop(); + 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; + std::shared_ptr m_logger; }; - commander(asio::io_service &service, std::filesystem::path socket, listener & listener, std::shared_ptr logger); - - void start(); - void stop(); - 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; - std::shared_ptr m_logger; -}; - -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/control_connection.cpp b/src/control_connection.cpp index 1061f44..13e0e5e 100644 --- a/src/control_connection.cpp +++ b/src/control_connection.cpp @@ -4,118 +4,117 @@ namespace wanda { - -control_connection::pointer make_control_connection(control_connection::protocol::socket &&socket) -{ + 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)} -{ -} + 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) -{ + bool control_connection::add(listener * listener) + { auto [_, inserted] = m_listeners.insert(listener); return inserted; -} + } -bool control_connection::remove(listener * listener) -{ + bool control_connection::remove(listener * listener) + { return m_listeners.erase(listener); -} + } -void control_connection::start() -{ + void control_connection::start() + { if (m_state == state::unknown) { - m_state = state::fresh; - perform_read(); + m_state = state::fresh; + perform_read(); } -} + } -void control_connection::send(message message) -{ + 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); - } + 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() -{ + 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); - } + 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_error(shared_from_this(), error); + } } - for (auto &listener : m_listeners) + for (auto & listener : m_listeners) { - listener->on_close(shared_from_this()); + listener->on_close(shared_from_this()); } m_listeners.clear(); -} + } -void control_connection::update(state state) -{ + void control_connection::update(state state) + { m_state = state; -} + } -control_connection::state control_connection::current_state() const -{ + 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) + 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) { - for (auto &listener : m_listeners) - { - listener->on_error(shared_from_this(), error); - } - close(); + 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 { - 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(); + for (auto & listener : m_listeners) + { + listener->on_received(shared_from_this(), msg); + } } + perform_read(); + } }); -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/control_connection.hpp b/src/control_connection.hpp index bd3db9d..8ed60f0 100644 --- a/src/control_connection.hpp +++ b/src/control_connection.hpp @@ -15,24 +15,23 @@ namespace wanda { - -struct control_connection : keyed, std::enable_shared_from_this -{ + struct control_connection : keyed, std::enable_shared_from_this + { using protocol = asio::local::stream_protocol; using pointer = std::shared_ptr; 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) {} + virtual void on_close(pointer connection) {} + virtual void on_received(pointer connection, message message) {} + virtual void on_error(pointer connection, std::error_code) {} }; enum struct state : std::underlying_type_t { - unknown, - fresh, - established, + unknown, + fresh, + established, }; /** @@ -48,14 +47,14 @@ struct control_connection : keyed, std::enable_shared_from_t * * @returns true iff. the listener was not already in the listener set */ - bool add(listener *listener); + 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); + bool remove(listener * listener); /** * @brief Start I/O processing for this control connection @@ -74,7 +73,7 @@ struct control_connection : keyed, std::enable_shared_from_t state current_state() const; private: - friend pointer make_control_connection(protocol::socket &&socket); + friend pointer make_control_connection(protocol::socket && socket); void perform_read(); @@ -85,10 +84,10 @@ struct control_connection : keyed, std::enable_shared_from_t std::ostream m_output{&m_out}; std::set m_listeners{}; state m_state{}; -}; + }; -control_connection::pointer make_control_connection(control_connection::protocol::socket &&socket); + control_connection::pointer make_control_connection(control_connection::protocol::socket && socket); -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/control_interface.cpp b/src/control_interface.cpp index 0a98553..c847f94 100644 --- a/src/control_interface.cpp +++ b/src/control_interface.cpp @@ -13,142 +13,142 @@ namespace wanda { + // 'socket_deleter' implementation -// 'socket_deleter' implementation - -socket_deleter::~socket_deleter() -{ + socket_deleter::~socket_deleter() + { if (std::filesystem::exists(path)) { - std::filesystem::remove(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, std::shared_ptr logger) - : keyed{key}, - m_service{service}, - m_endpoint{std::move(endpoint)}, - m_socket{m_service}, - m_acceptor{m_service}, - m_listener{listener}, - m_logger{logger} -{ -} - -std::error_code control_interface::start() -{ + } + + // 'control_interface' implementation + + control_interface::control_interface(control_interface::key key, asio::io_service & service, control_interface::protocol::endpoint endpoint, listener & listener, std::shared_ptr logger) + : keyed{key} + , m_service{service} + , m_endpoint{std::move(endpoint)} + , m_socket{m_service} + , m_acceptor{m_service} + , m_listener{listener} + , m_logger{logger} + { + } + + std::error_code control_interface::start() + { if (auto error = std::error_code{}; m_acceptor.open(m_endpoint.protocol(), error)) { - return error; + return error; } if (auto error = std::error_code{}; m_acceptor.bind(m_endpoint, error)) { - return error; + return error; } if (auto error = std::error_code{}; m_acceptor.listen(128, error)) { - return error; + return error; } else { - perform_accept(); - return error; + perform_accept(); + return error; } -} + } -std::error_code control_interface::shutdown() -{ - for (auto &connection : m_connections) + std::error_code control_interface::shutdown() + { + for (auto & connection : m_connections) { - connection->close(); + 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) + } + + 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) + { + m_logger->error("failed to accept connection because '{}'", error); + } + else + { + m_logger->info("new incoming controller connection"); + auto [connection, inserted] = m_connections.insert(make_control_connection(std::move(m_socket))); + if (inserted) { - m_logger->error("failed to accept connection because '{}'", error); - } - else - { - m_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(); + (*connection)->add(this); + (*connection)->start(); } + perform_accept(); + } }); -} + } -void control_interface::on_close(control_connection::pointer connection) -{ + void control_interface::on_close(control_connection::pointer connection) + { if (static_cast(connection->current_state()) >= static_cast(control_connection::state::established)) { - m_logger->info("controller connection closed"); + m_logger->info("controller connection closed"); } else { - m_logger->info("controller connection aborted before it could be established"); + m_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) -{ + void control_interface::on_received(control_connection::pointer connection, message message) + { using namespace wanda::std_ext; if (m_connections.find(connection) == m_connections.cend()) { - m_logger->error("received message from an unknown connection"); - return; + m_logger->error("received message from an unknown connection"); + return; } if (message.source != message_source_controller) { - m_logger->error("received a deamon message"); - return; + m_logger->error("received a deamon message"); + return; } if (auto state = connection->current_state(); message.command == message_command_hello && state == control_connection::state::fresh) { - m_logger->info("controller connection established"); - if (message.argument.has_value()) - { - m_logger->info("remote controller version '{}'", *message.argument); - } - connection->send({message_source_daemon, message_command_hello, message_argument_hello}); - connection->update(control_connection::state::established); + m_logger->info("controller connection established"); + if (message.argument.has_value()) + { + m_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); - }) || [&] { m_logger->warn("ignoring unknown message '{}'", message); }; + with(make_command(message), [&](auto const & command) { + m_listener.on_received(*this, command); + }) || + [&] { m_logger->warn("ignoring unknown message '{}'", message); }; } -} + } -control_interface::pointer make_interface(asio::io_service &service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr logger) -{ + control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr logger) + { if (std::filesystem::exists(file)) { - logger->error("file '{}' exists", file.native()); - return {}; + logger->error("file '{}' exists", file.native()); + return {}; } control_interface::protocol::endpoint endpoint{file.string()}; return std::make_shared(control_interface::key{}, service, std::move(endpoint), listener, logger); -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/control_interface.hpp b/src/control_interface.hpp index 2ac32be..9dc461c 100644 --- a/src/control_interface.hpp +++ b/src/control_interface.hpp @@ -6,7 +6,6 @@ #include "keyed.hpp" #include - #include #include @@ -20,25 +19,24 @@ namespace wanda { - -struct socket_deleter -{ + struct socket_deleter + { ~socket_deleter(); std::filesystem::path path; -}; + }; -struct control_interface : control_connection::listener, keyed, std::enable_shared_from_this -{ + struct control_interface : control_connection::listener, keyed, std::enable_shared_from_this + { using protocol = asio::local::stream_protocol; using pointer = std::shared_ptr; - struct listener + struct listener { - virtual void on_received(control_interface & interface, command command) { }; + virtual void on_received(control_interface & interface, command command){}; }; - control_interface(key, asio::io_service &service, protocol::endpoint endpoint, listener & listener, std::shared_ptr logger); + control_interface(key, asio::io_service & service, protocol::endpoint endpoint, listener & listener, std::shared_ptr logger); std::error_code start(); std::error_code shutdown(); @@ -49,9 +47,9 @@ struct control_interface : control_connection::listener, keyed logger); + friend pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr logger); - asio::io_service &m_service; + asio::io_service & m_service; protocol::endpoint m_endpoint; protocol::socket m_socket; protocol::acceptor m_acceptor; @@ -59,10 +57,10 @@ struct control_interface : control_connection::listener, keyed m_connections; std::shared_ptr m_logger; -}; + }; -control_interface::pointer make_interface(asio::io_service &service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr logger); + control_interface::pointer make_interface(asio::io_service & service, std::filesystem::path file, control_interface::listener & listener, std::shared_ptr logger); -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/deferred_failure.hpp b/src/deferred_failure.hpp index 54adb97..8039286 100644 --- a/src/deferred_failure.hpp +++ b/src/deferred_failure.hpp @@ -11,8 +11,8 @@ namespace { -template -using deferred_failure = std::false_type; -} // namespace + 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 index e5c8c6d..abcf62a 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -4,69 +4,68 @@ namespace wanda { - -environment::environment(char const *const *env) -{ + environment::environment(char const * const * env) + { if (!env) { - return; + 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); - } + 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) -{ + std::string & environment::operator[](std::string const & variable) + { return m_cache[variable]; -} + } -std::string const & environment::operator[](std::string const & variable) const -{ + std::string const & environment::operator[](std::string const & variable) const + { static std::string const empty{}; - if(auto needle = m_cache.find(variable); needle != cend()) + if (auto needle = m_cache.find(variable); needle != cend()) { - return needle->second; + return needle->second; } return empty; -} + } -environment::iterator environment::begin() -{ + environment::iterator environment::begin() + { return m_cache.begin(); -} + } -environment::const_iterator environment::begin() const -{ + environment::const_iterator environment::begin() const + { return m_cache.begin(); -} + } -environment::const_iterator environment::cbegin() const -{ + environment::const_iterator environment::cbegin() const + { return m_cache.cbegin(); -} + } -environment::iterator environment::end() -{ + environment::iterator environment::end() + { return m_cache.end(); -} + } -environment::const_iterator environment::end() const -{ + environment::const_iterator environment::end() const + { return m_cache.end(); -} + } -environment::const_iterator environment::cend() const -{ + environment::const_iterator environment::cend() const + { return m_cache.cend(); -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/environment.hpp b/src/environment.hpp index a6322cb..1b5bd80 100644 --- a/src/environment.hpp +++ b/src/environment.hpp @@ -8,19 +8,18 @@ namespace wanda { - -struct 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; - explicit environment(char const *const *env = ::environ); + explicit environment(char const * const * env = ::environ); - std::string &operator[](std::string const &variable); - std::string const &operator[](std::string const &variable) const; + std::string & operator[](std::string const & variable); + std::string const & operator[](std::string const & variable) const; iterator begin(); const_iterator begin() const; @@ -32,8 +31,8 @@ struct environment private: map_type m_cache{}; -}; + }; -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/expected.hpp b/src/expected.hpp index 8cacefe..4555d92 100644 --- a/src/expected.hpp +++ b/src/expected.hpp @@ -7,10 +7,9 @@ namespace wanda { - -template -struct unexpected -{ + 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!"); @@ -27,32 +26,41 @@ struct unexpected /** * @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)...) {} + 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 < + 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)...) {} + 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 < + 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)) {} + 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 < + template< typename Err, std::enable_if_t && @@ -65,12 +73,15 @@ struct unexpected !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) {} + 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 < + template< typename Err, std::enable_if_t && @@ -83,12 +94,15 @@ struct unexpected !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) {} + 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 < + template< typename Err, std::enable_if_t && @@ -101,12 +115,15 @@ struct unexpected !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)) {} + 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 < + template< typename Err, std::enable_if_t && @@ -119,109 +136,112 @@ struct unexpected !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)) {} + 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 & + constexpr ErrorType const & value() const & { - return m_error; + return m_error; } /** * @brief Get the error value contained in this @p unexpected instance */ - constexpr ErrorType &value() & + constexpr ErrorType & value() & { - return m_error; + return m_error; } /** * @brief Get the error value contained in this @p unexpected instance */ - constexpr ErrorType &&value() && + constexpr ErrorType && value() && { - return std::move(m_error); + return std::move(m_error); } /** * @brief Get the error value contained in this @p unexpected instance */ - constexpr ErrorType const &&value() const && + constexpr ErrorType const && value() const && { - return std::move(m_error); + 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) + void swap(unexpected & other) noexcept(std::is_nothrow_swappable_v) { - using std::swap; - swap(m_error, other.m_error); + 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 - friend constexpr bool operator!=(unexpected const &lhs, unexpected const &rhs); + template + friend constexpr bool operator!=(unexpected const & lhs, unexpected const & rhs); - template < + template< typename Err, std::enable_if_t> *> - friend void swap(unexpected &lhs, unexpected &rhs); + friend void swap(unexpected & lhs, unexpected & rhs); private: ErrorType m_error; -}; + }; -template -unexpected(ErrorType)->unexpected; + template + unexpected(ErrorType)->unexpected; -/** + /** * @brief Compare two @p unexpected instances for equality */ -template -constexpr bool operator==(unexpected const &lhs, unexpected const &rhs) -{ + 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) -{ + 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) -{ + 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 -{ + struct unexpect_t + { explicit unexpect_t() = default; -}; + }; -/** + /** * @brief A tap for @p unexpected */ -inline constexpr unexpect_t unexpect{}; + inline constexpr unexpect_t unexpect{}; -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 4dfa183..bb724d1 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -6,25 +6,24 @@ namespace wanda { - -std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) -{ + std::optional scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) + { if (!std::filesystem::is_directory(source)) { - return std::nullopt; + 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) -{ + 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 +} // namespace wanda \ No newline at end of file diff --git a/src/filesystem.hpp b/src/filesystem.hpp index bcc9e5e..5b2334a 100644 --- a/src/filesystem.hpp +++ b/src/filesystem.hpp @@ -7,29 +7,28 @@ namespace wanda { - -/** + /** * @brief Covenience alias for path lists */ -using path_list = std::vector; + 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) { + 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); + 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); + std::filesystem::path random_pick(path_list const & paths); -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/keyed.hpp b/src/keyed.hpp index de3500e..d8b22b8 100644 --- a/src/keyed.hpp +++ b/src/keyed.hpp @@ -3,18 +3,17 @@ namespace wanda { - -template -struct keyed -{ + template + struct keyed + { protected: struct key { }; explicit keyed(key) {} -}; + }; -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/message.cpp b/src/message.cpp index 47a4414..4c152fa 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -1,59 +1,58 @@ #include "message.hpp" -#include #include +#include #include namespace wanda { - -message::operator std::string() const -{ + message::operator std::string() const + { std::ostringstream buffer{}; buffer << source << ':' << command; - if(argument.has_value()) + if (argument.has_value()) { - buffer << ':' << *argument; + buffer << ':' << *argument; } return buffer.str(); -} + } -std::size_t message::size() const -{ + std::size_t message::size() const + { return static_cast(*this).size(); -} + } -template -OutputIt copy_until(InputIt first, InputIt last, OutputIt out, UnaryPredicate predicate) -{ + template + OutputIt copy_until(InputIt first, InputIt last, OutputIt out, UnaryPredicate predicate) + { while (first != last && !predicate(*first)) { - *out++ = *first++; + *out++ = *first++; } return out; -} + } -std::istream &operator>>(std::istream &in, message &message) -{ + 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 == ':'; }); + 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; + 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 == ':'; }); + copy_until(++pos, end, std::back_inserter(buffer), [](auto const & c) { return c == ':'; }); if (in.eof()) { - in.setstate(std::ios_base::failbit); + in.setstate(std::ios_base::failbit); } message.command = buffer; @@ -61,16 +60,16 @@ std::istream &operator>>(std::istream &in, message &message) copy(++pos, end, std::back_inserter(buffer)); if (buffer.size()) { - message.argument = std::optional{std::move(buffer)}; + 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) -{ + std::ostream & operator<<(std::ostream & out, message const & message) + { return out << static_cast(message); -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/message.hpp b/src/message.hpp index a834b15..86a455c 100644 --- a/src/message.hpp +++ b/src/message.hpp @@ -3,24 +3,23 @@ #include #include -#include #include +#include namespace wanda { + inline namespace v1 + { + auto constexpr message_argument_hello = "1.0.0"; + } -inline namespace v1 -{ -auto constexpr message_argument_hello = "1.0.0"; -} + auto constexpr message_source_controller = "C"; + auto constexpr message_source_daemon = "D"; -auto constexpr message_source_controller = "C"; -auto constexpr message_source_daemon = "D"; + auto constexpr message_command_hello = "HELLO"; -auto constexpr message_command_hello = "HELLO"; - -struct message -{ + struct message + { explicit operator std::string() const; std::size_t size() const; @@ -28,11 +27,11 @@ struct message std::string source; std::string command; std::optional argument; -}; + }; -std::istream &operator>>(std::istream &in, message &message); -std::ostream &operator<<(std::ostream &out, message const &message); + std::istream & operator>>(std::istream & in, message & message); + std::ostream & operator<<(std::ostream & out, message const & message); -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/optional.hpp b/src/optional.hpp index 5e1c630..3c964db 100644 --- a/src/optional.hpp +++ b/src/optional.hpp @@ -11,37 +11,37 @@ namespace wanda::std_ext { - -struct failable -{ + struct failable + { constexpr static auto success() { return failable{false}; } constexpr static auto failure() { return failable{true}; } - template + template constexpr auto operator||(Handler handler) const { - if (m_failed) - { - handler(); - } + if (m_failed) + { + handler(); + } } private: - constexpr explicit failable(bool failed) : m_failed{failed} {}; + constexpr explicit failable(bool failed) + : m_failed{failed} {}; bool const m_failed; -}; + }; -template -auto with(std::optional &&object, HandlerType handler) -{ + template + auto with(std::optional && object, HandlerType handler) + { if (object) { - handler(object.value()); - return failable::success(); + handler(object.value()); + return failable::success(); } return failable::failure(); -} + } -} // namespace wanda::std_ext +} // namespace wanda::std_ext #endif \ No newline at end of file diff --git a/src/setting.cpp b/src/setting.cpp index 39ae248..d9f8607 100644 --- a/src/setting.cpp +++ b/src/setting.cpp @@ -5,98 +5,98 @@ namespace wanda { + // UDL implementations -// UDL implementations - -key literals::operator""_key(char const *str, std::size_t len) -{ + 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) -{ + 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; + return std::nullopt; } auto schema = g_settings_schema_source_lookup(source, str, true); if (!schema) { - return std::nullopt; + return std::nullopt; } return setting{schema}; -} + } -// 'setting' implementation + // 'setting' implementation -setting::setting(GSettingsSchema *schema) - : m_schema{schema, &g_settings_schema_unref} -{ -} + setting::setting(GSettingsSchema * schema) + : m_schema{schema, &g_settings_schema_unref} + { + } -std::optional setting::operator[](key key) const -{ + std::optional setting::operator[](key key) const + { if (!g_settings_schema_has_key(m_schema.get(), key.get().c_str())) { - return std::nullopt; + return std::nullopt; } return setting::entry{*this, std::move(key)}; -} + } -// 'setting::entry' implementation + // '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::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 -{ + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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}; + 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}; + 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 +} // namespace wanda \ No newline at end of file diff --git a/src/setting.hpp b/src/setting.hpp index 6e10a8a..997109a 100644 --- a/src/setting.hpp +++ b/src/setting.hpp @@ -23,46 +23,44 @@ namespace wanda { + struct setting; -struct setting; - -/** - * @brief A convenience type to represent setting keys - */ -using key = type_wrapper; + /** + * @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); + 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 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 + /** + * @brief A simple wrapper for GSettings Schemas + */ + struct setting { + struct entry + { + using value_type = std::variant>; - using value_type = std::variant>; - - value_type operator*() const; + value_type operator*() const; - template - bool operator=(Type value) - { - struct setting_applier + template + bool operator=(Type value) { - setting_applier(GSettings *setting, gchar const *key, Type value) noexcept - : m_result{[&] { + 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); @@ -94,7 +92,7 @@ struct setting 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(); }); + 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 @@ -102,53 +100,53 @@ struct setting static_assert(deferred_failure{}, "Invalid argument type!"); } }()} - { - } + { + } - ~setting_applier() - { - g_settings_sync(); - } + ~setting_applier() + { + g_settings_sync(); + } - operator bool() const - { - return m_result; - } + operator bool() const + { + return m_result; + } - private: - gboolean const m_result; - }; + private: + gboolean const m_result; + }; - return setting_applier{m_settings.get(), m_key.get().c_str(), value}; - } + return setting_applier{m_settings.get(), m_key.get().c_str(), value}; + } - private: - entry(setting const &schema, key key); + private: + entry(setting const & schema, key key); - std::unique_ptr m_settings; + std::unique_ptr m_settings; - key m_key; + key m_key; - friend setting; - }; + 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; + /** + * @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); + private: + explicit setting(GSettingsSchema * schema); - std::unique_ptr m_schema; + std::unique_ptr m_schema; - friend std::optional literals::operator""_setting(char const *, std::size_t); -}; + friend std::optional literals::operator""_setting(char const *, std::size_t); + }; -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/type_wrapper.hpp b/src/type_wrapper.hpp index f14497a..f147d67 100644 --- a/src/type_wrapper.hpp +++ b/src/type_wrapper.hpp @@ -11,21 +11,22 @@ namespace wanda { - -template -struct type_wrapper -{ - - explicit type_wrapper(InnerType value) : m_value{std::move(value)} {} + template + struct type_wrapper + { + explicit type_wrapper(InnerType value) + : m_value{std::move(value)} + { + } constexpr explicit operator InnerType const &() const { return get(); } - constexpr InnerType const &get() const { return m_value; } + constexpr InnerType const & get() const { return m_value; } private: InnerType const m_value; -}; + }; -} // namespace wanda +} // namespace wanda #endif \ No newline at end of file diff --git a/src/wallpaper.cpp b/src/wallpaper.cpp index 8f8e8c8..f0ae468 100644 --- a/src/wallpaper.cpp +++ b/src/wallpaper.cpp @@ -1,21 +1,22 @@ +#include "wallpaper.hpp" #include "optional.hpp" #include "setting.hpp" -#include "wallpaper.hpp" namespace wanda { - -void set_wallpaper(std::filesystem::path wallpaper, std::shared_ptr logger) -{ + void set_wallpaper(std::filesystem::path wallpaper, std::shared_ptr logger) + { 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(); - }) || [&] { logger->error("invalid settings key"); }; - }) || [&] { logger->error("invalid setting"); }; -} + with("org.gnome.desktop.background"_setting, [&](auto & setting) { + with(setting["picture-uri"_key], [&](auto & value) { + value = "file://" + wallpaper.native(); + }) || + [&] { logger->error("invalid settings key"); }; + }) || + [&] { logger->error("invalid setting"); }; + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/wallpaper.hpp b/src/wallpaper.hpp index 8017d65..059a2f8 100644 --- a/src/wallpaper.hpp +++ b/src/wallpaper.hpp @@ -1,18 +1,15 @@ #ifndef WANDA_WALLPAPER_HPP #define WANDA_WALLPAPER_HPP -#include #include +#include #include #include namespace wanda { - -void set_wallpaper(std::filesystem::path wallpaper, std::shared_ptr logger); - -} // wanda - + void set_wallpaper(std::filesystem::path wallpaper, std::shared_ptr logger); +} // namespace wanda #endif \ No newline at end of file diff --git a/src/wandac.cpp b/src/wandac.cpp index 6f57e96..3a9531a 100644 --- a/src/wandac.cpp +++ b/src/wandac.cpp @@ -5,8 +5,8 @@ #include #include -#include #include +#include #include #include @@ -15,83 +15,83 @@ struct cli { - std::string command{}; - bool help{}; + std::string command{}; + bool help{}; - clara::Parser parser; + 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; - } -}; + 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); -struct listener : wanda::commander::listener -{ - listener(::cli &cli, std::shared_ptr logger) - : m_logger{logger}, - m_cli{cli} + auto result = parser.parse(clara::Args{argc, argv}); + + if (!result) { + error << "Error while processing command line arguments: " + << result.errorMessage() + << '\n' + << parser + << '\n'; + return false; } - - void on_connected(wanda::commander &commander) override + else if (command.empty()) { - if (m_cli.command == "change") - { - commander.send(wanda::make_change_command()); - commander.stop(); - } + error << "Missing required argument 'command'\n" + << parser + << '\n'; + return false; } - private: - std::shared_ptr m_logger; - ::cli &m_cli; + return true; + } }; -int main(int argc, char const *const *argv) +struct listener : wanda::commander::listener { - auto cli = ::cli{}; - if (!cli.parse(argc, argv, std::cerr)) - { - return EXIT_FAILURE; - } - else if (cli.help) + listener(::cli & cli, std::shared_ptr logger) + : m_logger{logger} + , m_cli{cli} + { + } + + void on_connected(wanda::commander & commander) override + { + if (m_cli.command == "change") { - std::cout << cli.parser << '\n'; - return EXIT_SUCCESS; + commander.send(wanda::make_change_command()); + commander.stop(); } + } - auto log = spdlog::stdout_color_mt("wandac"); - auto interface = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; - auto service = asio::io_service{}; - auto listener = ::listener{cli, log}; - - auto commander = wanda::commander{service, interface, listener, log}; - - log->info("trying to connect to wanda control interface on '{}'", interface.native()); - commander.start(); +private: + std::shared_ptr m_logger; + ::cli & m_cli; +}; - service.run(); +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; + } + + auto log = spdlog::stdout_color_mt("wandac"); + auto interface = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface"; + auto service = asio::io_service{}; + auto listener = ::listener{cli, log}; + + auto commander = wanda::commander{service, interface, listener, log}; + + log->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 index 6008b57..e955beb 100644 --- a/src/wandad.cpp +++ b/src/wandad.cpp @@ -9,8 +9,8 @@ #include -#include #include +#include #include #include @@ -18,51 +18,50 @@ namespace { + constexpr auto image_filter = [](auto const & path) { + static auto const extensions = std::set{ + std::filesystem::path{".jpg"}, + std::filesystem::path{".png"}, + }; -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(); -}; + if (!std::filesystem::is_regular_file(path)) + { + return false; + } -struct listener : wanda::control_interface::listener -{ - listener(std::vector const &wallpapers, std::shared_ptr logger) - : m_wallpapers{wallpapers}, - m_logger{logger} - { - } + return extensions.find(path.extension()) != extensions.cend(); + }; - void on_received(wanda::control_interface &interface, wanda::command command) override + struct listener : wanda::control_interface::listener { - switch (command.id) + listener(std::vector const & wallpapers, std::shared_ptr logger) + : m_wallpapers{wallpapers} + , m_logger{logger} { - case wanda::command_id::change: - { - auto wallpaper = wanda::random_pick(m_wallpapers); - m_logger->info("changing wallpaper to '{}'", wallpaper.native()); - wanda::set_wallpaper(wallpaper, m_logger); - break; } - default: - m_logger->error("received unknown command '{}'", static_cast(command.id)); + + 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); + m_logger->info("changing wallpaper to '{}'", wallpaper.native()); + wanda::set_wallpaper(wallpaper, m_logger); + break; + } + default: + m_logger->error("received unknown command '{}'", static_cast(command.id)); + } } - } -private: - std::vector const &m_wallpapers; - std::shared_ptr m_logger; -}; + private: + std::vector const & m_wallpapers; + std::shared_ptr m_logger; + }; -} // namespace +} // namespace int main() { @@ -71,7 +70,7 @@ int main() auto log = spdlog::stdout_color_mt("wandad"); log->info("wanda is starting up"); - with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [&](auto const &list) { + 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"; @@ -91,7 +90,7 @@ int main() } auto signals = asio::signal_set{service, SIGINT}; - signals.async_wait([&](auto const &error, auto const signal) { + signals.async_wait([&](auto const & error, auto const signal) { if (!error && signal == SIGINT) { interface->shutdown(); diff --git a/src/xdg.cpp b/src/xdg.cpp index 9293151..10f3603 100644 --- a/src/xdg.cpp +++ b/src/xdg.cpp @@ -4,44 +4,43 @@ namespace wanda { - -std::string xdg_variable(xdg_directory directory) -{ + std::string xdg_variable(xdg_directory directory) + { switch (directory) { - case xdg_directory::data_home: + case xdg_directory::data_home: return "XDG_DATA_HOME"; - case xdg_directory::config_home: + case xdg_directory::config_home: return "XDG_CONFIG_HOME"; - case xdg_directory::cache_home: + case xdg_directory::cache_home: return "XDG_CACHE_HOME"; - case xdg_directory::runtime_dir: + 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) -{ + std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment) + { if (auto path = environment[xdg_variable(directory)]; !path.empty()) { - return path; + return path; } auto home = std::filesystem::path{environment["HOME"]}; switch (directory) { - case xdg_directory::data_home: + case xdg_directory::data_home: return home / ".local/share"; - case xdg_directory::config_home: + case xdg_directory::config_home: return home / ".config"; - case xdg_directory::cache_home: + case xdg_directory::cache_home: return home / ".cache"; - case xdg_directory::runtime_dir: + case xdg_directory::runtime_dir: return std::filesystem::path{"/run/user"} / std::to_string(::getuid()); } return ""; -} + } -} // namespace wanda \ No newline at end of file +} // namespace wanda \ No newline at end of file diff --git a/src/xdg.hpp b/src/xdg.hpp index a5dace0..b57a34f 100644 --- a/src/xdg.hpp +++ b/src/xdg.hpp @@ -9,17 +9,17 @@ namespace wanda { -enum struct xdg_directory : std::underlying_type_t -{ + enum struct xdg_directory : std::underlying_type_t + { data_home, config_home, cache_home, runtime_dir, -}; + }; -std::string xdg_variable(xdg_directory directory); + std::string xdg_variable(xdg_directory directory); -std::filesystem::path xdg_path_for(xdg_directory directory, environment const &environment); -} // namespace wanda + std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment); +} // namespace wanda #endif \ No newline at end of file -- cgit v1.2.3