[Goodies-commits] r4023 - xfce4-battery-plugin/branches/hal_based/panel-plugin

Nick Schermer nick at xfce.org
Wed Mar 5 20:25:15 CET 2008


Author: nick
Date: 2008-03-05 19:25:15 +0000 (Wed, 05 Mar 2008)
New Revision: 4023

Modified:
   xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.c
   xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.h
   xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-plugin.c
Log:
* Use the remaining time from hal, no remaining time for the
  global battery for the moment.
* Set progressbar size.
* Some small fixes here and there...


Modified: xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.c
===================================================================
--- xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.c	2008-03-04 18:31:15 UTC (rev 4022)
+++ xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.c	2008-03-05 19:25:15 UTC (rev 4023)
@@ -41,8 +41,6 @@
 
 #define DEFAULT_LEVEL_LOW     (0.08)
 #define DEFAULT_LEVEL_WARNING (0.20)
-#define RATE_AVERANGE         (10)
-#define MAXIMUM_RATE_DIFF     (0.50)
 
 
 enum
@@ -251,93 +249,11 @@
 
 
 
-static gint
-battery_monitor_calculate_remaining_time (BatteryInfo *info)
-{
-  gint     remaining_time = 0;
-  gint     charge_diff, charge_rate;
-  GTimeVal current_time;
-
-  /* only valid when last full charge is valid */
-  if (info->charge_last_full > 0)
-    {
-      /* get the current time */
-      g_get_current_time (&current_time);
-
-      /* only update when we need to */
-      if (info->charge_current_prev > 0
-          && info->charge_current_prev != info->charge_current
-          && current_time.tv_sec > info->time.tv_sec)
-        {
-          /* charge rate difference */
-          charge_diff = ABS (info->charge_current_prev - info->charge_current);
-
-          /* convert to charge rate per minute */
-          charge_rate = charge_diff / (current_time.tv_sec - info->time.tv_sec);
-
-          /* calculate (dis)charge rate */
-          if (info->is_discharging)
-            {
-              if (G_LIKELY (info->rate_discharging > 0))
-                {
-                  /* avoid weird rates */
-                  charge_rate = CLAMP (charge_rate, info->rate_discharging * MAXIMUM_RATE_DIFF,
-                                       info->rate_discharging * (1.00 + MAXIMUM_RATE_DIFF));
-
-                  /* update average rate */
-                  info->rate_discharging = MAX (1, (info->rate_discharging * RATE_AVERANGE + charge_rate) / (RATE_AVERANGE + 1));
-                }
-              else
-                {
-                  /* set rate */
-                  info->rate_discharging = charge_rate;
-                }
-
-              /* calculate the remaining time */
-              remaining_time = info->charge_current / info->rate_discharging;
-            }
-          else /* charging */
-            {
-              if (G_LIKELY (info->rate_charging > 0))
-                {
-                  /* avoid weird rates */
-                  charge_rate = CLAMP (charge_rate, info->rate_charging * MAXIMUM_RATE_DIFF,
-                                       info->rate_charging * (1.00 + MAXIMUM_RATE_DIFF));
-
-                  /* update average rate */
-                  info->rate_charging = MAX (1, (info->rate_charging * RATE_AVERANGE + charge_rate) / (RATE_AVERANGE + 1));
-                }
-              else
-                {
-                  /* set rate */
-                  info->rate_charging = charge_rate;
-                }
-
-              /* calculate the remaining time */
-              remaining_time = (info->charge_last_full - info->charge_current) / info->rate_charging;
-            }
-        }
-    }
-
-  return MAX (0, remaining_time);
-}
-
-
-
 static void
 battery_monitor_calculate (BatteryInfo *info)
 {
   /* update the percentage */
   info->percentage = battery_monitor_calculate_percentage (info);
-
-  /* update remaining time */
-  info->remaining_time = battery_monitor_calculate_remaining_time (info);
-
-  /* set new time */
-  g_get_current_time (&(info->time));
-
-  /* update the previous charge rate */
-  info->charge_current_prev = info->charge_current;
 }
 
 
@@ -354,14 +270,10 @@
   info->udi = g_strdup (udi);
   info->model = NULL;
 
-  /* initialize time */
-  g_get_current_time (&(info->time));
-
   /* set defaults */
   info->charge_last_full = info->charge_low = info->charge_warning = 0;
-  info->charge_current = info->charge_current_prev = 0;
+  info->charge_current = 0;
   info->percentage = info->remaining_time = 0;
-  info->rate_charging = info->rate_discharging = 0;
   info->is_discharging = info->is_present = FALSE;
 
   return info;
