hal/hald util.c,1.30,1.31 util.h,1.14,1.15

Danny Kukawka dkukawka at freedesktop.org
Wed Nov 9 12:45:47 PST 2005


Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv11635/hald

Modified Files:
	util.c util.h 
Log Message:
2005-11-09  Danny Kukawka  <danny.kukawka at web.de>

        * doc/spec/hal-spec.xml.in: added battery.remaining_time.calculate_per_time

        * hald/linux2/acpi.c: (battery_refresh_poll): added check for new property
        battery.remaining_time.calculate_per_time for util_compute_time_remaining()

        * hald/linux2/pmu.c: (battery_refresh): added check for new property
        battery.remaining_time.calculate_per_time for util_compute_time_remaining()

        * hald/util.c (util_compute_time_remaining), hald/util.h: added slightly
        adopted and extended patch from Søren Hansen <sh at linux2go.dk> to fix
        calculation for machines which does not report current rate. This calculate
        the chargRate from time and chargeLevel.
        Added a new parameter to enable/disable the new calculation.



Index: util.c
===================================================================
RCS file: /cvs/hal/hal/hald/util.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- util.c	2 Nov 2005 16:14:16 -0000	1.30
+++ util.c	9 Nov 2005 20:45:44 -0000	1.31
@@ -31,6 +31,7 @@
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
+#include <time.h>
 #include <ctype.h>
 #include <stdint.h>
 #include <sys/stat.h>
@@ -50,6 +51,13 @@
 #include "hald_dbus.h"
 #include "util.h"
 
+typedef struct {
+	int last_level;
+	time_t last_time;
+} batteryInfo;
+
+GHashTable *saved_battery_info = NULL;
+
 /** Given all the required parameters, this function will return the percentage
  *  charge remaining. There are lots of checks here as ACPI is often broken.
  *
@@ -99,6 +107,7 @@
  *  @param  chargeLastFull      The last "full" charge of the battery (typically mWh)
  *  @param  isDischarging       If battery is discharging
  *  @param  isCharging          If battery is charging
+ *  @param  guessChargeRate     If ignore chargeRate and guess them.
  *  @return                     Number of seconds, or -1 if invalid
  */
 int 
@@ -107,17 +116,11 @@
 			     int chargeLevel,
 			     int chargeLastFull,
 			     gboolean isDischarging,
-			     gboolean isCharging)
+			     gboolean isCharging,
+			     gboolean guessChargeRate)
 {
 	int remaining_time = 0;
-	if (chargeRate == 0) {
-		/* Some ACPI BIOS's don't report rate */
-		if (isDischarging || isCharging)
-			HAL_WARNING (("chargeRate is 0, but discharging or charging. ACPI bug?."));
-		else
-			HAL_INFO (("chargeRate is 0 (or unknown)"));
-		return -1;
-	}
+
 	/* should not get negative values */
 	if (chargeRate < 0 || chargeLevel < 0 || chargeLastFull < 0) {
 		HAL_WARNING (("chargeRate, chargeLevel or chargeLastFull < 0, returning -1"));
@@ -128,6 +131,40 @@
 		HAL_WARNING (("isDischarging & isCharging TRUE, returning -1"));
 		return -1;
 	}
+	/* 
+	 * Some laptops don't supply any rate info, but that's no reason for HAL not
+	 * to. We use the current and previous chargeLevel to estimate the rate.
+	 * The info is stored in a GHashTable because there could be more than one battery.
+	 */
+	if (chargeRate == 0 || guessChargeRate) {
+		batteryInfo *battery_info;
+		time_t cur_time = time(NULL);
+
+		/* Initialize the save_battery_info GHashTable */
+		if (!saved_battery_info) 
+			saved_battery_info = g_hash_table_new(g_str_hash, g_str_equal);
+
+		if ((battery_info = g_hash_table_lookup(saved_battery_info, id))) {
+			chargeRate = ((chargeLevel - battery_info->last_level) * 60 * 60) / (cur_time - battery_info->last_time);
+			/*
+			 * During discharging chargeRate would be negative, which would
+			 * mess up the the calculation below, so we make sure it's always
+			 * positive.
+			 */ 
+			chargeRate = (chargeRate > 0) ? chargeRate : -chargeRate;
+
+			battery_info->last_level = chargeLevel;
+			battery_info->last_time = cur_time;
+		} else {
+			battery_info = g_new0(batteryInfo, 1);
+			g_hash_table_insert(saved_battery_info, id, battery_info);
+
+			battery_info->last_level = chargeLevel;
+			battery_info->last_time = cur_time;
+ 			return -1;
+		}
+	}
+
 	if (isDischarging)
 		remaining_time = ((double) chargeLevel / (double) chargeRate) * 60 * 60;
 	else if (isCharging) {
@@ -141,6 +178,7 @@
 		}
 		remaining_time = ((double) (chargeLastFull - chargeLevel) / (double) chargeRate) * 60 * 60;
 	}
+	
 	/* This shouldn't happen, but check for completeness */
 	if (remaining_time < 0) {
 		HAL_WARNING (("remaining_time %i, returning -1", remaining_time));
@@ -151,6 +189,7 @@
 		HAL_WARNING (("remaining_time *very* high, returning -1"));
 		remaining_time = -1;
 	}
+
 	return remaining_time;
 }
 

Index: util.h
===================================================================
RCS file: /cvs/hal/hal/hald/util.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- util.h	2 Nov 2005 15:38:13 -0000	1.14
+++ util.h	9 Nov 2005 20:45:44 -0000	1.15
@@ -29,7 +29,8 @@
 #include "device.h"
 #include "device_store.h"
 
-int util_compute_time_remaining (const char *id, int chargeRate, int chargeLevel, int chargeLastFull, gboolean isDischarging, gboolean isCharging);
+int util_compute_time_remaining (const char *id, int chargeRate, int chargeLevel, int chargeLastFull, 
+				 gboolean isDischarging, gboolean isCharging, gboolean guessChargeRate);
 
 int util_compute_percentage_charge (const char *id, int chargeLevel, int chargeLastFull);
 




More information about the hal-commit mailing list