2019-02-10 09:35:19 +00:00
|
|
|
#include "modules/backlight.hpp"
|
|
|
|
|
2020-04-21 07:11:42 +00:00
|
|
|
#include <fmt/format.h>
|
2019-02-10 09:35:19 +00:00
|
|
|
#include <libudev.h>
|
2023-10-15 19:14:06 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-02-10 09:35:19 +00:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-04-21 07:11:42 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2019-02-10 09:35:19 +00:00
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
#include "util/backend_common.hpp"
|
|
|
|
#include "util/backlight_backend.hpp"
|
2022-10-18 21:43:15 +00:00
|
|
|
|
2019-05-22 10:06:24 +00:00
|
|
|
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),
|
2023-10-15 19:14:06 +00:00
|
|
|
preferred_device_(config["device"].isString() ? config["device"].asString() : ""),
|
|
|
|
backend(interval_, [this] { dp.emit(); }) {
|
|
|
|
dp.emit();
|
2019-03-01 02:50:57 +00:00
|
|
|
|
2023-02-01 13:40:36 +00:00
|
|
|
// 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));
|
2019-02-10 09:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::Backlight::update() -> void {
|
2023-10-15 19:14:06 +00:00
|
|
|
GET_BEST_DEVICE(best, backend, preferred_device_);
|
2019-02-10 09:35:19 +00:00
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
const auto previous_best_device = backend.get_previous_best_device();
|
2019-02-10 09:35:19 +00:00
|
|
|
if (best != nullptr) {
|
2023-10-15 19:14:06 +00:00
|
|
|
if (previous_best_device != nullptr && *previous_best_device == *best &&
|
2019-02-24 21:15:41 +00:00
|
|
|
!previous_format_.empty() && previous_format_ == format_) {
|
2019-02-10 09:35:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-24 21:15:41 +00:00
|
|
|
|
2022-10-18 21:43:15 +00:00
|
|
|
if (best->get_powered()) {
|
2022-11-07 08:06:16 +00:00
|
|
|
event_box_.show();
|
2022-10-18 21:43:15 +00:00
|
|
|
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),
|
2023-07-16 02:14:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 21:43:15 +00:00
|
|
|
} else {
|
|
|
|
event_box_.hide();
|
2022-11-07 08:06:16 +00:00
|
|
|
}
|
2019-02-10 09:35:19 +00:00
|
|
|
} else {
|
2023-10-15 19:14:06 +00:00
|
|
|
if (previous_best_device == nullptr) {
|
2019-02-10 09:35:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.set_markup("");
|
2019-02-10 09:35:19 +00:00
|
|
|
}
|
2023-10-15 19:14:06 +00:00
|
|
|
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();
|
2019-02-10 09:35:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 13:40:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:58:52 +00:00
|
|
|
// Fail fast if the proxy could not be initialized
|
2023-10-15 19:14:06 +00:00
|
|
|
if (!backend.is_login_proxy_initialized()) {
|
2023-02-03 11:58:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:40:36 +00:00
|
|
|
// Check scroll direction
|
|
|
|
auto dir = AModule::getScrollDir(e);
|
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
util::ChangeType ct;
|
2023-02-01 13:40:36 +00:00
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
switch (dir) {
|
|
|
|
case SCROLL_DIR::UP:
|
|
|
|
[[fallthrough]];
|
|
|
|
case SCROLL_DIR::RIGHT:
|
|
|
|
ct = util::ChangeType::Increase;
|
|
|
|
break;
|
2023-02-01 13:40:36 +00:00
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
case SCROLL_DIR::DOWN:
|
|
|
|
[[fallthrough]];
|
|
|
|
case SCROLL_DIR::LEFT:
|
|
|
|
ct = util::ChangeType::Decrease;
|
|
|
|
break;
|
2023-02-01 13:40:36 +00:00
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
case SCROLL_DIR::NONE:
|
|
|
|
return true;
|
|
|
|
break;
|
2023-02-01 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
// Get scroll step
|
|
|
|
double step = 1;
|
2023-02-01 13:40:36 +00:00
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
if (config_["scroll-step"].isDouble()) {
|
|
|
|
step = config_["scroll-step"].asDouble();
|
2023-02-01 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:14:06 +00:00
|
|
|
backend.set_brightness(preferred_device_, ct, step);
|
2023-02-01 13:40:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|