2018-08-08 21:54:58 +00:00
|
|
|
#include "modules/battery.hpp"
|
2019-05-18 23:44:45 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
|
2019-04-18 15:52:00 +00:00
|
|
|
: ALabel(config, "{capacity}%", 60) {
|
2018-12-18 16:30:54 +00:00
|
|
|
label_.set_name("battery");
|
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-12-24 07:37:10 +00:00
|
|
|
getBatteries();
|
2018-08-19 11:39:57 +00:00
|
|
|
fd_ = inotify_init1(IN_CLOEXEC);
|
|
|
|
if (fd_ == -1) {
|
2018-08-13 12:05:13 +00:00
|
|
|
throw std::runtime_error("Unable to listen batteries.");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-09-04 21:50:08 +00:00
|
|
|
for (auto const& bat : batteries_) {
|
2018-12-24 07:37:10 +00:00
|
|
|
auto wd = inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS);
|
|
|
|
if (wd != -1) {
|
|
|
|
wds_.push_back(wd);
|
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
waybar::modules::Battery::~Battery() {
|
2018-12-24 07:37:10 +00:00
|
|
|
for (auto wd : wds_) {
|
|
|
|
inotify_rm_watch(fd_, wd);
|
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
void waybar::modules::Battery::worker() {
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_timer_ = [this] {
|
2018-10-25 15:39:15 +00:00
|
|
|
dp.emit();
|
2018-12-24 07:50:58 +00:00
|
|
|
thread_timer_.sleep_for(interval_);
|
2018-10-25 15:39:15 +00:00
|
|
|
};
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_ = [this] {
|
2018-08-17 12:24:00 +00:00
|
|
|
struct inotify_event event = {0};
|
2019-04-18 15:52:00 +00:00
|
|
|
int nbytes = read(fd_, &event, sizeof(event));
|
2018-12-24 07:37:10 +00:00
|
|
|
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
|
|
|
|
thread_.stop();
|
2018-08-13 12:05:13 +00:00
|
|
|
return;
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-11-09 22:02:46 +00:00
|
|
|
// TODO: don't stop timer for now since there is some bugs :?
|
2018-11-16 09:02:12 +00:00
|
|
|
// thread_timer_.stop();
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
void waybar::modules::Battery::getBatteries() {
|
2018-12-24 07:37:10 +00:00
|
|
|
try {
|
2019-05-12 17:53:14 +00:00
|
|
|
for (auto& node : fs::directory_iterator(data_dir_)) {
|
2018-12-24 07:37:10 +00:00
|
|
|
if (!fs::is_directory(node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto dir_name = node.path().filename();
|
|
|
|
auto bat_defined = config_["bat"].isString();
|
2019-04-18 15:52:00 +00:00
|
|
|
if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) &&
|
2019-05-12 17:53:14 +00:00
|
|
|
fs::exists(node.path() / "capacity") && fs::exists(node.path() / "uevent") &&
|
|
|
|
fs::exists(node.path() / "status")) {
|
|
|
|
batteries_.push_back(node.path());
|
2018-12-24 07:37:10 +00:00
|
|
|
}
|
|
|
|
auto adap_defined = config_["adapter"].isString();
|
2019-04-18 15:52:00 +00:00
|
|
|
if (((adap_defined && dir_name == config_["adapter"].asString()) || !adap_defined) &&
|
2019-05-12 17:53:14 +00:00
|
|
|
fs::exists(node.path() / "online")) {
|
|
|
|
adapter_ = node.path();
|
2018-12-24 07:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 15:52:00 +00:00
|
|
|
} catch (fs::filesystem_error& e) {
|
2018-12-24 07:37:10 +00:00
|
|
|
throw std::runtime_error(e.what());
|
|
|
|
}
|
|
|
|
if (batteries_.empty()) {
|
|
|
|
if (config_["bat"].isString()) {
|
|
|
|
throw std::runtime_error("No battery named " + config_["bat"].asString());
|
|
|
|
}
|
|
|
|
throw std::runtime_error("No batteries.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:36:14 +00:00
|
|
|
const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() const {
|
2018-08-08 21:54:58 +00:00
|
|
|
try {
|
2019-04-18 15:52:00 +00:00
|
|
|
uint16_t total = 0;
|
2019-05-21 17:36:14 +00:00
|
|
|
uint32_t total_power = 0; // μW
|
|
|
|
uint32_t total_energy = 0; // μWh
|
|
|
|
uint32_t total_energy_full = 0;
|
2018-11-02 21:50:01 +00:00
|
|
|
std::string status = "Unknown";
|
2018-09-04 21:50:08 +00:00
|
|
|
for (auto const& bat : batteries_) {
|
2019-04-18 15:52:00 +00:00
|
|
|
uint16_t capacity;
|
2019-05-21 17:36:14 +00:00
|
|
|
uint32_t power_now;
|
|
|
|
uint32_t energy_full;
|
|
|
|
uint32_t energy_now;
|
2018-08-15 20:19:17 +00:00
|
|
|
std::string _status;
|
2018-08-09 09:21:08 +00:00
|
|
|
std::ifstream(bat / "capacity") >> capacity;
|
2018-08-15 20:19:17 +00:00
|
|
|
std::ifstream(bat / "status") >> _status;
|
2019-05-21 17:36:14 +00:00
|
|
|
std::ifstream(bat / "power_now") >> power_now;
|
|
|
|
std::ifstream(bat / "energy_now") >> energy_now;
|
|
|
|
std::ifstream(bat / "energy_full") >> energy_full;
|
2018-08-16 12:29:41 +00:00
|
|
|
if (_status != "Unknown") {
|
2018-08-15 20:19:17 +00:00
|
|
|
status = _status;
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-13 12:05:13 +00:00
|
|
|
total += capacity;
|
2019-05-21 17:36:14 +00:00
|
|
|
total_power += power_now;
|
|
|
|
total_energy += energy_now;
|
|
|
|
total_energy_full += energy_full;
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
2019-05-17 08:59:54 +00:00
|
|
|
if (!adapter_.empty() && status == "Discharging") {
|
|
|
|
bool online;
|
|
|
|
std::ifstream(adapter_ / "online") >> online;
|
|
|
|
if (online) {
|
|
|
|
status = "Plugged";
|
|
|
|
}
|
2019-05-14 13:43:57 +00:00
|
|
|
}
|
2019-05-21 17:36:14 +00:00
|
|
|
float time_remaining = 0;
|
|
|
|
if (status == "Discharging" && total_power != 0) {
|
|
|
|
time_remaining = (float)total_energy / total_power;
|
|
|
|
} else if (status == "Charging" && total_power != 0) {
|
|
|
|
time_remaining = -(float)(total_energy_full - total_energy) / total_power;
|
|
|
|
}
|
2019-05-17 08:59:54 +00:00
|
|
|
uint16_t capacity = total / batteries_.size();
|
2019-05-21 17:36:14 +00:00
|
|
|
return {capacity, time_remaining, status};
|
2018-11-02 10:23:29 +00:00
|
|
|
} catch (const std::exception& e) {
|
2019-05-18 23:44:45 +00:00
|
|
|
spdlog::error("Battery: {}", e.what());
|
2019-05-21 17:36:14 +00:00
|
|
|
return {0, 0, "Unknown"};
|
2018-11-02 10:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:35:39 +00:00
|
|
|
const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const {
|
2018-12-24 07:37:10 +00:00
|
|
|
if (!adapter_.empty()) {
|
|
|
|
bool online;
|
|
|
|
std::ifstream(adapter_ / "online") >> online;
|
|
|
|
if (capacity == 100) {
|
|
|
|
return "Full";
|
|
|
|
}
|
2019-05-14 13:43:57 +00:00
|
|
|
if (online) {
|
2019-05-17 08:59:54 +00:00
|
|
|
return "Charging";
|
2019-05-14 13:43:57 +00:00
|
|
|
}
|
|
|
|
return "Discharging";
|
2018-12-24 07:37:10 +00:00
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:36:14 +00:00
|
|
|
const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemaining) {
|
|
|
|
hoursRemaining = std::fabs(hoursRemaining);
|
|
|
|
uint16_t full_hours = static_cast<uint16_t>(hoursRemaining);
|
|
|
|
uint16_t minutes = static_cast<uint16_t>(60 * (hoursRemaining - full_hours));
|
|
|
|
return std::to_string(full_hours) + " h " + std::to_string(minutes) + " min";
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Battery::update() -> void {
|
2019-05-21 17:36:14 +00:00
|
|
|
auto [capacity, time_remaining, status] = getInfos();
|
2018-12-24 11:17:07 +00:00
|
|
|
if (status == "Unknown") {
|
2019-05-21 17:35:39 +00:00
|
|
|
status = getAdapterStatus(capacity);
|
2018-12-24 11:17:07 +00:00
|
|
|
}
|
2019-02-22 10:35:26 +00:00
|
|
|
if (tooltipEnabled()) {
|
2019-05-21 17:36:14 +00:00
|
|
|
std::string tooltip_text;
|
|
|
|
if (time_remaining != 0) {
|
|
|
|
std::string time_to = std::string("Time to ") +
|
|
|
|
((time_remaining > 0) ? "empty" : "full");
|
|
|
|
tooltip_text = time_to + ": " + formatTimeRemaining(time_remaining);
|
|
|
|
} else {
|
|
|
|
tooltip_text = status;
|
|
|
|
}
|
|
|
|
label_.set_tooltip_text(tooltip_text);
|
2019-02-22 10:35:26 +00:00
|
|
|
}
|
2018-11-02 21:04:43 +00:00
|
|
|
std::transform(status.begin(), status.end(), status.begin(), ::tolower);
|
2018-11-02 10:23:29 +00:00
|
|
|
auto format = format_;
|
2019-05-13 09:36:34 +00:00
|
|
|
auto state = getState(capacity, true);
|
2019-05-14 13:43:57 +00:00
|
|
|
if (!old_status_.empty()) {
|
|
|
|
label_.get_style_context()->remove_class(old_status_);
|
|
|
|
}
|
2018-11-02 21:04:43 +00:00
|
|
|
label_.get_style_context()->add_class(status);
|
|
|
|
old_status_ = status;
|
|
|
|
if (!state.empty() && config_["format-" + status + "-" + state].isString()) {
|
|
|
|
format = config_["format-" + status + "-" + state].asString();
|
2018-11-02 21:50:01 +00:00
|
|
|
} else if (config_["format-" + status].isString()) {
|
2018-11-02 21:04:43 +00:00
|
|
|
format = config_["format-" + status].asString();
|
2018-11-02 21:50:01 +00:00
|
|
|
} else if (!state.empty() && config_["format-" + state].isString()) {
|
2018-11-02 10:23:29 +00:00
|
|
|
format = config_["format-" + state].asString();
|
|
|
|
}
|
|
|
|
if (format.empty()) {
|
|
|
|
event_box_.hide();
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
2019-05-16 15:18:27 +00:00
|
|
|
label_.set_markup(fmt::format(
|
2019-05-21 17:44:05 +00:00
|
|
|
format, fmt::arg("capacity", capacity), fmt::arg("icon", getIcon(capacity, state)), fmt::arg("time", formatTimeRemaining(time_remaining))));
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
|
|
|
}
|