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 <git@d4ve.email>
This commit is contained in:
David Bimmler 2023-07-07 17:27:31 +02:00
parent 08f5f444eb
commit b694fc1583
2 changed files with 14 additions and 3 deletions

View file

@ -18,6 +18,7 @@
* dwl: support for specifying name of tags ([#256][256]) * dwl: support for specifying name of tags ([#256][256])
* i3/sway: extend option `sort`; use `native` to sort numbered workspaces only. * i3/sway: extend option `sort`; use `native` to sort numbered workspaces only.
* modules/dwl: handle the appid status ([#284][284]) * 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 [246]: https://codeberg.org/dnkl/yambar/issues/246
[256]: https://codeberg.org/dnkl/yambar/pulls/256 [256]: https://codeberg.org/dnkl/yambar/pulls/256

View file

@ -44,6 +44,7 @@ struct private {
long charge; long charge;
long current; long current;
long time_to_empty; long time_to_empty;
long time_to_full;
}; };
static void static void
@ -85,10 +86,14 @@ content(struct module *mod)
unsigned long hours; unsigned long hours;
unsigned long minutes; unsigned long minutes;
if (m->time_to_empty >= 0) { if (m->time_to_empty > 0) {
minutes = m->time_to_empty / 60; minutes = m->time_to_empty / 60;
hours = minutes / 60; hours = minutes / 60;
minutes = 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) { } else if (m->energy_full >= 0 && m->charge && m->power >= 0) {
unsigned long energy = m->state == STATE_CHARGING unsigned long energy = m->state == STATE_CHARGING
? m->energy_full - m->energy : m->energy; ? 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 charge_fd = openat(base_dir_fd, "charge_now", O_RDONLY);
int current_fd = openat(base_dir_fd, "current_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_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 capacity = readint_from_fd(capacity_fd);
long energy = energy_fd >= 0 ? readint_from_fd(energy_fd) : -1; 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 charge = charge_fd >= 0 ? readint_from_fd(charge_fd) : -1;
long current = current_fd >= 0 ? readint_from_fd(current_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_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]; char buf[512];
const char *status = readline_from_fd(status_fd, sizeof(buf), buf); const char *status = readline_from_fd(status_fd, sizeof(buf), buf);
@ -357,6 +364,8 @@ update_status(struct module *mod)
close(current_fd); close(current_fd);
if (time_to_empty_fd >= 0) if (time_to_empty_fd >= 0)
close(time_to_empty_fd); close(time_to_empty_fd);
if (time_to_full_fd >= 0)
close(time_to_full_fd);
if (base_dir_fd >= 0) if (base_dir_fd >= 0)
close(base_dir_fd); 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, " LOG_DBG("capacity: %ld, energy: %ld, power: %ld, charge=%ld, current=%ld, "
"time-to-empty: %ld", capacity, energy, power, charge, current, "time-to-empty: %ld, time-to-full: %ld", capacity, energy, power,
time_to_empty); charge, current, time_to_empty, time_to_full);
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
m->state = state; m->state = state;
@ -392,6 +401,7 @@ update_status(struct module *mod)
m->charge = charge; m->charge = charge;
m->current = current; m->current = current;
m->time_to_empty = time_to_empty; m->time_to_empty = time_to_empty;
m->time_to_full = time_to_full;
mtx_unlock(&mod->lock); mtx_unlock(&mod->lock);
return true; return true;
} }