-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbattery.cpp
More file actions
277 lines (229 loc) · 6.75 KB
/
battery.cpp
File metadata and controls
277 lines (229 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "battery.hpp"
#include <gtk-utils.hpp>
#include <iostream>
#include <algorithm>
#define UPOWER_NAME "org.freedesktop.UPower"
#define DISPLAY_DEVICE "/org/freedesktop/UPower/devices/DisplayDevice"
#define ICON "IconName"
#define TYPE "Type"
#define STATE "State"
#define PERCENTAGE "Percentage"
#define TIMETOFULL "TimeToFull"
#define TIMETOEMPTY "TimeToEmpty"
#define SHOULD_DISPLAY "IsPresent"
static std::string get_device_type_description(uint32_t type)
{
if (type == 2)
{
return "Battery ";
}
if (type == 3)
{
return "UPS ";
}
return "";
}
void WayfireBatteryInfo::on_properties_changed(
const Gio::DBus::Proxy::MapChangedProperties& properties,
const std::vector<Glib::ustring>& invalidated)
{
bool invalid_icon = false, invalid_details = false;
bool invalid_state = false;
for (auto& prop : properties)
{
if (prop.first == ICON)
{
invalid_icon = true;
}
if ((prop.first == TYPE) || (prop.first == STATE) || (prop.first == PERCENTAGE) ||
(prop.first == TIMETOFULL) || (prop.first == TIMETOEMPTY))
{
invalid_details = true;
}
if (prop.first == SHOULD_DISPLAY)
{
invalid_state = true;
}
}
if (invalid_icon)
{
update_icon();
}
if (invalid_details)
{
update_details();
}
if (invalid_state)
{
update_state();
}
}
void WayfireBatteryInfo::update_icon()
{
Glib::Variant<Glib::ustring> icon_name;
display_device->get_cached_property(icon_name, ICON);
WfIconLoadOptions options;
options.invert = invert_opt;
options.user_scale = button.get_scale_factor();
set_image_icon(icon, icon_name.get(), size_opt, options);
}
static std::string state_descriptions[] = {
"Unknown", // 0
"Charging", // 1
"Discharging", // 2
"Empty", // 3
"Fully charged", // 4
"Pending charge", // 5
"Pending discharge", // 6
};
static bool is_charging(uint32_t state)
{
return (state == 1) || (state == 5);
}
static bool is_discharging(uint32_t state)
{
return (state == 2) || (state == 6);
}
static std::string format_digit(int digit)
{
return digit <= 9 ? ("0" + std::to_string(digit)) :
std::to_string(digit);
}
static std::string uint_to_time(int64_t time)
{
int hrs = time / 3600;
int min = (time / 60) % 60;
return format_digit(hrs) + ":" + format_digit(min);
}
void WayfireBatteryInfo::update_font()
{
if ((std::string)font_opt == "default")
{
label.unset_font();
} else
{
label.override_font(Pango::FontDescription((std::string)font_opt));
}
}
void WayfireBatteryInfo::update_details()
{
Glib::Variant<guint32> type;
display_device->get_cached_property(type, TYPE);
Glib::Variant<guint32> vstate;
display_device->get_cached_property(vstate, STATE);
uint32_t state = vstate.get();
Glib::Variant<gdouble> vpercentage;
display_device->get_cached_property(vpercentage, PERCENTAGE);
auto percentage_string = std::to_string((int)vpercentage.get()) + "%";
Glib::Variant<gint64> time_to_full;
display_device->get_cached_property(time_to_full, TIMETOFULL);
Glib::Variant<gint64> time_to_empty;
display_device->get_cached_property(time_to_empty, TIMETOEMPTY);
std::string description = percentage_string + ", " + state_descriptions[state];
if (is_charging(state))
{
description += ", " + uint_to_time(time_to_full.get()) + " until full";
} else if (is_discharging(state))
{
description += ", " + uint_to_time(time_to_empty.get()) + " remaining";
}
button.set_tooltip_text(
get_device_type_description(type.get()) + description);
if (status_opt.value() == BATTERY_STATUS_PERCENT)
{
label.set_text(percentage_string);
} else if (status_opt.value() == BATTERY_STATUS_FULL)
{
label.set_text(description);
}
if (status_opt.value() == BATTERY_STATUS_ICON)
{
label.hide();
} else
{
label.show();
}
}
void WayfireBatteryInfo::on_battery_scroll(GdkEventScroll *event)
{
if (scroll_counter <= 0) {
if (event->delta_y < 0)
{
std::system(battery_scrollup_command.value().c_str());
} else if (0 < event->delta_y)
{
std::system(battery_scrolldown_command.value().c_str());
}
scroll_counter = 3;
}
scroll_counter--;
}
void WayfireBatteryInfo::update_state()
{
std::cout << "unimplemented reached, in battery.cpp: "
"\n\tWayfireBatteryInfo::update_state()" << std::endl;
}
bool WayfireBatteryInfo::setup_dbus()
{
auto cancellable = Gio::Cancellable::create();
connection = Gio::DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SYSTEM, cancellable);
if (!connection)
{
std::cerr << "Failed to connect to dbus" << std::endl;
return false;
}
upower_proxy = Gio::DBus::Proxy::create_sync(connection, UPOWER_NAME,
"/org/freedesktop/UPower",
"org.freedesktop.UPower");
if (!upower_proxy)
{
std::cerr << "Failed to connect to UPower" << std::endl;
return false;
}
display_device = Gio::DBus::Proxy::create_sync(connection,
UPOWER_NAME,
DISPLAY_DEVICE,
"org.freedesktop.UPower.Device");
if (!display_device)
{
return false;
}
Glib::Variant<bool> present;
display_device->get_cached_property(present, SHOULD_DISPLAY);
if (present.get())
{
display_device->signal_properties_changed().connect(
sigc::mem_fun(this, &WayfireBatteryInfo::on_properties_changed));
return true;
}
return false;
}
// TODO: simplify config loading
static const std::string default_font = "default";
void WayfireBatteryInfo::init(Gtk::HBox *container)
{
scroll_counter = 0;
if (!setup_dbus())
{
return;
}
button_box.add(icon);
button.get_style_context()->add_class("flat");
button.set_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
button.signal_scroll_event().connect_notify(
sigc::mem_fun(this, &WayfireBatteryInfo::on_battery_scroll));
status_opt.set_callback([=] () { update_details(); });
font_opt.set_callback([=] () { update_font(); });
size_opt.set_callback([=] () { update_icon(); });
invert_opt.set_callback([=] () { update_icon(); });
update_details();
update_font();
update_icon();
container->pack_start(button, Gtk::PACK_SHRINK);
button_box.add(label);
button_box.set_spacing(5);
button.add(button_box);
button.property_scale_factor().signal_changed()
.connect(sigc::mem_fun(this, &WayfireBatteryInfo::update_icon));
button.show_all();
}