1
1
#include " modules/power_profiles_daemon.hpp"
2
2
3
- // In the 80000 version of fmt library authors decided to optimize imports
4
- // and moved declarations required for fmt::dynamic_format_arg_store in new
5
- // header fmt/args.h
6
- #if (FMT_VERSION >= 80000)
7
3
#include < fmt/args.h>
8
- #else
9
- #include < fmt/core.h>
10
- #endif
11
-
12
4
#include < glibmm.h>
13
5
#include < glibmm/variant.h>
14
6
#include < spdlog/spdlog.h>
15
7
16
8
namespace waybar ::modules {
17
9
18
10
PowerProfilesDaemon::PowerProfilesDaemon (const std::string& id, const Json::Value& config)
19
- : ALabel(config, " power-profiles-daemon" , id, " {profile}" , 0 , false , true ), connected_(false ) {
20
- if (config_[" format" ].isString ()) {
21
- format_ = config_[" format" ].asString ();
22
- } else {
23
- format_ = " {icon}" ;
24
- }
25
-
11
+ : ALabel(config, " power-profiles-daemon" , id, " {icon}" , 0 , false , true ), connected_(false ) {
26
12
if (config_[" tooltip-format" ].isString ()) {
27
13
tooltipFormat_ = config_[" tooltip-format" ].asString ();
28
14
} else {
@@ -58,27 +44,19 @@ PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Valu
58
44
sigc::mem_fun (*this , &PowerProfilesDaemon::busConnectedCb));
59
45
}
60
46
61
- PowerProfilesDaemon::~PowerProfilesDaemon () {
62
- if (powerProfileChangeSignal_.connected ()) {
63
- powerProfileChangeSignal_.disconnect ();
64
- }
65
- if (powerProfilesProxy_) {
66
- powerProfilesProxy_.reset ();
67
- }
68
- }
69
-
70
47
void PowerProfilesDaemon::busConnectedCb (Glib::RefPtr<Gio::AsyncResult>& r) {
71
48
try {
72
49
powerProfilesProxy_ = Gio::DBus::Proxy::create_for_bus_finish (r);
73
50
using GetAllProfilesVar = Glib::Variant<std::tuple<Glib::ustring>>;
74
51
auto callArgs = GetAllProfilesVar::create (std::make_tuple (" net.hadess.PowerProfiles" ));
75
-
76
- auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(callArgs);
77
52
powerProfilesProxy_->call (" org.freedesktop.DBus.Properties.GetAll" ,
78
- sigc::mem_fun (*this , &PowerProfilesDaemon::getAllPropsCb), container );
53
+ sigc::mem_fun (*this , &PowerProfilesDaemon::getAllPropsCb), callArgs );
79
54
// Connect active profile callback
80
55
} catch (const std::exception & e) {
81
- spdlog::error (" Failed to create the power profiles daemon DBus proxy" );
56
+ spdlog::error (" Failed to create the power profiles daemon DBus proxy: {}" , e.what ());
57
+ } catch (const Glib::Error& e) {
58
+ spdlog::error (" Failed to create the power profiles daemon DBus proxy: {}" ,
59
+ std::string (e.what ()));
82
60
}
83
61
}
84
62
@@ -93,12 +71,14 @@ void PowerProfilesDaemon::getAllPropsCb(Glib::RefPtr<Gio::AsyncResult>& r) {
93
71
// available, we can safely attach the activeProfile monitoring
94
72
// now.
95
73
connected_ = true ;
96
- powerProfileChangeSignal_ = powerProfilesProxy_->signal_properties_changed ().connect (
74
+ powerProfilesProxy_->signal_properties_changed ().connect (
97
75
sigc::mem_fun (*this , &PowerProfilesDaemon::profileChangedCb));
98
76
populateInitState ();
99
77
dp.emit ();
100
78
} catch (const std::exception & err) {
101
79
spdlog::error (" Failed to query power-profiles-daemon via dbus: {}" , err.what ());
80
+ } catch (const Glib::Error& err) {
81
+ spdlog::error (" Failed to query power-profiles-daemon via dbus: {}" , std::string (err.what ()));
102
82
}
103
83
}
104
84
@@ -111,18 +91,22 @@ void PowerProfilesDaemon::populateInitState() {
111
91
using ProfilesType = std::vector<std::map<Glib::ustring, Glib::Variant<std::string>>>;
112
92
Glib::Variant<ProfilesType> profilesVariant;
113
93
powerProfilesProxy_->get_cached_property (profilesVariant, " Profiles" );
114
- Glib::ustring name;
115
- Glib::ustring driver;
116
- Profile profile;
117
94
for (auto & variantDict : profilesVariant.get ()) {
95
+ Glib::ustring name;
96
+ Glib::ustring driver;
118
97
if (auto p = variantDict.find (" Profile" ); p != variantDict.end ()) {
119
98
name = p->second .get ();
120
99
}
121
100
if (auto d = variantDict.find (" Driver" ); d != variantDict.end ()) {
122
101
driver = d->second .get ();
123
102
}
124
- profile = {name, driver};
125
- availableProfiles_.push_back (profile);
103
+ if (!name.empty ()) {
104
+ availableProfiles_.emplace_back (std::move (name), std::move (driver));
105
+ } else {
106
+ spdlog::error (
107
+ " Power profiles daemon: power-profiles-daemon sent us an empty power profile name. "
108
+ " Something is wrong." );
109
+ }
126
110
}
127
111
128
112
// Find the index of the current activated mode (to toggle)
@@ -157,12 +141,14 @@ void PowerProfilesDaemon::switchToProfile(std::string const& str) {
157
141
auto pred = [str](Profile const & p) { return p.name == str; };
158
142
this ->activeProfile_ = std::find_if (availableProfiles_.begin (), availableProfiles_.end (), pred);
159
143
if (activeProfile_ == availableProfiles_.end ()) {
160
- throw std::runtime_error (" FATAL, can't find the active profile in the available profiles list" );
144
+ spdlog::error (
145
+ " Power profile daemon: can't find the active profile {} in the available profiles list" ,
146
+ str);
161
147
}
162
148
}
163
149
164
150
auto PowerProfilesDaemon::update () -> void {
165
- if (connected_) {
151
+ if (connected_ && activeProfile_ != availableProfiles_. end () ) {
166
152
auto profile = (*activeProfile_);
167
153
// Set label
168
154
fmt::dynamic_format_arg_store<fmt::format_context> store;
@@ -180,35 +166,41 @@ auto PowerProfilesDaemon::update() -> void {
180
166
}
181
167
label_.get_style_context ()->add_class (profile.name );
182
168
currentStyle_ = profile.name ;
183
-
184
- ALabel::update ();
169
+ event_box_.set_visible (true );
170
+ } else {
171
+ event_box_.set_visible (false );
185
172
}
173
+
174
+ ALabel::update ();
186
175
}
187
176
188
177
bool PowerProfilesDaemon::handleToggle (GdkEventButton* const & e) {
189
- if (connected_) {
190
- if (e->type == GdkEventType::GDK_BUTTON_PRESS && powerProfilesProxy_) {
191
- activeProfile_++;
192
- if (activeProfile_ == availableProfiles_.end ()) {
193
- activeProfile_ = availableProfiles_.begin ();
194
- }
195
-
196
- using VarStr = Glib::Variant<Glib::ustring>;
197
- using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring, Glib::ustring, VarStr>>;
198
- VarStr activeProfileVariant = VarStr::create (activeProfile_->name );
199
- auto callArgs = SetPowerProfileVar::create (
200
- std::make_tuple (" net.hadess.PowerProfiles" , " ActiveProfile" , activeProfileVariant));
201
- auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(callArgs);
202
- powerProfilesProxy_->call (" org.freedesktop.DBus.Properties.Set" ,
203
- sigc::mem_fun (*this , &PowerProfilesDaemon::setPropCb), container);
178
+ if (e->type == GdkEventType::GDK_BUTTON_PRESS && connected_) {
179
+ activeProfile_++;
180
+ if (activeProfile_ == availableProfiles_.end ()) {
181
+ activeProfile_ = availableProfiles_.begin ();
204
182
}
183
+
184
+ using VarStr = Glib::Variant<Glib::ustring>;
185
+ using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring, Glib::ustring, VarStr>>;
186
+ VarStr activeProfileVariant = VarStr::create (activeProfile_->name );
187
+ auto callArgs = SetPowerProfileVar::create (
188
+ std::make_tuple (" net.hadess.PowerProfiles" , " ActiveProfile" , activeProfileVariant));
189
+ powerProfilesProxy_->call (" org.freedesktop.DBus.Properties.Set" ,
190
+ sigc::mem_fun (*this , &PowerProfilesDaemon::setPropCb), callArgs);
205
191
}
206
192
return true ;
207
193
}
208
194
209
195
void PowerProfilesDaemon::setPropCb (Glib::RefPtr<Gio::AsyncResult>& r) {
210
- auto _ = powerProfilesProxy_->call_finish (r);
211
- update ();
196
+ try {
197
+ auto _ = powerProfilesProxy_->call_finish (r);
198
+ update ();
199
+ } catch (const std::exception & e) {
200
+ spdlog::error (" Failed to set the the active power profile: {}" , e.what ());
201
+ } catch (const Glib::Error& e) {
202
+ spdlog::error (" Failed to set the active power profile: {}" , std::string (e.what ()));
203
+ }
212
204
}
213
205
214
206
} // namespace waybar::modules
0 commit comments