Waybar/src/modules/backlight.cpp

119 lines
3.4 KiB
C++
Raw Normal View History

#include "modules/backlight.hpp"
2020-04-21 07:11:42 +00:00
#include <fmt/format.h>
#include <libudev.h>
#include <spdlog/spdlog.h>
#include <sys/epoll.h>
#include <unistd.h>
2020-04-21 07:11:42 +00:00
#include <algorithm>
#include <chrono>
#include <memory>
#include "util/backend_common.hpp"
#include "util/backlight_backend.hpp"
waybar::modules::Backlight::Backlight(const std::string &id, const Json::Value &config)
2022-11-24 11:28:52 +00:00
: ALabel(config, "backlight", id, "{percent}%", 2),
preferred_device_(config["device"].isString() ? config["device"].asString() : ""),
backend(interval_, [this] { dp.emit(); }) {
dp.emit();
// Set up scroll handler
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &Backlight::handleScroll));
}
auto waybar::modules::Backlight::update() -> void {
GET_BEST_DEVICE(best, backend, preferred_device_);
const auto previous_best_device = backend.get_previous_best_device();
if (best != nullptr) {
if (previous_best_device != nullptr && *previous_best_device == *best &&
2019-02-24 21:15:41 +00:00
!previous_format_.empty() && previous_format_ == format_) {
return;
}
2019-02-24 21:15:41 +00:00
if (best->get_powered()) {
event_box_.show();
const uint8_t percent =
best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max());
2023-07-18 06:28:19 +00:00
std::string desc = fmt::format(fmt::runtime(format_), fmt::arg("percent", percent),
fmt::arg("icon", getIcon(percent)));
2023-04-11 13:01:02 +00:00
label_.set_markup(desc);
2023-04-17 07:01:14 +00:00
getState(percent);
2023-04-11 13:01:02 +00:00
if (tooltipEnabled()) {
std::string tooltip_format;
if (config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
if (!tooltip_format.empty()) {
2023-04-17 07:01:14 +00:00
label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
fmt::arg("percent", percent),
2023-04-17 07:01:14 +00:00
fmt::arg("icon", getIcon(percent))));
2023-04-11 13:01:02 +00:00
} else {
label_.set_tooltip_text(desc);
}
}
} else {
event_box_.hide();
}
} else {
if (previous_best_device == nullptr) {
return;
}
2022-11-24 11:28:52 +00:00
label_.set_markup("");
}
backend.set_previous_best_device(best);
2019-02-24 21:15:41 +00:00
previous_format_ = format_;
2022-11-24 11:28:52 +00:00
ALabel::update();
}
bool waybar::modules::Backlight::handleScroll(GdkEventScroll *e) {
// Check if the user has set a custom command for scrolling
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
return AModule::handleScroll(e);
}
// Fail fast if the proxy could not be initialized
if (!backend.is_login_proxy_initialized()) {
return true;
}
// Check scroll direction
auto dir = AModule::getScrollDir(e);
2023-10-15 20:49:45 +00:00
// No worries, it will always be set because of the switch below. This is purely to suppress a
// warning
util::ChangeType ct = util::ChangeType::Increase;
switch (dir) {
case SCROLL_DIR::UP:
[[fallthrough]];
case SCROLL_DIR::RIGHT:
ct = util::ChangeType::Increase;
break;
case SCROLL_DIR::DOWN:
[[fallthrough]];
case SCROLL_DIR::LEFT:
ct = util::ChangeType::Decrease;
break;
case SCROLL_DIR::NONE:
return true;
break;
}
// Get scroll step
double step = 1;
if (config_["scroll-step"].isDouble()) {
step = config_["scroll-step"].asDouble();
}
backend.set_brightness(preferred_device_, ct, step);
return true;
}