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 <dbus/dbus-glib.h>
25
#include "powermanager-draw.h"
26
#include "powermanager-struct.h"
27
#include "powermanager-common.h"
28
#include "powermanager-sys-class.h"
30
#define CD_BATTERY_DIR "/sys/class/power_supply"
32
/*POWER_SUPPLY_NAME=BAT1
33
POWER_SUPPLY_STATUS=Discharging
34
POWER_SUPPLY_PRESENT=1
35
POWER_SUPPLY_TECHNOLOGY=Li-ion
36
POWER_SUPPLY_CYCLE_COUNT=0
37
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
38
POWER_SUPPLY_VOLTAGE_NOW=11100000
39
POWER_SUPPLY_CURRENT_NOW=0
40
POWER_SUPPLY_CHARGE_FULL_DESIGN=2200000
41
POWER_SUPPLY_CHARGE_FULL=2200000
42
POWER_SUPPLY_CHARGE_NOW=1760000
43
POWER_SUPPLY_MODEL_NAME=
44
POWER_SUPPLY_MANUFACTURER=DELL
45
POWER_SUPPLY_SERIAL_NUMBER=11
48
static gboolean _find_battery_in_dir (const gchar *cBatteryPath)
50
// open the folder containing battery data.
51
GDir *dir = g_dir_open (cBatteryPath, 0, NULL);
54
cd_debug ("powermanager: no battery in %s",cBatteryPath );
58
// parse the folder and search the battery files.
59
GString *sBatteryInfoFilePath = g_string_new ("");
60
gchar *cContent = NULL, *cPresentLine;
62
const gchar *cBatteryName;
63
gboolean bBatteryFound = FALSE;
66
cBatteryName = g_dir_read_name (dir); // usually "BAT0" or "BAT1".
67
if (cBatteryName == NULL)
70
// check the battery type.
71
g_string_printf (sBatteryInfoFilePath, "%s/%s/type", cBatteryPath, cBatteryName);
73
cd_debug (" examen de la batterie '%s' ...", sBatteryInfoFilePath->str);
74
g_file_get_contents (sBatteryInfoFilePath->str, &cContent, &length, NULL);
75
if (cContent != NULL && strncmp (cContent, "Battery", 7) == 0) // there may be a \n at the end, so let's ignore it.
77
myData.cBatteryStateFilePath = g_strdup_printf ("%s/%s/uevent", cBatteryPath, cBatteryName);
78
bBatteryFound = TRUE; // get the capacity when we read the uevent for the first time.
79
cd_debug (" myData.cBatteryStateFilePath: %s", myData.cBatteryStateFilePath);
83
while (! bBatteryFound);
87
gboolean cd_find_battery_sys_class (void)
89
gboolean bBatteryFound = _find_battery_in_dir (CD_BATTERY_DIR);
93
#define _get_static_value(s, x) \
94
str = strstr (cContent, s);\
97
cr = strchr (str, '\n');\
98
if (cr) x = g_strndup (str, cr - str);\
99
else x = g_strdup (str); }
101
gboolean cd_get_stats_from_sys_class (void)
103
//\_______________ get the content of the stats file.
104
gchar *cContent = NULL;
106
GError *erreur = NULL;
107
g_file_get_contents (myData.cBatteryStateFilePath, &cContent, &length, &erreur);
110
cd_warning ("powermanager : %s", erreur->message);
111
g_error_free(erreur);
115
g_return_val_if_fail (cContent != NULL, FALSE);
119
//\_______________ check 'on battery' state.
120
gchar *str = strstr (cContent, "STATUS");
121
g_return_val_if_fail (str != NULL, FALSE);
123
gboolean bOnBattery = (*str == 'D'); // "Discharging"
124
if (bOnBattery != myData.bOnBattery) // state changed
126
/**for (k = 0; k < PM_NB_VALUES; k ++) // reset the history.
127
myData.fRateHistory[k] = 0;
128
myData.iCurrentIndex = 0;
129
myData.iIndexMax = 0;*/
130
myData.iStatPercentageBegin = 0;
131
myData.iStatPercentage = 0;
132
myData.bOnBattery = bOnBattery;
135
//\_______________ check the battery presence.
136
str = strstr (cContent, "PRESENT");
137
g_return_val_if_fail (str != NULL, FALSE);
139
gboolean bBatteryPresent = (*str == '1');
140
if (bBatteryPresent != myData.bBatteryPresent) // the battery has just been inserted/removed.
142
myData.bBatteryPresent = bBatteryPresent;
143
if (! bBatteryPresent) // if the battery has been removed, we are obviously on the sector.
145
cd_debug ("la batterie a ete enlevee\n");
146
myData.bOnBattery = FALSE;
152
// reset the history.
153
cd_debug ("la batterie a ete connectee");
154
myData.iPrevTime = 0;
155
myData.iPrevPercentage = 0;
156
/**for (k = 0; k < PM_NB_VALUES; k ++)
157
myData.fRateHistory[k] = 0;
158
myData.iCurrentIndex = 0;
159
myData.iIndexMax = 0;*/
160
myData.iStatPercentageBegin = 0;
161
myData.iStatPercentage = 0;
164
//\_______________ get the current charge.
165
if (myData.iCapacity == 0) // not yet got
167
str = strstr (cContent, "CHARGE_FULL=");
168
g_return_val_if_fail (str != NULL, FALSE);
170
myData.iCapacity = atoi (str);
171
g_return_val_if_fail (myData.iCapacity != 0, FALSE);
172
// also get few other static params.
174
_get_static_value ("TECHNOLOGY", myData.cTechnology)
175
_get_static_value ("MANUFACTURER", myData.cVendor)
176
_get_static_value ("MODEL_NAME", myData.cModel)
177
str = strstr (cContent, "FULL_DESIGN");
181
int iMaxCapacity = atoi (str);
182
if (iMaxCapacity != 0)
183
myData.fMaxAvailableCapacity = 100. * myData.iCapacity / iMaxCapacity;
187
str = strstr (cContent, "CHARGE_NOW");
188
g_return_val_if_fail (str != NULL, FALSE);
190
int iRemainingCapacity = atoi (str);
192
myData.iPercentage = 100. * iRemainingCapacity / myData.iCapacity;
193
cd_debug ("myData.iPercentage : %.2f%% (%d / %d)", (double)myData.iPercentage, iRemainingCapacity, myData.iCapacity);
194
if (myData.iPercentage > 100)
195
myData.iPercentage = 100;
196
if (myData.iPercentage < 0)
197
myData.iPercentage = 0.;
199
//\_______________ now compute the time.
200
myData.iTime = cd_estimate_time ();