@@ -454,9 +366,6 @@
         global->is_present = TRUE;
     }
 
-  /* set charge previous equal */
-  global->charge_current_prev = global->charge_current;
-
   /* cleanup */
   g_list_free (devices);
 
@@ -472,6 +381,7 @@
                                           BatteryMonitor *monitor)
 {
   gint       current;
+  gint       remaining_time;
   gchar     *model;
   DBusError  error;
 
@@ -483,7 +393,7 @@
   /* make sure the device exists */
   if (libhal_device_exists (monitor->context, udi, NULL))
     {
-      /* update static battery information is needed */
+      /* update static battery information if needed */
       if (G_UNLIKELY (info->charge_last_full == 0))
         {
           /* get the last full capacity, leave when there is none, since it makes the device useless */
@@ -508,10 +418,14 @@
           if (libhal_device_property_exists (monitor->context, udi, "battery.model", &error))
             {
               model = libhal_device_get_property_string (monitor->context, udi, "battery.model", &error);
-              if (g_utf8_strlen (model, -1) > 0)
-                info->model = model;
-              else
-                g_free (model);
+              
+              if (G_LIKELY (model))
+                {
+                  if (g_utf8_strlen (model, -1) > 0)
+                    info->model = model;
+                  else
+                    g_free (model);
+                }
             }
         }
 
@@ -524,7 +438,17 @@
         info->is_present = libhal_device_get_property_bool (monitor->context, udi, "battery.present", &error);
       else
         info->is_present = FALSE;
+        
+      /* current remaining time */
+      if (libhal_device_property_exists (monitor->context, udi, "battery.remaining_time", &error))
+        {
+          remaining_time = libhal_device_get_property_int (monitor->context, udi, "battery.remaining_time", &error);
 
+          /* only set when valid */
+          if (G_LIKELY (remaining_time > 0))
+            info->remaining_time = remaining_time;
+        }
+      
       /* current change level */
       if (libhal_device_property_exists (monitor->context, udi, "battery.charge_level.current", &error))
         {
@@ -533,6 +457,13 @@
           /* only set when valid */
           if (G_LIKELY (current > 0))
             info->charge_current = current;
+            
+          /* fix some weirdness for unplugged batteries but not properly removed inside hal */
+          if (info->charge_current == 0)
+            {
+              info->remaining_time = 0;
+              info->is_present = FALSE;
+            }
         }
 
       /* update the global battery */
@@ -546,7 +477,7 @@
             monitor->global->is_present = TRUE;
 
           /* when one battery is charging, global is charging */
-          if (!info->is_discharging)
+          if (!info->is_discharging && info->is_present)
             monitor->global->is_discharging = FALSE;
         }
 
@@ -850,6 +781,7 @@
   /* get the list of devices */
   devices = g_hash_table_get_values (monitor->devices);
 
+  /* sort the list of devices */
   devices = g_list_sort (devices, battery_monitor_sort_devices_func);
 
   return devices;

Modified: xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.h
===================================================================
--- xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.h	2008-03-04 18:31:15 UTC (rev 4022)
+++ xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-monitor.h	2008-03-05 19:25:15 UTC (rev 4023)
@@ -49,25 +49,15 @@
   /* device model */
   gchar    *model;
 
-  /* last update time */
-  GTimeVal  time;
-
   /* charge levels */
   gint      charge_last_full;
   gint      charge_low;
   gint      charge_warning;
   gint      charge_current;
-  gint      charge_current_prev;
 
-  /* discharge rate */
-  gint      rate_discharging;
-  gint      rate_charging;
-
   /* status */
   guint     is_discharging : 1;
   guint     is_present : 1;
-
-  /* calculations */
   gint      percentage;
   gint      remaining_time;
 };

Modified: xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-plugin.c
===================================================================
--- xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-plugin.c	2008-03-04 18:31:15 UTC (rev 4022)
+++ xfce4-battery-plugin/branches/hal_based/panel-plugin/battery-plugin.c	2008-03-05 19:25:15 UTC (rev 4023)
@@ -163,8 +163,13 @@
     string = g_string_append (string, "medium\">");
 
   /* create the label */
-  if (BATTERY_FULLY_CHARGED (info))
+  if (info->is_present == FALSE)
     {
+      /* battery not present */
+      string = g_string_append (string, _("Not Present"));
+    }
+  else if (BATTERY_FULLY_CHARGED (info))
+    {
       /* append "Changed" */
       string = g_string_append (string, _("Charged"));
     }
@@ -311,7 +316,7 @@
 
   /* get pixbuf */
   icon_name = battery_plugin_get_icon_name (info);
