[Goodies-commits] r1460 - xfce4-battery-plugin/branches/XERVERIUS/panel-plugin

Nick Schermer nick at xfce.org
Tue Jun 27 13:32:11 CEST 2006


Author: nick
Date: 2006-06-27 11:32:10 +0000 (Tue, 27 Jun 2006)
New Revision: 1460

Added:
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.c
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.h
Modified:
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/Makefile.am
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-actions.c
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-dialogs.c
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-hal.c
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-overview.c
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.c
   xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.h
Log:
* New way to calculate the remaining battery time and percentage
  The time is now calculated by using the average of the last steps.
  This value is also stored in the rc file, to ensure a decent
  time calculation
* Various cleanups and improvements


Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/Makefile.am
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/Makefile.am	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/Makefile.am	2006-06-27 11:32:10 UTC (rev 1460)
@@ -4,6 +4,8 @@
 xfce4_battery_plugin_SOURCES =  \
 	battery.h               \
 	battery.c               \
+	battery-calc.h          \
+	battery-calc.c          \
 	battery-hal.h           \
 	battery-hal.c           \
 	battery-dialogs.h       \

Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-actions.c
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-actions.c	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-actions.c	2006-06-27 11:32:10 UTC (rev 1460)
@@ -48,6 +48,7 @@
 #endif
 
 #include "battery.h"
+#include "battery-calc.h"
 #include "battery-actions.h"
 
 /* Global counters */

Added: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.c
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.c	                        (rev 0)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.c	2006-06-27 11:32:10 UTC (rev 1460)
@@ -0,0 +1,125 @@
+/* vim: set expandtab ts=8 sw=4: */
+
+/*  $Id$
+ *
+ *  Copyright (c) 2006 Nick Schermer <nick at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+ 
+#define FAULTTOLERANCE 10
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "battery.h"
+#include "battery-calc.h"
+
+static gboolean
+battery_calc_normal_value (gint one,
+                           gint two)
+{
+    gdouble i;
+    
+    i = (gdouble) one / (gdouble) two;
+    
+    /* Don't allow a new value with more than 200% diff */
+    if (i < 0.5 || i > 2)
+	return FALSE;
+    else
+        return TRUE;
+}
+
+void
+battery_calc_averange (BatteryStatus *bat,
+                       gdouble        elapsedTime)
+{
+    gint chargeDiff, chargeTime, powerDelta;
+    
+    /* Time since last update to int */
+    chargeTime = (gint) elapsedTime;
+    
+    /* Calc the difference between the new and old value, positive */
+    chargeDiff = MAX (bat->chargePrevious, bat->chargeLevel) - 
+                 MIN (bat->chargePrevious, bat->chargeLevel);
+    
+    /* Quit if there is no time or diffrence */
+    if (G_UNLIKELY (chargeTime == 0 || chargeDiff == 0))
+	return;
+    
+    /* Calc power usage/charge */
+    powerDelta = chargeDiff / chargeTime;
+    
+    DBG ("Time: %d, Diff: %d, Delta: %d", chargeTime, chargeDiff, powerDelta);
+    
+    /* Update the previous value */
+    bat->chargePrevious = bat->chargeLevel;
+    
+    /* Calc the new charge rate */
+    if (bat->isCharging)
+    {
+        if (G_LIKELY (battery_calc_normal_value (bat->chargeRate, powerDelta)))
+	    bat->chargeRate = (bat->chargeRate * FAULTTOLERANCE + powerDelta) / (FAULTTOLERANCE + 1);
+	
+	DBG ("Charge rate: %d", bat->chargeRate);
+    }
+    else
+    {
+	if (G_LIKELY (battery_calc_normal_value (bat->dischargeRate, powerDelta)))
+	    bat->dischargeRate = (bat->dischargeRate * FAULTTOLERANCE + powerDelta) / (FAULTTOLERANCE + 1);
+	
+	DBG ("Discharge rate: %d", bat->dischargeRate);
+    }
+}
+
+gint
+battery_calc_time (BatteryStatus *bat)
+{
+    gint time;
+    
+    if (bat->isCharging && bat->chargeRate)
+        time = (bat->chargeLastFull - bat->chargeLevel) / bat->chargeRate;
+    else if (bat->dischargeRate)
+	time = bat->chargeLevel / bat->dischargeRate;
+    else
+	time = 0;
+    
+    return time;
+}
+
+gboolean
+battery_calc_fully_charged (BatteryStatus *bat)
+{
+    return (bat->chargeLastFull == bat->chargeLevel);
+}
+
+gint
+battery_calc_percentage (BatteryStatus *bat)
+{
+    gint percentage;
+    
+    /* Calc */
+    percentage = (gdouble) bat->chargeLevel / (gdouble) bat->chargeLastFull * 100;
+    
+    /* Check value */
+    if (G_UNLIKELY (percentage < 0))
+	percentage = 0;
+    
+    if (G_UNLIKELY (percentage > 100))
+	percentage = 100;
+    
+    return percentage;
+}

