2018-08-08 23:54:58 +02:00
|
|
|
#include "modules/battery.hpp"
|
|
|
|
|
2018-08-09 13:30:11 +02:00
|
|
|
waybar::modules::Battery::Battery(Json::Value config)
|
2018-08-18 11:43:48 +02:00
|
|
|
: ALabel(std::move(config))
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
|
|
|
try {
|
2018-08-16 14:29:41 +02:00
|
|
|
for (auto &node : fs::directory_iterator(data_dir_)) {
|
2018-08-13 17:11:47 +02:00
|
|
|
if (fs::is_directory(node) && fs::exists(node / "capacity")
|
2018-08-16 14:29:41 +02:00
|
|
|
&& fs::exists(node / "status") && fs::exists(node / "uevent")) {
|
|
|
|
batteries_.push_back(node);
|
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
|
|
|
} catch (fs::filesystem_error &e) {
|
2018-08-13 14:05:13 +02:00
|
|
|
throw std::runtime_error(e.what());
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
2018-08-16 14:29:41 +02:00
|
|
|
if (batteries_.empty()) {
|
2018-08-13 14:05:13 +02:00
|
|
|
throw std::runtime_error("No batteries.");
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-19 13:39:57 +02:00
|
|
|
fd_ = inotify_init1(IN_CLOEXEC);
|
|
|
|
if (fd_ == -1) {
|
2018-08-13 14:05:13 +02:00
|
|
|
throw std::runtime_error("Unable to listen batteries.");
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
|
|
|
for (auto &bat : batteries_) {
|
2018-08-19 13:39:57 +02:00
|
|
|
inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS);
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-19 13:39:57 +02:00
|
|
|
// Trigger first values
|
|
|
|
update();
|
2018-08-16 14:29:41 +02:00
|
|
|
label_.set_name("battery");
|
2018-08-19 13:39:57 +02:00
|
|
|
thread_.sig_update.connect(sigc::mem_fun(*this, &Battery::update));
|
|
|
|
thread_ = [this] {
|
2018-08-17 14:24:00 +02:00
|
|
|
struct inotify_event event = {0};
|
2018-08-19 13:39:57 +02:00
|
|
|
int nbytes = read(fd_, &event, sizeof(event));
|
2018-08-16 14:29:41 +02:00
|
|
|
if (nbytes != sizeof(event)) {
|
2018-08-13 14:05:13 +02:00
|
|
|
return;
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-19 20:37:33 +02:00
|
|
|
thread_.emit();
|
2018-08-08 23:54:58 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-19 13:39:57 +02:00
|
|
|
waybar::modules::Battery::~Battery()
|
|
|
|
{
|
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:54:58 +02:00
|
|
|
auto waybar::modules::Battery::update() -> void
|
|
|
|
{
|
|
|
|
try {
|
2018-08-13 14:05:13 +02:00
|
|
|
uint16_t total = 0;
|
|
|
|
std::string status;
|
2018-08-16 14:29:41 +02:00
|
|
|
for (auto &bat : batteries_) {
|
2018-08-13 14:05:13 +02:00
|
|
|
uint16_t capacity;
|
2018-08-15 22:19:17 +02:00
|
|
|
std::string _status;
|
2018-08-09 11:21:08 +02:00
|
|
|
std::ifstream(bat / "capacity") >> capacity;
|
2018-08-15 22:19:17 +02:00
|
|
|
std::ifstream(bat / "status") >> _status;
|
2018-08-16 14:29:41 +02:00
|
|
|
if (_status != "Unknown") {
|
2018-08-15 22:19:17 +02:00
|
|
|
status = _status;
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-13 14:05:13 +02:00
|
|
|
total += capacity;
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
2018-08-16 14:29:41 +02:00
|
|
|
uint16_t capacity = total / batteries_.size();
|
|
|
|
auto format = config_["format"]
|
|
|
|
? config_["format"].asString() : "{capacity}%";
|
|
|
|
label_.set_text(fmt::format(format, fmt::arg("capacity", capacity),
|
|
|
|
fmt::arg("icon", getIcon(capacity))));
|
|
|
|
label_.set_tooltip_text(status);
|
2018-08-15 22:19:17 +02:00
|
|
|
bool charging = status == "Charging";
|
2018-08-16 14:29:41 +02:00
|
|
|
if (charging) {
|
|
|
|
label_.get_style_context()->add_class("charging");
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class("charging");
|
|
|
|
}
|
|
|
|
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 14:05:13 +02:00
|
|
|
} catch (const std::exception& e) {
|
2018-08-08 23:54:58 +02:00
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
std::string waybar::modules::Battery::getIcon(uint16_t percentage)
|
2018-08-11 13:15:31 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
if (!config_["format-icons"] || !config_["format-icons"].isArray()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
auto size = config_["format-icons"].size();
|
2018-08-13 22:33:07 +02:00
|
|
|
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
|
2018-08-16 14:29:41 +02:00
|
|
|
return config_["format-icons"][idx].asString();
|
2018-08-11 13:15:31 +02:00
|
|
|
}
|