Merge pull request #1734 from baltitenger/backlight-hide-when-powered-off

closes https://github.com/Alexays/Waybar/issues/393
This commit is contained in:
Alex 2022-11-07 14:09:54 +01:00 committed by GitHub
commit 8be5bab8ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 17 deletions

View File

@ -18,12 +18,14 @@ class Backlight : public AButton {
class BacklightDev { class BacklightDev {
public: public:
BacklightDev() = default; 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; std::string_view name() const;
int get_actual() const; int get_actual() const;
void set_actual(int actual); void set_actual(int actual);
int get_max() const; int get_max() const;
void set_max(int max); void set_max(int max);
bool get_powered() const;
void set_powered(bool powered);
friend inline bool operator==(const BacklightDev &lhs, const BacklightDev &rhs) { friend inline bool operator==(const BacklightDev &lhs, const BacklightDev &rhs) {
return lhs.name_ == rhs.name_ && lhs.actual_ == rhs.actual_ && lhs.max_ == rhs.max_; return lhs.name_ == rhs.name_ && lhs.actual_ == rhs.actual_ && lhs.max_ == rhs.max_;
} }
@ -32,6 +34,7 @@ class Backlight : public AButton {
std::string name_; std::string name_;
int actual_ = 1; int actual_ = 1;
int max_ = 1; int max_ = 1;
bool powered_ = true;
}; };
public: public:

View File

@ -73,8 +73,8 @@ void check_nn(const void *ptr, const char *message = "ptr was null") {
} }
} // namespace } // namespace
waybar::modules::Backlight::BacklightDev::BacklightDev(std::string name, int actual, int max) waybar::modules::Backlight::BacklightDev::BacklightDev(std::string name, int actual, int max, bool powered)
: name_(std::move(name)), actual_(actual), max_(max) {} : name_(std::move(name)), actual_(actual), max_(max), powered_(powered) {}
std::string_view waybar::modules::Backlight::BacklightDev::name() const { return name_; } 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; } 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) waybar::modules::Backlight::Backlight(const std::string &id, const Json::Value &config)
: AButton(config, "backlight", id, "{percent}%", 2), : AButton(config, "backlight", id, "{percent}%", 2),
preferred_device_(config["device"].isString() ? config["device"].asString() : "") { preferred_device_(config["device"].isString() ? config["device"].asString() : "") {
@ -172,21 +176,15 @@ auto waybar::modules::Backlight::update() -> void {
return; return;
} }
const uint8_t percent = if (best->get_powered()) {
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 {
event_box_.show(); 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)))); fmt::arg("icon", getIcon(percent))));
getState(percent);
} else {
event_box_.hide();
} }
} else { } else {
if (!previous_best_.has_value()) { 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 *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr);
const char *max = udev_device_get_sysattr_value(dev, "max_brightness"); const char *max = udev_device_get_sysattr_value(dev, "max_brightness");
const char *power = udev_device_get_sysattr_value(dev, "bl_power");
auto found = auto found =
std::find_if(first, last, [name](const auto &device) { return device.name() == name; }); 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) { if (max != nullptr) {
found->set_max(std::stoi(max)); found->set_max(std::stoi(max));
} }
if (power != nullptr) {
found->set_powered(std::stoi(power) == 0);
}
} else { } else {
const int actual_int = actual == nullptr ? 0 : std::stoi(actual); const int actual_int = actual == nullptr ? 0 : std::stoi(actual);
const int max_int = max == nullptr ? 0 : std::stoi(max); 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; ++inserter;
} }
} }