Added: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.h
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.h	                        (rev 0)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-calc.h	2006-06-27 11:32:10 UTC (rev 1460)
@@ -0,0 +1,41 @@
+/* vim: set expandtab ts=8 sw=4: */
+
+/*  $Id$
+ *
+ *  Copyright (c) 2006 Nick Schermer <nick at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _BATTERY_CALC_H
+#define _BATTERY_CALC_H
+
+G_BEGIN_DECLS
+
+void
+battery_calc_averange      (BatteryStatus *bat, gdouble elapsedTime);
+
+gint
+battery_calc_time          (BatteryStatus *bat);
+
+gboolean
+battery_calc_fully_charged (BatteryStatus *bat);
+
+gint
+battery_calc_percentage   (BatteryStatus *bat);
+
+G_END_DECLS
+
+#endif /* _BATTERY_CALC_H */

Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-dialogs.c
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-dialogs.c	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-dialogs.c	2006-06-27 11:32:10 UTC (rev 1460)
@@ -26,11 +26,7 @@
 #include <config.h>
 #endif
 
-#include <string.h>
-#include <gtk/gtk.h>
-
 #include <libxfcegui4/libxfcegui4.h>
-#include <libxfce4panel/xfce-panel-plugin.h>
 
 #include "battery.h"
 #include "battery-dialogs.h"

Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-hal.c
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-hal.c	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-hal.c	2006-06-27 11:32:10 UTC (rev 1460)
@@ -23,17 +23,12 @@
 #include <config.h>
 #endif
 
-#include <string.h>
-#include <glib.h>
-#include <gtk/gtk.h>
-
 #include <dbus/dbus-glib-lowlevel.h>
 #include <hal/libhal.h>
 
-#include <libxfce4panel/xfce-panel-plugin.h>
-
 #include "battery.h"
 #include "battery-hal.h"
+#include "battery-calc.h"
 
 /* Minimum time in seconds beween HAL events */
 #define MAX_UPDATE_INTERVAL 0.1
@@ -82,23 +77,18 @@
 }
 
 static void
-battery_store_properties (BatteryStatus *bat,
-                          const gchar   *udi)
+battery_store_properties (BatteryStatus *bat)
 {
     /* Check if the device exists */
-    if (G_UNLIKELY (battery_hal_device_exists (udi) == FALSE))
+    if (G_UNLIKELY (battery_hal_device_exists (bat->udi) == FALSE))
 	return;
     
     /* Get current status */
-    bat->charging   = !battery_hal_get_bool (udi, "battery.rechargeable.is_discharging");
-    bat->present    =  battery_hal_get_bool (udi, "battery.present");
+    bat->isCharging   = !battery_hal_get_bool (bat->udi, "battery.rechargeable.is_discharging");
+    bat->isPresent    =  battery_hal_get_bool (bat->udi, "battery.present");
 
-    bat->time       = battery_hal_get_int (udi, "battery.remaining_time");
-    bat->percentage = battery_hal_get_int (udi, "battery.charge_level.percentage");
-    
-    /* People have this problem, sometimes... */
-    if (G_UNLIKELY (bat->percentage > 100))
-        bat->percentage = 100;
+    /* Get current capacity */
+    bat->chargeLevel = battery_hal_get_int  (bat->udi, "battery.charge_level.current");
 }
 
 static void
