From 1bbefd777881fee2534b976d161743c5fe6fc536 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 10 Sep 2014 17:26:16 +0200 Subject: [PATCH] battery plugin: Only report "current" when actually supplied by the battery. I think the commend in the file sums it up pretty good, read that ;) --- src/battery.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/battery.c b/src/battery.c index 1d0757d3..87f20437 100644 --- a/src/battery.c +++ b/src/battery.c @@ -461,10 +461,11 @@ static int read_acpi_callback (char const *dir, /* {{{ */ { int *battery_index = user_data; - gauge_t current = NAN; + gauge_t power = NAN; gauge_t voltage = NAN; gauge_t charge = NAN; _Bool charging = 0; + _Bool is_current = 0; char const *plugin_instance; char filename[PATH_MAX]; @@ -510,14 +511,18 @@ static int read_acpi_callback (char const *dir, /* {{{ */ continue; } - /* FIXME: The unit of "present rate" depends on the battery. - * Modern batteries export watts, not amperes, i.e. it's not a - * current anymore. We should check if the fourth column - * contains "mA" and only use a current then. Otherwise, export - * a power. */ + /* The unit of "present rate" depends on the battery. Modern + * batteries export power (watts), older batteries (used to) + * export current (amperes). We check the fourth column and try + * to find old batteries this way. */ if ((strcmp (fields[0], "present") == 0) && (strcmp (fields[1], "rate:") == 0)) - strtogauge (fields[2], ¤t); + { + strtogauge (fields[2], &power); + + if ((numfields >= 4) && (strcmp ("mA", fields[3]) == 0)) + is_current = 1; + } else if ((strcmp (fields[0], "remaining") == 0) && (strcmp (fields[1], "capacity:") == 0)) strtogauge (fields[2], &charge); @@ -529,7 +534,7 @@ static int read_acpi_callback (char const *dir, /* {{{ */ fclose (fh); if (!charging) - current *= -1.0; + power *= -1.0; /* FIXME: This is a dirty hack for backwards compatibility: The battery * plugin, for a very long time, has had the plugin_instance @@ -540,7 +545,9 @@ static int read_acpi_callback (char const *dir, /* {{{ */ (*battery_index)++; battery_submit (plugin_instance, "charge", charge / 1000.0); - battery_submit (plugin_instance, "current", current / 1000.0); + battery_submit (plugin_instance, + is_current ? "current" : "power", + power / 1000.0); battery_submit (plugin_instance, "voltage", voltage / 1000.0); return 0; -- 2.11.0