modules/power-profiles-daemon: run clang format

pull/2971/head
Félix Baylac Jacqué 2024-02-26 14:40:28 +01:00
parent c38d05b04f
commit 968f469289
2 changed files with 28 additions and 25 deletions

View File

@ -1,4 +1,4 @@
# pragma once #pragma once
#include <fmt/format.h> #include <fmt/format.h>
@ -12,14 +12,16 @@ typedef struct {
std::string driver; std::string driver;
} Profile; } Profile;
class PowerProfilesDaemon : public ALabel { class PowerProfilesDaemon : public ALabel {
public: public:
PowerProfilesDaemon(const std::string&, const Json::Value&); PowerProfilesDaemon(const std::string&, const Json::Value&);
~PowerProfilesDaemon(); ~PowerProfilesDaemon();
auto update() -> void override; auto update() -> void override;
void profileChanged_cb( const Gio::DBus::Proxy::MapChangedProperties&, const std::vector<Glib::ustring>&); void profileChanged_cb(const Gio::DBus::Proxy::MapChangedProperties&,
const std::vector<Glib::ustring>&);
void populateInitState(); void populateInitState();
virtual bool handleToggle(GdkEventButton* const& e); virtual bool handleToggle(GdkEventButton* const& e);
private: private:
// Look for a profile name in the list of available profiles and // Look for a profile name in the list of available profiles and
// switch activeProfile_ to it. // switch activeProfile_ to it.
@ -35,4 +37,4 @@ class PowerProfilesDaemon : public ALabel {
sigc::connection powerProfileChangeSignal_; sigc::connection powerProfileChangeSignal_;
}; };
} } // namespace waybar::modules

View File

