2018-08-08 21:54:58 +00:00
|
|
|
#include "modules/battery.hpp"
|
|
|
|
|
2018-08-20 12:50:45 +00:00
|
|
|
waybar::modules::Battery::Battery(const Json::Value& config)
|
2018-08-26 23:36:25 +00:00
|
|
|
: ALabel(config, "{capacity}%")
|
2018-08-08 21:54:58 +00:00
|
|
|
{
|
|
|
|
try {
|
2018-10-26 07:27:16 +00:00
|
|
|
if (config_["bat"].isString()) {
|
2018-10-25 15:30:26 +00:00
|
|
|
auto dir = data_dir_ / config_["bat"].asString();
|
|
|
|
if (fs::is_directory(dir) && fs::exists(dir / "capacity")
|
|
|
|
&& fs::exists(dir / "status") && fs::exists(dir / "uevent")) {
|
|
|
|
batteries_.push_back(dir);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto const& node : fs::directory_iterator(data_dir_)) {
|
|
|
|
if (fs::is_directory(node) && fs::exists(node / "capacity")
|
|
|
|
&& fs::exists(node / "status") && fs::exists(node / "uevent")) {
|
|
|
|
batteries_.push_back(node);
|
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
|
|
|
} 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-16 12:29:41 +00:00
|
|
|
if (batteries_.empty()) {
|
2018-10-26 07:27:16 +00:00
|
|
|
if (config_["bat"].isString()) {
|
2018-10-25 15:30:26 +00:00
|
|
|
throw std::runtime_error("No battery named " + config_["bat"].asString());
|
|
|
|
}
|
2018-08-13 12:05:13 +00:00
|
|
|
throw std::runtime_error("No batteries.");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
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-08-19 11:39:57 +00:00
|
|
|
inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS);
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-11-02 21:08:55 +00:00
|
|
|
old_status_ = "";
|
2018-08-20 12:50:45 +00:00
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
|
|
|
waybar::modules::Battery::~Battery()
|
|
|
|
{
|
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::Battery::worker()
|
|
|
|
{
|
|
|
|
// Trigger first values
|
2018-08-21 08:50:09 +00:00
|
|
|
update();
|
2018-10-26 07:27:16 +00:00
|
|
|
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
|
2018-10-25 15:39:15 +00:00
|
|
|
threadTimer_ = [this, interval] {
|
|
|
|
thread_.sleep_for(chrono::seconds(interval));
|
|
|
|
dp.emit();
|
|
|
|
};
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_ = [this] {
|
2018-08-17 12:24:00 +00:00
|
|
|
struct inotify_event event = {0};
|
2018-08-19 11:39:57 +00:00
|
|
|
int nbytes = read(fd_, &event, sizeof(event));
|
2018-08-16 12:29:41 +00:00
|
|
|
if (nbytes != sizeof(event)) {
|
2018-08-13 12:05:13 +00:00
|
|
|
return;
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-10-25 15:39:15 +00:00
|
|
|
threadTimer_.stop();
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-02 10:23:29 +00:00
|
|
|
std::tuple<uint16_t, std::string> waybar::modules::Battery::getInfos()
|
2018-08-08 21:54:58 +00:00
|
|
|
{
|
|
|
|
try {
|
2018-08-13 12:05:13 +00:00
|
|
|
uint16_t total = 0;
|
|
|
|
std::string status;
|
2018-09-04 21:50:08 +00:00
|
|
|
for (auto const& bat : batteries_) {
|
2018-08-13 12:05:13 +00:00
|
|
|
uint16_t capacity;
|
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;
|
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;
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
uint16_t capacity = total / batteries_.size();
|
2018-11-02 10:23:29 +00:00
|
|
|
return {capacity, status};
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return {0, "Unknown"};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 21:08:55 +00:00
|
|
|
std::string waybar::modules::Battery::getState(uint16_t capacity)
|
2018-11-02 10:23:29 +00:00
|
|
|
{
|
|
|
|
// Get current state
|
|
|
|
std::vector<std::pair<std::string, uint16_t>> states;
|
|
|
|
if (config_["states"].isObject()) {
|
|
|
|
for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) {
|
|
|
|
if (it->isUInt() && it.key().isString()) {
|
|
|
|
states.push_back({it.key().asString(), it->asUInt()});
|
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-11-02 10:23:29 +00:00
|
|
|
}
|
|
|
|
// Sort states
|
|
|
|
std::sort(states.begin(), states.end(), [](auto &a, auto &b) {
|
|
|
|
return a.second < b.second;
|
|
|
|
});
|
|
|
|
std::string validState = "";
|
|
|
|
for (auto state : states) {
|
2018-11-02 21:08:55 +00:00
|
|
|
if (capacity <= state.second && validState.empty()) {
|
2018-11-02 10:23:29 +00:00
|
|
|
label_.get_style_context()->add_class(state.first);
|
|
|
|
validState = state.first;
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-11-02 10:23:29 +00:00
|
|
|
label_.get_style_context()->remove_class(state.first);
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-11-02 10:23:29 +00:00
|
|
|
}
|
|
|
|
return validState;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::Battery::update() -> void
|
|
|
|
{
|
|
|
|
auto [capacity, status] = getInfos();
|
|
|
|
label_.set_tooltip_text(status);
|
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_;
|
2018-11-02 21:08:55 +00:00
|
|
|
auto state = getState(capacity);
|
2018-11-02 21:04:43 +00:00
|
|
|
label_.get_style_context()->remove_class(old_status_);
|
|
|
|
label_.get_style_context()->add_class(status);
|
|
|
|
old_status_ = status;
|
|
|
|
if (!state.empty() && config_["format-" + status + "-" + state].isString()) {
|
|
|
|
format = config_["format-" + status + "-" + state].asString();
|
|
|
|
}else if (config_["format-" + status].isString()) {
|
|
|
|
format = config_["format-" + status].asString();
|
|
|
|
}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();
|
|
|
|
label_.set_name("");
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
|
|
|
label_.set_name("battery");
|
|
|
|
label_.set_text(fmt::format(format, fmt::arg("capacity", capacity),
|
|
|
|
fmt::arg("icon", getIcon(capacity))));
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
|
|
|
}
|