feat(backlight): hide if the display is powered off

pull/1734/head
Baltazár Radics 2022-10-18 23:43:15 +02:00
parent d2b22c6ec5
commit d02e23c759
2 changed files with 23 additions and 17 deletions

View File

@ -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:

View File

@ -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;
}
}