kwo pushed a commit to branch master.

commit ed72a3550d1a0ce960b11ddb4520ac04c08a3e72
Author: Kim Woelders <[email protected]>
Date:   Sat Jul 13 00:05:50 2013 +0200

    E-Power: Simplifications.
---
 epplets/E-Power.c | 90 ++++++++++++++++++++-----------------------------------
 1 file changed, 33 insertions(+), 57 deletions(-)

diff --git a/epplets/E-Power.c b/epplets/E-Power.c
index 8173123..3f01aa5 100644
--- a/epplets/E-Power.c
+++ b/epplets/E-Power.c
@@ -181,7 +181,7 @@ _bat_info_fetch_acpi(bat_info_t * bi)
             char                present[256];
             char                capacity_state[256];
             char                charging_state[256];
-            int                 rate = 1;
+            int                 rate = 0;
             int                 level = 0;
 
             getline(&line, &lsize, f);
@@ -203,6 +203,7 @@ _bat_info_fetch_acpi(bat_info_t * bi)
             else
                sscanf(line, "%*[^:]: %i %*s", &level);
             fclose(f);
+
             if (!strcmp(present, "yes"))
                bi->battery++;
             if (!strcmp(charging_state, "discharging"))
@@ -249,20 +250,21 @@ _bat_info_fetch_sys(bat_info_t * bi)
             int                 last_full = 0;
             char                present[256];
             char                key[256];
+            char                value[256];
             char                charging_state[256];
-            char                name[256];
-            int                 rate = 1;
+            int                 rate = 0;
             int                 level = 0;
 
             while (getline(&line, &lsize, f) != -1)
               {
-                 sscanf(line, "%[^=]= %250s", key, name);
+                 sscanf(line, "%[^=]= %250s", key, value);
+
                  if (strcmp(key, "POWER_SUPPLY_NAME") == 0)
                    {
                    }
                  else if (strcmp(key, "POWER_SUPPLY_STATUS") == 0)
                    {
-                      sscanf(line, "%*[^=]= %250s", charging_state);
+                      sscanf(value, "%250s", charging_state);
                       if (!strcmp(charging_state, "Discharging"))
                          bi->discharging++;
                       if (!strcmp(charging_state, "Charging"))
@@ -270,31 +272,31 @@ _bat_info_fetch_sys(bat_info_t * bi)
                    }
                  else if (strcmp(key, "POWER_SUPPLY_PRESENT") == 0)
                    {
-                      sscanf(line, "%*[^=]= %250s", present);
+                      sscanf(value, "%250s", present);
                       if (!strcmp(present, "1"))
                          bi->battery++;
                    }
                  else if (strcmp(key, "POWER_SUPPLY_CURRENT_NOW") == 0)
                    {
-                      sscanf(line, "%*[^=]= %i %*s", &rate);
+                      sscanf(value, "%i", &rate);
                       bi->rate_unknown = 0;
                       bi->bat_drain += rate;
                    }
                  else if (strcmp(key, "POWER_SUPPLY_CHARGE_FULL_DESIGN") == 0)
                    {
-                      sscanf(line, "%*[^=]=%i", &design_cap);
+                      sscanf(value, "%i", &design_cap);
                       bi->design_cap_unknown = 0;
                       bi->bat_max += design_cap;
                    }
                  else if (strcmp(key, "POWER_SUPPLY_CHARGE_FULL") == 0)
                    {
-                      sscanf(line, "%*[^=]= %i", &last_full);
+                      sscanf(value, "%i", &last_full);
                       bi->last_full_unknown = 0;
                       bi->bat_filled += last_full;
                    }
                  else if (strcmp(key, "POWER_SUPPLY_CHARGE_NOW") == 0)
                    {
-                      sscanf(line, "%*[^=]= %i", &level);
+                      sscanf(value, "%i", &level);
                       bi->level_unknown = 0;
                       bi->bat_level += level;
                    }
@@ -313,75 +315,49 @@ cb_timer_gen(bi_fetch_f * bi_fetch)
     * so we have to calculate and measure them.
     * (Measure the time and calculate the percentage.)
     */
-   static int          prev_bat_drain = 1;
-
    bat_info_t          bi;
-
    int                 bat_val;
-
    char                current_status[256];
-
    int                 hours, minutes;
+   const char         *pwr;
 
    memset(&bi, 0, sizeof(bi));
-   bi.bat_drain = 1;
-
    bi_fetch(&bi);
 
-   if (prev_bat_drain < 1)
-      prev_bat_drain = 1;
-   if (bi.bat_drain < 1)
-      bi.bat_drain = prev_bat_drain;
-   prev_bat_drain = bi.bat_drain;
-
    if (bi.bat_filled > 0)
       bat_val = (100 * bi.bat_level) / bi.bat_filled;
    else
       bat_val = 100;
 
-   if (bi.discharging)
-      minutes = (60 * bi.bat_level) / bi.bat_drain;
-   else
+   minutes = 0;
+   if (bi.bat_drain > 0)
      {
-       if (bi.bat_filled > 0)
+       if (bi.discharging)
+          minutes = (60 * bi.bat_level) / bi.bat_drain;
+       else if (bi.charging)
           minutes = (60 * (bi.bat_filled - bi.bat_level)) / bi.bat_drain;
-       else
-          minutes = 0;
      }
    hours = minutes / 60;
    minutes -= (hours * 60);
 
-   if (bi.charging)
-     {
-       if (bi.level_unknown)
-          snprintf(current_status, sizeof(current_status),
-                   "Level ???\n" "Bad Driver");
-       else if (bi.rate_unknown)
-          snprintf(current_status, sizeof(current_status),
-                   "%i%% PWR\n" "Time ???", bat_val);
-       else
-          snprintf(current_status, sizeof(current_status),
-                   "%i%% PWR\n" "%02i:%02i", bat_val, hours, minutes);
-     }
-   else if (bi.discharging)
-     {
-       if (bi.level_unknown)
-          snprintf(current_status, sizeof(current_status),
-                   "Level ???\n" "Bad Driver");
-       else if (bi.rate_unknown)
-          snprintf(current_status, sizeof(current_status),
-                   "%i%%\n" "Time ???", bat_val);
-       else
-          snprintf(current_status, sizeof(current_status),
-                   "%i%%\n" "%02i:%02i", bat_val, hours, minutes);
-     }
-   else if (!bi.battery)
+   pwr = bi.charging ? " PWR" : "";
+   if (!bi.battery)
       snprintf(current_status, sizeof(current_status), "No Bat");
-   else
+   else if (bi.level_unknown)
+      snprintf(current_status, sizeof(current_status),
+              "Level ???\n" "Bad Driver");
+   else if (bat_val >= 100 && bi.bat_drain <= 0)
       snprintf(current_status, sizeof(current_status), "Full");
-
-   /* Display current status */
+   else if (bi.rate_unknown || bi.bat_drain <= 0)
+      snprintf(current_status, sizeof(current_status),
+              "%i%%%s\n" "Time ???", bat_val, pwr);
+   else if (bi.charging || bi.discharging)
+      snprintf(current_status, sizeof(current_status),
+              "%i%%%s\n" "%02i:%02i", bat_val, pwr, hours, minutes);
+   else
+      snprintf(current_status, sizeof(current_status), "???");
    Epplet_change_label(label, current_status);
+
    sprintf(current_status, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
    Epplet_change_image(image, 44, 24, current_status);
 }

-- 

------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk

Reply via email to