@@ -109,6 +99,7 @@
                        dbus_bool_t    is_added)
 {
     guint          i;
+    gdouble        elapsedTime;
     BatteryPlugin *battery;
     BatteryStatus *bat;
     
@@ -118,20 +109,19 @@
     if (strncmp (key, "battery", 7) != 0)
         return;
     
+    elapsedTime = g_timer_elapsed (update_timer, NULL);
+    
     /* Sometimes HAL Sends > 3 Signals when something changed. We're using just the
        first time to get all info so the widgets are updated once to save cpu time */
-    if (g_timer_elapsed (update_timer, NULL) > MAX_UPDATE_INTERVAL)
-    {
-	DBG ("Update new properties, last run %.1f seconds ago", g_timer_elapsed (update_timer, NULL));
+    if (elapsedTime > MAX_UPDATE_INTERVAL)
         g_timer_reset (update_timer);
-    }
     else
         return;
     
     /* Get the plugin structure */
     battery = libhal_ctx_get_user_data (ctx);
         
-    /* Search the battery from the list */
+    /* Update all batteries in the array */
     for (i = battery->batteries->len; i--; )
     {
         bat = g_ptr_array_index (battery->batteries, i);
@@ -142,7 +132,10 @@
 	    continue;
 #endif
 	/* Store the new battery values */
-	battery_store_properties (bat, bat->udi);
+	battery_store_properties (bat);
+	
+	/* Calc the averange power usage/charge */
+	battery_calc_averange (bat, elapsedTime);
     }
     
     /* Update the plugin */
@@ -169,10 +162,8 @@
     bat = g_new0 (BatteryStatus, 1);
 
     bat->udi        = g_strdup ("dummy");
-    bat->charging   = FALSE;
-    bat->present    = TRUE;
-    bat->percentage = 81;
-    bat->time       = 2000;
+    bat->isCharging = FALSE;
+    bat->isPresent  = TRUE;
     bat->status     = NONE;
 
     g_ptr_array_add (battery->batteries, bat);
@@ -181,10 +172,8 @@
     bat = g_new0 (BatteryStatus, 1);
 
     bat->udi        = g_strdup ("dummy");
-    bat->charging   = TRUE;
-    bat->present    = TRUE;
-    bat->percentage = 44;
-    bat->time       = 3800;
+    bat->isCharging = TRUE;
+    bat->isPresent  = TRUE;
     bat->status     = NONE;
     
     g_ptr_array_add (battery->batteries, bat);
@@ -193,10 +182,8 @@
     bat = g_new0 (BatteryStatus, 1);
 
     bat->udi        = g_strdup ("dummy");
-    bat->charging   = TRUE;
-    bat->present    = FALSE;
-    bat->percentage = 10;
-    bat->time       = 450;
+    bat->isCharging = TRUE;
+    bat->isPresent  = FALSE;
     bat->status     = NONE;
 
     g_ptr_array_add (battery->batteries, bat);
@@ -205,10 +192,8 @@
     bat = g_new0 (BatteryStatus, 1);
 
     bat->udi        = g_strdup ("dummy");
-    bat->charging   = FALSE;
-    bat->present    = TRUE;
-    bat->percentage = 15;
-    bat->time       = 3600;
+    bat->isCharging = FALSE;
+    bat->isPresent  = TRUE;
     bat->status     = NONE;
 
     g_ptr_array_add (battery->batteries, bat);
@@ -222,14 +207,23 @@
 {
     BatteryStatus *bat;
     
+    if (G_UNLIKELY (battery_hal_device_exists (udi) == FALSE))
+	return;
+    
     bat = g_new0 (BatteryStatus, 1);
 
     /* Set battery information */
-    bat->udi = g_strdup (udi);
-    bat->status = NONE;
-
+    bat->udi       = g_strdup (udi);
+    bat->status    = NONE;
+    
+    /* Load static battery information */
+    bat->chargeLastFull = battery_hal_get_int  (udi, "battery.charge_level.last_full");
+    
     /* Load all the current battery settings */
-    battery_store_properties (bat, udi);
+    battery_store_properties (bat);
+    
+    /* Set previous charge rate to the current one */
+    bat->chargePrevious = bat->chargeLevel;
 
     /* Add new battery to the batteries list */
     g_ptr_array_add (battery->batteries, bat);
@@ -247,14 +241,8 @@
 
     if (G_UNLIKELY (device_names == NULL || num_devices == 0))
     {
-#ifdef DUMMIES
-	DBG ("Unable to get device list or no batteries found, but you're compiling with dummies, so you can still preview");
-	battery_add_dummies (battery);
-	return TRUE;
-#else
-	DBG ("Unable to get device list or no batteries found");
+        DBG ("Unable to get device list or no batteries found");
         return FALSE;
-#endif
     }
     
     DBG ("%d batter%s found", num_devices, num_devices > 1 ? "ies" : "y");
@@ -289,7 +277,7 @@
         if (G_LIKELY (libhal_device_exists (context, bat->udi, &error)))
 	{
             if (G_LIKELY (libhal_device_rescan (context, bat->udi, &error)))
-                battery_store_properties (bat, bat->udi);
+                battery_store_properties (bat);
 	}	
     }
     

Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-overview.c
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-overview.c	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery-overview.c	2006-06-27 11:32:10 UTC (rev 1460)
@@ -25,25 +25,22 @@
 #include <config.h>
 #endif
 
-#include <string.h>
-#include <gtk/gtk.h>
-
 #include <libxfcegui4/libxfcegui4.h>
-#include <libxfce4panel/xfce-panel-plugin.h>
 
 #include "battery.h"
+#include "battery-calc.h"
 #include "battery-hal.h"
 
 static const gchar *
 battery_get_status (BatteryStatus *bat)
 {
-    if (G_UNLIKELY (!bat->present))
+    if (G_UNLIKELY (!bat->isPresent))
         return _("Battery Not Present");
     
-    else if (bat->percentage == 100 && bat->charging)
+    else if (battery_calc_fully_charged (bat) && bat->isCharging)
         return _("Battery Fully Charged");
     
-    else if (bat->charging)
+    else if (bat->isCharging)
         return _("Battery Charging");
     
     else
@@ -89,6 +86,10 @@
 {
     GtkWidget *hbox, *vbox, *label, *image, *expander;
     gchar     *value;
+    gint       time, percentage;
+    
+    time       = battery_calc_time (bat);
+    percentage = battery_calc_percentage (bat);
 
     hbox = gtk_hbox_new (FALSE, BORDER);
     gtk_box_pack_start (GTK_BOX (box), hbox, FALSE, FALSE, 0);
@@ -111,11 +112,11 @@
     battery_add_overview_item (vbox, sg, _("Status"), value);
     
     /* Percentage */
-    value = g_strdup_printf ("%d%%", bat->percentage > 100 ? 100 : bat->percentage);
+    value = g_strdup_printf ("%d%%", percentage);
     battery_add_overview_item (vbox, sg, _("Percentage"), value);
     
     /* Time remaining */
-    value = battery_get_time_string (bat->time);
+    value = battery_get_time_string (time);
     battery_add_overview_item (vbox, sg, _("Time"), value);
     
     /* Check if the device exists, just for safety and to exclude the dummies */

Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.c
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.c	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.c	2006-06-27 11:32:10 UTC (rev 1460)
@@ -26,14 +26,11 @@
 #include <config.h>
 #endif
 
-#include <string.h>
-#include <gtk/gtk.h>
-
 #include <libxfce4util/libxfce4util.h>
 #include <libxfcegui4/libxfcegui4.h>
-#include <libxfce4panel/xfce-panel-plugin.h>
 
 #include "battery.h"
+#include "battery-calc.h"
 #include "battery-hal.h"
 #include "battery-dialogs.h"
 #include "battery-actions.h"
@@ -50,7 +47,7 @@
 
 /* Battery Panel Widgets */
 gchar *
-battery_get_time_string (guint time)
+battery_get_time_string (gint time)
 {
     gchar *string;
     
@@ -70,50 +67,52 @@
 static void
 battery_tooltip (BatteryPlugin *battery,
                  GString       *tooltip,
-                 BatteryStatus *bat)
+                 BatteryStatus *bat,
+                 gint           percentage,
+                 gint           time)
 {
     const gchar *charging, *completed, *remaining;
-    gchar *time;
+    gchar *strTime;
     
     /* Get time */
-    time = battery_get_time_string (bat->time);
+    strTime = battery_get_time_string (time);
     
     /* Be nice for translators */
     charging  = _("Charging");
     completed = _("completed");
     remaining = _("remaining");
     
-    if (bat->charging && G_LIKELY (bat->present)) /* Charging */
+    if (bat->isCharging && G_LIKELY (bat->isPresent)) /* Charging */
     {
-        if (bat->percentage >= 100)
+        if (battery_calc_fully_charged (bat))
             g_string_append_printf (tooltip, "%s (100%%)",
 	                            _("Fully Charged"));
-        else if (battery->tip_time && time)
+        else if (battery->tip_time && strTime)
             g_string_append_printf (tooltip, "%s (%d%% %s)\n%s %s",
-	                            charging, bat->percentage, completed, time, remaining);
+	                            charging, percentage, completed, strTime, remaining);
         else
             g_string_append_printf (tooltip, "%s (%d%% %s)",
-	                            charging, bat->percentage, completed);
+	                            charging, percentage, completed);
     }
-    else if (G_LIKELY (bat->present))/* Discharging */
+    else if (G_LIKELY (bat->isPresent))/* Discharging */
     {
-        if (battery->tip_time && time)
+        if (battery->tip_time && strTime)
             g_string_append_printf (tooltip, "%s (%d%%) %s",
-	                            time, bat->percentage, remaining);
+	                            strTime, percentage, remaining);
         else
             g_string_append_printf (tooltip, "%d%% %s",
-	                            bat->percentage, remaining);
+	                            percentage, remaining);
     }
     else /* Battery not present in system */
     {
         tooltip = g_string_append (tooltip, _("Battery not present"));
     }
     
