From 5c9bfc147c53485c13a287b2e78b18104b99f192 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 11 Jan 2017 04:16:33 +0100 Subject: [PATCH] fix(xbacklight): Lower inc/dec value to 5 and fix flooring --- include/modules/xbacklight.hpp | 2 +- include/utils/math.hpp | 10 ++++++++++ include/x11/extensions/randr.hpp | 16 ++++++++-------- src/modules/xbacklight.cpp | 29 ++++++++++++++++------------- src/x11/extensions/randr.cpp | 2 +- tests/unit_tests/utils/math.cpp | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 23 deletions(-) diff --git a/include/modules/xbacklight.hpp b/include/modules/xbacklight.hpp index 12c0d118..8d6c257c 100644 --- a/include/modules/xbacklight.hpp +++ b/include/modules/xbacklight.hpp @@ -56,7 +56,7 @@ namespace modules { progressbar_t m_progressbar; bool m_scroll{true}; - int m_percentage{0}; + std::atomic m_percentage{0}; }; } diff --git a/include/utils/math.hpp b/include/utils/math.hpp index d65767cd..6d2f12e8 100644 --- a/include/utils/math.hpp +++ b/include/utils/math.hpp @@ -69,6 +69,16 @@ namespace math_util { else return cap(percentage * (max_value - min_value) / 100.0f, 0.0f, max_value - min_value) + min_value; } + + template + ReturnType nearest_10(double value) { + return static_cast(static_cast(value / 10.0 + 0.5) * 10.0); + } + + template + ReturnType nearest_5(double value) { + return static_cast(static_cast(value / 5.0 + 0.5) * 5.0); + } } POLYBAR_NS_END diff --git a/include/x11/extensions/randr.hpp b/include/x11/extensions/randr.hpp index 5e8d5bcc..558245cf 100644 --- a/include/x11/extensions/randr.hpp +++ b/include/x11/extensions/randr.hpp @@ -22,18 +22,18 @@ namespace evt { } struct backlight_values { - uint32_t atom = 0; - uint32_t min = 0; - uint32_t max = 0; - uint32_t val = 0; + uint32_t atom{0}; + double min{0.0}; + double max{0.0}; + double val{0.0}; }; struct randr_output { string name; - uint16_t w = 0; - uint16_t h = 0; - int16_t x = 0; - int16_t y = 0; + uint16_t w{0U}; + uint16_t h{0U}; + int16_t x{0}; + int16_t y{0}; xcb_randr_output_t output; backlight_values backlight; diff --git a/src/modules/xbacklight.cpp b/src/modules/xbacklight.cpp index 20a9bb91..366c67bc 100644 --- a/src/modules/xbacklight.cpp +++ b/src/modules/xbacklight.cpp @@ -92,10 +92,16 @@ namespace modules { * Query the RandR extension for the new values */ void xbacklight_module::update() { + if (!m_updatelock.try_lock()) { + return; + } + + std::lock_guard guard(m_updatelock, std::adopt_lock); + // Query for the new backlight value auto& bl = m_output->backlight; randr_util::get_backlight_value(m_connection, m_output, bl); - m_percentage = math_util::percentage(bl.val, bl.min, bl.max); + m_percentage = math_util::nearest_5(math_util::percentage(bl.val, bl.min, bl.max)); // Update label tokens if (m_label) { @@ -116,16 +122,14 @@ namespace modules { // the format prefix/suffix also gets wrapped // with the cmd handlers string output{module::get_output()}; - bool scroll_up{m_scroll && m_percentage < 100}; - bool scroll_down{m_scroll && m_percentage > 0}; - m_builder->cmd(mousebtn::SCROLL_UP, string{EVENT_SCROLLUP}, scroll_up); - m_builder->cmd(mousebtn::SCROLL_DOWN, string{EVENT_SCROLLDOWN}, scroll_down); + m_builder->cmd(mousebtn::SCROLL_UP, EVENT_SCROLLUP); + m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_SCROLLDOWN); m_builder->append(output); - m_builder->cmd_close(scroll_down); - m_builder->cmd_close(scroll_up); + m_builder->cmd_close(); + m_builder->cmd_close(); return m_builder->flush(); } @@ -150,22 +154,21 @@ namespace modules { * Process scroll events by changing backlight value */ bool xbacklight_module::input(string&& cmd) { - int value_mod = 0; + double value_mod{0.0}; if (cmd == EVENT_SCROLLUP) { - value_mod = 10; + value_mod = 5.0; m_log.info("%s: Increasing value by %i%", name(), value_mod); } else if (cmd == EVENT_SCROLLDOWN) { - value_mod = -10; + value_mod = -5.0; m_log.info("%s: Decreasing value by %i%", name(), -value_mod); } else { return false; } try { - const int new_perc = math_util::cap(m_percentage + value_mod, 0, 100); - const int new_value = math_util::percentage_to_value(new_perc, m_output->backlight.max); - const int values[1]{new_value}; + int rounded = math_util::cap(m_percentage + value_mod, 0.0, 100.0) + 0.5; + const int values[1]{math_util::percentage_to_value(rounded, m_output->backlight.max)}; m_connection.change_output_property_checked( m_output->output, m_output->backlight.atom, XCB_ATOM_INTEGER, 32, XCB_PROP_MODE_REPLACE, 1, values); diff --git a/src/x11/extensions/randr.cpp b/src/x11/extensions/randr.cpp index 6be0f32a..6b68c48c 100644 --- a/src/x11/extensions/randr.cpp +++ b/src/x11/extensions/randr.cpp @@ -159,7 +159,7 @@ namespace randr_util { auto reply = conn.get_output_property(mon->output, dst.atom, XCB_ATOM_NONE, 0, 4, 0, 0); if (reply->num_items == 1 && reply->format == 32 && reply->type == XCB_ATOM_INTEGER) { - dst.val = *reinterpret_cast(xcb_randr_get_output_property_data(reply.get().get())); + dst.val = *xcb_randr_get_output_property_data(reply.get().get()); } } } diff --git a/tests/unit_tests/utils/math.cpp b/tests/unit_tests/utils/math.cpp index 605a362c..4e9912ae 100644 --- a/tests/unit_tests/utils/math.cpp +++ b/tests/unit_tests/utils/math.cpp @@ -58,4 +58,20 @@ int main() { expect(math_util::percentage_to_value(50, 200, 300) == 250); expect(math_util::percentage_to_value(50, 1, 5) == 3); }; + + "round_to_nearest_10"_test = [] { + expect(math_util::nearest_10(52) == 50); + expect(math_util::nearest_10(9.1) == 10); + expect(math_util::nearest_10(95.0) == 100); + expect(math_util::nearest_10(94.9) == 90); + }; + + "round_to_nearest_5"_test = [] { + expect(math_util::nearest_5(52) == 55); + expect(math_util::nearest_5(9.1) == 5); + expect(math_util::nearest_5(95.0) == 95); + expect(math_util::nearest_5(94.9) == 95); + expect(math_util::nearest_5(1) == 0); + expect(math_util::nearest_5(99.99) == 100); + }; }