From 18cf9df86cb2482f9b0a5e0ccf32fe989428a1ba Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 27 Dec 2016 04:58:41 +0100 Subject: [PATCH] refactor: Cleanup --- include/components/controller.hpp | 11 +++++++---- include/components/screen.hpp | 2 +- include/components/types.hpp | 7 ++++++- include/utils/color.hpp | 4 ++-- include/x11/color.hpp | 10 +++++++--- include/x11/connection.hpp | 1 - src/components/controller.cpp | 25 +++++++++++++------------ src/modules/bspwm.cpp | 1 - src/modules/meta/base.cpp | 2 +- src/x11/color.cpp | 23 +++++++++++------------ 10 files changed, 48 insertions(+), 38 deletions(-) diff --git a/include/components/controller.hpp b/include/components/controller.hpp index 7af3f14d..a1ae93af 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -7,6 +7,7 @@ #include "events/signal_fwd.hpp" #include "events/signal_receiver.hpp" #include "events/types.hpp" +#include "utils/file.hpp" #include "x11/types.hpp" #include "x11/events.hpp" @@ -83,10 +84,7 @@ class controller : public signal_receiver m_confwatch; unique_ptr m_command; - unique_ptr m_fdevent_rd; - unique_ptr m_fdevent_wr; - - thread m_event_thread; + array, 2> m_queuefd{}; /** * @brief Controls weather the output gets printed to stdout @@ -133,6 +131,11 @@ class controller : public signal_receiver + #include #include #include "common.hpp" -#include "x11/extensions/randr.hpp" POLYBAR_NS +// fwd +struct randr_output; +using monitor_t = shared_ptr; + struct enum_hash { template inline typename std::enable_if::value, size_t>::type operator()(T const value) const { diff --git a/include/utils/color.hpp b/include/utils/color.hpp index bb830d9b..ab826b22 100644 --- a/include/utils/color.hpp +++ b/include/utils/color.hpp @@ -74,8 +74,8 @@ namespace color_util { } inline string parse_hex(string hex) { - if (hex.substr(0, 1) != "#") - hex = "#" + hex; + if (hex[0] != '#') + hex.insert(0, 1, '#'); if (hex.length() == 4) hex = {'#', hex[1], hex[1], hex[2], hex[2], hex[3], hex[3]}; if (hex.length() == 7) diff --git a/include/x11/color.hpp b/include/x11/color.hpp index ed1c3f6a..40b9264c 100644 --- a/include/x11/color.hpp +++ b/include/x11/color.hpp @@ -3,9 +3,11 @@ #include #include +#include #include "common.hpp" #include "utils/color.hpp" +#include "utils/concurrency.hpp" POLYBAR_NS @@ -30,8 +32,10 @@ class color { string m_source; }; -extern color g_colorempty; -extern color g_colorblack; -extern color g_colorwhite; +extern mutex_wrapper> g_colorstore; + +extern const color& g_colorempty; +extern const color& g_colorblack; +extern const color& g_colorwhite; POLYBAR_NS_END diff --git a/include/x11/connection.hpp b/include/x11/connection.hpp index d08258a6..5db48292 100644 --- a/include/x11/connection.hpp +++ b/include/x11/connection.hpp @@ -6,7 +6,6 @@ #include "common.hpp" #include "components/screen.hpp" -#include "utils/file.hpp" #include "x11/events.hpp" #include "x11/extensions/all.hpp" #include "x11/registry.hpp" diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 0c00419b..75e71306 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -25,7 +25,7 @@ POLYBAR_NS -int g_eventpipe[2]{0, 0}; +array g_eventpipe{{-1, -1}}; sig_atomic_t g_reload{0}; sig_atomic_t g_terminate{0}; @@ -61,13 +61,13 @@ controller::controller(connection& conn, signal_emitter& emitter, const logger& m_swallow_limit = m_conf.deprecated("settings", "eventqueue-swallow", "throttle-output", m_swallow_limit); m_swallow_update = m_conf.deprecated("settings", "eventqueue-swallow-time", "throttle-output-for", m_swallow_update); - if (pipe(g_eventpipe) != 0) { + if (pipe(g_eventpipe.data()) == 0) { + m_queuefd[PIPE_READ] = make_unique(g_eventpipe[PIPE_READ]); + m_queuefd[PIPE_WRITE] = make_unique(g_eventpipe[PIPE_WRITE]); + } else { throw system_error("Failed to create event channel pipes"); } - m_fdevent_rd = file_util::make_file_descriptor(g_eventpipe[PIPE_READ]); - m_fdevent_wr = file_util::make_file_descriptor(g_eventpipe[PIPE_WRITE]); - m_log.trace("controller: Install signal handler"); struct sigaction act {}; memset(&act, 0, sizeof(act)); @@ -234,13 +234,13 @@ bool controller::enqueue(string&& input_data) { void controller::read_events() { m_log.info("Entering event loop (thread-id=%lu)", this_thread::get_id()); - int fd_connection{m_connection.get_file_descriptor()}; - int fd_confwatch{0}; - int fd_ipc{0}; + int fd_connection{-1}; + int fd_confwatch{-1}; + int fd_ipc{-1}; vector fds; - fds.emplace_back(*m_fdevent_rd); - fds.emplace_back(fd_connection); + fds.emplace_back(*m_queuefd[PIPE_READ]); + fds.emplace_back(fd_connection = m_connection.get_file_descriptor()); if (m_confwatch) { m_log.trace("controller: Attach config watch"); @@ -271,9 +271,9 @@ void controller::read_events() { } // Process event on the internal fd - if (m_fdevent_rd && FD_ISSET(*m_fdevent_rd, &readfds)) { + if (m_queuefd[PIPE_READ] && FD_ISSET(static_cast(*m_queuefd[PIPE_READ]), &readfds)) { char buffer[BUFSIZ]{'\0'}; - if (read(*m_fdevent_rd, &buffer, BUFSIZ) == -1) { + if (read(static_cast(*m_queuefd[PIPE_READ]), &buffer, BUFSIZ) == -1) { m_log.err("Failed to read from eventpipe (err: %s)", strerror(errno)); } } @@ -454,6 +454,7 @@ bool controller::on(const sig_ev::update&) { block_contents += margin_left; } + block_contents.reserve(module_contents.size()); block_contents += module_contents; } diff --git a/src/modules/bspwm.cpp b/src/modules/bspwm.cpp index 53aced5d..44ac5692 100644 --- a/src/modules/bspwm.cpp +++ b/src/modules/bspwm.cpp @@ -142,7 +142,6 @@ namespace modules { m_log.warn("%s: Reconnecting to socket...", name()); m_subscriber = bspwm_util::make_subscriber(); } - return m_subscriber->peek(1); } diff --git a/src/modules/meta/base.cpp b/src/modules/meta/base.cpp index 46f4d335..967fcf42 100644 --- a/src/modules/meta/base.cpp +++ b/src/modules/meta/base.cpp @@ -78,7 +78,7 @@ namespace modules { format->bg = m_conf.get(m_modname, name + "-background", ""s); format->ul = m_conf.get(m_modname, name + "-underline", ""s); format->ol = m_conf.get(m_modname, name + "-overline", ""s); - format->spacing = m_conf.get(m_modname, name + "-spacing", 0_z); + format->spacing = m_conf.get(m_modname, name + "-spacing", 1_z); format->padding = m_conf.get(m_modname, name + "-padding", 0_z); format->margin = m_conf.get(m_modname, name + "-margin", 0_z); format->offset = m_conf.get(m_modname, name + "-offset", 0_z); diff --git a/src/x11/color.cpp b/src/x11/color.cpp index a9550ac9..38eb1571 100644 --- a/src/x11/color.cpp +++ b/src/x11/color.cpp @@ -1,5 +1,4 @@ #include -#include #include #include "errors.hpp" @@ -9,11 +8,11 @@ POLYBAR_NS -std::unordered_map g_colorstore; +mutex_wrapper> g_colorstore; -color g_colorempty{"#00000000"}; -color g_colorblack{"#ff000000"}; -color g_colorwhite{"#ffffffff"}; +const color& g_colorempty{"#00000000"}; +const color& g_colorblack{"#ff000000"}; +const color& g_colorwhite{"#ffffffff"}; color::color(string hex) : m_source(hex) { if (hex.empty()) { @@ -59,16 +58,16 @@ color::operator uint32_t() const { const color& color::parse(string input, const color& fallback) { if (input.empty()) { throw application_error("Cannot parse empty color"); - } - auto it = g_colorstore.find(input); - if (it != g_colorstore.end()) { - return it->second; - } else if ((input = color_util::parse_hex(input)).empty()) { + } else if ((input = color_util::parse_hex(move(input))).empty()) { return fallback; } - g_colorstore.emplace_hint(it, input, color{input}); - return g_colorstore.at(input); + std::lock_guard guard(g_colorstore); + auto it = g_colorstore.find(input); + if (it == g_colorstore.end()) { + it = g_colorstore.emplace_hint(it, make_pair(input, color{input})); + } + return it->second; } const color& color::parse(string input) {