-    g_free (time);
+    g_free (strTime);
 }
 
 static const gchar *
-battery_icon_group (guint percentage)
+battery_icon_group (gint percentage)
 {
     if (percentage      <= 10)
         return "000";
@@ -138,21 +137,24 @@
 battery_icon_name (BatteryStatus *bat)
 {
     gchar *name;
+    gint   percentage;
+    
+    percentage = battery_calc_percentage (bat);
 
-    if (G_UNLIKELY (!bat->present))
+    if (G_UNLIKELY (!bat->isPresent))
         name = g_strdup ("battery-missing");
 
-    else if ((bat->percentage >= 100) && bat->charging)
+    else if (battery_calc_fully_charged (bat) && bat->isCharging)
         name = g_strdup ("battery-charged");
 
-    else if (bat->charging)
+    else if (bat->isCharging)
         name = g_strconcat ("battery-charging-",
-                            battery_icon_group (bat->percentage),
+                            battery_icon_group (percentage),
                             NULL);
 
     else
         name = g_strconcat ("battery-discharging-",
-                            battery_icon_group (bat->percentage),
+                            battery_icon_group (percentage),
                             NULL);
 
     return name;
@@ -160,7 +162,8 @@
 
 static void
 battery_widget_icon (BatteryPlugin *battery,
-                     BatteryStatus *bat)
+                     BatteryStatus *bat,
+                     gint           percentage)
 {
     gchar     *name;
     guint      psize, isize;
@@ -208,14 +211,15 @@
 
 static void
 battery_widget_label (BatteryPlugin *battery,
-                      BatteryStatus *bat)
+                      gint           percentage,
+                      gint           time)
 {
     gchar       *label;
-    gchar       *time;
-    gchar       *percentage;
+    gchar       *strTime;
+    gchar       *strPercentage;
     guint        psize;
     gboolean     both;
-    const gchar *textsize;
+    const gchar *textSize;
 
     if (!(battery->show_percentage | battery->show_time))
         return;
@@ -228,42 +232,42 @@
 
     /* Create percentage string */
     if (battery->show_percentage)
-        percentage = g_strdup_printf ("%d%%", bat->percentage);
+        strPercentage = g_strdup_printf ("%d%%", percentage);
     else
-	percentage = NULL;
+	strPercentage = NULL;
 
     /* Create time string */
-    if (battery->show_time && bat->time > 0)
-        time = g_strdup_printf ("%d:%02d", bat->time / 3600, bat->time / 60 % 60);
+    if (battery->show_time && time > 0)
+        strTime = g_strdup_printf ("%d:%02d", time / 3600, time / 60 % 60);
     else if (battery->show_time)
-        time = g_strdup ("0:00");
+        strTime = g_strdup ("0:00");
     else
-	time = NULL;
+	strTime = NULL;
     
     /* Are both values visible? */
     both = battery->show_time && battery->show_percentage;
     
     /* Get textsize (arbitrary) */
     if      ((both && psize <= 25))
-	textsize = "xx-small";
+	textSize = "xx-small";
     else if ((both && psize <= 30) || (!both && psize <= 20))
-	textsize = "x-small";
+	textSize = "x-small";
     else if ((both && psize <= 40) || (!both && psize <= 40))
-	textsize = "small";
+	textSize = "small";
     else if (psize <= 50)
-	textsize = "medium";
+	textSize = "medium";
     else /* For (almost) blind people ^_^ */
-	textsize = "large";
+	textSize = "large";
     
     /* Build the label */
     label = g_strdup_printf ("<span size=\"%s\">%s%s%s</span>",
-                             textsize,
-                             percentage ? percentage : "",
+                             textSize,
+                             strPercentage ? strPercentage : "",
                              both ? "\n" : "",
-                             time ? time : "");
+                             strTime ? strTime : "");
     /* Free */
-    g_free (time);
-    g_free (percentage);
+    g_free (strTime);
+    g_free (strPercentage);
     
     /* Set the label, no checking here because it's almost different any time */
     gtk_label_set_label (GTK_LABEL(battery->label), label);
@@ -273,14 +277,14 @@
 
 static void
 battery_widget_progressbar (BatteryPlugin *battery,
-                            BatteryStatus *bat)
+                            gint           percentage)
 {
     if (G_UNLIKELY (!battery->show_progressbar ||
                     !GTK_IS_WIDGET (battery->progressbar)))
         return;
 
     gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (battery->progressbar),
-                                   bat->percentage / 100.0);
+                                   percentage / 100.0);
 }
 
 void
