Waybar/include/modules/backlight.hpp

62 lines
1.6 KiB
C++
Raw Normal View History

#pragma once
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "AButton.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"
struct udev;
struct udev_device;
namespace waybar::modules {
class Backlight : public AButton {
class BacklightDev {
2019-04-18 15:52:00 +00:00
public:
BacklightDev() = default;
BacklightDev(std::string name, int actual, int max);
2022-04-06 06:37:19 +00:00
std::string_view name() const;
int get_actual() const;
void set_actual(int actual);
int get_max() const;
void set_max(int max);
2019-04-18 15:52:00 +00:00
friend inline bool operator==(const BacklightDev &lhs, const BacklightDev &rhs) {
return lhs.name_ == rhs.name_ && lhs.actual_ == rhs.actual_ && lhs.max_ == rhs.max_;
}
2019-04-18 15:52:00 +00:00
private:
std::string name_;
2022-04-06 06:37:19 +00:00
int actual_ = 1;
int max_ = 1;
};
2019-04-18 15:52:00 +00:00
public:
Backlight(const std::string &, const Json::Value &);
~Backlight();
auto update() -> void;
2019-04-18 15:52:00 +00:00
private:
template <class ForwardIt>
2019-04-18 15:52:00 +00:00
static const BacklightDev *best_device(ForwardIt first, ForwardIt last, std::string_view);
template <class ForwardIt, class Inserter>
2019-04-18 15:52:00 +00:00
static void upsert_device(ForwardIt first, ForwardIt last, Inserter inserter, udev_device *dev);
template <class ForwardIt, class Inserter>
2019-04-18 15:52:00 +00:00
static void enumerate_devices(ForwardIt first, ForwardIt last, Inserter inserter, udev *udev);
2022-04-06 06:37:19 +00:00
const std::string preferred_device_;
static constexpr int EPOLL_MAX_EVENTS = 16;
std::optional<BacklightDev> previous_best_;
2022-04-06 06:37:19 +00:00
std::string previous_format_;
2022-04-06 06:37:19 +00:00
std::mutex udev_thread_mutex_;
std::vector<BacklightDev> devices_;
// thread must destruct before shared data
2019-05-29 15:53:22 +00:00
util::SleeperThread udev_thread_;
};
2019-04-18 15:52:00 +00:00
} // namespace waybar::modules