aboutsummaryrefslogtreecommitdiffstats
path: root/community/xfce4-battery-plugin/00-fix-percentage-detection.patch
blob: 82d28c52d67bc3f673c9faf51ac6e79487618f41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
diff --git a/panel-plugin/battery.c b/panel-plugin/battery.c
index b730274..aa0d993 100644
--- a/panel-plugin/battery.c
+++ b/panel-plugin/battery.c
@@ -160,6 +160,7 @@ update_apm_status(t_battmon *battmon)
     int method = BM_BROKEN;
     int present = 0, charge = 0, rate = 0;
     int lcapacity = 0, ccapacity = 0;
+    int percentage = 0;
     gboolean fan = FALSE;
     const char *temp;
     static int old_state = -1, new_state = BM_MISSING;
@@ -216,6 +217,7 @@ update_apm_status(t_battmon *battmon)
             lcapacity += acpiinfo->last_full_capacity;
             ccapacity += acpistate->rcapacity;
             rate += acpistate->prate;
+            percentage += acpistate->percentage;
         }
 
         sum_lcapacity += lcapacity;
@@ -241,7 +243,10 @@ update_apm_status(t_battmon *battmon)
             rate = last_rate;
         }
 
-        charge = (((float)ccapacity)/((float)lcapacity))*100;
+        if (lcapacity > 0)
+            charge = (((float)ccapacity)/((float)lcapacity))*100;
+        else if (percentage > 0 && present > 0)
+            charge = percentage/present;
 
         if (last_acline)
             time_remaining = ((float)(lcapacity-ccapacity)/(float)(rate))*60;
diff --git a/panel-plugin/libacpi.c b/panel-plugin/libacpi.c
index 0755585..671d624 100644
--- a/panel-plugin/libacpi.c
+++ b/panel-plugin/libacpi.c
@@ -518,6 +518,7 @@ read_acpi_state_sysfs(int battery)
     DIR *sysfs;
     struct dirent *propety;
     char *name;
+    int percentage_found;
 
     sysfs = opendir(batteries[battery]);
     if (sysfs == 0)
@@ -555,11 +556,15 @@ read_acpi_state_sysfs(int battery)
         }
 
         /* on my system this is called charge_now */
-        if ((strcmp(name,"energy_now") == 0) || (strcmp(name,"charge_now") == 0))
+        if ((strcmp(name,"energy_now") == 0) || (strcmp(name,"charge_now") == 0) || (strcmp(name,"charge_counter") == 0))
         {
             sprintf(buf,"%s/%s",batteries[battery], name);
             acpistate->rcapacity = read_sysfs_int(buf);
-            acpistate->percentage = (((float) acpistate->rcapacity)/acpiinfo->last_full_capacity) * 100;
+            /* calculate percentage based on remaining capacity only if actual percentage is not found */
+            if (!percentage_found)
+            {
+                acpistate->percentage = (((float) acpistate->rcapacity)/acpiinfo->last_full_capacity) * 100;
+            }
         }
 
         if ((strcmp(name,"current_now") == 0) || (strcmp(name,"power_now") == 0))
@@ -577,6 +582,13 @@ read_acpi_state_sysfs(int battery)
             sprintf(buf,"%s/%s",batteries[battery], name);
             acpistate->pvoltage = read_sysfs_int(buf);
         }
+
+        if (strcmp(name,"capacity") == 0)
+        {
+            sprintf(buf,"%s/%s",batteries[battery], name);
+            acpistate->percentage = read_sysfs_int(buf);
+            percentage_found = 1;
+        }
     }
 
     closedir(sysfs);