2016-11-02 15:22:45 -04:00
|
|
|
#include "modules/battery.hpp"
|
2016-11-19 09:49:03 -05:00
|
|
|
#include "drawtypes/animation.hpp"
|
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/progressbar.hpp"
|
|
|
|
#include "drawtypes/ramp.hpp"
|
|
|
|
#include "utils/file.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
#include "utils/math.hpp"
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 17:04:31 -05:00
|
|
|
template class module<battery_module>;
|
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
template <typename ValueReader>
|
|
|
|
typename ValueReader::return_type read(ValueReader& reader) {
|
|
|
|
std::lock_guard<ValueReader> guard(reader);
|
|
|
|
return reader.read();
|
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Bootstrap module by setting up required components
|
|
|
|
*/
|
2016-12-21 02:00:09 -05:00
|
|
|
battery_module::battery_module(const bar_settings& bar, string name_)
|
|
|
|
: inotify_module<battery_module>(bar, move(name_)) {
|
2016-12-30 22:20:46 -05:00
|
|
|
// Load configuration values
|
2016-12-31 22:00:35 -05:00
|
|
|
m_fullat = math_util::min(m_conf.get(name(), "full-at", m_fullat), 100);
|
2016-12-30 22:20:46 -05:00
|
|
|
m_interval = m_conf.get<decltype(m_interval)>(name(), "poll-interval", 5s);
|
|
|
|
m_lastpoll = chrono::system_clock::now();
|
2016-11-19 09:42:31 -05:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
auto path_adapter = string_util::replace(PATH_ADAPTER, "%adapter%", m_conf.get(name(), "adapter", "ADP1"s)) + "/";
|
|
|
|
auto path_battery = string_util::replace(PATH_BATTERY, "%battery%", m_conf.get(name(), "battery", "BAT0"s)) + "/";
|
2016-11-21 02:22:12 -05:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
// Make state reader
|
|
|
|
if (file_util::exists((m_fstate = path_adapter + "online"))) {
|
2017-01-07 06:08:12 -05:00
|
|
|
m_state_reader = make_unique<state_reader>([=] { return file_util::contents(m_fstate).compare(0, 1, "1") == 0; });
|
2016-12-30 22:20:46 -05:00
|
|
|
} else if (file_util::exists((m_fstate = path_battery + "status"))) {
|
2017-01-07 06:08:12 -05:00
|
|
|
m_state_reader = make_unique<state_reader>([=] { return file_util::contents(m_fstate).compare(0, 8, "Charging") == 0; });
|
2016-12-30 22:20:46 -05:00
|
|
|
} else {
|
|
|
|
throw module_error("No suitable way to get current charge state");
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-21 02:22:12 -05:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
// Make capacity reader
|
|
|
|
if ((m_fcapnow = file_util::pick({path_battery + "charge_now", path_battery + "energy_now"})).empty()) {
|
|
|
|
throw module_error("No suitable way to get current capacity value");
|
|
|
|
} else if ((m_fcapfull = file_util::pick({path_battery + "charge_full", path_battery + "energy_full"})).empty()) {
|
|
|
|
throw module_error("No suitable way to get max capacity value");
|
2016-11-19 09:42:31 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
m_capacity_reader = make_unique<capacity_reader>([=] {
|
|
|
|
auto cap_now = std::strtoul(file_util::contents(m_fcapnow).c_str(), nullptr, 10);
|
|
|
|
auto cap_max = std::strtoul(file_util::contents(m_fcapfull).c_str(), nullptr, 10);
|
|
|
|
return math_util::percentage(cap_now, 0UL, cap_max);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make rate reader
|
|
|
|
if ((m_fvoltage = file_util::pick({path_battery + "voltage_now"})).empty()) {
|
|
|
|
throw module_error("No suitable way to get current voltage value");
|
|
|
|
} else if ((m_frate = file_util::pick({path_battery + "current_now", path_battery + "power_now"})).empty()) {
|
|
|
|
throw module_error("No suitable way to get current charge rate value");
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-21 02:22:12 -05:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
m_rate_reader = make_unique<rate_reader>([this] {
|
2017-01-09 08:58:14 -05:00
|
|
|
unsigned long rate{std::strtoul(file_util::contents(m_frate).c_str(), nullptr, 10)};
|
2016-12-30 22:20:46 -05:00
|
|
|
unsigned long volt{std::strtoul(file_util::contents(m_fvoltage).c_str(), nullptr, 10) / 1000UL};
|
2017-01-09 08:58:14 -05:00
|
|
|
unsigned long now{std::strtoul(file_util::contents(m_fcapnow).c_str(), nullptr, 10)};
|
|
|
|
unsigned long max{std::strtoul(file_util::contents(m_fcapfull).c_str(), nullptr, 10)};
|
2016-12-30 22:20:46 -05:00
|
|
|
unsigned long cap{read(*m_state_reader) ? max - now : now};
|
|
|
|
|
|
|
|
if (rate && volt && cap) {
|
2017-01-09 08:58:14 -05:00
|
|
|
auto remaining = (cap / volt);
|
|
|
|
auto current_rate = (rate / volt);
|
|
|
|
|
|
|
|
if (remaining && current_rate) {
|
|
|
|
return 3600UL * remaining / current_rate;
|
|
|
|
}
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2017-01-09 08:58:14 -05:00
|
|
|
|
|
|
|
return 0UL;
|
2016-12-30 22:20:46 -05:00
|
|
|
});
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
// Load state and capacity level
|
2016-11-02 15:22:45 -04:00
|
|
|
m_state = current_state();
|
2016-12-30 22:20:46 -05:00
|
|
|
m_percentage = current_percentage(m_state);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
// Add formats and elements
|
2016-11-02 15:22:45 -04:00
|
|
|
m_formatter->add(FORMAT_CHARGING, TAG_LABEL_CHARGING,
|
|
|
|
{TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_ANIMATION_CHARGING, TAG_LABEL_CHARGING});
|
|
|
|
m_formatter->add(
|
2016-11-19 09:42:31 -05:00
|
|
|
FORMAT_DISCHARGING, TAG_LABEL_DISCHARGING, {TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_LABEL_DISCHARGING});
|
|
|
|
m_formatter->add(FORMAT_FULL, TAG_LABEL_FULL, {TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_LABEL_FULL});
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_formatter->has(TAG_ANIMATION_CHARGING, FORMAT_CHARGING)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_animation_charging = load_animation(m_conf, name(), TAG_ANIMATION_CHARGING);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_BAR_CAPACITY)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_bar_capacity = load_progressbar(m_bar, m_conf, name(), TAG_BAR_CAPACITY);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_RAMP_CAPACITY)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_ramp_capacity = load_ramp(m_conf, name(), TAG_RAMP_CAPACITY);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL_CHARGING, FORMAT_CHARGING)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_label_charging = load_optional_label(m_conf, name(), TAG_LABEL_CHARGING, "%percentage%");
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL_DISCHARGING, FORMAT_DISCHARGING)) {
|
2016-11-19 09:42:31 -05:00
|
|
|
m_label_discharging = load_optional_label(m_conf, name(), TAG_LABEL_DISCHARGING, "%percentage%");
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL_FULL, FORMAT_FULL)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_label_full = load_optional_label(m_conf, name(), TAG_LABEL_FULL, "%percentage%");
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
// Create inotify watches
|
2016-12-30 22:20:46 -05:00
|
|
|
watch(m_fcapnow, IN_ACCESS);
|
|
|
|
watch(m_fstate, IN_ACCESS);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
// Setup time if token is used
|
2016-12-30 22:20:46 -05:00
|
|
|
if ((m_label_charging && m_label_charging->has_token("%time%")) ||
|
|
|
|
(m_label_discharging && m_label_discharging->has_token("%time%"))) {
|
2016-11-25 07:55:15 -05:00
|
|
|
if (!m_bar.locale.empty()) {
|
2016-11-19 09:42:31 -05:00
|
|
|
setlocale(LC_TIME, m_bar.locale.c_str());
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-12-30 17:32:05 -05:00
|
|
|
m_timeformat = m_conf.get(name(), "time-format", "%H:%M:%S"s);
|
2016-11-19 09:42:31 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Dispatch the subthread used to update the
|
|
|
|
* charging animation when the module is started
|
|
|
|
*/
|
2016-11-02 15:22:45 -04:00
|
|
|
void battery_module::start() {
|
2016-12-21 17:22:02 -05:00
|
|
|
this->inotify_module::start();
|
2016-12-30 22:20:46 -05:00
|
|
|
m_subthread = thread(&battery_module::subthread, this);
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Release wake lock when stopping the module
|
|
|
|
*/
|
2016-11-02 15:22:45 -04:00
|
|
|
void battery_module::teardown() {
|
2016-12-30 22:20:46 -05:00
|
|
|
if (m_subthread.joinable()) {
|
|
|
|
m_subthread.join();
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Idle between polling inotify watches for events.
|
|
|
|
*
|
|
|
|
* If the defined interval has been reached, trigger a manual
|
|
|
|
* poll in case the inotify events aren't fired.
|
|
|
|
*
|
|
|
|
* This fallback is needed because some systems won't
|
|
|
|
* report inotify events for files on sysfs.
|
|
|
|
*/
|
2016-11-03 07:56:33 -04:00
|
|
|
void battery_module::idle() {
|
2016-11-03 10:20:38 -04:00
|
|
|
if (m_interval.count() > 0) {
|
2016-11-03 07:56:33 -04:00
|
|
|
auto now = chrono::system_clock::now();
|
2016-11-03 10:20:38 -04:00
|
|
|
if (chrono::duration_cast<decltype(m_interval)>(now - m_lastpoll) > m_interval) {
|
2016-11-03 07:56:33 -04:00
|
|
|
m_lastpoll = now;
|
2016-11-03 10:20:38 -04:00
|
|
|
m_log.info("%s: Polling values (inotify fallback)", name());
|
2016-12-30 22:20:46 -05:00
|
|
|
read(*m_capacity_reader);
|
2016-11-03 07:56:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:22:02 -05:00
|
|
|
this->inotify_module::idle();
|
2016-11-03 07:56:33 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Update values when tracked files have changed
|
|
|
|
*/
|
2016-11-02 15:22:45 -04:00
|
|
|
bool battery_module::on_event(inotify_event* event) {
|
2016-12-30 22:20:46 -05:00
|
|
|
auto state = current_state();
|
|
|
|
auto percentage = current_percentage(state);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-03 10:20:38 -04:00
|
|
|
// Reset timer to avoid unnecessary polling
|
|
|
|
m_lastpoll = chrono::system_clock::now();
|
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
if (event != nullptr) {
|
|
|
|
m_log.trace("%s: Inotify event reported for %s", name(), event->filename);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
if (state == m_state && percentage == m_percentage && m_unchanged--) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
m_unchanged = SKIP_N_UNCHANGED;
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_state = state;
|
2016-12-30 22:20:46 -05:00
|
|
|
m_percentage = percentage;
|
2016-11-19 09:42:31 -05:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
const auto label = [this] {
|
2016-12-31 01:32:57 -05:00
|
|
|
if (m_state == battery_module::state::FULL) {
|
2016-12-30 22:20:46 -05:00
|
|
|
return m_label_full;
|
2016-12-31 01:32:57 -05:00
|
|
|
} else if (m_state == battery_module::state::DISCHARGING) {
|
2016-12-30 22:20:46 -05:00
|
|
|
return m_label_discharging;
|
|
|
|
} else {
|
|
|
|
return m_label_charging;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-12-30 22:20:46 -05:00
|
|
|
}();
|
|
|
|
|
|
|
|
label->reset_tokens();
|
|
|
|
label->replace_token("%percentage%", to_string(m_percentage) + "%");
|
|
|
|
|
2016-12-31 01:32:57 -05:00
|
|
|
if (m_state != battery_module::state::FULL && !m_timeformat.empty()) {
|
2016-12-30 22:20:46 -05:00
|
|
|
label->replace_token("%time%", current_time());
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Get the output format based on state
|
|
|
|
*/
|
2016-11-02 15:22:45 -04:00
|
|
|
string battery_module::get_format() const {
|
2016-12-31 01:32:57 -05:00
|
|
|
if (m_state == battery_module::state::CHARGING) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return FORMAT_CHARGING;
|
2016-12-31 01:32:57 -05:00
|
|
|
} else if (m_state == battery_module::state::DISCHARGING) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return FORMAT_DISCHARGING;
|
2016-12-30 22:20:46 -05:00
|
|
|
} else {
|
|
|
|
return FORMAT_FULL;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
2016-12-30 22:20:46 -05:00
|
|
|
* Generate module output using defined drawtypes
|
2016-11-19 09:42:31 -05:00
|
|
|
*/
|
2016-11-25 07:55:15 -05:00
|
|
|
bool battery_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_ANIMATION_CHARGING) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_animation_charging->get());
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_BAR_CAPACITY) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_bar_capacity->output(m_percentage));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_RAMP_CAPACITY) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_ramp_capacity->get_by_percentage(m_percentage));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_LABEL_CHARGING) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label_charging);
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_LABEL_DISCHARGING) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label_discharging);
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_LABEL_FULL) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label_full);
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-11-02 15:22:45 -04:00
|
|
|
return false;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-12-30 22:20:46 -05:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Get the current battery state
|
|
|
|
*/
|
2016-12-21 02:38:44 -05:00
|
|
|
battery_module::state battery_module::current_state() {
|
2016-12-30 22:20:46 -05:00
|
|
|
if (!read(*m_state_reader)) {
|
2016-12-31 01:32:57 -05:00
|
|
|
return battery_module::state::DISCHARGING;
|
2016-12-30 22:20:46 -05:00
|
|
|
} else if (read(*m_capacity_reader) < m_fullat) {
|
2016-12-31 01:32:57 -05:00
|
|
|
return battery_module::state::CHARGING;
|
2016-11-02 15:22:45 -04:00
|
|
|
} else {
|
2016-12-31 01:32:57 -05:00
|
|
|
return battery_module::state::FULL;
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current capacity level
|
|
|
|
*/
|
2016-12-30 22:20:46 -05:00
|
|
|
int battery_module::current_percentage(state state) {
|
|
|
|
int percentage{read(*m_capacity_reader)};
|
2016-12-31 01:32:57 -05:00
|
|
|
if (state == battery_module::state::FULL && percentage >= m_fullat) {
|
2016-12-30 22:20:46 -05:00
|
|
|
percentage = 100;
|
|
|
|
}
|
|
|
|
return percentage;
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
/**
|
|
|
|
* Get estimate of remaining time until fully dis-/charged
|
|
|
|
*/
|
|
|
|
string battery_module::current_time() {
|
|
|
|
struct tm t {
|
2016-11-25 07:55:15 -05:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr
|
2016-11-19 09:42:31 -05:00
|
|
|
};
|
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
chrono::seconds sec{read(*m_rate_reader)};
|
|
|
|
if (sec.count() > 0) {
|
|
|
|
t.tm_hour = chrono::duration_cast<chrono::hours>(sec).count();
|
|
|
|
sec -= chrono::seconds{3600 * t.tm_hour};
|
|
|
|
t.tm_min = chrono::duration_cast<chrono::minutes>(sec).count();
|
|
|
|
sec -= chrono::seconds{60 * t.tm_min};
|
|
|
|
t.tm_sec = chrono::duration_cast<chrono::seconds>(sec).count();
|
2016-11-19 09:42:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[256]{0};
|
|
|
|
strftime(buffer, sizeof(buffer), m_timeformat.c_str(), &t);
|
|
|
|
return {buffer};
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
/**
|
|
|
|
* Subthread runner that emit update events
|
|
|
|
* to refresh <animation-charging> in case it is used.
|
|
|
|
*/
|
|
|
|
void battery_module::subthread() {
|
2016-12-30 22:20:46 -05:00
|
|
|
chrono::duration<double> dur{0.0};
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
if (m_animation_charging) {
|
2016-12-30 22:20:46 -05:00
|
|
|
dur += chrono::milliseconds{m_animation_charging->framerate()};
|
|
|
|
} else {
|
|
|
|
dur += 1s;
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (running()) {
|
2016-11-03 07:56:33 -04:00
|
|
|
for (int i = 0; running() && i < dur.count(); ++i) {
|
2016-12-31 01:32:57 -05:00
|
|
|
if (m_state == battery_module::state::CHARGING) {
|
2016-11-02 15:22:45 -04:00
|
|
|
broadcast();
|
|
|
|
}
|
2016-11-03 07:56:33 -04:00
|
|
|
sleep(dur);
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_log.trace("%s: End of subthread", name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|