Waybar/src/modules/battery.cpp

82 lines
2.5 KiB
C++
Raw Normal View History

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-13 15:11:47 +00:00
if (fs::is_directory(node) && fs::exists(node / "capacity")
&& fs::exists(node / "status") && fs::exists(node / "uevent"))
2018-08-08 21:54:58 +00:00
_batteries.push_back(node);
}
} catch (fs::filesystem_error &e) {
2018-08-13 12:05:13 +00:00
throw std::runtime_error(e.what());
2018-08-08 21:54:58 +00:00
}
2018-08-13 12:05:13 +00:00
if (!_batteries.size())
throw std::runtime_error("No batteries.");
2018-08-10 16:02:12 +00:00
2018-08-13 12:05:13 +00:00
auto fd = inotify_init();
if (fd == -1)
throw std::runtime_error("Unable to listen batteries.");
for (auto &bat : _batteries)
inotify_add_watch(fd, (bat / "uevent").c_str(), IN_ACCESS);
// Trigger first value
update();
2018-08-09 08:50:16 +00:00
_label.get_style_context()->add_class("battery");
2018-08-13 12:05:13 +00:00
_thread = [this, fd] {
struct inotify_event event;
int nbytes = read(fd, &event, sizeof(event));
if (nbytes != sizeof(event))
return;
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Battery::update));
2018-08-08 21:54:58 +00:00
};
}
auto waybar::modules::Battery::update() -> void
{
try {
2018-08-13 12:05:13 +00:00
uint16_t total = 0;
2018-08-09 09:21:08 +00:00
bool charging = false;
2018-08-13 12:05:13 +00:00
std::string status;
2018-08-08 21:54:58 +00:00
for (auto &bat : _batteries) {
2018-08-13 12:05:13 +00:00
uint16_t capacity;
2018-08-09 09:21:08 +00:00
std::ifstream(bat / "capacity") >> capacity;
2018-08-08 21:54:58 +00:00
std::ifstream(bat / "status") >> status;
2018-08-13 12:05:13 +00:00
if (status == "Charging")
2018-08-09 09:21:08 +00:00
charging = true;
2018-08-13 12:05:13 +00:00
total += capacity;
2018-08-08 21:54:58 +00:00
}
2018-08-13 12:05:13 +00:00
uint16_t capacity = total / _batteries.size();
2018-08-13 20:33:07 +00:00
auto format = _config["format"]
? _config["format"].asString() : "{capacity}%";
_label.set_text(fmt::format(format, fmt::arg("capacity", capacity),
2018-08-13 12:05:13 +00:00
fmt::arg("icon", _getIcon(capacity))));
_label.set_tooltip_text(status);
if (charging)
_label.get_style_context()->add_class("charging");
else
_label.get_style_context()->remove_class("charging");
2018-08-14 09:26:06 +00:00
auto critical = _config["critical"] ? _config["critical"].asUInt() : 15;
if (capacity <= critical && !charging)
_label.get_style_context()->add_class("warning");
else
_label.get_style_context()->remove_class("warning");
2018-08-13 12:05:13 +00:00
} catch (const std::exception& e) {
2018-08-08 21:54:58 +00:00
std::cerr << e.what() << std::endl;
}
}
2018-08-13 12:05:13 +00:00
std::string waybar::modules::Battery::_getIcon(uint16_t percentage)
2018-08-11 11:15:31 +00:00
{
if (!_config["format-icons"] || !_config["format-icons"].isArray()) return "";
2018-08-13 20:33:07 +00:00
auto size = _config["format-icons"].size();
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
return _config["format-icons"][idx].asString();
2018-08-11 11:15:31 +00:00
}
2018-08-08 21:54:58 +00:00
waybar::modules::Battery::operator Gtk::Widget &()
{
return _label;
}