2018-08-08 23:54:58 +02:00
|
|
|
#include "modules/battery.hpp"
|
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
|
2018-11-23 11:57:37 +01:00
|
|
|
: ALabel(config, "{capacity}%", 60)
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
2018-12-18 17:30:54 +01:00
|
|
|
label_.set_name("battery");
|
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-12-24 08:37:10 +01:00
|
|
|
getBatteries();
|
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
|
|
|
}
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& bat : batteries_) {
|
2018-12-24 08:37:10 +01:00
|
|
|
auto wd = inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS);
|
|
|
|
if (wd != -1) {
|
|
|
|
wds_.push_back(wd);
|
|
|
|
}
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-20 14:50:45 +02:00
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
|
|
|
waybar::modules::Battery::~Battery()
|
|
|
|
{
|
2018-12-24 08:37:10 +01:00
|
|
|
for (auto wd : wds_) {
|
|
|
|
inotify_rm_watch(fd_, wd);
|
|
|
|
}
|
2018-08-20 14:50:45 +02:00
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::Battery::worker()
|
|
|
|
{
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_timer_ = [this] {
|
2018-10-25 17:39:15 +02:00
|
|
|
dp.emit();
|
2018-12-24 08:50:58 +01:00
|
|
|
thread_timer_.sleep_for(interval_);
|
2018-10-25 17:39:15 +02:00
|
|
|
};
|
2018-08-19 13:39:57 +02:00
|
|
|
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-12-24 08:37:10 +01:00
|
|
|
if (nbytes != sizeof(event) || event.mask & IN_IGNORED) {
|
|
|
|
thread_.stop();
|
2018-08-13 14:05:13 +02:00
|
|
|
return;
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-11-09 23:02:46 +01:00
|
|
|
// TODO: don't stop timer for now since there is some bugs :?
|
2018-11-16 10:02:12 +01:00
|
|
|
// thread_timer_.stop();
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2018-08-08 23:54:58 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-24 08:37:10 +01:00
|
|
|
void waybar::modules::Battery::getBatteries()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
for (auto const& node : fs::directory_iterator(data_dir_)) {
|
|
|
|
if (!fs::is_directory(node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto dir_name = node.path().filename();
|
|
|
|
auto bat_defined = config_["bat"].isString();
|
|
|
|
if (((bat_defined && dir_name == config_["bat"].asString())
|
|
|
|
|| !bat_defined) && fs::exists(node / "capacity")
|
|
|
|
&& fs::exists(node / "uevent") && fs::exists(node / "status")) {
|
|
|
|
batteries_.push_back(node);
|
|
|
|
}
|
|
|
|
auto adap_defined = config_["adapter"].isString();
|
|
|
|
if (((adap_defined && dir_name == config_["adapter"].asString())
|
|
|
|
|| !adap_defined) && fs::exists(node / "online")) {
|
|
|
|
adapter_ = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (fs::filesystem_error &e) {
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 11:57:37 +01:00
|
|
|
const std::tuple<uint8_t, std::string> waybar::modules::Battery::getInfos() const
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
|
|
|
try {
|
2018-08-13 14:05:13 +02:00
|
|
|
uint16_t total = 0;
|
2018-11-02 22:50:01 +01:00
|
|
|
std::string status = "Unknown";
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& 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();
|
2018-11-02 11:23:29 +01:00
|
|
|
return {capacity, status};
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return {0, "Unknown"};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 08:37:10 +01:00
|
|
|
const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const
|
|
|
|
{
|
|
|
|
if (!adapter_.empty()) {
|
|
|
|
bool online;
|
|
|
|
std::ifstream(adapter_ / "online") >> online;
|
|
|
|
if (capacity == 100) {
|
|
|
|
return "Full";
|
|
|
|
}
|
|
|
|
return online ? "Charging" : "Discharging";
|
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2018-11-23 11:57:37 +01:00
|
|
|
const std::string waybar::modules::Battery::getState(uint8_t capacity) const
|
2018-11-02 11:23:29 +01:00
|
|
|
{
|
|
|
|
// Get current state
|
2018-11-23 11:57:37 +01:00
|
|
|
std::vector<std::pair<std::string, uint8_t>> states;
|
2018-11-02 11:23:29 +01:00
|
|
|
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 14:29:41 +02:00
|
|
|
}
|
2018-11-02 11:23:29 +01:00
|
|
|
}
|
|
|
|
// Sort states
|
|
|
|
std::sort(states.begin(), states.end(), [](auto &a, auto &b) {
|
|
|
|
return a.second < b.second;
|
|
|
|
});
|
2018-11-23 11:57:37 +01:00
|
|
|
std::string valid_state;
|
2018-11-23 16:04:29 +01:00
|
|
|
for (auto const& state : states) {
|
2018-11-23 11:57:37 +01:00
|
|
|
if (capacity <= state.second && valid_state.empty()) {
|
2018-11-02 11:23:29 +01:00
|
|
|
label_.get_style_context()->add_class(state.first);
|
2018-11-23 11:57:37 +01:00
|
|
|
valid_state = state.first;
|
2018-08-16 14:29:41 +02:00
|
|
|
} else {
|
2018-11-02 11:23:29 +01:00
|
|
|
label_.get_style_context()->remove_class(state.first);
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-11-02 11:23:29 +01:00
|
|
|
}
|
2018-11-23 11:57:37 +01:00
|
|
|
return valid_state;
|
2018-11-02 11:23:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::Battery::update() -> void
|
|
|
|
{
|
|
|
|
auto [capacity, status] = getInfos();
|
2018-12-24 12:17:07 +01:00
|
|
|
if (status == "Unknown") {
|
|
|
|
status = getAdapterStatus(capacity);
|
|
|
|
}
|
2018-11-02 11:23:29 +01:00
|
|
|
label_.set_tooltip_text(status);
|
2018-11-02 22:04:43 +01:00
|
|
|
std::transform(status.begin(), status.end(), status.begin(), ::tolower);
|
2018-11-02 11:23:29 +01:00
|
|
|
auto format = format_;
|
2018-11-02 22:08:55 +01:00
|
|
|
auto state = getState(capacity);
|
2018-11-02 22:04:43 +01: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();
|
2018-11-02 22:50:01 +01:00
|
|
|
} else if (config_["format-" + status].isString()) {
|
2018-11-02 22:04:43 +01:00
|
|
|
format = config_["format-" + status].asString();
|
2018-11-02 22:50:01 +01:00
|
|
|
} else if (!state.empty() && config_["format-" + state].isString()) {
|
2018-11-02 11:23:29 +01:00
|
|
|
format = config_["format-" + state].asString();
|
|
|
|
}
|
|
|
|
if (format.empty()) {
|
|
|
|
event_box_.hide();
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
2018-11-21 20:49:09 +01:00
|
|
|
label_.set_markup(fmt::format(format, fmt::arg("capacity", capacity),
|
2018-11-02 11:23:29 +01:00
|
|
|
fmt::arg("icon", getIcon(capacity))));
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
|
|
|
}
|