modules/power-profiles-daemon: apply clang-tidy suggestions

pull/2971/head
Félix Baylac Jacqué 2024-02-26 15:07:21 +01:00 committed by Félix Baylac Jacqué
parent 968f469289
commit 162b41c4d0
2 changed files with 28 additions and 27 deletions

View File

@ -7,25 +7,25 @@
namespace waybar::modules { namespace waybar::modules {
typedef struct { struct Profile {
std::string name; std::string name;
std::string driver; std::string driver;
} 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() override;
auto update() -> void override; auto update() -> void override;
void profileChanged_cb(const Gio::DBus::Proxy::MapChangedProperties&, void profileChangedCb(const Gio::DBus::Proxy::MapChangedProperties&,
const std::vector<Glib::ustring>&); const std::vector<Glib::ustring>&);
void populateInitState(); void populateInitState();
virtual bool handleToggle(GdkEventButton* const& e); bool handleToggle(GdkEventButton* const& e) override;
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.
void switchToProfile_(std::string); void switchToProfile(std::string const&);
// Used to toggle/display the profiles // Used to toggle/display the profiles
std::vector<Profile> availableProfiles_; std::vector<Profile> availableProfiles_;
// Points to the active profile in the profiles list // Points to the active profile in the profiles list
@ -33,7 +33,7 @@ class PowerProfilesDaemon : public ALabel {
// Current CSS class applied to the label // Current CSS class applied to the label
std::string currentStyle_; std::string currentStyle_;
// DBus Proxy used to track the current active profile // DBus Proxy used to track the current active profile
Glib::RefPtr<Gio::DBus::Proxy> power_profiles_proxy_; Glib::RefPtr<Gio::DBus::Proxy> powerProfilesProxy_;
sigc::connection powerProfileChangeSignal_; sigc::connection powerProfileChangeSignal_;
}; };

View File

@ -27,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( powerProfilesProxy_ = Gio::DBus::Proxy::create_for_bus_sync(
Gio::DBus::BusType::BUS_TYPE_SYSTEM, "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 (!powerProfilesProxy_) {
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().connect( powerProfileChangeSignal_ = powerProfilesProxy_->signal_properties_changed().connect(
sigc::mem_fun(*this, &PowerProfilesDaemon::profileChanged_cb)); sigc::mem_fun(*this, &PowerProfilesDaemon::profileChangedCb));
populateInitState(); populateInitState();
dp.emit(); dp.emit();
} }
@ -45,9 +45,9 @@ PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Valu
// vector to store the profiles ain't the smartest move // vector to store the profiles ain't the smartest move
// complexity-wise, but it makes toggling between the mode easy. This // complexity-wise, but it makes toggling between the mode easy. This
// vector is 3 elements max, we'll be fine :P // vector is 3 elements max, we'll be fine :P
void PowerProfilesDaemon::switchToProfile_(std::string str) { void PowerProfilesDaemon::switchToProfile(std::string const& str) {
auto pred = [str](Profile p) { return p.name == str; }; auto pred = [str](Profile const& p) { return p.name == str; };
activeProfile_ = std::find_if(availableProfiles_.begin(), availableProfiles_.end(), pred); this->activeProfile_ = std::find_if(availableProfiles_.begin(), availableProfiles_.end(), pred);
if (activeProfile_ == availableProfiles_.end()) { if (activeProfile_ == availableProfiles_.end()) {
throw std::runtime_error("FATAL, can't find the active profile in the available profiles list"); throw std::runtime_error("FATAL, can't find the active profile in the available profiles list");
} }
@ -56,13 +56,14 @@ void PowerProfilesDaemon::switchToProfile_(std::string str) {
void PowerProfilesDaemon::populateInitState() { void PowerProfilesDaemon::populateInitState() {
// Retrieve current active profile // Retrieve current active profile
Glib::Variant<std::string> profileStr; Glib::Variant<std::string> profileStr;
power_profiles_proxy_->get_cached_property(profileStr, "ActiveProfile"); powerProfilesProxy_->get_cached_property(profileStr, "ActiveProfile");
// Retrieve profiles list, it's aa{sv}. // Retrieve profiles list, it's aa{sv}.
using ProfilesType = std::vector<std::map<Glib::ustring, Glib::Variant<std::string>>>; using ProfilesType = std::vector<std::map<Glib::ustring, Glib::Variant<std::string>>>;
Glib::Variant<ProfilesType> profilesVariant; Glib::Variant<ProfilesType> profilesVariant;
power_profiles_proxy_->get_cached_property(profilesVariant, "Profiles"); powerProfilesProxy_->get_cached_property(profilesVariant, "Profiles");
Glib::ustring name, driver; Glib::ustring name;
Glib::ustring 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()) {
@ -77,7 +78,7 @@ void PowerProfilesDaemon::populateInitState() {
// Find the index of the current activated mode (to toggle) // Find the index of the current activated mode (to toggle)
std::string str = profileStr.get(); std::string str = profileStr.get();
switchToProfile_(str); switchToProfile(str);
update(); update();
} }
@ -86,12 +87,12 @@ PowerProfilesDaemon::~PowerProfilesDaemon() {
if (powerProfileChangeSignal_.connected()) { if (powerProfileChangeSignal_.connected()) {
powerProfileChangeSignal_.disconnect(); powerProfileChangeSignal_.disconnect();
} }
if (power_profiles_proxy_) { if (powerProfilesProxy_) {
power_profiles_proxy_.reset(); powerProfilesProxy_.reset();
} }
} }
void PowerProfilesDaemon::profileChanged_cb( void PowerProfilesDaemon::profileChangedCb(
const Gio::DBus::Proxy::MapChangedProperties& changedProperties, const Gio::DBus::Proxy::MapChangedProperties& changedProperties,
const std::vector<Glib::ustring>& invalidatedProperties) { const std::vector<Glib::ustring>& invalidatedProperties) {
if (auto activeProfileVariant = changedProperties.find("ActiveProfile"); if (auto activeProfileVariant = changedProperties.find("ActiveProfile");
@ -99,7 +100,7 @@ void PowerProfilesDaemon::profileChanged_cb(
std::string activeProfile = std::string activeProfile =
Glib::VariantBase::cast_dynamic<Glib::Variant<std::string>>(activeProfileVariant->second) Glib::VariantBase::cast_dynamic<Glib::Variant<std::string>>(activeProfileVariant->second)
.get(); .get();
switchToProfile_(activeProfile); switchToProfile(activeProfile);
update(); update();
} }
} }
@ -125,7 +126,7 @@ auto PowerProfilesDaemon::update() -> void {
} }
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 && powerProfilesProxy_) {
activeProfile_++; activeProfile_++;
if (activeProfile_ == availableProfiles_.end()) { if (activeProfile_ == availableProfiles_.end()) {
activeProfile_ = availableProfiles_.begin(); activeProfile_ = availableProfiles_.begin();
@ -134,10 +135,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( auto callArgs = SetPowerProfileVar::create(
std::make_tuple("net.hadess.PowerProfiles", "ActiveProfile", activeProfileVariant)); 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>(callArgs);
power_profiles_proxy_->call_sync("org.freedesktop.DBus.Properties.Set", container); powerProfilesProxy_->call_sync("org.freedesktop.DBus.Properties.Set", container);
update(); update();
} }