-  pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon_name, 22 , 0, NULL);
+  pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon_name, 32 , 0, NULL);
   g_free (icon_name);
 
   if (G_LIKELY (pixbuf))
@@ -476,7 +481,7 @@
     }
   else
     {
-      /* TODO: on ac */
+      /* TODO: on ac, no batteries */
     }
 
 #if !GTK_CHECK_VERSION (2,12,0)
@@ -492,8 +497,6 @@
 {
   gchar       *file;
   XfceRc      *rc;
-  GList       *devices, *li;
-  BatteryInfo *info;
 
   /* set defaults */
   plugin->show_frame = TRUE;
@@ -523,25 +526,6 @@
           plugin->show_progressbar = xfce_rc_read_bool_entry (rc, "ShowProgressBar", FALSE);
           plugin->show_label = xfce_rc_read_bool_entry (rc, "ShowLabel",TRUE);
 
-          /* get list of all the devices */
-          devices = battery_monitor_get_devices (plugin->monitor);
-
-          /* load settings */
-          for (li = devices; li != NULL; li = li->next)
-            {
-              info = li->data;
-
-              /* set device udi as group */
-              xfce_rc_set_group (rc, info->udi);
-
-              /* set (dis)charge rates */
-              info->rate_charging = xfce_rc_read_int_entry (rc, "RateCharging", 0);
-              info->rate_discharging = xfce_rc_read_int_entry (rc, "RateDischarging", 0);
-            }
-
-          /* cleanup */
-          g_list_free (devices);
-
           /* close the rc file */
           xfce_rc_close (rc);
         }
@@ -555,8 +539,6 @@
 {
   gchar       *file;
   XfceRc      *rc;
-  GList       *devices, *li;
-  BatteryInfo *info;
 
   /* get rc file, create one if needed */
   file = xfce_panel_plugin_save_location (plugin->panel_plugin, TRUE);
@@ -580,25 +562,6 @@
           xfce_rc_write_bool_entry (rc, "ShowProgressBar", plugin->show_progressbar);
           xfce_rc_write_bool_entry (rc, "ShowLabel", plugin->show_label);
 
-          /* get list of known devices */
-          devices = battery_monitor_get_devices (plugin->monitor);
-
-          /* store */
-          for (li = devices; li != NULL; li = li->next)
-            {
-              info = li->data;
-
-              /* set device udi as group */
-              xfce_rc_set_group (rc, info->udi);
-
-              /* save rates */
-              xfce_rc_write_int_entry (rc, "RateCharging", info->rate_charging);
-              xfce_rc_write_int_entry (rc, "RateDischarging", info->rate_discharging);
-            }
-
-          /* cleanup */
-          g_list_free (devices);
-
           /* close the rc file */
           xfce_rc_close (rc);
         }
@@ -611,13 +574,18 @@
 battery_plugin_orientation_changed (BatteryPlugin  *plugin,
                                     GtkOrientation  orientation)
 {
+  gint size;
+  
   /* set the orientation of the hvbox */
   xfce_hvbox_set_orientation (XFCE_HVBOX (plugin->box), orientation);
+  
+  /* get the panel size */
+  size = xfce_panel_plugin_get_size (plugin->panel_plugin);
 
   if (orientation == GTK_ORIENTATION_HORIZONTAL)
     {
       /* progressbar size and orienation */
-      gtk_widget_set_size_request (plugin->progressbar, PROGRESS_BAR_THINKNESS, -1);
+      gtk_widget_set_size_request (plugin->progressbar, PROGRESS_BAR_THINKNESS, size);
       gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (plugin->progressbar), GTK_PROGRESS_BOTTOM_TO_TOP);
 
       /* label justification */
@@ -626,7 +594,7 @@
   else
     {
       /* progressbar size and orienation */
-      gtk_widget_set_size_request (plugin->progressbar, -1, PROGRESS_BAR_THINKNESS);
+      gtk_widget_set_size_request (plugin->progressbar, size, PROGRESS_BAR_THINKNESS);
       gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (plugin->progressbar), GTK_PROGRESS_LEFT_TO_RIGHT);
 
       /* label justification */
@@ -642,7 +610,10 @@
 {
   /* set the border width of the frame */
   gtk_container_set_border_width (GTK_CONTAINER (plugin->frame), size > 26 ? PANEL_PADDING : 0);
-
+  
+  /* poke function above to set proper progressbar sizings */
+  battery_plugin_orientation_changed (plugin, xfce_panel_plugin_get_orientation (plugin->panel_plugin));
+  
   /* free last icon name */
   g_free (plugin->status_icon_name);
   plugin->status_icon_name = NULL;




More information about the Goodies-commits mailing list