Debian Codegraph @

powermanager/src/powermanager-common.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <math.h>
21 #include <string.h>
22
23 #include "powermanager-struct.h"
24 #include "powermanager-proc-acpi.h"
25 #include "powermanager-sys-class.h"
26 #include "powermanager-draw.h"
27 #include "powermanager-common.h"
28
29
30 static double cd_compute_current_rate (void)
31 {
32 double fPresentRate = 0.;
33 if (myData.iPrevPercentage > 0)
34 {
35 if (myData.iStatPercentageBegin != 0)
36 myData.iStatTimeCount += myConfig.iCheckInterval;
37 if (myData.iPrevPercentage != myData.iPercentage) // we compute the rate from 2 changing points.
38 {
39 if (myData.iStatPercentageBegin == 0)
40 {
41 myData.iStatPercentageBegin = myData.iPercentage;
42 myData.iStatTimeCount= 0;
43 myData.iStatTime = 0;
44 }
45 else
46 {
47 myData.iStatPercentage = myData.iPercentage;
48 myData.iStatTime = myData.iStatTimeCount;
49 }
50 }
51 if (myData.iStatPercentage != 0)
52 fPresentRate = (double)fabs (myData.iStatPercentage - myData.iStatPercentageBegin) / myData.iStatTime * 36.;
53 cd_message ("instant rate : %.1f -> %.1f (%ds) => %.3f", (double)myData.iStatPercentageBegin, (double)myData.iStatPercentage, myData.iStatTime, fPresentRate);
54 /**
55 fPresentRate = (myData.iPrevPercentage - myData.iPercentage) * 36. * myData.iCapacity / myConfig.iCheckInterval;
56 cd_message ("instant rate : %.2f -> %.2f => %.2f", (double)myData.iPrevPercentage, (double)myData.iPercentage, fPresentRate);
57
58 if (fPresentRate != 0)
59 {
60 myData.fRateHistory[myData.iCurrentIndex] = fPresentRate;
61
62 double fMeanRate = 0.;
63 int nb_values=0;
64 double fNextValue = 0.;
65 int iNbStackingValues = 0;
66 int i, k;
67 for (k = 0; k < myData.iIndexMax; k ++)
68 {
69 if (myData.iIndexMax == PM_NB_VALUES)
70 i = (myData.iCurrentIndex + 1 + k) % PM_NB_VALUES;
71 else
72 i = k;
73 if (myData.fRateHistory[i] != 0)
74 {
75 if (fNextValue != 0)
76 {
77 nb_values += iNbStackingValues;
78 fMeanRate += fNextValue;
79 }
80 fNextValue = myData.fRateHistory[i];
81 iNbStackingValues = 1;
82 }
83 else
84 {
85 iNbStackingValues ++;
86 }
87 }
88 if (nb_values != 0)
89 fPresentRate = fabs (fMeanRate) / nb_values;
90 cd_message ("mean calculated on %d value(s) : %.2f (current index:%d/%d)", nb_values, fPresentRate, myData.iCurrentIndex, myData.iIndexMax);
91
92 myData.iCurrentIndex ++;
93 if (myData.iIndexMax < PM_NB_VALUES)
94 myData.iIndexMax = myData.iCurrentIndex;
95 if (myData.iCurrentIndex == PM_NB_VALUES)
96 myData.iCurrentIndex = 0;
97 }*/
98 }
99 return fPresentRate;
100 }
101
102 static void cd_store_current_rate (double fPresentRate)
103 {
104 if (myData.bOnBattery)
105 {
106 myData.fDischargeMeanRate = (myData.fDischargeMeanRate * myData.iNbDischargeMeasures + fPresentRate) / (myData.iNbDischargeMeasures + 1);
107 myData.iNbDischargeMeasures ++;
108 cd_debug ("fDischargeMeanRate : %.3f (%d)", myData.fDischargeMeanRate, myData.iNbDischargeMeasures);
109
110 if (myConfig.fLastDischargeMeanRate == 0 || (double) fabs (myConfig.fLastDischargeMeanRate - myData.fDischargeMeanRate) / myConfig.fLastDischargeMeanRate > .01) // l'ecart avec la valeur stockee en conf est devenue grande, on met a jour cette derniere.
111 {
112 cd_debug ("write discharge rate : %.2f -> %.2f", myConfig.fLastDischargeMeanRate, myData.fDischargeMeanRate);
113 cairo_dock_update_conf_file (CD_APPLET_MY_CONF_FILE,
114 G_TYPE_DOUBLE, "Configuration", "discharge rate", myConfig.fLastDischargeMeanRate,
115 G_TYPE_INVALID);
116 myConfig.fLastDischargeMeanRate = myData.fDischargeMeanRate;
117 }
118 }
119 else
120 {
121 myData.fChargeMeanRate = (myData.fChargeMeanRate * myData.iNbChargeMeasures + fPresentRate) / (myData.iNbChargeMeasures + 1);
122 myData.iNbChargeMeasures ++;
123 cd_debug ("fChargeMeanRate : %.3f (%d)", myData.fChargeMeanRate, myData.iNbChargeMeasures);
124 if (myConfig.fLastChargeMeanRate == 0 || (double) fabs (myConfig.fLastChargeMeanRate - myData.fChargeMeanRate) / myConfig.fLastChargeMeanRate > .01) // l'ecart avec la valeur stockee en conf est devenue grande, on met a jour cette derniere.
125 {
126 cd_debug ("charge rate : %.2f -> %.2f", myConfig.fLastChargeMeanRate, myData.fChargeMeanRate);
127 cairo_dock_update_conf_file (CD_APPLET_MY_CONF_FILE,
128 G_TYPE_DOUBLE, "Configuration", "charge rate", myConfig.fLastChargeMeanRate,
129 G_TYPE_INVALID);
130 myConfig.fLastChargeMeanRate = myData.fChargeMeanRate;
131 }
132 }
133 }
134
135 static double cd_compute_time (double fPresentRate)
136 {
137 double time = 0.;
138 if (myData.iPercentage < 99.9) // not charged or almost charged.
139 {
140 if (myData.bOnBattery) // time before discharged.
141 {
142 if (fPresentRate > 0)
143 {
144 time = 36. * myData.iPercentage / fPresentRate;
145 }
146 }
147 else // time before charged
148 {
149 if (fPresentRate > 0)
150 {
151 time = 36. * (100. - myData.iPercentage) / fPresentRate;
152 }
153 }
154 }
155 return time;
156 }
157
158
159 int cd_estimate_time (void)
160 {
161 //\_______________ compute the variation rate.
162 double fPresentRate = cd_compute_current_rate ();
163
164 //\_______________ store this value in conf if it has changed too much.
165 if (fPresentRate > 0)
166 {
167 cd_store_current_rate (fPresentRate);
168 }
169 else if (myData.bOnBattery || myData.iPercentage < 99.9) // if we are on sector and fully charged, the rate is of course 0.
170 {
171 cd_debug ("no rate, using last know values : %.2f ; %.2f", myConfig.fLastDischargeMeanRate, myConfig.fLastChargeMeanRate);
172 fPresentRate = (myData.bOnBattery ? myConfig.fLastDischargeMeanRate : myConfig.fLastChargeMeanRate);
173 }
174
175 //\_______________ now compute the time.
176 return cd_compute_time (fPresentRate);
177 }
178
179
180 static gboolean _update_stats (void)
181 {
182 if (myData.bProcAcpiFound)
183 cd_get_stats_from_proc_acpi ();
184 else
185 cd_get_stats_from_sys_class ();
186
187 update_icon ();
188
189 return (myData.cBatteryStateFilePath != NULL);
190 }
191
192 static gboolean _update_stats_loop (void)
193 {
194 CD_APPLET_ENTER;
195
196 gboolean bContinue = _update_stats ();
197
198 if (! bContinue)
199 myData.checkLoop = 0;
200 CD_APPLET_LEAVE (bContinue);
201 }
202
203 void cd_check_power_files (void)
204 {
205 // try to find the battery
206 myData.bProcAcpiFound = cd_find_battery_proc_acpi ();
207 if (! myData.bProcAcpiFound)
208 myData.bSysClassFound = cd_find_battery_sys_class ();
209
210 // if found, get the current stats.
211 if (myData.cBatteryStateFilePath != NULL)
212 _update_stats ();
213
214 // and keep them up-to-date.
215 if (myData.cBatteryStateFilePath != NULL)
216 {
217 myData.fDischargeMeanRate = myConfig.fLastDischargeMeanRate;
218 myData.fChargeMeanRate = myConfig.fLastChargeMeanRate;
219 myData.checkLoop = g_timeout_add_seconds (myConfig.iCheckInterval, (GSourceFunc) _update_stats_loop, (gpointer) NULL);
220 }
221 }
222
223
224 void cd_powermanager_change_loop_frequency (gint iSeconds)
225 {
226 if (myData.checkLoop != 0)
227 {
228 g_source_remove (myData.checkLoop);
229 myData.checkLoop = g_timeout_add_seconds (iSeconds, (GSourceFunc) _update_stats_loop, (gpointer) NULL);
230 }
231 }