@ -9,17 +9,14 @@
#include <fmt/core.h> #include <fmt/core.h>
#endif #endif
#include <spdlog/spdlog.h>
#include <glibmm.h> #include <glibmm.h>
#include <glibmm/variant.h> #include <glibmm/variant.h>
#include <spdlog/spdlog.h>
namespace waybar::modules { namespace waybar::modules {
PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Value& config) PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Value& config)
: ALabel(config, "power-profiles-daemon", id, "{profile}", 0, false, true) : ALabel(config, "power-profiles-daemon", id, "{profile}", 0, false, true) {
{
// NOTE: the DBus adresses are under migration. They should be // NOTE: the DBus adresses are under migration. They should be
// changed to org.freedesktop.UPower.PowerProfiles at some point. // changed to org.freedesktop.UPower.PowerProfiles at some point.
// //
@ -30,15 +27,15 @@ PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Valu
// adresses for compatibility sake. // adresses for compatibility sake.
// //
// Revisit this in 2026, systems should be updated by then. // Revisit this in 2026, systems should be updated by then.
power_profiles_proxy_ = Gio::DBus::Proxy::create_for_bus_sync(Gio::DBus::BusType::BUS_TYPE_SYSTEM, power_profiles_proxy_ = Gio::DBus::Proxy::create_for_bus_sync(
"net.hadess.PowerProfiles", "/net/hadess/PowerProfiles", Gio::DBus::BusType::BUS_TYPE_SYSTEM, "net.hadess.PowerProfiles", "/net/hadess/PowerProfiles",
"net.hadess.PowerProfiles"); "net.hadess.PowerProfiles");
if (!power_profiles_proxy_) { if (!power_profiles_proxy_) {
spdlog::error("PowerProfilesDaemon: DBus error, cannot connect to net.hasdess.PowerProfile"); spdlog::error("PowerProfilesDaemon: DBus error, cannot connect to net.hasdess.PowerProfile");
} else { } else {
// Connect active profile callback // Connect active profile callback
powerProfileChangeSignal_ = power_profiles_proxy_->signal_properties_changed() powerProfileChangeSignal_ = power_profiles_proxy_->signal_properties_changed().connect(
.connect(sigc::mem_fun(*this, &PowerProfilesDaemon::profileChanged_cb)); sigc::mem_fun(*this, &PowerProfilesDaemon::profileChanged_cb));
populateInitState(); populateInitState();
dp.emit(); dp.emit();
} }
@ -67,14 +64,14 @@ void PowerProfilesDaemon::populateInitState() {
power_profiles_proxy_->get_cached_property(profilesVariant, "Profiles"); power_profiles_proxy_->get_cached_property(profilesVariant, "Profiles");
Glib::ustring name, driver; Glib::ustring name, driver;
Profile profile; Profile profile;
for (auto & variantDict: profilesVariant.get()) { for (auto& variantDict : profilesVariant.get()) {
if (auto p = variantDict.find("Profile"); p != variantDict.end()) { if (auto p = variantDict.find("Profile"); p != variantDict.end()) {
name = p->second.get(); name = p->second.get();
} }
if (auto d = variantDict.find("Driver"); d != variantDict.end()) { if (auto d = variantDict.find("Driver"); d != variantDict.end()) {
driver = d->second.get(); driver = d->second.get();
} }
profile = { name, driver }; profile = {name, driver};
availableProfiles_.push_back(profile); availableProfiles_.push_back(profile);
} }
@ -94,16 +91,20 @@ PowerProfilesDaemon::~PowerProfilesDaemon() {
} }
} }
void PowerProfilesDaemon::profileChanged_cb(const Gio::DBus::Proxy::MapChangedProperties& changedProperties, void PowerProfilesDaemon::profileChanged_cb(
const std::vector<Glib::ustring>& invalidatedProperties) { const Gio::DBus::Proxy::MapChangedProperties& changedProperties,
if (auto activeProfileVariant = changedProperties.find("ActiveProfile"); activeProfileVariant != changedProperties.end()) { const std::vector<Glib::ustring>& invalidatedProperties) {
std::string activeProfile = Glib::VariantBase::cast_dynamic<Glib::Variant<std::string>>(activeProfileVariant->second).get(); if (auto activeProfileVariant = changedProperties.find("ActiveProfile");
activeProfileVariant != changedProperties.end()) {
std::string activeProfile =
Glib::VariantBase::cast_dynamic<Glib::Variant<std::string>>(activeProfileVariant->second)
.get();
switchToProfile_(activeProfile); switchToProfile_(activeProfile);
update(); update();
} }
} }
auto PowerProfilesDaemon::update () -> void { auto PowerProfilesDaemon::update() -> void {
auto profile = (*activeProfile_); auto profile = (*activeProfile_);
// Set label // Set label
fmt::dynamic_format_arg_store<fmt::format_context> store; fmt::dynamic_format_arg_store<fmt::format_context> store;
@ -123,7 +124,6 @@ auto PowerProfilesDaemon::update () -> void {
ALabel::update(); ALabel::update();
} }
bool PowerProfilesDaemon::handleToggle(GdkEventButton* const& e) { bool PowerProfilesDaemon::handleToggle(GdkEventButton* const& e) {
if (e->type == GdkEventType::GDK_BUTTON_PRESS && power_profiles_proxy_) { if (e->type == GdkEventType::GDK_BUTTON_PRESS && power_profiles_proxy_) {
activeProfile_++; activeProfile_++;
@ -132,9 +132,10 @@ bool PowerProfilesDaemon::handleToggle(GdkEventButton* const& e) {
} }
using VarStr = Glib::Variant<Glib::ustring>; using VarStr = Glib::Variant<Glib::ustring>;
using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring,Glib::ustring,VarStr>>; using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring, Glib::ustring, VarStr>>;
VarStr activeProfileVariant = VarStr::create(activeProfile_->name); VarStr activeProfileVariant = VarStr::create(activeProfile_->name);
auto call_args = SetPowerProfileVar::create(std::make_tuple("net.hadess.PowerProfiles", "ActiveProfile", activeProfileVariant)); auto call_args = SetPowerProfileVar::create(
std::make_tuple("net.hadess.PowerProfiles", "ActiveProfile", activeProfileVariant));
auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(call_args); auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(call_args);
power_profiles_proxy_->call_sync("org.freedesktop.DBus.Properties.Set", container); power_profiles_proxy_->call_sync("org.freedesktop.DBus.Properties.Set", container);
@ -143,4 +144,4 @@ bool PowerProfilesDaemon::handleToggle(GdkEventButton* const& e) {
return true; return true;
} }
} } // namespace waybar::modules