@@ -387,12 +391,13 @@
 battery_update_plugin (BatteryPlugin *battery)
 {
     guint          i, actions = 0;
+    gint           time, percentage;
     GString       *tooltip;
     BatteryStatus *bat;
     GtkWidget     *messages;
 
     g_return_if_fail (battery->running);
-
+    
     tooltip = g_string_new ("");
 
     /* Destroy the old dialog and reset the counter */
@@ -405,34 +410,38 @@
     for (i = 0; i < battery->batteries->len; ++i)
     {
         bat = g_ptr_array_index (battery->batteries, i);
+	
+	/* Calculate the new values */
+	time       = battery_calc_time (bat);
+	percentage = battery_calc_percentage (bat);
 
         /* If this battery is the battery that is shown in the panel, update it */
         if (G_LIKELY (i == battery->show_battery))
         {
-            battery_widget_progressbar (battery, bat);
-            battery_widget_label       (battery, bat);
-            battery_widget_icon        (battery, bat);
+            battery_widget_progressbar (battery, percentage);
+            battery_widget_label       (battery, percentage, time);
+            battery_widget_icon        (battery, bat, percentage);
         }
 
         /* Build the tooltip */
         if (G_LIKELY (battery->batteries->len == 1))
         {
-            battery_tooltip (battery, tooltip, bat);
+            battery_tooltip (battery, tooltip, bat, percentage, time);
         }
         else
         {
             g_string_append_printf (tooltip, _("Battery %d:\n"), i+1);
 
-            battery_tooltip (battery, tooltip, bat);
+            battery_tooltip (battery, tooltip, bat, percentage, time);
 
             if (G_LIKELY (i < (battery->batteries->len-1)))
                 tooltip = g_string_append (tooltip, "\n\n");
         }
 
         /* Store new actions */
-        if (bat->charging && G_LIKELY (bat->present))
+        if (bat->isCharging && G_LIKELY (bat->isPresent))
         {
-            if (G_UNLIKELY (bat->percentage >= 100))
+            if (G_UNLIKELY (battery_calc_fully_charged (bat)))
             {
                 if (bat->status != CHARGED)
                 {
@@ -451,9 +460,9 @@
                 bat->status = NONE;
             }
         }
-        else if (G_LIKELY (bat->present)) /* Discharging */
+        else if (G_LIKELY (bat->isPresent)) /* Discharging */
         {
-            if (bat->percentage <= battery->perc_critical)
+            if (percentage <= battery->perc_critical)
             {
                 if (bat->status != CRITICAL)
                 {
@@ -467,7 +476,7 @@
                                            battery->command_critical);
                 }
             }
-            else if (bat->percentage <= battery->perc_low)
+            else if (percentage <= battery->perc_low)
             {
                 if (bat->status != LOW)
                 {
@@ -506,15 +515,17 @@
 
     /* Check if we need to reset the timeouts */
     if (actions)
-    battery_actions_restart_timeout (battery);
+        battery_actions_restart_timeout (battery);
 }
 
 static void
 battery_open (BatteryPlugin *battery)
 {
-    XfceRc      *rc;
-    gchar       *file;
-    const gchar *s;
+    XfceRc        *rc;
+    gchar         *file;
+    const gchar   *s;
+    guint          i;
+    BatteryStatus *bat;
 
     file = xfce_panel_plugin_save_location (battery->plugin, FALSE);
 
@@ -553,6 +564,17 @@
 
     if ((s = xfce_rc_read_entry (rc, "command_charged", NULL)) != NULL)
         battery->command_charged = g_strdup (s);
+    
+    /* Load battery information */
+    for (i = battery->batteries->len; i--;)
+    {
+        bat = g_ptr_array_index (battery->batteries, i);
+	
+	xfce_rc_set_group (rc, bat->udi);
+	
+	bat->chargeRate    = xfce_rc_read_int_entry  (rc, "charge_rate",    5000);
+        bat->dischargeRate = xfce_rc_read_int_entry  (rc, "discharge_rate", 5000);
+    }
 
     xfce_rc_close (rc);
 }
@@ -561,8 +583,10 @@
 battery_save (XfcePanelPlugin *plugin,
               BatteryPlugin   *battery)
 {
-    XfceRc *rc;
-    gchar  *file;
+    XfceRc        *rc;
+    gchar         *file;
+    guint          i;
+    BatteryStatus *bat;
 
     file = xfce_panel_plugin_save_location (plugin, TRUE);
 
@@ -576,6 +600,7 @@
 
     xfce_rc_set_group (rc, "Properties");
 
+    /* Load user settings */
     xfce_rc_write_int_entry  (rc, "show_battery",     battery->show_battery);
 
     xfce_rc_write_bool_entry (rc, "show_frame",       battery->show_frame);
@@ -596,6 +621,18 @@
     xfce_rc_write_entry (rc, "command_critical", battery->command_critical ? battery->command_critical : "");
     xfce_rc_write_entry (rc, "command_low",      battery->command_low      ? battery->command_low      : "");
     xfce_rc_write_entry (rc, "command_charged",  battery->command_charged  ? battery->command_charged  : "");
+    
+    /* Save battery information */
+    for (i = battery->batteries->len; i--;)
+    {
+        bat = g_ptr_array_index (battery->batteries, i);
+	
+	xfce_rc_set_group (rc, bat->udi);
+	
+	xfce_rc_write_int_entry  (rc, "charge_rate",    bat->chargeRate);
+        xfce_rc_write_int_entry  (rc, "discharge_rate", bat->dischargeRate);
+	
+    }
 
     xfce_rc_close (rc);
 }
@@ -715,13 +752,14 @@
 
     battery->batteries = g_ptr_array_new ();
 
-    battery_open (battery);
-
     if (G_UNLIKELY (battery_start_monitor (battery) == FALSE))
     {
         battery_error_widget (battery);
         return battery;
     }
+    
+    /* Load user settings */
+    battery_open (battery);
 
     /* Set battery count for actions */
     battery_actions_set_len (battery->batteries->len);

Modified: xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.h
===================================================================
--- xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.h	2006-06-26 13:37:55 UTC (rev 1459)
+++ xfce4-battery-plugin/branches/XERVERIUS/panel-plugin/battery.h	2006-06-27 11:32:10 UTC (rev 1460)
@@ -19,6 +19,10 @@
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include <string.h>
+#include <gtk/gtk.h>
+#include <libxfce4panel/xfce-panel-plugin.h>
+
 #ifndef _BATTERY_H
 #define _BATTERY_H
 
@@ -46,15 +50,18 @@
 
 typedef struct
 {
-    gchar        *udi; /* /org/freedesktop/Hal/devices/acpi_BAT0 */
+    /* Plugin information */
+    ActiveStatus  status;
+    gint          chargeRate;     /* Averange capacity change per second */
+    gint          dischargeRate;
     
-    gboolean      charging;
-    gboolean      present;
-    
-    ActiveStatus  status;
-
-    gint          time;
-    guint         percentage;
+    /* HAL information */
+    gchar        *udi;
+    gboolean      isCharging;
+    gboolean      isPresent;
+    gint          chargeLevel;
+    gint          chargePrevious;
+    gint          chargeLastFull;
 }
 BatteryStatus;
 
@@ -86,7 +93,7 @@
 BatteryPlugin;
 
 gchar *
-battery_get_time_string (guint time);
+battery_get_time_string (gint time);
 
 gchar *
 battery_icon_name       (BatteryStatus *bat);




More information about the Goodies-commits mailing list