hal/hald util.c,1.33,1.34
Danny Kukawka
dkukawka at freedesktop.org
Tue Nov 29 09:38:02 PST 2005
Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv15674/hald
Modified Files:
util.c
Log Message:
2005-11-29 Danny Kukawka <danny.kukawka at web.de>
* hald/util.c: (util_compute_time_remaining): added calculated
chargeRate to hash table and use if nothing changed between events
to prevent remove battery.remaining_time.
Index: util.c
===================================================================
RCS file: /cvs/hal/hal/hald/util.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- util.c 14 Nov 2005 21:39:48 -0000 1.33
+++ util.c 29 Nov 2005 17:38:00 -0000 1.34
@@ -53,6 +53,7 @@
typedef struct {
int last_level;
+ int last_chargeRate;
time_t last_time;
} batteryInfo;
@@ -146,36 +147,41 @@
if ((battery_info = g_hash_table_lookup(saved_battery_info, id))) {
/* check this to prevent division by zero */
- if ((cur_time == battery_info->last_time) || (chargeLevel == battery_info->last_level))
- return -1;
-
- 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;
+ if ((cur_time == battery_info->last_time) || (chargeLevel == battery_info->last_level)) {
+ /* if we can't calculate because nothing changed, use last
+ * chargeRate to prevent removing battery.remaining_time.
+ */
+ chargeRate = battery_info->last_chargeRate;
+ } else {
+ 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;
+ battery_info->last_chargeRate = chargeRate;
+ }
} else {
battery_info = g_new0(batteryInfo, 1);
g_hash_table_insert(saved_battery_info, (char*) id, battery_info);
battery_info->last_level = chargeLevel;
battery_info->last_time = cur_time;
+ battery_info->last_chargeRate = 0;
return -1;
}
}
- /* paranoic, but better than crash: check this to prevent division by zero */
if (chargeRate == 0)
return -1;
- if (isDischarging)
+ if (isDischarging) {
remaining_time = ((double) chargeLevel / (double) chargeRate) * 60 * 60;
- else if (isCharging) {
+ } else if (isCharging) {
/*
* Some ACPI BIOS's don't update chargeLastFull,
* so return 0 as we don't know how much more there is left
More information about the hal-commit
mailing list