2018-08-08 21:54:58 +00:00
|
|
|
#include "modules/battery.hpp"
|
|
|
|
|
2018-08-09 11:30:11 +00:00
|
|
|
waybar::modules::Battery::Battery(Json::Value config)
|
|
|
|
: _config(config)
|
2018-08-08 21:54:58 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
for (auto &node : fs::directory_iterator(_data_dir)) {
|
2018-08-09 09:21:08 +00:00
|
|
|
if (fs::is_directory(node) && fs::exists(node / "capacity")
|
|
|
|
&& fs::exists(node / "status")) {
|
2018-08-08 21:54:58 +00:00
|
|
|
_batteries.push_back(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (fs::filesystem_error &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:02:12 +00:00
|
|
|
if (!_batteries.size()) {
|
2018-08-10 16:04:48 +00:00
|
|
|
std::cerr << "No batteries." << std::endl;
|
2018-08-10 16:02:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-09 08:50:16 +00:00
|
|
|
_label.get_style_context()->add_class("battery");
|
2018-08-10 21:21:21 +00:00
|
|
|
int interval = _config["interval"] ? _config["inveral"].asInt() : 1;
|
|
|
|
_thread = [this, interval] {
|
2018-08-11 00:40:13 +00:00
|
|
|
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Battery::update));
|
2018-08-10 21:21:21 +00:00
|
|
|
_thread.sleep_for(chrono::seconds(interval));
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::Battery::update() -> void
|
|
|
|
{
|
|
|
|
try {
|
2018-08-09 09:21:08 +00:00
|
|
|
int total = 0;
|
|
|
|
bool charging = false;
|
2018-08-08 21:54:58 +00:00
|
|
|
for (auto &bat : _batteries) {
|
2018-08-09 09:21:08 +00:00
|
|
|
int capacity;
|
2018-08-08 21:54:58 +00:00
|
|
|
std::string status;
|
2018-08-09 09:21:08 +00:00
|
|
|
std::ifstream(bat / "capacity") >> capacity;
|
|
|
|
total += capacity;
|
2018-08-08 21:54:58 +00:00
|
|
|
std::ifstream(bat / "status") >> status;
|
|
|
|
if (status == "Charging") {
|
2018-08-09 09:21:08 +00:00
|
|
|
charging = true;
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-09 11:30:11 +00:00
|
|
|
auto format = _config["format"] ? _config["format"].asString() : "{}%";
|
2018-08-11 10:49:28 +00:00
|
|
|
auto value = total / _batteries.size();
|
2018-08-11 11:15:31 +00:00
|
|
|
_label.set_text(fmt::format(format, fmt::arg("value", value),
|
|
|
|
fmt::arg("icon", _getIcon(value))));
|
2018-08-09 15:30:23 +00:00
|
|
|
_label.set_tooltip_text(charging ? "Charging" : "Discharging");
|
2018-08-11 10:49:28 +00:00
|
|
|
if (charging)
|
|
|
|
_label.get_style_context()->add_class("charging");
|
|
|
|
else
|
|
|
|
_label.get_style_context()->remove_class("charging");
|
2018-08-11 11:03:35 +00:00
|
|
|
if (value < 16 && !charging)
|
2018-08-11 10:49:28 +00:00
|
|
|
_label.get_style_context()->add_class("warning");
|
|
|
|
else
|
|
|
|
_label.get_style_context()->remove_class("warning");
|
2018-08-08 21:54:58 +00:00
|
|
|
} catch (std::exception &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 11:15:31 +00:00
|
|
|
std::string waybar::modules::Battery::_getIcon(uint32_t percentage)
|
|
|
|
{
|
|
|
|
if (!_config["format-icons"] || !_config["format-icons"].isArray()) return "";
|
|
|
|
auto step = 100 / _config["format-icons"].size();
|
|
|
|
return _config["format-icons"][percentage / step].asString();
|
|
|
|
}
|
|
|
|
|
2018-08-08 21:54:58 +00:00
|
|
|
waybar::modules::Battery::operator Gtk::Widget &()
|
|
|
|
{
|
|
|
|
return _label;
|
|
|
|
}
|