GTK4. Migration. backlight, slider, backend_common

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
pull/2956/head
Viktar Lukashonak 2024-02-27 23:17:50 +03:00
parent 0721fd593c
commit 69648ab8f4
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
14 changed files with 59 additions and 85 deletions

View File

@ -24,7 +24,7 @@ class ALabel : public AModule, public Gtk::Label {
bool alt_ = false; bool alt_ = false;
std::string default_format_; 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); virtual std::string getState(uint8_t value, bool lesser = false);
}; };

View File

@ -31,6 +31,7 @@ class AModule : public IModule {
const Json::Value &config_; const Json::Value &config_;
Glib::RefPtr<Gtk::GestureClick> controllClick_; Glib::RefPtr<Gtk::GestureClick> controllClick_;
Glib::RefPtr<Gtk::EventControllerScroll> controllScroll_; Glib::RefPtr<Gtk::EventControllerScroll> controllScroll_;
enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };
void bindEvents(Gtk::Widget& wg); void bindEvents(Gtk::Widget& wg);
bool tooltipEnabled(); bool tooltipEnabled();
@ -38,9 +39,9 @@ class AModule : public IModule {
virtual void handleToggle(int n_press, double dx, double dy); virtual void handleToggle(int n_press, double dx, double dy);
virtual void handleRelease(int n_press, double dx, double dy); virtual void handleRelease(int n_press, double dx, double dy);
virtual bool handleScroll(double dx, double dy); virtual bool handleScroll(double dx, double dy);
const SCROLL_DIR getScrollDir(Glib::RefPtr<const Gdk::Event> e);
private: private:
enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };
const bool isTooltip; const bool isTooltip;
std::vector<int> pid_; std::vector<int> pid_;
double distance_scrolled_x_{0.0}; 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"} {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); void handleClickEvent(uint n_button, int n_press, Gdk::Event::Type n_evtype);
const SCROLL_DIR getScrollDir(Glib::RefPtr<const Gdk::Event> e);
}; };
} // namespace waybar } // namespace waybar

View File

@ -11,9 +11,9 @@ class ASlider : public AModule {
virtual void onValueChanged(); virtual void onValueChanged();
protected: protected:
bool vertical_ = false; bool vertical_{false};
int min_ = 0, max_ = 100, curr_ = 50; int min_{0}, max_{100}, curr_{50};
Gtk::Scale scale_; Gtk::Scale scale_;
}; };
} // namespace waybar } // namespace waybar

View File

