refactor: move backlight backend out of backlight module

This commit is contained in:
Brenno Lemos
2023-10-15 16:14:06 -03:00
parent 442a4b0da0
commit c3779dd16e
8 changed files with 419 additions and 317 deletions

View File

@ -1,14 +1,14 @@
#pragma once
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "ALabel.hpp"
#include "giomm/dbusproxy.h"
#include "util/backlight_backend.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"
struct udev;
struct udev_device;
@ -16,54 +16,17 @@ struct udev_device;
namespace waybar::modules {
class Backlight : public ALabel {
class BacklightDev {
public:
BacklightDev() = default;
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_;
}
private:
std::string name_;
int actual_ = 1;
int max_ = 1;
bool powered_ = true;
};
public:
Backlight(const std::string &, const Json::Value &);
virtual ~Backlight();
virtual ~Backlight() = default;
auto update() -> void override;
private:
template <class ForwardIt>
static const BacklightDev *best_device(ForwardIt first, ForwardIt last, std::string_view);
template <class ForwardIt, class Inserter>
static void upsert_device(ForwardIt first, ForwardIt last, Inserter inserter, udev_device *dev);
template <class ForwardIt, class Inserter>
static void enumerate_devices(ForwardIt first, ForwardIt last, Inserter inserter, udev *udev);
bool handleScroll(GdkEventScroll *e) override;
const std::string preferred_device_;
static constexpr int EPOLL_MAX_EVENTS = 16;
std::optional<BacklightDev> previous_best_;
std::string previous_format_;
std::mutex udev_thread_mutex_;
std::vector<BacklightDev> devices_;
// thread must destruct before shared data
util::SleeperThread udev_thread_;
Glib::RefPtr<Gio::DBus::Proxy> login_proxy_;
util::BacklightBackend backend;
};
} // namespace waybar::modules

View File

@ -10,12 +10,10 @@
#include <memory>
#include <string>
#include "util/backend_common.hpp"
namespace waybar::util {
enum class ChangeType : char { Increase, Decrease };
void noop();
class AudioBackend {
private:
static void subscribeCb(pa_context*, pa_subscription_event_type_t, uint32_t, void*);
@ -50,7 +48,7 @@ class AudioBackend {
std::vector<std::string> ignored_sinks_;
std::function<void()> on_updated_cb_ = noop;
std::function<void()> on_updated_cb_ = NOOP;
/* Hack to keep constructor inaccessible but still public.
* This is required to be able to use std::make_shared.
@ -61,7 +59,7 @@ class AudioBackend {
struct private_constructor_tag {};
public:
static std::shared_ptr<AudioBackend> getInstance(std::function<void()> on_updated_cb = noop);
static std::shared_ptr<AudioBackend> getInstance(std::function<void()> on_updated_cb = NOOP);
AudioBackend(std::function<void()> on_updated_cb, private_constructor_tag tag);
~AudioBackend();

View File

@ -0,0 +1,10 @@
#pragma once
#include "AModule.hpp"
namespace waybar::util {
const static auto NOOP = []() {};
enum class ChangeType : char { Increase, Decrease };
} // namespace waybar::util

View File

@ -0,0 +1,91 @@
#pragma once
#include <libudev.h>
#include <spdlog/spdlog.h>
#include <chrono>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "giomm/dbusproxy.h"
#include "util/backend_common.hpp"
#include "util/sleeper_thread.hpp"
#define GET_BEST_DEVICE(varname, backend, preferred_device) \
decltype((backend).devices_) __devices; \
{ \
std::scoped_lock<std::mutex> lock((backend).udev_thread_mutex_); \
__devices = (backend).devices_; \
} \
auto varname = (backend).best_device(__devices.cbegin(), __devices.cend(), preferred_device);
namespace waybar::util {
class BacklightDevice {
public:
BacklightDevice() = default;
BacklightDevice(std::string name, int actual, int max, bool powered);
std::string 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 BacklightDevice &lhs, const BacklightDevice &rhs) {
return lhs.name_ == rhs.name_ && lhs.actual_ == rhs.actual_ && lhs.max_ == rhs.max_;
}
private:
std::string name_;
int actual_ = 1;
int max_ = 1;
bool powered_ = true;
};
class BacklightBackend {
public:
BacklightBackend(std::chrono::milliseconds interval, std::function<void()> on_updated_cb = NOOP);
// const inline BacklightDevice *get_best_device(std::string_view preferred_device);
const BacklightDevice *get_previous_best_device();
void set_previous_best_device(const BacklightDevice *device);
void set_brightness(std::string preferred_device, int brightness);
void set_brightness(std::string preferred_device, ChangeType change_type, double step);
template <class ForwardIt, class Inserter>
static void upsert_device(ForwardIt first, ForwardIt last, Inserter inserter, udev_device *dev);
template <class ForwardIt, class Inserter>
static void enumerate_devices(ForwardIt first, ForwardIt last, Inserter inserter, udev *udev);
bool is_login_proxy_initialized() const { return static_cast<bool>(login_proxy_); }
template <class ForwardIt>
static const BacklightDevice *best_device(ForwardIt first, ForwardIt last, std::string_view);
std::vector<BacklightDevice> devices_;
std::mutex udev_thread_mutex_;
private:
void set_brightness_internal(std::string device_name, int brightness, int max_brightness);
std::function<void()> on_updated_cb_;
std::chrono::milliseconds polling_interval_;
std::optional<BacklightDevice> previous_best_;
// thread must destruct before shared data
util::SleeperThread udev_thread_;
Glib::RefPtr<Gio::DBus::Proxy> login_proxy_;
static constexpr int EPOLL_MAX_EVENTS = 16;
};
} // namespace waybar::util