diff --git a/include/ALabel.hpp b/include/ALabel.hpp index 816fd4f5..c4fa9234 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -24,7 +24,7 @@ class ALabel : public AModule, public Gtk::Label { bool alt_ = false; std::string default_format_; - void handleToggle(int n_press, double dx, double dy); + void handleToggle(int n_press, double dx, double dy) override; virtual std::string getState(uint8_t value, bool lesser = false); }; diff --git a/include/AModule.hpp b/include/AModule.hpp index d610b0df..07b085c8 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -31,6 +31,7 @@ class AModule : public IModule { const Json::Value &config_; Glib::RefPtr controllClick_; Glib::RefPtr controllScroll_; + enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT }; void bindEvents(Gtk::Widget& wg); bool tooltipEnabled(); @@ -38,9 +39,9 @@ class AModule : public IModule { virtual void handleToggle(int n_press, double dx, double dy); virtual void handleRelease(int n_press, double dx, double dy); virtual bool handleScroll(double dx, double dy); + const SCROLL_DIR getScrollDir(Glib::RefPtr e); private: - enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT }; const bool isTooltip; std::vector pid_; double distance_scrolled_x_{0.0}; @@ -70,7 +71,6 @@ class AModule : public IModule { {std::make_pair(std::make_pair(9u, 3), Gdk::Event::Type::BUTTON_PRESS), "on-triple-click-forward"} }; void handleClickEvent(uint n_button, int n_press, Gdk::Event::Type n_evtype); - const SCROLL_DIR getScrollDir(Glib::RefPtr e); }; } // namespace waybar diff --git a/include/ASlider.hpp b/include/ASlider.hpp index 44cde507..665c52e7 100644 --- a/include/ASlider.hpp +++ b/include/ASlider.hpp @@ -11,9 +11,9 @@ class ASlider : public AModule { virtual void onValueChanged(); protected: - bool vertical_ = false; - int min_ = 0, max_ = 100, curr_ = 50; + bool vertical_{false}; + int min_{0}, max_{100}, curr_{50}; Gtk::Scale scale_; }; -} // namespace waybar \ No newline at end of file +} // namespace waybar diff --git a/include/modules/backlight.hpp b/include/modules/backlight.hpp index 110cd434..12361294 100644 --- a/include/modules/backlight.hpp +++ b/include/modules/backlight.hpp @@ -1,32 +1,22 @@ #pragma once -#include -#include -#include -#include -#include - #include "ALabel.hpp" #include "util/backlight_backend.hpp" -#include "util/json.hpp" - -struct udev; -struct udev_device; namespace waybar::modules { -class Backlight : public ALabel { +class Backlight final : public ALabel { public: Backlight(const std::string &, const Json::Value &); virtual ~Backlight() = default; auto update() -> void override; - bool handleScroll(GdkEventScroll *e) override; - + private: const std::string preferred_device_; - std::string previous_format_; - util::BacklightBackend backend; + + bool handleScroll(double dx, double dy) override; }; + } // namespace waybar::modules diff --git a/include/modules/backlight_slider.hpp b/include/modules/backlight_slider.hpp index 437c53c4..30fad3fc 100644 --- a/include/modules/backlight_slider.hpp +++ b/include/modules/backlight_slider.hpp @@ -1,13 +1,11 @@ #pragma once -#include - #include "ASlider.hpp" #include "util/backlight_backend.hpp" namespace waybar::modules { -class BacklightSlider : public ASlider { +class BacklightSlider final : public ASlider { public: BacklightSlider(const std::string&, const Json::Value&); virtual ~BacklightSlider() = default; @@ -21,4 +19,4 @@ class BacklightSlider : public ASlider { util::BacklightBackend backend; }; -} // namespace waybar::modules \ No newline at end of file +} // namespace waybar::modules diff --git a/include/util/backend_common.hpp b/include/util/backend_common.hpp index dda6ac57..6f6f348b 100644 --- a/include/util/backend_common.hpp +++ b/include/util/backend_common.hpp @@ -1,10 +1,8 @@ #pragma once -#include "AModule.hpp" - namespace waybar::util { const static auto NOOP = []() {}; enum class ChangeType : char { Increase, Decrease }; -} // namespace waybar::util \ No newline at end of file +} // namespace waybar::util diff --git a/include/util/backlight_backend.hpp b/include/util/backlight_backend.hpp index 8dcb8958..0d6afa47 100644 --- a/include/util/backlight_backend.hpp +++ b/include/util/backlight_backend.hpp @@ -1,14 +1,6 @@ #pragma once #include -#include - -#include -#include -#include -#include -#include -#include #include "giomm/dbusproxy.h" #include "util/backend_common.hpp" @@ -42,9 +34,9 @@ class BacklightDevice { private: std::string name_; - int actual_ = 1; - int max_ = 1; - bool powered_ = true; + int actual_{1}; + int max_{1}; + bool powered_{true}; }; class BacklightBackend { @@ -87,7 +79,7 @@ class BacklightBackend { Glib::RefPtr login_proxy_; - static constexpr int EPOLL_MAX_EVENTS = 16; + static constexpr int EPOLL_MAX_EVENTS{16}; }; -} // namespace waybar::util \ No newline at end of file +} // namespace waybar::util diff --git a/meson.build b/meson.build index 1fce8144..a9d16df6 100644 --- a/meson.build +++ b/meson.build @@ -86,6 +86,7 @@ sigcpp = dependency('sigc++-3.0', version: ['>=3.4.0']) gtkmm = dependency('gtkmm-4.0', version : ['>=4.12.0']) giounix = dependency('gio-unix-2.0', version: ['>=2.76.4']) spdlog = dependency('spdlog', version : ['>=1.10.0'], fallback : ['spdlog', 'spdlog_dep'], default_options : ['external_fmt=enabled']) +libudev = dependency('libudev', required: get_option('libudev')) gtk_layer_shell = dependency('gtk4-layer-shell-0', version : ['>=1.0.2'], @@ -137,7 +138,8 @@ src_files = files( 'src/ALabel.cpp', 'src/factory.cpp', 'src/util/ustring_clen.cpp', - 'src/group.cpp' + 'src/group.cpp', + 'src/ASlider.cpp' ) man_files = files( @@ -166,6 +168,19 @@ if get_option('experimental') add_project_arguments('-DUSE_EXPERIMENTAL', language: 'cpp') endif +if libudev.found() and (is_linux or libepoll.found()) + add_project_arguments('-DHAVE_LIBUDEV', language: 'cpp') + src_files += files( + 'src/modules/backlight.cpp', + 'src/modules/backlight_slider.cpp', + 'src/util/backlight_backend.cpp', + ) + man_files += files( + 'man/waybar-backlight.5.scd', + 'man/waybar-backlight-slider.5.scd', + ) +endif + cava = dependency('cava', version : '>=0.10.1', required: get_option('cava'), @@ -189,6 +204,7 @@ executable( gtkmm, giounix, sigcpp, + libudev, jsoncpp, wayland_client, wayland_cursor, diff --git a/src/AModule.cpp b/src/AModule.cpp index 47533e4c..8d3fca9c 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -182,9 +182,9 @@ bool AModule::handleScroll(double dx, double dy) { std::string format{}; const auto dir{getScrollDir(currEvent_)}; - if (dir == SCROLL_DIR::UP) + if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::RIGHT) format = "on-scroll-up"; - else if (dir == SCROLL_DIR::DOWN) + else if (dir == SCROLL_DIR::DOWN || dir == SCROLL_DIR::LEFT) format = "on-scroll-down"; // First call module action diff --git a/src/ASlider.cpp b/src/ASlider.cpp index b434be30..0858dfc0 100644 --- a/src/ASlider.cpp +++ b/src/ASlider.cpp @@ -1,20 +1,18 @@ #include "ASlider.hpp" #include "gtkmm/adjustment.h" -#include "gtkmm/enums.h" namespace waybar { ASlider::ASlider(const Json::Value& config, const std::string& name, const std::string& id) : AModule(config, name, id, false, false), vertical_(config_["orientation"].asString() == "vertical"), - scale_(vertical_ ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL) { + scale_(vertical_ ? Gtk::Orientation::VERTICAL : Gtk::Orientation::HORIZONTAL) { scale_.set_name(name); if (!id.empty()) { scale_.get_style_context()->add_class(id); } scale_.get_style_context()->add_class(MODULE_CLASS); - event_box_.add(scale_); scale_.signal_value_changed().connect(sigc::mem_fun(*this, &ASlider::onValueChanged)); if (config_["min"].isUInt()) { @@ -32,4 +30,4 @@ ASlider::ASlider(const Json::Value& config, const std::string& name, const std:: void ASlider::onValueChanged() {} -} // namespace waybar \ No newline at end of file +} // namespace waybar diff --git a/src/factory.cpp b/src/factory.cpp index 13e19b64..2505204a 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -250,7 +250,7 @@ gtk4 todo if (ref == "network") { return new waybar::modules::Network(id, config_[name]); } -#endif +#endif*/ #ifdef HAVE_LIBUDEV if (ref == "backlight") { return new waybar::modules::Backlight(id, config_[name]); @@ -258,7 +258,7 @@ gtk4 todo if (ref == "backlight/slider") { return new waybar::modules::BacklightSlider(id, config_[name]); } -#endif +#endif/* #ifdef HAVE_LIBEVDEV if (ref == "keyboard-state") { return new waybar::modules::KeyboardState(id, bar_, config_[name]); diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index 4ae511eb..1e51da9b 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -1,27 +1,12 @@ #include "modules/backlight.hpp" #include -#include -#include -#include -#include - -#include -#include -#include - -#include "util/backend_common.hpp" -#include "util/backlight_backend.hpp" waybar::modules::Backlight::Backlight(const std::string &id, const Json::Value &config) - : ALabel(config, "backlight", id, "{percent}%", 2), + : ALabel(config, "backlight", id, "{percent}%", 2, false, false, true), preferred_device_(config["device"].isString() ? config["device"].asString() : ""), backend(interval_, [this] { dp.emit(); }) { dp.emit(); - - // 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)); } auto waybar::modules::Backlight::update() -> void { @@ -35,12 +20,12 @@ auto waybar::modules::Backlight::update() -> void { } if (best->get_powered()) { - event_box_.show(); + Gtk::Label::show(); const uint8_t percent = best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max()); std::string desc = fmt::format(fmt::runtime(format_), fmt::arg("percent", percent), fmt::arg("icon", getIcon(percent))); - label_.set_markup(desc); + Gtk::Label::set_markup(desc); getState(percent); if (tooltipEnabled()) { std::string tooltip_format; @@ -48,31 +33,31 @@ auto waybar::modules::Backlight::update() -> void { tooltip_format = config_["tooltip-format"].asString(); } if (!tooltip_format.empty()) { - label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format), + Gtk::Label::set_tooltip_text(fmt::format(fmt::runtime(tooltip_format), fmt::arg("percent", percent), fmt::arg("icon", getIcon(percent)))); } else { - label_.set_tooltip_text(desc); + Gtk::Label::set_tooltip_text(desc); } } } else { - event_box_.hide(); + Gtk::Label::hide(); } } else { if (previous_best_device == nullptr) { return; } - label_.set_markup(""); + Gtk::Label::set_markup(""); } backend.set_previous_best_device(best); previous_format_ = format_; ALabel::update(); } -bool waybar::modules::Backlight::handleScroll(GdkEventScroll *e) { +bool waybar::modules::Backlight::handleScroll(double dx, double dy) { // 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); + return AModule::handleScroll(dx, dy); } // Fail fast if the proxy could not be initialized @@ -81,7 +66,7 @@ bool waybar::modules::Backlight::handleScroll(GdkEventScroll *e) { } // Check scroll direction - auto dir = AModule::getScrollDir(e); + auto dir = AModule::getScrollDir(controllScroll_->get_current_event()); // No worries, it will always be set because of the switch below. This is purely to suppress a // warning diff --git a/src/modules/backlight_slider.cpp b/src/modules/backlight_slider.cpp index 6269dddb..09a178f7 100644 --- a/src/modules/backlight_slider.cpp +++ b/src/modules/backlight_slider.cpp @@ -1,6 +1,5 @@ -#include "modules/backlight_slider.hpp" - #include "ASlider.hpp" +#include "modules/backlight_slider.hpp" namespace waybar::modules { @@ -20,4 +19,4 @@ void BacklightSlider::onValueChanged() { backend.set_scaled_brightness(preferred_device_, brightness); } -} // namespace waybar::modules \ No newline at end of file +} // namespace waybar::modules diff --git a/src/util/backlight_backend.cpp b/src/util/backlight_backend.cpp index 1512103c..429f8a53 100644 --- a/src/util/backlight_backend.cpp +++ b/src/util/backlight_backend.cpp @@ -1,11 +1,9 @@ #include "util/backlight_backend.hpp" +#include #include -#include #include -#include - namespace { class FileDescriptor { public: @@ -103,7 +101,7 @@ BacklightBackend::BacklightBackend(std::chrono::milliseconds interval, // Connect to the login interface login_proxy_ = Gio::DBus::Proxy::create_for_bus_sync( - Gio::DBus::BusType::BUS_TYPE_SYSTEM, "org.freedesktop.login1", + Gio::DBus::BusType::SYSTEM, "org.freedesktop.login1", "/org/freedesktop/login1/session/self", "org.freedesktop.login1.Session"); udev_thread_ = [this] { @@ -193,7 +191,7 @@ void BacklightBackend::set_scaled_brightness(std::string preferred_device, int b if (best != nullptr) { const auto max = best->get_max(); - const auto abs_val = static_cast(round(brightness * max / 100.0f)); + const auto abs_val = static_cast(std::round(brightness * max / 100.0f)); set_brightness_internal(best->name(), abs_val, best->get_max()); } } @@ -205,7 +203,7 @@ void BacklightBackend::set_brightness(std::string preferred_device, ChangeType c if (best != nullptr) { const auto max = best->get_max(); - const auto abs_step = static_cast(round(step * max / 100.0f)); + const auto abs_step = static_cast(std::round(step * max / 100.0f)); const int new_brightness = change_type == ChangeType::Increase ? best->get_actual() + abs_step : best->get_actual() - abs_step; @@ -285,4 +283,4 @@ void BacklightBackend::enumerate_devices(ForwardIt first, ForwardIt last, Insert } } -} // namespace waybar::util \ No newline at end of file +} // namespace waybar::util