GTK4. Migration. backlight, slider, backend_common
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>pull/2956/head
parent
0721fd593c
commit
69648ab8f4
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ class AModule : public IModule {
|
|||
const Json::Value &config_;
|
||||
Glib::RefPtr<Gtk::GestureClick> controllClick_;
|
||||
Glib::RefPtr<Gtk::EventControllerScroll> 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<const Gdk::Event> e);
|
||||
|
||||
private:
|
||||
enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };
|
||||
const bool isTooltip;
|
||||
std::vector<int> 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<const Gdk::Event> e);
|
||||
};
|
||||
|
||||
} // namespace waybar
|
||||
|
|
|
@ -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
|
||||
} // namespace waybar
|
||||
|
|
|
@ -1,32 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#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
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#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
|
||||
} // namespace waybar::modules
|
||||
|
|
|
@ -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
|
||||
} // namespace waybar::util
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
#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"
|
||||
|
@ -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<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
|
||||
|
|
18
meson.build
18
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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
} // namespace waybar
|
||||
|
|
|
@ -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]);
|
||||
|
|
|
@ -1,27 +1,12 @@
|
|||
#include "modules/backlight.hpp"
|
||||
|
||||
#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)
|
||||
: 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
|
||||
|
|
|
@ -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
|
||||
} // namespace waybar::modules
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
#include "util/backlight_backend.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <fmt/core.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include <optional>
|
||||
|
||||
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<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());
|
||||
}
|
||||
}
|
||||
|
@ -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<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
|
||||
: best->get_actual() - abs_step;
|
||||
|
@ -285,4 +283,4 @@ void BacklightBackend::enumerate_devices(ForwardIt first, ForwardIt last, Insert
|
|||
}
|
||||
}
|
||||
|
||||
} // namespace waybar::util
|
||||
} // namespace waybar::util
|
||||
|
|
Loading…
Reference in New Issue