feat: backlight slider

This commit is contained in:
Brenno Lemos 2023-10-15 17:42:19 -03:00
parent c3779dd16e
commit 11d7ca1d73
7 changed files with 161 additions and 3 deletions

View File

@ -0,0 +1,24 @@
#pragma once
#include <chrono>
#include "ASlider.hpp"
#include "util/backlight_backend.hpp"
namespace waybar::modules {
class BacklightSlider : public ASlider {
public:
BacklightSlider(const std::string&, const Json::Value&);
virtual ~BacklightSlider() = default;
void update() override;
void onValueChanged() override;
private:
std::chrono::milliseconds interval_;
std::string preferred_device_;
util::BacklightBackend backend;
};
} // namespace waybar::modules

View File

@ -56,9 +56,11 @@ class BacklightBackend {
void set_previous_best_device(const BacklightDevice *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); void set_brightness(std::string preferred_device, ChangeType change_type, double step);
void set_scaled_brightness(std::string preferred_device, int brightness);
int get_scaled_brightness(std::string preferred_device);
template <class ForwardIt, class Inserter> template <class ForwardIt, class Inserter>
static void upsert_device(ForwardIt first, ForwardIt last, Inserter inserter, udev_device *dev); static void upsert_device(ForwardIt first, ForwardIt last, Inserter inserter, udev_device *dev);

View File

@ -0,0 +1,88 @@
waybar-backlight-slider(5)
# NAME
waybar - backlight slider module
# DESCRIPTION
The *backlight slider* module displays and controls the current brightness of the default or preferred device.
The brightness can be controlled by dragging the slider accross the bar, or clicking on a specific position.
# CONFIGURATION
*min*: ++
typeof: int ++
default: 0 ++
The minimum volume value the slider should display and set.
*max*: ++
typeof: int ++
default: 100 ++
The maximum volume value the slider should display and set.
*orientation*: ++
typeof: string ++
default: horizontal ++
The orientation of the slider. Can be either `horizontal` or `vertical`.
*device*: ++
typeof: string ++
The name of the preferred device to control. If left empty, a device will be chosen automatically.
# EXAMPLES
```
"modules-right": [
"backlight-slider",
],
"backlight/slider": {
"min": 0,
"max": 100,
"orientation": "horizontal",
"device": "intel_backlight"
}
```
# STYLE
The slider is a component with multiple CSS Nodes, of which the following are exposed:
*#backlight-slider*: ++
Controls the style of the box *around* the slider and bar.
*#backlight-slider slider*: ++
Controls the style of the slider handle.
*#backlight-slider trough*: ++
Controls the style of the part of the bar that has not been filled.
*#backlight-slider highlight*: ++
Controls the style of the part of the bar that has been filled.
## STYLE EXAMPLE
```
#backlight-slider slider {
min-height: 0px;
min-width: 0px;
opacity: 0;
background-image: none;
border: none;
box-shadow: none;
}
#backlight-slider trough {
min-height: 80px;
min-width: 10px;
border-radius: 5px;
background-color: black;
}
#backlight-slider highlight {
min-width: 10px;
border-radius: 5px;
background-color: red;
}
```

View File

@ -301,6 +301,7 @@ endif
if libudev.found() and (is_linux or libepoll.found()) if libudev.found() and (is_linux or libepoll.found())
add_project_arguments('-DHAVE_LIBUDEV', language: 'cpp') add_project_arguments('-DHAVE_LIBUDEV', language: 'cpp')
src_files += 'src/modules/backlight.cpp' src_files += 'src/modules/backlight.cpp'
src_files += 'src/modules/backlight_slider.cpp'
src_files += 'src/util/backlight_backend.cpp' src_files += 'src/util/backlight_backend.cpp'
endif endif
@ -429,6 +430,7 @@ if scdoc.found()
man_files = [ man_files = [
main_manpage_path, main_manpage_path,
'waybar-backlight.5.scd', 'waybar-backlight.5.scd',
'waybar-backlight-slider.5.scd',
'waybar-battery.5.scd', 'waybar-battery.5.scd',
'waybar-cava.5.scd', 'waybar-cava.5.scd',
'waybar-clock.5.scd', 'waybar-clock.5.scd',

View File

@ -4,6 +4,10 @@
#include "modules/pulseaudio_slider.hpp" #include "modules/pulseaudio_slider.hpp"
#endif #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::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_(bar), config_(config) {}
waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { 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") { if (ref == "backlight") {
return new waybar::modules::Backlight(id, config_[name]); return new waybar::modules::Backlight(id, config_[name]);
} }
if (ref == "backlight/slider") {
return new waybar::modules::BacklightSlider(id, config_[name]);
}
#endif #endif
#ifdef HAVE_LIBEVDEV #ifdef HAVE_LIBEVDEV
if (ref == "keyboard-state") { if (ref == "keyboard-state") {

View 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

View File

@ -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); GET_BEST_DEVICE(best, (*this), preferred_device);
if (best != nullptr) { 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); 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> template <class ForwardIt, class Inserter>
void BacklightBackend::upsert_device(ForwardIt first, ForwardIt last, Inserter inserter, void BacklightBackend::upsert_device(ForwardIt first, ForwardIt last, Inserter inserter,
udev_device *dev) { udev_device *dev) {