From d02e23c7591e4b30160aa24fe58ca827dc544f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baltaz=C3=A1r=20Radics?= Date: Tue, 18 Oct 2022 23:43:15 +0200 Subject: [PATCH] feat(backlight): hide if the display is powered off --- include/modules/backlight.hpp | 5 ++++- src/modules/backlight.cpp | 35 +++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/modules/backlight.hpp b/include/modules/backlight.hpp index 4f60d6ec..90567716 100644 --- a/include/modules/backlight.hpp +++ b/include/modules/backlight.hpp @@ -18,12 +18,14 @@ class Backlight : public AButton { class BacklightDev { public: BacklightDev() = default; - BacklightDev(std::string name, int actual, int max); + BacklightDev(std::string name, int actual, int max, bool powered); std::string_view name() const; int get_actual() const; void set_actual(int actual); int get_max() const; void set_max(int max); + bool get_powered() const; + void set_powered(bool powered); friend inline bool operator==(const BacklightDev &lhs, const BacklightDev &rhs) { return lhs.name_ == rhs.name_ && lhs.actual_ == rhs.actual_ && lhs.max_ == rhs.max_; } @@ -32,6 +34,7 @@ class Backlight : public AButton { std::string name_; int actual_ = 1; int max_ = 1; + bool powered_ = true; }; public: diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index 276fbd4c..216ef232 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -73,8 +73,8 @@ void check_nn(const void *ptr, const char *message = "ptr was null") { } } // namespace -waybar::modules::Backlight::BacklightDev::BacklightDev(std::string name, int actual, int max) - : name_(std::move(name)), actual_(actual), max_(max) {} +waybar::modules::Backlight::BacklightDev::BacklightDev(std::string name, int actual, int max, bool powered) + : name_(std::move(name)), actual_(actual), max_(max), powered_(powered) {} std::string_view waybar::modules::Backlight::BacklightDev::name() const { return name_; } @@ -86,6 +86,10 @@ int waybar::modules::Backlight::BacklightDev::get_max() const { return max_; } void waybar::modules::Backlight::BacklightDev::set_max(int max) { max_ = max; } +bool waybar::modules::Backlight::BacklightDev::get_powered() const { return powered_; } + +void waybar::modules::Backlight::BacklightDev::set_powered(bool powered) { powered_ = powered; } + waybar::modules::Backlight::Backlight(const std::string &id, const Json::Value &config) : AButton(config, "backlight", id, "{percent}%", 2), preferred_device_(config["device"].isString() ? config["device"].asString() : "") { @@ -172,21 +176,15 @@ auto waybar::modules::Backlight::update() -> void { return; } - const uint8_t percent = - best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max()); - - auto format = format_; - auto state = getState(percent); - if (!state.empty() && config_["format-" + state].isString()) { - format = config_["format-" + state].asString(); - } - - if (format.empty()) { - event_box_.hide(); - } else { + if (best->get_powered()) { event_box_.show(); - label_->set_markup(fmt::format(format, fmt::arg("percent", std::to_string(percent)), + const uint8_t percent = + best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max()); + label_->set_markup(fmt::format(format_, fmt::arg("percent", std::to_string(percent)), fmt::arg("icon", getIcon(percent)))); + getState(percent); + } else { + event_box_.hide(); } } else { if (!previous_best_.has_value()) { @@ -226,6 +224,7 @@ void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last, const char *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr); const char *max = udev_device_get_sysattr_value(dev, "max_brightness"); + const char *power = udev_device_get_sysattr_value(dev, "bl_power"); auto found = std::find_if(first, last, [name](const auto &device) { return device.name() == name; }); @@ -236,10 +235,14 @@ void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last, if (max != nullptr) { found->set_max(std::stoi(max)); } + if (power != nullptr) { + found->set_powered(std::stoi(power) == 0); + } } else { const int actual_int = actual == nullptr ? 0 : std::stoi(actual); const int max_int = max == nullptr ? 0 : std::stoi(max); - *inserter = BacklightDev{name, actual_int, max_int}; + const bool power_bool = power == nullptr ? true : std::stoi(power) == 0; + *inserter = BacklightDev{name, actual_int, max_int, power_bool}; ++inserter; } }