2019-02-10 10:35:19 +01:00
|
|
|
#include "modules/backlight.hpp"
|
|
|
|
|
2020-04-21 09:11:42 +02:00
|
|
|
#include <fmt/format.h>
|
2019-02-10 10:35:19 +01:00
|
|
|
#include <libudev.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-04-21 09:11:42 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2019-02-10 10:35:19 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class FileDescriptor {
|
2019-04-18 17:52:00 +02:00
|
|
|
public:
|
2019-02-10 10:35:19 +01:00
|
|
|
explicit FileDescriptor(int fd) : fd_(fd) {}
|
|
|
|
FileDescriptor(const FileDescriptor &other) = delete;
|
|
|
|
FileDescriptor(FileDescriptor &&other) noexcept = delete;
|
|
|
|
FileDescriptor &operator=(const FileDescriptor &other) = delete;
|
|
|
|
FileDescriptor &operator=(FileDescriptor &&other) noexcept = delete;
|
|
|
|
~FileDescriptor() {
|
|
|
|
if (fd_ != -1) {
|
|
|
|
if (close(fd_) != 0) {
|
|
|
|
fmt::print(stderr, "Failed to close fd: {}\n", errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int get() const { return fd_; }
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
private:
|
2019-02-10 10:35:19 +01:00
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UdevDeleter {
|
|
|
|
void operator()(udev *ptr) { udev_unref(ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UdevDeviceDeleter {
|
|
|
|
void operator()(udev_device *ptr) { udev_device_unref(ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UdevEnumerateDeleter {
|
|
|
|
void operator()(udev_enumerate *ptr) { udev_enumerate_unref(ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UdevMonitorDeleter {
|
|
|
|
void operator()(udev_monitor *ptr) { udev_monitor_unref(ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
void check_eq(int rc, int expected, const char *message = "eq, rc was: ") {
|
|
|
|
if (rc != expected) {
|
2023-01-16 22:24:55 +01:00
|
|
|
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_neq(int rc, int bad_rc, const char *message = "neq, rc was: ") {
|
|
|
|
if (rc == bad_rc) {
|
2023-01-16 22:24:55 +01:00
|
|
|
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void check0(int rc, const char *message = "rc wasn't 0") { check_eq(rc, 0, message); }
|
2019-02-10 10:35:19 +01:00
|
|
|
|
|
|
|
void check_gte(int rc, int gte, const char *message = "rc was: ") {
|
|
|
|
if (rc < gte) {
|
2023-01-16 22:24:55 +01:00
|
|
|
throw std::runtime_error(fmt::format(fmt::runtime(message), rc));
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_nn(const void *ptr, const char *message = "ptr was null") {
|
|
|
|
if (ptr == nullptr) {
|
|
|
|
throw std::runtime_error(message);
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
} // namespace
|
2019-02-10 10:35:19 +01:00
|
|
|
|
2022-11-10 09:19:49 +01:00
|
|
|
waybar::modules::Backlight::BacklightDev::BacklightDev(std::string name, int actual, int max,
|
|
|
|
bool powered)
|
2022-10-18 23:43:15 +02:00
|
|
|
: name_(std::move(name)), actual_(actual), max_(max), powered_(powered) {}
|
2019-02-10 10:35:19 +01:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
std::string_view waybar::modules::Backlight::BacklightDev::name() const { return name_; }
|
2019-02-10 10:35:19 +01:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
int waybar::modules::Backlight::BacklightDev::get_actual() const { return actual_; }
|
2019-02-10 10:35:19 +01:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Backlight::BacklightDev::set_actual(int actual) { actual_ = actual; }
|
2019-02-10 10:35:19 +01:00
|
|
|
|
|
|
|
int waybar::modules::Backlight::BacklightDev::get_max() const { return max_; }
|
|
|
|
|
|
|
|
void waybar::modules::Backlight::BacklightDev::set_max(int max) { max_ = max; }
|
|
|
|
|
2022-10-18 23:43:15 +02:00
|
|
|
bool waybar::modules::Backlight::BacklightDev::get_powered() const { return powered_; }
|
|
|
|
|
|
|
|
void waybar::modules::Backlight::BacklightDev::set_powered(bool powered) { powered_ = powered; }
|
|
|
|
|
2019-05-22 12:06:24 +02:00
|
|
|
waybar::modules::Backlight::Backlight(const std::string &id, const Json::Value &config)
|
2022-11-24 12:28:52 +01:00
|
|
|
: ALabel(config, "backlight", id, "{percent}%", 2),
|
2019-04-18 17:52:00 +02:00
|
|
|
preferred_device_(config["device"].isString() ? config["device"].asString() : "") {
|
2019-03-01 03:50:57 +01:00
|
|
|
// Get initial state
|
|
|
|
{
|
|
|
|
std::unique_ptr<udev, UdevDeleter> udev_check{udev_new()};
|
|
|
|
check_nn(udev_check.get(), "Udev check new failed");
|
2022-04-06 08:37:19 +02:00
|
|
|
enumerate_devices(devices_.begin(), devices_.end(), std::back_inserter(devices_),
|
|
|
|
udev_check.get());
|
2019-03-01 03:50:57 +01:00
|
|
|
if (devices_.empty()) {
|
|
|
|
throw std::runtime_error("No backlight found");
|
|
|
|
}
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
2023-02-01 14:40:36 +01: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));
|
|
|
|
|
2023-02-03 12:58:52 +01:00
|
|
|
// Connect to the login interface
|
|
|
|
login_proxy_ = Gio::DBus::Proxy::create_for_bus_sync(
|
|
|
|
Gio::DBus::BusType::BUS_TYPE_SYSTEM, "org.freedesktop.login1",
|
|
|
|
"/org/freedesktop/login1/session/self", "org.freedesktop.login1.Session");
|
|
|
|
|
2019-02-10 10:35:19 +01:00
|
|
|
udev_thread_ = [this] {
|
|
|
|
std::unique_ptr<udev, UdevDeleter> udev{udev_new()};
|
|
|
|
check_nn(udev.get(), "Udev new failed");
|
|
|
|
|
|
|
|
std::unique_ptr<udev_monitor, UdevMonitorDeleter> mon{
|
|
|
|
udev_monitor_new_from_netlink(udev.get(), "udev")};
|
|
|
|
check_nn(mon.get(), "udev monitor new failed");
|
2022-04-06 08:37:19 +02:00
|
|
|
check_gte(udev_monitor_filter_add_match_subsystem_devtype(mon.get(), "backlight", nullptr), 0,
|
2019-04-18 17:52:00 +02:00
|
|
|
"udev failed to add monitor filter: ");
|
2019-02-10 10:35:19 +01:00
|
|
|
udev_monitor_enable_receiving(mon.get());
|
|
|
|
|
|
|
|
auto udev_fd = udev_monitor_get_fd(mon.get());
|
|
|
|
|
2019-04-24 12:37:24 +02:00
|
|
|
auto epoll_fd = FileDescriptor{epoll_create1(EPOLL_CLOEXEC)};
|
2019-02-10 10:35:19 +01:00
|
|
|
check_neq(epoll_fd.get(), -1, "epoll init failed: ");
|
2019-04-24 12:37:24 +02:00
|
|
|
epoll_event ctl_event{};
|
2019-02-10 10:35:19 +01:00
|
|
|
ctl_event.events = EPOLLIN;
|
|
|
|
ctl_event.data.fd = udev_fd;
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
check0(epoll_ctl(epoll_fd.get(), EPOLL_CTL_ADD, ctl_event.data.fd, &ctl_event),
|
|
|
|
"epoll_ctl failed: {}");
|
2019-02-10 10:35:19 +01:00
|
|
|
epoll_event events[EPOLL_MAX_EVENTS];
|
|
|
|
|
|
|
|
while (udev_thread_.isRunning()) {
|
2022-04-06 08:37:19 +02:00
|
|
|
const int event_count = epoll_wait(epoll_fd.get(), events, EPOLL_MAX_EVENTS,
|
|
|
|
std::chrono::milliseconds{interval_}.count());
|
2019-02-26 04:05:44 +01:00
|
|
|
if (!udev_thread_.isRunning()) {
|
|
|
|
break;
|
|
|
|
}
|
2019-02-10 10:35:19 +01:00
|
|
|
decltype(devices_) devices;
|
|
|
|
{
|
|
|
|
std::scoped_lock<std::mutex> lock(udev_thread_mutex_);
|
|
|
|
devices = devices_;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < event_count; ++i) {
|
|
|
|
const auto &event = events[i];
|
|
|
|
check_eq(event.data.fd, udev_fd, "unexpected udev fd");
|
2019-04-18 17:52:00 +02:00
|
|
|
std::unique_ptr<udev_device, UdevDeviceDeleter> dev{udev_monitor_receive_device(mon.get())};
|
2019-02-10 10:35:19 +01:00
|
|
|
check_nn(dev.get(), "epoll dev was null");
|
2019-04-18 17:52:00 +02:00
|
|
|
upsert_device(devices.begin(), devices.end(), std::back_inserter(devices), dev.get());
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh state if timed out
|
|
|
|
if (event_count == 0) {
|
2019-04-18 17:52:00 +02:00
|
|
|
enumerate_devices(devices.begin(), devices.end(), std::back_inserter(devices), udev.get());
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
std::scoped_lock<std::mutex> lock(udev_thread_mutex_);
|
|
|
|
devices_ = devices;
|
|
|
|
}
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
waybar::modules::Backlight::~Backlight() = default;
|
|
|
|
|
|
|
|
auto waybar::modules::Backlight::update() -> void {
|
|
|
|
decltype(devices_) devices;
|
|
|
|
{
|
|
|
|
std::scoped_lock<std::mutex> lock(udev_thread_mutex_);
|
|
|
|
devices = devices_;
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
const auto best = best_device(devices.cbegin(), devices.cend(), preferred_device_);
|
2019-02-10 10:35:19 +01:00
|
|
|
if (best != nullptr) {
|
2019-02-24 22:15:41 +01:00
|
|
|
if (previous_best_.has_value() && previous_best_.value() == *best &&
|
|
|
|
!previous_format_.empty() && previous_format_ == format_) {
|
2019-02-10 10:35:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-24 22:15:41 +01:00
|
|
|
|
2022-10-18 23:43:15 +02:00
|
|
|
if (best->get_powered()) {
|
2022-11-07 09:06:16 +01:00
|
|
|
event_box_.show();
|
2022-10-18 23:43:15 +02:00
|
|
|
const uint8_t percent =
|
|
|
|
best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max());
|
2023-07-18 08:28:19 +02:00
|
|
|
std::string desc = fmt::format(fmt::runtime(format_), fmt::arg("percent", percent),
|
|
|
|
fmt::arg("icon", getIcon(percent)));
|
2023-04-11 15:01:02 +02:00
|
|
|
label_.set_markup(desc);
|
2023-04-17 09:01:14 +02:00
|
|
|
getState(percent);
|
2023-04-11 15:01:02 +02: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 09:01:14 +02:00
|
|
|
label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
|
2023-07-16 04:14:43 +02:00
|
|
|
fmt::arg("percent", percent),
|
2023-04-17 09:01:14 +02:00
|
|
|
fmt::arg("icon", getIcon(percent))));
|
2023-04-11 15:01:02 +02:00
|
|
|
} else {
|
|
|
|
label_.set_tooltip_text(desc);
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 23:43:15 +02:00
|
|
|
} else {
|
|
|
|
event_box_.hide();
|
2022-11-07 09:06:16 +01:00
|
|
|
}
|
2019-02-10 10:35:19 +01:00
|
|
|
} else {
|
|
|
|
if (!previous_best_.has_value()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-24 12:28:52 +01:00
|
|
|
label_.set_markup("");
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
previous_best_ = best == nullptr ? std::nullopt : std::optional{*best};
|
2019-02-24 22:15:41 +01:00
|
|
|
previous_format_ = format_;
|
2020-04-12 18:30:21 +02:00
|
|
|
// Call parent update
|
2022-11-24 12:28:52 +01:00
|
|
|
ALabel::update();
|
2019-02-10 10:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ForwardIt>
|
2019-04-18 17:52:00 +02:00
|
|
|
const waybar::modules::Backlight::BacklightDev *waybar::modules::Backlight::best_device(
|
|
|
|
ForwardIt first, ForwardIt last, std::string_view preferred_device) {
|
|
|
|
const auto found = std::find_if(
|
|
|
|
first, last, [preferred_device](const auto &dev) { return dev.name() == preferred_device; });
|
2019-02-10 10:35:19 +01:00
|
|
|
if (found != last) {
|
|
|
|
return &(*found);
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
const auto max = std::max_element(
|
|
|
|
first, last, [](const auto &l, const auto &r) { return l.get_max() < r.get_max(); });
|
2019-02-10 10:35:19 +01:00
|
|
|
|
|
|
|
return max == last ? nullptr : &(*max);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ForwardIt, class Inserter>
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last, Inserter inserter,
|
2019-02-10 10:35:19 +01:00
|
|
|
udev_device *dev) {
|
|
|
|
const char *name = udev_device_get_sysname(dev);
|
|
|
|
check_nn(name);
|
|
|
|
|
2019-05-22 12:06:24 +02:00
|
|
|
const char *actual_brightness_attr =
|
2020-04-21 09:11:42 +02:00
|
|
|
strncmp(name, "amdgpu_bl", 9) == 0 ? "brightness" : "actual_brightness";
|
2019-05-17 21:30:45 +02:00
|
|
|
|
|
|
|
const char *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr);
|
2019-02-10 10:35:19 +01:00
|
|
|
const char *max = udev_device_get_sysattr_value(dev, "max_brightness");
|
2022-10-18 23:43:15 +02:00
|
|
|
const char *power = udev_device_get_sysattr_value(dev, "bl_power");
|
2019-02-10 10:35:19 +01:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto found =
|
|
|
|
std::find_if(first, last, [name](const auto &device) { return device.name() == name; });
|
2019-02-10 10:35:19 +01:00
|
|
|
if (found != last) {
|
2022-04-22 05:19:05 +02:00
|
|
|
if (actual != nullptr) {
|
|
|
|
found->set_actual(std::stoi(actual));
|
|
|
|
}
|
|
|
|
if (max != nullptr) {
|
|
|
|
found->set_max(std::stoi(max));
|
|
|
|
}
|
2022-10-18 23:43:15 +02:00
|
|
|
if (power != nullptr) {
|
|
|
|
found->set_powered(std::stoi(power) == 0);
|
|
|
|
}
|
2019-02-10 10:35:19 +01:00
|
|
|
} else {
|
2022-04-22 05:19:05 +02:00
|
|
|
const int actual_int = actual == nullptr ? 0 : std::stoi(actual);
|
|
|
|
const int max_int = max == nullptr ? 0 : std::stoi(max);
|
2022-10-18 23:43:15 +02:00
|
|
|
const bool power_bool = power == nullptr ? true : std::stoi(power) == 0;
|
|
|
|
*inserter = BacklightDev{name, actual_int, max_int, power_bool};
|
2019-02-10 10:35:19 +01:00
|
|
|
++inserter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ForwardIt, class Inserter>
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Backlight::enumerate_devices(ForwardIt first, ForwardIt last,
|
|
|
|
Inserter inserter, udev *udev) {
|
|
|
|
std::unique_ptr<udev_enumerate, UdevEnumerateDeleter> enumerate{udev_enumerate_new(udev)};
|
2019-02-10 10:35:19 +01:00
|
|
|
udev_enumerate_add_match_subsystem(enumerate.get(), "backlight");
|
|
|
|
udev_enumerate_scan_devices(enumerate.get());
|
2019-04-18 17:52:00 +02:00
|
|
|
udev_list_entry *enum_devices = udev_enumerate_get_list_entry(enumerate.get());
|
2019-02-10 10:35:19 +01:00
|
|
|
udev_list_entry *dev_list_entry;
|
|
|
|
udev_list_entry_foreach(dev_list_entry, enum_devices) {
|
2022-04-06 08:37:19 +02:00
|
|
|
const char *path = udev_list_entry_get_name(dev_list_entry);
|
2019-04-18 17:52:00 +02:00
|
|
|
std::unique_ptr<udev_device, UdevDeviceDeleter> dev{udev_device_new_from_syspath(udev, path)};
|
2019-02-10 10:35:19 +01:00
|
|
|
check_nn(dev.get(), "dev new failed");
|
|
|
|
upsert_device(first, last, inserter, dev.get());
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 14:40:36 +01: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 12:58:52 +01:00
|
|
|
// Fail fast if the proxy could not be initialized
|
|
|
|
if (!login_proxy_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-01 14:40:36 +01:00
|
|
|
// Check scroll direction
|
|
|
|
auto dir = AModule::getScrollDir(e);
|
|
|
|
if (dir == SCROLL_DIR::NONE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get scroll step
|
|
|
|
double step = 1;
|
|
|
|
|
|
|
|
if (config_["scroll-step"].isDouble()) {
|
|
|
|
step = config_["scroll-step"].asDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the best device
|
|
|
|
decltype(devices_) devices;
|
|
|
|
{
|
|
|
|
std::scoped_lock<std::mutex> lock(udev_thread_mutex_);
|
|
|
|
devices = devices_;
|
|
|
|
}
|
|
|
|
const auto best = best_device(devices.cbegin(), devices.cend(), preferred_device_);
|
|
|
|
|
|
|
|
if (best == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the absolute step
|
|
|
|
const auto abs_step = static_cast<int>(round(step * best->get_max() / 100.0f));
|
|
|
|
|
|
|
|
// Compute the new value
|
|
|
|
int new_value = best->get_actual();
|
|
|
|
|
|
|
|
if (dir == SCROLL_DIR::UP) {
|
2023-02-03 18:18:44 +01:00
|
|
|
new_value += abs_step;
|
2023-02-01 14:40:36 +01:00
|
|
|
} else if (dir == SCROLL_DIR::DOWN) {
|
2023-02-03 18:18:44 +01:00
|
|
|
new_value -= abs_step;
|
2023-02-01 14:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clamp the value
|
|
|
|
new_value = std::clamp(new_value, 0, best->get_max());
|
|
|
|
|
|
|
|
// Set the new value
|
2023-02-03 18:18:44 +01:00
|
|
|
auto call_args = Glib::VariantContainerBase(
|
|
|
|
g_variant_new("(ssu)", "backlight", std::string(best->name()).c_str(), new_value));
|
|
|
|
|
2023-02-03 12:58:52 +01:00
|
|
|
login_proxy_->call_sync("SetBrightness", call_args);
|
2023-02-01 14:40:36 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|