From b694fc15833258b4277420ac13f97b28ca5f4e77 Mon Sep 17 00:00:00 2001 From: David Bimmler Date: Fri, 7 Jul 2023 17:27:31 +0200 Subject: [PATCH] battery: also show time_to_full The kernel also provides time_to_full, also in seconds, so let's use that too. Both time_to_empty and time_to_full have 0 as their default value, hence only consider them for the estimate if they are positive (instead of non-negative). Signed-off-by: David Bimmler --- CHANGELOG.md | 1 + modules/battery.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd36c33..fc05f95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * dwl: support for specifying name of tags ([#256][256]) * i3/sway: extend option `sort`; use `native` to sort numbered workspaces only. * modules/dwl: handle the appid status ([#284][284]) +* battery: also show estimation for time to full ([#303][303]). [246]: https://codeberg.org/dnkl/yambar/issues/246 [256]: https://codeberg.org/dnkl/yambar/pulls/256 diff --git a/modules/battery.c b/modules/battery.c index 2b04d65..c7e7d58 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -44,6 +44,7 @@ struct private { long charge; long current; long time_to_empty; + long time_to_full; }; static void @@ -85,10 +86,14 @@ content(struct module *mod) unsigned long hours; unsigned long minutes; - if (m->time_to_empty >= 0) { + if (m->time_to_empty > 0) { minutes = m->time_to_empty / 60; hours = minutes / 60; minutes = minutes % 60; + } else if (m->time_to_full > 0) { + minutes = m->time_to_full / 60; + hours = minutes / 60; + minutes = minutes % 60; } else if (m->energy_full >= 0 && m->charge && m->power >= 0) { unsigned long energy = m->state == STATE_CHARGING ? m->energy_full - m->energy : m->energy; @@ -332,6 +337,7 @@ update_status(struct module *mod) int charge_fd = openat(base_dir_fd, "charge_now", O_RDONLY); int current_fd = openat(base_dir_fd, "current_now", O_RDONLY); int time_to_empty_fd = openat(base_dir_fd, "time_to_empty_now", O_RDONLY); + int time_to_full_fd = openat(base_dir_fd, "time_to_full_now", O_RDONLY); long capacity = readint_from_fd(capacity_fd); long energy = energy_fd >= 0 ? readint_from_fd(energy_fd) : -1; @@ -339,6 +345,7 @@ update_status(struct module *mod) long charge = charge_fd >= 0 ? readint_from_fd(charge_fd) : -1; long current = current_fd >= 0 ? readint_from_fd(current_fd) : -1; long time_to_empty = time_to_empty_fd >= 0 ? readint_from_fd(time_to_empty_fd) : -1; + long time_to_full = time_to_full_fd >= 0 ? readint_from_fd(time_to_full_fd) : -1; char buf[512]; const char *status = readline_from_fd(status_fd, sizeof(buf), buf); @@ -357,6 +364,8 @@ update_status(struct module *mod) close(current_fd); if (time_to_empty_fd >= 0) close(time_to_empty_fd); + if (time_to_full_fd >= 0) + close(time_to_full_fd); if (base_dir_fd >= 0) close(base_dir_fd); @@ -381,8 +390,8 @@ update_status(struct module *mod) } LOG_DBG("capacity: %ld, energy: %ld, power: %ld, charge=%ld, current=%ld, " - "time-to-empty: %ld", capacity, energy, power, charge, current, - time_to_empty); + "time-to-empty: %ld, time-to-full: %ld", capacity, energy, power, + charge, current, time_to_empty, time_to_full); mtx_lock(&mod->lock); m->state = state; @@ -392,6 +401,7 @@ update_status(struct module *mod) m->charge = charge; m->current = current; m->time_to_empty = time_to_empty; + m->time_to_full = time_to_full; mtx_unlock(&mod->lock); return true; }