Gtk4: battery migration

build: drop std::filesystem checks

The `<experimental/filesystem>` and `-lc++experimental` aren't needed
since LLVM 9.0. And since we now require C++20, checking for the
`<filesystem>` support shouldn't be necessary either.

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
pull/2956/head
Viktar Lukashonak 2024-03-05 18:04:59 +03:00
parent 69648ab8f4
commit 7802cab40d
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
8 changed files with 86 additions and 55 deletions

View File

@ -1,45 +1,32 @@
#pragma once #pragma once
#ifdef FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
#else
#include <filesystem> #include <filesystem>
#endif
#include <fmt/format.h>
#if defined(__linux__) #if defined(__linux__)
#include <sys/inotify.h> #include <sys/inotify.h>
#endif #endif
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#include "ALabel.hpp" #include "ALabel.hpp"
#include "util/sleeper_thread.hpp" #include "util/sleeper_thread.hpp"
namespace waybar::modules { namespace waybar::modules {
#ifdef FILESYSTEM_EXPERIMENTAL
namespace fs = std::experimental::filesystem;
#else
namespace fs = std::filesystem; namespace fs = std::filesystem;
#endif
class Battery : public ALabel { class Battery final : public ALabel {
public: public:
Battery(const std::string&, const Json::Value&); Battery(const std::string&, const Json::Value&);
virtual ~Battery(); virtual ~Battery();
auto update() -> void override; auto update() -> void override;
private: private:
static inline const fs::path data_dir_ = "/sys/class/power_supply/"; static inline const fs::path data_dir_{"/sys/class/power_supply/"};
void refreshBatteries(); void refreshBatteries();
void worker(); void worker();
const std::string getAdapterStatus(uint8_t capacity) const; const std::string getAdapterStatus(uint8_t capacity) const;
const std::tuple<uint8_t, float, std::string, float> getInfos(); const std::tuple<uint8_t, float, std::string, float> getInfos();
const std::string formatTimeRemaining(float hoursRemaining); const std::string formatTimeRemaining(float hoursRemaining);
void setBarClass(std::string&);
int global_watch; int global_watch;
std::map<fs::path, int> batteries_; std::map<fs::path, int> batteries_;

View File

@ -1,15 +1,16 @@
#pragma once #pragma once
#include <string>
#include <giomm/dbusproxy.h> #include <giomm/dbusproxy.h>
#include <string>
#include "ALabel.hpp" #include "ALabel.hpp"
namespace waybar::modules { namespace waybar::modules {
class SystemdFailedUnits : public ALabel { class SystemdFailedUnits : public ALabel {
public: public:
SystemdFailedUnits(const std::string&, const Json::Value&); SystemdFailedUnits(const std::string &, const Json::Value &);
virtual ~SystemdFailedUnits(); virtual ~SystemdFailedUnits();
auto update() -> void override; auto update() -> void override;

View File

@ -167,3 +167,10 @@ The *battery* module allows one to define custom formats based on up to two fact
- *<state>* can be defined in the *config*. For more information see *states*. - *<state>* can be defined in the *config*. For more information see *states*.
- *#battery.<status>.<state>* - *#battery.<status>.<state>*
- Combination of both *<status>* and *<state>*. - Combination of both *<status>* and *<state>*.
The following classes are applied to the entire Waybar rather than just the
battery widget:
- *window#waybar.battery-<state>*
- *<state>* can be defined in the *config*, as previously mentioned.

View File

@ -22,8 +22,6 @@ endif
if compiler.has_link_argument('-lc++fs') if compiler.has_link_argument('-lc++fs')
cpp_link_args += ['-lc++fs'] cpp_link_args += ['-lc++fs']
elif compiler.has_link_argument('-lc++experimental')
cpp_link_args += ['-lc++experimental']
elif compiler.has_link_argument('-lstdc++fs') elif compiler.has_link_argument('-lstdc++fs')
cpp_link_args += ['-lstdc++fs'] cpp_link_args += ['-lstdc++fs']
endif endif
@ -44,15 +42,6 @@ else
endif endif
endif endif
if not compiler.has_header('filesystem')
if compiler.has_header('experimental/filesystem')
add_project_arguments('-DFILESYSTEM_EXPERIMENTAL', language: 'cpp')
else
add_project_arguments('-DNO_FILESYSTEM', language: 'cpp')
warning('No filesystem header found, some modules may not work')
endif
endif
code = ''' code = '''
#include <langinfo.h> #include <langinfo.h>
#include <locale.h> #include <locale.h>
@ -194,6 +183,17 @@ if cava.found()
endif endif
inc_dirs = ['include'] inc_dirs = ['include']
if is_linux
src_files += files('src/modules/battery.cpp')
man_files += files('man/waybar-battery.5.scd')
endif
if is_freebsd
src_files += files('src/modules/battery.cpp')
man_files += files('man/waybar-battery.5.scd')
endif
subdir('protocol') subdir('protocol')
executable( executable(

View File

@ -535,7 +535,7 @@ auto waybar::Bar::setupWidgets() -> void {
for (auto const& module : modules_center_) { for (auto const& module : modules_center_) {
center_.prepend(*module); center_.prepend(*module);
} }
std::reverse(modules_right_.begin(), modules_right_.end()); // std::reverse(modules_right_.begin(), modules_right_.end());
for (auto const& module : modules_right_) { for (auto const& module : modules_right_) {
right_.append(*module); right_.append(*module);
} }

View File

@ -116,12 +116,12 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
auto hash_pos = name.find('#'); auto hash_pos = name.find('#');
auto ref = name.substr(0, hash_pos); auto ref = name.substr(0, hash_pos);
auto id = hash_pos != std::string::npos ? name.substr(hash_pos + 1) : ""; auto id = hash_pos != std::string::npos ? name.substr(hash_pos + 1) : "";
/* gtk4 todo #if defined(__FreeBSD__) || defined(__linux__)
#if defined(__FreeBSD__) || (defined(__linux__) && !defined(NO_FILESYSTEM))
if (ref == "battery") { if (ref == "battery") {
return new waybar::modules::Battery(id, config_[name]); return new waybar::modules::Battery(id, config_[name]);
} }
#endif #endif
/* gtk4 todo
#ifdef HAVE_GAMEMODE #ifdef HAVE_GAMEMODE
if (ref == "gamemode") { if (ref == "gamemode") {
return new waybar::modules::Gamemode(id, config_[name]); return new waybar::modules::Gamemode(id, config_[name]);

View File

@ -1,10 +1,12 @@
#include "modules/battery.hpp" #include "modules/battery.hpp"
#include <fstream>
#include <spdlog/spdlog.h>
#if defined(__FreeBSD__) #if defined(__FreeBSD__)
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
#include <spdlog/spdlog.h>
#include <iostream>
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config) waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
: ALabel(config, "battery", id, "{capacity}%", 60) { : ALabel(config, "battery", id, "{capacity}%", 60) {
#if defined(__linux__) #if defined(__linux__)
@ -627,7 +629,7 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai
auto waybar::modules::Battery::update() -> void { auto waybar::modules::Battery::update() -> void {
#if defined(__linux__) #if defined(__linux__)
if (batteries_.empty()) { if (batteries_.empty()) {
event_box_.hide(); Gtk::Label::hide();
return; return;
} }
#endif #endif
@ -641,6 +643,7 @@ auto waybar::modules::Battery::update() -> void {
[](char ch) { return ch == ' ' ? '-' : std::tolower(ch); }); [](char ch) { return ch == ' ' ? '-' : std::tolower(ch); });
auto format = format_; auto format = format_;
auto state = getState(capacity, true); auto state = getState(capacity, true);
setBarClass(state);
auto time_remaining_formatted = formatTimeRemaining(time_remaining); auto time_remaining_formatted = formatTimeRemaining(time_remaining);
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_text_default; std::string tooltip_text_default;
@ -660,15 +663,15 @@ auto waybar::modules::Battery::update() -> void {
} else if (config_["tooltip-format"].isString()) { } else if (config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString(); tooltip_format = config_["tooltip-format"].asString();
} }
label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format), Gtk::Label::set_tooltip_text(fmt::format(fmt::runtime(tooltip_format),
fmt::arg("timeTo", tooltip_text_default), fmt::arg("timeTo", tooltip_text_default),
fmt::arg("power", power), fmt::arg("capacity", capacity), fmt::arg("power", power), fmt::arg("capacity", capacity),
fmt::arg("time", time_remaining_formatted))); fmt::arg("time", time_remaining_formatted)));
} }
if (!old_status_.empty()) { if (!old_status_.empty()) {
label_.get_style_context()->remove_class(old_status_); Gtk::Label::get_style_context()->remove_class(old_status_);
} }
label_.get_style_context()->add_class(status); Gtk::Label::get_style_context()->add_class(status);
old_status_ = status; old_status_ = status;
if (!state.empty() && config_["format-" + status + "-" + state].isString()) { if (!state.empty() && config_["format-" + status + "-" + state].isString()) {
format = config_["format-" + status + "-" + state].asString(); format = config_["format-" + status + "-" + state].asString();
@ -678,14 +681,50 @@ auto waybar::modules::Battery::update() -> void {
format = config_["format-" + state].asString(); format = config_["format-" + state].asString();
} }
if (format.empty()) { if (format.empty()) {
event_box_.hide(); Gtk::Label::hide();
} else { } else {
event_box_.show(); Gtk::Label::show();
auto icons = std::vector<std::string>{status + "-" + state, status, state}; auto icons = std::vector<std::string>{status + "-" + state, status, state};
label_.set_markup(fmt::format( Gtk::Label::set_markup(fmt::format(
fmt::runtime(format), fmt::arg("capacity", capacity), fmt::arg("power", power), fmt::runtime(format), fmt::arg("capacity", capacity), fmt::arg("power", power),
fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", time_remaining_formatted))); fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", time_remaining_formatted)));
} }
// Call parent update // Call parent update
ALabel::update(); ALabel::update();
} }
void waybar::modules::Battery::setBarClass(std::string& state) {
// Drop style
auto classes = Gtk::Widget::get_css_classes();
const Glib::ustring prefix{"battery-"};
auto old_class_it = std::find_if(classes.cbegin(), classes.cend(), [&prefix](auto classname) {
return classname.rfind(prefix, 0) == 0;
});
auto new_class = prefix + state;
// If the bar doesn't have any `battery-` class
if (old_class_it == classes.end()) {
if (!state.empty()) {
Gtk::Label::get_style_context()->add_class(new_class);
}
return;
}
auto old_class = *old_class_it;
// If the bar has a `battery-` class,
// but `state` is empty
if (state.empty()) {
Gtk::Label::get_style_context()->remove_class(old_class);
return;
}
// If the bar has a `battery-` class,
// and `state` is NOT empty
if (old_class != new_class) {
Gtk::Label::get_style_context()->remove_class(old_class);
Gtk::Label::get_style_context()->add_class(new_class);
}
}

View File

@ -1,10 +1,11 @@
#include "modules/systemd_failed_units.hpp" #include "modules/systemd_failed_units.hpp"
#include <cstdint>
#include <giomm/dbusproxy.h> #include <giomm/dbusproxy.h>
#include <glibmm/variant.h> #include <glibmm/variant.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <cstdint>
static const unsigned UPDATE_DEBOUNCE_TIME_MS = 1000; static const unsigned UPDATE_DEBOUNCE_TIME_MS = 1000;
namespace waybar::modules { namespace waybar::modules {
@ -55,23 +56,21 @@ SystemdFailedUnits::~SystemdFailedUnits() {
if (user_proxy) user_proxy.reset(); if (user_proxy) user_proxy.reset();
} }
auto SystemdFailedUnits::notify_cb( auto SystemdFailedUnits::notify_cb(const Glib::ustring& sender_name,
const Glib::ustring &sender_name, const Glib::ustring& signal_name,
const Glib::ustring &signal_name, const Glib::VariantContainerBase& arguments) -> void {
const Glib::VariantContainerBase &arguments) -> void {
if (signal_name == "PropertiesChanged" && !update_pending) { if (signal_name == "PropertiesChanged" && !update_pending) {
update_pending = true; update_pending = true;
/* The fail count may fluctuate due to restarting. */ /* The fail count may fluctuate due to restarting. */
Glib::signal_timeout().connect_once( Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &SystemdFailedUnits::updateData),
sigc::mem_fun(*this, &SystemdFailedUnits::updateData), UPDATE_DEBOUNCE_TIME_MS);
UPDATE_DEBOUNCE_TIME_MS);
} }
} }
void SystemdFailedUnits::updateData() { void SystemdFailedUnits::updateData() {
update_pending = false; update_pending = false;
auto load = [](const char* kind, Glib::RefPtr<Gio::DBus::Proxy> &proxy) -> uint32_t { auto load = [](const char* kind, Glib::RefPtr<Gio::DBus::Proxy>& proxy) -> uint32_t {
try { try {
auto parameters = Glib::VariantContainerBase( auto parameters = Glib::VariantContainerBase(
g_variant_new("(ss)", "org.freedesktop.systemd1.Manager", "NFailedUnits")); g_variant_new("(ss)", "org.freedesktop.systemd1.Manager", "NFailedUnits"));
@ -123,11 +122,9 @@ auto SystemdFailedUnits::update() -> void {
last_status = status; last_status = status;
label_.set_markup(fmt::format( label_.set_markup(fmt::format(
fmt::runtime(nr_failed == 0 ? format_ok : format_), fmt::runtime(nr_failed == 0 ? format_ok : format_), fmt::arg("nr_failed", nr_failed),
fmt::arg("nr_failed", nr_failed), fmt::arg("nr_failed_system", nr_failed_system), fmt::arg("nr_failed_user", nr_failed_user)));
fmt::arg("nr_failed_system", nr_failed_system),
fmt::arg("nr_failed_user", nr_failed_user)));
ALabel::update(); ALabel::update();
} }
} // namespace waybar::modules::systemd_failed_units } // namespace waybar::modules