mirror of
https://github.com/polybar/polybar.git
synced 2024-11-18 13:55:11 -05:00
fix(xbacklight): Lower inc/dec value to 5 and fix flooring
This commit is contained in:
parent
e2480da8c6
commit
5c9bfc147c
6 changed files with 52 additions and 23 deletions
|
@ -56,7 +56,7 @@ namespace modules {
|
|||
progressbar_t m_progressbar;
|
||||
|
||||
bool m_scroll{true};
|
||||
int m_percentage{0};
|
||||
std::atomic<int> m_percentage{0};
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -69,6 +69,16 @@ namespace math_util {
|
|||
else
|
||||
return cap<ReturnType>(percentage * (max_value - min_value) / 100.0f, 0.0f, max_value - min_value) + min_value;
|
||||
}
|
||||
|
||||
template <typename ReturnType = int>
|
||||
ReturnType nearest_10(double value) {
|
||||
return static_cast<ReturnType>(static_cast<int>(value / 10.0 + 0.5) * 10.0);
|
||||
}
|
||||
|
||||
template <typename ReturnType = int>
|
||||
ReturnType nearest_5(double value) {
|
||||
return static_cast<ReturnType>(static_cast<int>(value / 5.0 + 0.5) * 5.0);
|
||||
}
|
||||
}
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<mutex> 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<double>(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<int>(new_perc, m_output->backlight.max);
|
||||
const int values[1]{new_value};
|
||||
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
|
||||
const int values[1]{math_util::percentage_to_value<int>(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);
|
||||
|
|
|
@ -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<uint32_t*>(xcb_randr_get_output_property_data(reply.get().get()));
|
||||
dst.val = *xcb_randr_get_output_property_data(reply.get().get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue