mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-13 22:52:30 +02:00
feat: backlight slider
This commit is contained in:
@ -4,6 +4,10 @@
|
||||
#include "modules/pulseaudio_slider.hpp"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LIBUDEV
|
||||
#include "modules/backlight_slider.hpp"
|
||||
#endif
|
||||
|
||||
waybar::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_(bar), config_(config) {}
|
||||
|
||||
waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
|
||||
@ -130,6 +134,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
|
||||
if (ref == "backlight") {
|
||||
return new waybar::modules::Backlight(id, config_[name]);
|
||||
}
|
||||
if (ref == "backlight/slider") {
|
||||
return new waybar::modules::BacklightSlider(id, config_[name]);
|
||||
}
|
||||
#endif
|
||||
#ifdef HAVE_LIBEVDEV
|
||||
if (ref == "keyboard-state") {
|
||||
|
23
src/modules/backlight_slider.cpp
Normal file
23
src/modules/backlight_slider.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "modules/backlight_slider.hpp"
|
||||
|
||||
#include "ASlider.hpp"
|
||||
|
||||
namespace waybar::modules {
|
||||
|
||||
BacklightSlider::BacklightSlider(const std::string& id, const Json::Value& config)
|
||||
: ASlider(config, "backlight-slider", id),
|
||||
interval_(config_["interval"].isUInt() ? config_["interval"].asUInt() : 1000),
|
||||
preferred_device_(config["device"].isString() ? config["device"].asString() : ""),
|
||||
backend(interval_, [this] { this->dp.emit(); }) {}
|
||||
|
||||
void BacklightSlider::update() {
|
||||
uint16_t brightness = backend.get_scaled_brightness(preferred_device_);
|
||||
scale_.set_value(brightness);
|
||||
}
|
||||
|
||||
void BacklightSlider::onValueChanged() {
|
||||
auto brightness = scale_.get_value();
|
||||
backend.set_scaled_brightness(preferred_device_, brightness);
|
||||
}
|
||||
|
||||
} // namespace waybar::modules
|
@ -188,11 +188,13 @@ void BacklightBackend::set_previous_best_device(const BacklightDevice *device) {
|
||||
}
|
||||
}
|
||||
|
||||
void BacklightBackend::set_brightness(std::string preferred_device, int brightness) {
|
||||
void BacklightBackend::set_scaled_brightness(std::string preferred_device, int brightness) {
|
||||
GET_BEST_DEVICE(best, (*this), preferred_device);
|
||||
|
||||
if (best != nullptr) {
|
||||
set_brightness_internal(best->name(), brightness, best->get_max());
|
||||
const auto max = best->get_max();
|
||||
const auto abs_val = static_cast<int>(round(brightness * max / 100.0f));
|
||||
set_brightness_internal(best->name(), abs_val, best->get_max());
|
||||
}
|
||||
}
|
||||
|
||||
@ -221,6 +223,16 @@ void BacklightBackend::set_brightness_internal(std::string device_name, int brig
|
||||
login_proxy_->call_sync("SetBrightness", call_args);
|
||||
}
|
||||
|
||||
int BacklightBackend::get_scaled_brightness(std::string preferred_device) {
|
||||
GET_BEST_DEVICE(best, (*this), preferred_device);
|
||||
|
||||
if (best != nullptr) {
|
||||
return best->get_actual() * 100 / best->get_max();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <class ForwardIt, class Inserter>
|
||||
void BacklightBackend::upsert_device(ForwardIt first, ForwardIt last, Inserter inserter,
|
||||
udev_device *dev) {
|
||||
|
Reference in New Issue
Block a user