2024-02-18 21:06:21 +00:00
|
|
|
#include "modules/power_profiles_daemon.hpp"
|
|
|
|
|
|
|
|
#include <fmt/args.h>
|
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/variant.h>
|
2024-02-26 13:40:28 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2024-02-18 21:06:21 +00:00
|
|
|
|
|
|
|
namespace waybar::modules {
|
|
|
|
|
|
|
|
PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Value& config)
|
2024-03-01 18:37:20 +00:00
|
|
|
: ALabel(config, "power-profiles-daemon", id, "{icon}", 0, false, true), connected_(false) {
|
2024-03-01 10:13:57 +00:00
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
tooltipFormat_ = config_["tooltip-format"].asString();
|
|
|
|
} else {
|
|
|
|
tooltipFormat_ = "Power profile: {profile}\nDriver: {driver}";
|
|
|
|
}
|
2024-03-01 11:33:36 +00:00
|
|
|
// Fasten your seatbelt, we're up for quite a ride. The rest of the
|
|
|
|
// init is performed asynchronously. There's 2 callbacks involved.
|
|
|
|
// Here's the overall idea:
|
|
|
|
// 1. Async connect to the system bus.
|
|
|
|
// 2. In the system bus connect callback, try to call
|
|
|
|
// org.freedesktop.DBus.Properties.GetAll to see if
|
|
|
|
// power-profiles-daemon is able to respond.
|
|
|
|
// 3. In the GetAll callback, connect the activeProfile monitoring
|
|
|
|
// callback, consider the init to be successful. Meaning start
|
|
|
|
// drawing the module.
|
|
|
|
//
|
|
|
|
// There's sadly no other way around that, we have to try to call a
|
|
|
|
// method on the proxy to see whether or not something's responding
|
|
|
|
// on the other side.
|
2024-03-01 10:13:57 +00:00
|
|
|
|
2024-02-18 21:06:21 +00:00
|
|
|
// NOTE: the DBus adresses are under migration. They should be
|
|
|
|
// changed to org.freedesktop.UPower.PowerProfiles at some point.
|
|
|
|
//
|
|
|
|
// See
|
|
|
|
// https://gitlab.freedesktop.org/upower/power-profiles-daemon/-/releases/0.20
|
|
|
|
//
|
|
|
|
// The old name is still announced for now. Let's rather use the old
|
|
|
|
// adresses for compatibility sake.
|
|
|
|
//
|
|
|
|
// Revisit this in 2026, systems should be updated by then.
|
2024-03-01 11:33:36 +00:00
|
|
|
Gio::DBus::Proxy::create_for_bus(Gio::DBus::BusType::BUS_TYPE_SYSTEM, "net.hadess.PowerProfiles",
|
|
|
|
"/net/hadess/PowerProfiles", "net.hadess.PowerProfiles",
|
|
|
|
sigc::mem_fun(*this, &PowerProfilesDaemon::busConnectedCb));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerProfilesDaemon::busConnectedCb(Glib::RefPtr<Gio::AsyncResult>& r) {
|
|
|
|
try {
|
|
|
|
powerProfilesProxy_ = Gio::DBus::Proxy::create_for_bus_finish(r);
|
|
|
|
using GetAllProfilesVar = Glib::Variant<std::tuple<Glib::ustring>>;
|
|
|
|
auto callArgs = GetAllProfilesVar::create(std::make_tuple("net.hadess.PowerProfiles"));
|
|
|
|
powerProfilesProxy_->call("org.freedesktop.DBus.Properties.GetAll",
|
2024-03-01 18:37:20 +00:00
|
|
|
sigc::mem_fun(*this, &PowerProfilesDaemon::getAllPropsCb), callArgs);
|
2024-02-18 21:06:21 +00:00
|
|
|
// Connect active profile callback
|
2024-03-01 11:33:36 +00:00
|
|
|
} catch (const std::exception& e) {
|
2024-03-01 18:37:20 +00:00
|
|
|
spdlog::error("Failed to create the power profiles daemon DBus proxy: {}", e.what());
|
|
|
|
} catch (const Glib::Error& e) {
|
|
|
|
spdlog::error("Failed to create the power profiles daemon DBus proxy: {}",
|
|
|
|
std::string(e.what()));
|
2024-03-01 11:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for the GetAll call.
|
|
|
|
//
|
|
|
|
// We're abusing this call to make sure power-profiles-daemon is
|
|
|
|
// available on the host. We're not really using
|
|
|
|
void PowerProfilesDaemon::getAllPropsCb(Glib::RefPtr<Gio::AsyncResult>& r) {
|
|
|
|
try {
|
|
|
|
auto _ = powerProfilesProxy_->call_finish(r);
|
|
|
|
// Power-profiles-daemon responded something, we can assume it's
|
|
|
|
// available, we can safely attach the activeProfile monitoring
|
|
|
|
// now.
|
|
|
|
connected_ = true;
|
2024-03-01 18:37:20 +00:00
|
|
|
powerProfilesProxy_->signal_properties_changed().connect(
|
2024-02-26 14:07:21 +00:00
|
|
|
sigc::mem_fun(*this, &PowerProfilesDaemon::profileChangedCb));
|
2024-02-18 21:06:21 +00:00
|
|
|
populateInitState();
|
|
|
|
dp.emit();
|
2024-03-01 11:33:36 +00:00
|
|
|
} catch (const std::exception& err) {
|
|
|
|
spdlog::error("Failed to query power-profiles-daemon via dbus: {}", err.what());
|
2024-03-01 18:37:20 +00:00
|
|
|
} catch (const Glib::Error& err) {
|
|
|
|
spdlog::error("Failed to query power-profiles-daemon via dbus: {}", std::string(err.what()));
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerProfilesDaemon::populateInitState() {
|
|
|
|
// Retrieve current active profile
|
|
|
|
Glib::Variant<std::string> profileStr;
|
2024-02-26 14:07:21 +00:00
|
|
|
powerProfilesProxy_->get_cached_property(profileStr, "ActiveProfile");
|
2024-02-18 21:06:21 +00:00
|
|
|
|
|
|
|
// Retrieve profiles list, it's aa{sv}.
|
|
|
|
using ProfilesType = std::vector<std::map<Glib::ustring, Glib::Variant<std::string>>>;
|
|
|
|
Glib::Variant<ProfilesType> profilesVariant;
|
2024-02-26 14:07:21 +00:00
|
|
|
powerProfilesProxy_->get_cached_property(profilesVariant, "Profiles");
|
2024-02-26 13:40:28 +00:00
|
|
|
for (auto& variantDict : profilesVariant.get()) {
|
2024-03-01 18:37:20 +00:00
|
|
|
Glib::ustring name;
|
|
|
|
Glib::ustring driver;
|
2024-02-18 21:06:21 +00:00
|
|
|
if (auto p = variantDict.find("Profile"); p != variantDict.end()) {
|
|
|
|
name = p->second.get();
|
|
|
|
}
|
|
|
|
if (auto d = variantDict.find("Driver"); d != variantDict.end()) {
|
|
|
|
driver = d->second.get();
|
|
|
|
}
|
2024-03-01 18:37:20 +00:00
|
|
|
if (!name.empty()) {
|
|
|
|
availableProfiles_.emplace_back(std::move(name), std::move(driver));
|
|
|
|
} else {
|
|
|
|
spdlog::error(
|
|
|
|
"Power profiles daemon: power-profiles-daemon sent us an empty power profile name. "
|
|
|
|
"Something is wrong.");
|
|
|
|
}
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the index of the current activated mode (to toggle)
|
|
|
|
std::string str = profileStr.get();
|
2024-02-26 14:07:21 +00:00
|
|
|
switchToProfile(str);
|
2024-02-18 21:06:21 +00:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2024-02-26 14:07:21 +00:00
|
|
|
void PowerProfilesDaemon::profileChangedCb(
|
2024-02-26 13:40:28 +00:00
|
|
|
const Gio::DBus::Proxy::MapChangedProperties& changedProperties,
|
|
|
|
const std::vector<Glib::ustring>& invalidatedProperties) {
|
2024-03-01 11:33:36 +00:00
|
|
|
// We're likely connected if this callback gets triggered.
|
|
|
|
// But better be safe than sorry.
|
|
|
|
if (connected_) {
|
|
|
|
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);
|
|
|
|
update();
|
|
|
|
}
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 11:33:36 +00:00
|
|
|
// Look for the profile str in our internal profiles list. Using a
|
|
|
|
// vector to store the profiles ain't the smartest move
|
|
|
|
// complexity-wise, but it makes toggling between the mode easy. This
|
|
|
|
// vector is 3 elements max, we'll be fine :P
|
|
|
|
void PowerProfilesDaemon::switchToProfile(std::string const& str) {
|
|
|
|
auto pred = [str](Profile const& p) { return p.name == str; };
|
|
|
|
this->activeProfile_ = std::find_if(availableProfiles_.begin(), availableProfiles_.end(), pred);
|
|
|
|
if (activeProfile_ == availableProfiles_.end()) {
|
2024-03-01 18:37:20 +00:00
|
|
|
spdlog::error(
|
|
|
|
"Power profile daemon: can't find the active profile {} in the available profiles list",
|
|
|
|
str);
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
2024-03-01 11:33:36 +00:00
|
|
|
}
|
2024-02-18 21:06:21 +00:00
|
|
|
|
2024-03-01 11:33:36 +00:00
|
|
|
auto PowerProfilesDaemon::update() -> void {
|
2024-03-01 18:37:20 +00:00
|
|
|
if (connected_ && activeProfile_ != availableProfiles_.end()) {
|
2024-03-01 11:33:36 +00:00
|
|
|
auto profile = (*activeProfile_);
|
|
|
|
// Set label
|
|
|
|
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
|
|
|
store.push_back(fmt::arg("profile", profile.name));
|
|
|
|
store.push_back(fmt::arg("driver", profile.driver));
|
|
|
|
store.push_back(fmt::arg("icon", getIcon(0, profile.name)));
|
|
|
|
label_.set_markup(fmt::vformat(format_, store));
|
|
|
|
if (tooltipEnabled()) {
|
|
|
|
label_.set_tooltip_text(fmt::vformat(tooltipFormat_, store));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set CSS class
|
|
|
|
if (!currentStyle_.empty()) {
|
|
|
|
label_.get_style_context()->remove_class(currentStyle_);
|
|
|
|
}
|
|
|
|
label_.get_style_context()->add_class(profile.name);
|
|
|
|
currentStyle_ = profile.name;
|
2024-03-01 18:37:20 +00:00
|
|
|
event_box_.set_visible(true);
|
|
|
|
} else {
|
|
|
|
event_box_.set_visible(false);
|
2024-03-01 11:33:36 +00:00
|
|
|
}
|
2024-03-01 18:37:20 +00:00
|
|
|
|
|
|
|
ALabel::update();
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PowerProfilesDaemon::handleToggle(GdkEventButton* const& e) {
|
2024-03-01 18:37:20 +00:00
|
|
|
if (e->type == GdkEventType::GDK_BUTTON_PRESS && connected_) {
|
|
|
|
activeProfile_++;
|
|
|
|
if (activeProfile_ == availableProfiles_.end()) {
|
|
|
|
activeProfile_ = availableProfiles_.begin();
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
2024-03-01 18:37:20 +00:00
|
|
|
|
|
|
|
using VarStr = Glib::Variant<Glib::ustring>;
|
|
|
|
using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring, Glib::ustring, VarStr>>;
|
|
|
|
VarStr activeProfileVariant = VarStr::create(activeProfile_->name);
|
|
|
|
auto callArgs = SetPowerProfileVar::create(
|
|
|
|
std::make_tuple("net.hadess.PowerProfiles", "ActiveProfile", activeProfileVariant));
|
|
|
|
powerProfilesProxy_->call("org.freedesktop.DBus.Properties.Set",
|
|
|
|
sigc::mem_fun(*this, &PowerProfilesDaemon::setPropCb), callArgs);
|
2024-02-18 21:06:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-01 11:33:36 +00:00
|
|
|
void PowerProfilesDaemon::setPropCb(Glib::RefPtr<Gio::AsyncResult>& r) {
|
2024-03-01 18:37:20 +00:00
|
|
|
try {
|
|
|
|
auto _ = powerProfilesProxy_->call_finish(r);
|
|
|
|
update();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
spdlog::error("Failed to set the the active power profile: {}", e.what());
|
|
|
|
} catch (const Glib::Error& e) {
|
|
|
|
spdlog::error("Failed to set the active power profile: {}", std::string(e.what()));
|
|
|
|
}
|
2024-03-01 11:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 13:40:28 +00:00
|
|
|
} // namespace waybar::modules
|