$OpenBSD: patch-src_openbsd_up-backend_c,v 1.3 2011/07/08 08:16:09 dcoppa Exp $

Return true if apm_fd wasn't initialized yet - fixes history

XXX Sometimes apm(4) is not fast enough in calculating the right
time-to-empty value and 'minutes_left' is left as -1, causing
gnome-power-manager to show something like "1193046 hours 27 minutes
of battery power remaining". To avoid this, check that battery is
discharging *and* minutes_left is positive, otherwise set
new_time_to_emtpy to zero XXX

Properly initialize update-time when creating devices

--- src/openbsd/up-backend.c.orig	Tue May 31 12:07:03 2011
+++ src/openbsd/up-backend.c	Thu Jul  7 19:24:21 2011
@@ -62,7 +62,7 @@ enum {
 
 static guint signals [SIGNAL_LAST] = { 0 };
 
-int apm_fd; /* ugly global.. needs to move to a device native object */
+int apm_fd = 0; /* ugly global.. needs to move to a device native object */
 
 G_DEFINE_TYPE (UpBackend, up_backend, G_TYPE_OBJECT)
 
@@ -324,8 +324,11 @@ up_backend_update_battery_state(UpDevice* device)
 	if (a.ac_state == APM_AC_ON)
 		new_state = UP_DEVICE_STATE_CHARGING;
 
-	// zero out new_time_to empty if we're not discharging
-	new_time_to_empty = (new_state == UP_DEVICE_STATE_DISCHARGING ? a.minutes_left : 0);
+	// zero out new_time_to_empty if we're not discharging, if minutes_left has a negative value
+	if (new_state == UP_DEVICE_STATE_DISCHARGING && (int)a.minutes_left > 0)
+		new_time_to_empty = a.minutes_left;
+	else
+		new_time_to_empty = 0;
 
 	if (cur_state != new_state ||
 		percentage != (gdouble) a.battery_life ||
@@ -424,6 +427,11 @@ up_apm_device_refresh(UpDevice* device)
 	GTimeVal timeval;
 	gboolean ret;
 
+	if (apm_fd == 0) {
+		g_debug("refresh callback called but apm_fd is not initialized yet");
+		return TRUE;
+	}
+
 	g_object_get (device, "type", &type, NULL);
 
 	switch (type) {
@@ -544,6 +552,7 @@ static void
 up_backend_init (UpBackend *backend)
 {
 	GError *err = NULL;
+	GTimeVal timeval;
 	UpDeviceClass *device_class;
 
 	backend->priv = UP_BACKEND_GET_PRIVATE (backend);
@@ -573,6 +582,7 @@ up_backend_init (UpBackend *backend)
 		}
 
 		/* setup dummy */
+		g_get_current_time (&timeval);
 		g_object_set (backend->priv->battery,
 			      "type", UP_DEVICE_KIND_BATTERY,
 			      "power-supply", TRUE,
@@ -582,11 +592,13 @@ up_backend_init (UpBackend *backend)
 			      "state", UP_DEVICE_STATE_UNKNOWN,
 			      "percentage", 0.0f,
 			      "time-to-empty", (gint64) 0,
+			      "update-time", (guint64) timeval.tv_sec,
 			      (void*) NULL);
 		g_object_set (backend->priv->ac,
 			      "type", UP_DEVICE_KIND_LINE_POWER,
 			      "online", TRUE,
 			      "power-supply", TRUE,
+			      "update-time", (guint64) timeval.tv_sec,
 			      (void*) NULL);
 	} else {
 		backend->priv->ac = NULL;
