Skip to content

Commit

Permalink
GTK4. Migration. backlight, slider, backend_common
Browse files Browse the repository at this point in the history
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
  • Loading branch information
LukashonakV committed Feb 27, 2024
1 parent 0721fd5 commit 69648ab
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 85 deletions.
2 changes: 1 addition & 1 deletion include/ALabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
4 changes: 2 additions & 2 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ 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();

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};
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions include/ASlider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 5 additions & 15 deletions include/modules/backlight.hpp
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions include/modules/backlight_slider.hpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,4 +19,4 @@ class BacklightSlider : public ASlider {
util::BacklightBackend backend;
};

} // namespace waybar::modules
} // namespace waybar::modules
4 changes: 1 addition & 3 deletions include/util/backend_common.hpp
Original file line number Diff line number Diff line change
@@ -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
18 changes: 5 additions & 13 deletions include/util/backlight_backend.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 changes: 17 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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'),
Expand All @@ -189,6 +204,7 @@ executable(
gtkmm,
giounix,
sigcpp,
libudev,
jsoncpp,
wayland_client,
wayland_cursor,
Expand Down
4 changes: 2 additions & 2 deletions src/AModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/ASlider.cpp
Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -32,4 +30,4 @@ ASlider::ASlider(const Json::Value& config, const std::string& name, const std::

void ASlider::onValueChanged() {}

} // namespace waybar
} // namespace waybar
4 changes: 2 additions & 2 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ 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]);
}
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]);
Expand Down
35 changes: 10 additions & 25 deletions src/modules/backlight.cpp
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -35,44 +20,44 @@ 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;
if (config_["tooltip-format"].isString()) {
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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/modules/backlight_slider.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "modules/backlight_slider.hpp"

#include "ASlider.hpp"
#include "modules/backlight_slider.hpp"

namespace waybar::modules {

Expand All @@ -20,4 +19,4 @@ void BacklightSlider::onValueChanged() {
backend.set_scaled_brightness(preferred_device_, brightness);
}

} // namespace waybar::modules
} // namespace waybar::modules
Loading

0 comments on commit 69648ab

Please sign in to comment.