2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
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"
30
static double cd_compute_current_rate (void)
32
double fPresentRate = 0.;
33
if (myData.iPrevPercentage > 0)
35
if (myData.iStatPercentageBegin != 0)
36
myData.iStatTimeCount += myConfig.iCheckInterval;
37
if (myData.iPrevPercentage != myData.iPercentage) // we compute the rate from 2 changing points.
39
if (myData.iStatPercentageBegin == 0)
41
myData.iStatPercentageBegin = myData.iPercentage;
42
myData.iStatTimeCount= 0;
47
myData.iStatPercentage = myData.iPercentage;
48
myData.iStatTime = myData.iStatTimeCount;
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);
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);
58
if (fPresentRate != 0)
60
myData.fRateHistory[myData.iCurrentIndex] = fPresentRate;
62
double fMeanRate = 0.;
64
double fNextValue = 0.;
65
int iNbStackingValues = 0;
67
for (k = 0; k < myData.iIndexMax; k ++)
69
if (myData.iIndexMax == PM_NB_VALUES)
70
i = (myData.iCurrentIndex + 1 + k) % PM_NB_VALUES;
73
if (myData.fRateHistory[i] != 0)
77
nb_values += iNbStackingValues;
78
fMeanRate += fNextValue;
80
fNextValue = myData.fRateHistory[i];
81
iNbStackingValues = 1;
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);
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;
102
static void cd_store_current_rate (double fPresentRate)
104
if (myData.bOnBattery)
106
myData.fDischargeMeanRate = (myData.fDischargeMeanRate * myData.iNbDischargeMeasures + fPresentRate) / (myData.iNbDischargeMeasures + 1);
107
myData.iNbDischargeMeasures ++;
108
cd_debug ("fDischargeMeanRate : %.3f (%d)\n", myData.fDischargeMeanRate, myData.iNbDischargeMeasures);
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.
112
g_print ("write discharge rate : %.2f -> %.2f\n", myConfig.fLastDischargeMeanRate, myData.fDischargeMeanRate);
113
cairo_dock_update_conf_file (CD_APPLET_MY_CONF_FILE,
114
G_TYPE_DOUBLE, "Configuration", "discharge rate", myConfig.fLastDischargeMeanRate,
116
myConfig.fLastDischargeMeanRate = myData.fDischargeMeanRate;
121
myData.fChargeMeanRate = (myData.fChargeMeanRate * myData.iNbChargeMeasures + fPresentRate) / (myData.iNbChargeMeasures + 1);
122
myData.iNbChargeMeasures ++;
123
cd_debug ("fChargeMeanRate : %.3f (%d)\n", 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.
126
g_print ("charge rate : %.2f -> %.2f\n", myConfig.fLastChargeMeanRate, myData.fChargeMeanRate);
127
cairo_dock_update_conf_file (CD_APPLET_MY_CONF_FILE,
128
G_TYPE_DOUBLE, "Configuration", "charge rate", myConfig.fLastChargeMeanRate,
130
myConfig.fLastChargeMeanRate = myData.fChargeMeanRate;
135
static double cd_compute_time (double fPresentRate)
138
if (myData.iPercentage < 99.9) // not charged or almost charged.
140
if (myData.bOnBattery) // time before discharged.
142
if (fPresentRate > 0)
144
time = 36. * myData.iPercentage / fPresentRate;
147
else // time before charged
149
if (fPresentRate > 0)
151
time = 36. * (100. - myData.iPercentage) / fPresentRate;
159
int cd_estimate_time (void)
161
//\_______________ compute the variation rate.
162
double fPresentRate = cd_compute_current_rate ();
164
//\_______________ store this value in conf if it has changed too much.
165
if (fPresentRate > 0)
167
cd_store_current_rate (fPresentRate);
169
else if (myData.bOnBattery || myData.iPercentage < 99.9) // if we are on sector and fully charged, the rate is of course 0.
171
cd_debug ("no rate, using last know values : %.2f ; %.2f\n", myConfig.fLastDischargeMeanRate, myConfig.fLastChargeMeanRate);
172
fPresentRate = (myData.bOnBattery ? myConfig.fLastDischargeMeanRate : myConfig.fLastChargeMeanRate);
175
//\_______________ now compute the time.
176
return cd_compute_time (fPresentRate);
180
static gboolean _update_stats (void)
182
if (myData.bProcAcpiFound)
183
cd_get_stats_from_proc_acpi ();
185
cd_get_stats_from_sys_class ();
189
return (myData.cBatteryStateFilePath != NULL);
192
static gboolean _update_stats_loop (void)
196
gboolean bContinue = _update_stats ();
199
myData.checkLoop = 0;
200
CD_APPLET_LEAVE (bContinue);
203
void cd_check_power_files (void)
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 ();
210
// if found, get the current stats.
211
if (myData.cBatteryStateFilePath != NULL)
214
// and keep them up-to-date.
215
if (myData.cBatteryStateFilePath != NULL)
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);
224
void cd_powermanager_change_loop_frequency (gint iSeconds)
226
if (myData.checkLoop != 0) // la frequence peut avoir change.
228
g_source_remove (myData.checkLoop);
229
myData.checkLoop = g_timeout_add_seconds (iSeconds, (GSourceFunc) _update_stats_loop, (gpointer) NULL);