@ -1,32 +1,22 @@
#pragma once #pragma once
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "ALabel.hpp" #include "ALabel.hpp"
#include "util/backlight_backend.hpp" #include "util/backlight_backend.hpp"
#include "util/json.hpp"
struct udev;
struct udev_device;
namespace waybar::modules { namespace waybar::modules {
class Backlight : public ALabel { class Backlight final : public ALabel {
public: public:
Backlight(const std::string &, const Json::Value &); Backlight(const std::string &, const Json::Value &);
virtual ~Backlight() = default; virtual ~Backlight() = default;
auto update() -> void override; auto update() -> void override;
bool handleScroll(GdkEventScroll *e) override; private:
const std::string preferred_device_; const std::string preferred_device_;
std::string previous_format_; std::string previous_format_;
util::BacklightBackend backend; util::BacklightBackend backend;
bool handleScroll(double dx, double dy) override;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -1,13 +1,11 @@
#pragma once #pragma once
#include <chrono>
#include "ASlider.hpp" #include "ASlider.hpp"
#include "util/backlight_backend.hpp" #include "util/backlight_backend.hpp"
namespace waybar::modules { namespace waybar::modules {
class BacklightSlider : public ASlider { class BacklightSlider final : public ASlider {
public: public:
BacklightSlider(const std::string&, const Json::Value&); BacklightSlider(const std::string&, const Json::Value&);
virtual ~BacklightSlider() = default; virtual ~BacklightSlider() = default;
@ -21,4 +19,4 @@ class BacklightSlider : public ASlider {
util::BacklightBackend backend; util::BacklightBackend backend;
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

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

View File

@ -1,14 +1,6 @@
#pragma once #pragma once
#include <libudev.h> #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 "giomm/dbusproxy.h"
#include "util/backend_common.hpp" #include "util/backend_common.hpp"
@ -42,9 +34,9 @@ class BacklightDevice {
private: private:
std::string name_; std::string name_;
int actual_ = 1; int actual_{1};
int max_ = 1; int max_{1};
bool powered_ = true; bool powered_{true};
}; };
class BacklightBackend { class BacklightBackend {
@ -87,7 +79,7 @@ class BacklightBackend {
Glib::RefPtr<Gio::DBus::Proxy> login_proxy_; Glib::RefPtr<Gio::DBus::Proxy> login_proxy_;
static constexpr int EPOLL_MAX_EVENTS = 16; static constexpr int EPOLL_MAX_EVENTS{16};
}; };
} // namespace waybar::util } // namespace waybar::util

View File

@ -86,6 +86,7 @@ sigcpp = dependency('sigc++-3.0', version: ['>=3.4.0'])
gtkmm = dependency('gtkmm-4.0', version : ['>=4.12.0']) gtkmm = dependency('gtkmm-4.0', version : ['>=4.12.0'])
giounix = dependency('gio-unix-2.0', version: ['>=2.76.4']) 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']) 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', gtk_layer_shell = dependency('gtk4-layer-shell-0',
version : ['>=1.0.2'], version : ['>=1.0.2'],
@ -137,7 +138,8 @@ src_files = files(
'src/ALabel.cpp', 'src/ALabel.cpp',
'src/factory.cpp', 'src/factory.cpp',
'src/util/ustring_clen.cpp', 'src/util/ustring_clen.cpp',
'src/group.cpp' 'src/group.cpp',
'src/ASlider.cpp'
) )
man_files = files( man_files = files(
@ -166,6 +168,19 @@ if get_option('experimental')
add_project_arguments('-DUSE_EXPERIMENTAL', language: 'cpp') add_project_arguments('-DUSE_EXPERIMENTAL', language: 'cpp')
endif 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', cava = dependency('cava',
version : '>=0.10.1', version : '>=0.10.1',
required: get_option('cava'), required: get_option('cava'),
@ -189,6 +204,7 @@ executable(
gtkmm, gtkmm,
giounix, giounix,
sigcpp, sigcpp,
libudev,
jsoncpp, jsoncpp,
wayland_client, wayland_client,
wayland_cursor, wayland_cursor,

View File

@ -182,9 +182,9 @@ bool AModule::handleScroll(double dx, double dy) {
std::string format{}; std::string format{};
const auto dir{getScrollDir(currEvent_)}; const auto dir{getScrollDir(currEvent_)};
if (dir == SCROLL_DIR::UP) if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::RIGHT)
format = "on-scroll-up"; format = "on-scroll-up";
else if (dir == SCROLL_DIR::DOWN) else if (dir == SCROLL_DIR::DOWN || dir == SCROLL_DIR::LEFT)
format = "on-scroll-down"; format = "on-scroll-down";
// First call module action // First call module action

View File

@ -1,20 +1,18 @@
#include "ASlider.hpp" #include "ASlider.hpp"
#include "gtkmm/adjustment.h" #include "gtkmm/adjustment.h"
#include "gtkmm/enums.h"
namespace waybar { namespace waybar {
ASlider::ASlider(const Json::Value& config, const std::string& name, const std::string& id) ASlider::ASlider(const Json::Value& config, const std::string& name, const std::string& id)
: AModule(config, name, id, false, false), : AModule(config, name, id, false, false),
vertical_(config_["orientation"].asString() == "vertical"), 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); scale_.set_name(name);
if (!id.empty()) { if (!id.empty()) {
scale_.get_style_context()->add_class(id); scale_.get_style_context()->add_class(id);
} }
scale_.get_style_context()->add_class(MODULE_CLASS); scale_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(scale_);
scale_.signal_value_changed().connect(sigc::mem_fun(*this, &ASlider::onValueChanged)); scale_.signal_value_changed().connect(sigc::mem_fun(*this, &ASlider::onValueChanged));
if (config_["min"].isUInt()) { if (config_["min"].isUInt()) {
@ -32,4 +30,4 @@ ASlider::ASlider(const Json::Value& config, const std::string& name, const std::
void ASlider::onValueChanged() {} void ASlider::onValueChanged() {}
} // namespace waybar } // namespace waybar

View File

@ -250,7 +250,7 @@ gtk4 todo
if (ref == "network") { if (ref == "network") {
return new waybar::modules::Network(id, config_[name]); return new waybar::modules::Network(id, config_[name]);
} }
#endif #endif*/
#ifdef HAVE_LIBUDEV #ifdef HAVE_LIBUDEV
if (ref == "backlight") { if (ref == "backlight") {
return new waybar::modules::Backlight(id, config_[name]); return new waybar::modules::Backlight(id, config_[name]);
@ -258,7 +258,7 @@ gtk4 todo
if (ref == "backlight/slider") { if (ref == "backlight/slider") {
return new waybar::modules::BacklightSlider(id, config_[name]); return new waybar::modules::BacklightSlider(id, config_[name]);
} }
#endif #endif/*
#ifdef HAVE_LIBEVDEV #ifdef HAVE_LIBEVDEV
if (ref == "keyboard-state") { if (ref == "keyboard-state") {
return new waybar::modules::KeyboardState(id, bar_, config_[name]); return new waybar::modules::KeyboardState(id, bar_, config_[name]);

View File

@ -1,27 +1,12 @@
#include "modules/backlight.hpp" #include "modules/backlight.hpp"
#include <fmt/format.h> #include <fmt/format.h>
#include <libudev.h>
#include <spdlog/spdlog.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <memory>
#include "util/backend_common.hpp"
#include "util/backlight_backend.hpp"
waybar::modules::Backlight::Backlight(const std::string &id, const Json::Value &config) 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() : ""), preferred_device_(config["device"].isString() ? config["device"].asString() : ""),
backend(interval_, [this] { dp.emit(); }) { backend(interval_, [this] { dp.emit(); }) {
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 { auto waybar::modules::Backlight::update() -> void {
@ -35,12 +20,12 @@ auto waybar::modules::Backlight::update() -> void {
} }
if (best->get_powered()) { if (best->get_powered()) {
event_box_.show(); Gtk::Label::show();
const uint8_t percent = const uint8_t percent =
best->get_max() == 0 ? 100 : round(best->get_actual() * 100.0f / best->get_max()); 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), std::string desc = fmt::format(fmt::runtime(format_), fmt::arg("percent", percent),
fmt::arg("icon", getIcon(percent))); fmt::arg("icon", getIcon(percent)));
label_.set_markup(desc); Gtk::Label::set_markup(desc);
getState(percent); getState(percent);
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_format; std::string tooltip_format;
@ -48,31 +33,31 @@ auto waybar::modules::Backlight::update() -> void {
tooltip_format = config_["tooltip-format"].asString(); tooltip_format = config_["tooltip-format"].asString();
} }
if (!tooltip_format.empty()) { 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("percent", percent),
fmt::arg("icon", getIcon(percent)))); fmt::arg("icon", getIcon(percent))));
} else { } else {
label_.set_tooltip_text(desc); Gtk::Label::set_tooltip_text(desc);
} }
} }
} else { } else {
event_box_.hide(); Gtk::Label::hide();
} }
} else { } else {
if (previous_best_device == nullptr) { if (previous_best_device == nullptr) {
return; return;
} }
label_.set_markup(""); Gtk::Label::set_markup("");
} }
backend.set_previous_best_device(best); backend.set_previous_best_device(best);
previous_format_ = format_; previous_format_ = format_;
ALabel::update(); 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 // Check if the user has set a custom command for scrolling
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) { 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 // Fail fast if the proxy could not be initialized
@ -81,7 +66,7 @@ bool waybar::modules::Backlight::handleScroll(GdkEventScroll *e) {
} }
// Check scroll direction // 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 // No worries, it will always be set because of the switch below. This is purely to suppress a
// warning // warning

View File

@ -1,6 +1,5 @@
#include "modules/backlight_slider.hpp"
#include "ASlider.hpp" #include "ASlider.hpp"
#include "modules/backlight_slider.hpp"
namespace waybar::modules { namespace waybar::modules {
@ -20,4 +19,4 @@ void BacklightSlider::onValueChanged() {
backend.set_scaled_brightness(preferred_device_, brightness); backend.set_scaled_brightness(preferred_device_, brightness);
} }
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -1,11 +1,9 @@
#include "util/backlight_backend.hpp" #include "util/backlight_backend.hpp"
#include <cmath>
#include <fmt/core.h> #include <fmt/core.h>
#include <spdlog/spdlog.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <optional>
namespace { namespace {
class FileDescriptor { class FileDescriptor {
public: public:
@ -103,7 +101,7 @@ BacklightBackend::BacklightBackend(std::chrono::milliseconds interval,
// Connect to the login interface // Connect to the login interface
login_proxy_ = Gio::DBus::Proxy::create_for_bus_sync( 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"); "/org/freedesktop/login1/session/self", "org.freedesktop.login1.Session");
udev_thread_ = [this] { udev_thread_ = [this] {
@ -193,7 +191,7 @@ void BacklightBackend::set_scaled_brightness(std::string preferred_device, int b
if (best != nullptr) { if (best != nullptr) {
const auto max = best->get_max(); const auto max = best->get_max();
const auto abs_val = static_cast<int>(round(brightness * max / 100.0f)); const auto abs_val = static_cast<int>(std::round(brightness * max / 100.0f));
set_brightness_internal(best->name(), abs_val, best->get_max()); 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) { if (best != nullptr) {
const auto max = best->get_max(); const auto max = best->get_max();
const auto abs_step = static_cast<int>(round(step * max / 100.0f)); const auto abs_step = static_cast<int>(std::round(step * max / 100.0f));
const int new_brightness = change_type == ChangeType::Increase ? best->get_actual() + abs_step const int new_brightness = change_type == ChangeType::Increase ? best->get_actual() + abs_step
: 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 } // namespace waybar::util